i2c.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "gd32f3x0.h"
  2. #include "gd32f3x0_i2c.h"
  3. #include "gpio.h"
  4. #include "i2c.h"
  5. static int _gd32_i2c_rw_bytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length, int write);
  6. #define iic_device(id) ((id == 0)?I2C0:I2C1)
  7. static uint32_t i2c_clk[2];
  8. static uint8_t i2c_busy_count[2];
  9. static void _delay_1us(uint32_t n)
  10. {
  11. while(n-->0)
  12. {
  13. int N=100;
  14. while(N-->0);
  15. }
  16. }
  17. static void _i2c_deinit(uint32_t i2c_periph)
  18. {
  19. switch(i2c_periph){
  20. case I2C0:
  21. /* reset I2C0 */
  22. rcu_periph_reset_enable(RCU_I2C0RST);
  23. _delay_1us(10);
  24. rcu_periph_reset_disable(RCU_I2C0RST);
  25. break;
  26. case I2C1:
  27. /* reset I2C1 */
  28. rcu_periph_reset_enable(RCU_I2C1RST);
  29. _delay_1us(10);
  30. rcu_periph_reset_disable(RCU_I2C1RST);
  31. break;
  32. default:
  33. break;
  34. }
  35. }
  36. /*
  37. * 当读写i2c slave设备的时候,MCU复位,有概率会导致slave设备永远会锁住总线,导致永远busy,复位MCU无效
  38. * 这里,需要通过i2c的reset位配合sda,sck拉高,强行让slave释放总线
  39. */
  40. static void gd32_i2c_busy_recovery(uint32_t i2c_periph){
  41. if (i2c_periph == I2C0) {
  42. gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8|GPIO_PIN_9);
  43. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8|GPIO_PIN_9);
  44. gpio_bit_set(GPIOB, GPIO_PIN_8);
  45. gpio_bit_set(GPIOB, GPIO_PIN_9);
  46. } else {
  47. gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11);
  48. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10|GPIO_PIN_11);
  49. gpio_bit_set(GPIOB, GPIO_PIN_10);
  50. gpio_bit_set(GPIOB, GPIO_PIN_11);
  51. }
  52. i2c_software_reset_config(i2c_periph, 1);
  53. _delay_1us(10);
  54. i2c_software_reset_config(i2c_periph, 0);
  55. }
  56. void gd32_i2c_init(uint32_t i2c_device, uint32_t rate){
  57. uint32_t device = iic_device(i2c_device);
  58. rcu_periph_clock_enable(RCU_GPIOB);
  59. if (device == I2C0) {
  60. rcu_periph_clock_enable(RCU_I2C0);
  61. gd32_i2c_busy_recovery(I2C0);
  62. _i2c_deinit(I2C0);
  63. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_8|GPIO_PIN_9);
  64. gpio_mode_af(GPIOB ,GPIO_PUPD_PULLUP, GPIO_PIN_8|GPIO_PIN_9);
  65. //gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_8|GPIO_PIN_9);
  66. } else {
  67. rcu_periph_clock_enable(RCU_I2C1);
  68. gd32_i2c_busy_recovery(I2C1);
  69. _i2c_deinit(I2C1);
  70. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10|GPIO_PIN_11);
  71. gpio_mode_af(GPIOB ,GPIO_PUPD_PULLUP, GPIO_PIN_10|GPIO_PIN_11);
  72. //gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_10|GPIO_PIN_11);
  73. }
  74. i2c_clock_config(device, rate, I2C_DTCY_2);
  75. i2c_enable(device);
  76. }
  77. void gd32_i2c_deinit(uint32_t i2c_index){
  78. uint32_t device = iic_device(i2c_index);
  79. i2c_disable(device);
  80. _i2c_deinit(device);
  81. rcu_periph_clock_disable(RCU_I2C0);
  82. if (device == I2C0) {
  83. gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8|GPIO_PIN_9);
  84. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_8|GPIO_PIN_9);
  85. gpio_bit_set(GPIOB, GPIO_PIN_8);
  86. gpio_bit_set(GPIOB, GPIO_PIN_9);
  87. } else {
  88. gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11);
  89. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10|GPIO_PIN_11);
  90. gpio_bit_set(GPIOB, GPIO_PIN_10);
  91. gpio_bit_set(GPIOB, GPIO_PIN_11);
  92. }
  93. }
  94. static int gd32_i2c_write_address(uint32_t deivce, uint8_t address, uint8_t write){
  95. uint32_t times;
  96. if (!write){
  97. address |= 1;
  98. }
  99. I2C_DATA((deivce)) = address;
  100. for (times = 500; times > 0; times--) {
  101. if (i2c_flag_get((deivce), I2C_FLAG_ADDSEND)) {
  102. return 0;
  103. }
  104. }
  105. return -1;
  106. }
  107. int gd32_i2c_read_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value){
  108. return _gd32_i2c_rw_bytes(index, address, reg, value, 1, 0);
  109. }
  110. int gd32_i2c_read_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length){
  111. return _gd32_i2c_rw_bytes(index, address, reg, buffer, length, 0);
  112. }
  113. int gd32_i2c_write_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t value){
  114. return _gd32_i2c_rw_bytes(index, address, reg, &value, 1, 1);
  115. }
  116. int gd32_i2c_write_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value, int length){
  117. return _gd32_i2c_rw_bytes(index, address, reg, value, length, 1);
  118. }
  119. static int gd32_i2c_wait_flags(uint32_t i2c_periph, i2c_flag_enum flag, FlagStatus status){
  120. int remain = 500;
  121. while (i2c_flag_get(i2c_periph, flag) != status) {
  122. if (remain-- <= 0){
  123. return -1;
  124. }
  125. }
  126. return 0;
  127. }
  128. static void gd32_i2c_wait_stop(uint32_t i2c_periph){
  129. int remain = 500;
  130. while(I2C_CTL0(i2c_periph) & 0x0200){
  131. if (remain-- <= 0){
  132. break;
  133. }
  134. };
  135. }
  136. static int _gd32_i2c_rw_bytes(uint32_t i2c_index, uint8_t address, uint8_t reg, uint8_t *buffer, int length, int write){
  137. int ret = length;
  138. uint32_t device = iic_device(i2c_index);
  139. i2c_ackpos_config(device, length == 2?I2C_ACKPOS_NEXT:I2C_ACKPOS_CURRENT);
  140. i2c_ack_config(device, I2C_ACK_ENABLE);
  141. if (gd32_i2c_wait_flags(device, I2C_FLAG_I2CBSY, RESET) < 0){
  142. i2c_busy_count[i2c_index]++;
  143. if (i2c_busy_count[i2c_index] >= 10) {
  144. gd32_i2c_deinit(i2c_index);
  145. gd32_i2c_init(i2c_index, i2c_clk[i2c_index]);
  146. i2c_busy_count[i2c_index] = 0;
  147. }
  148. return -1;
  149. }
  150. i2c_busy_count[i2c_index] = 0;
  151. //send reg
  152. i2c_start_on_bus(device);
  153. if (gd32_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
  154. ret = -2;
  155. goto out_i2c_stop_on_bus;
  156. }
  157. if (gd32_i2c_write_address(device, address, 1) < 0){
  158. ret = -3;
  159. goto out_i2c_stop_on_bus;
  160. }
  161. i2c_flag_clear(device, I2C_FLAG_ADDSEND);
  162. /* wait until the transmit data buffer is empty */
  163. if (gd32_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
  164. ret = -4;
  165. goto out_i2c_stop_on_bus;
  166. }
  167. i2c_data_transmit(device, reg);
  168. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  169. ret = -5;
  170. goto out_i2c_stop_on_bus;
  171. }
  172. if (write){
  173. int index = 0;
  174. for (; index < length; index++){
  175. if (gd32_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
  176. ret = -6;
  177. goto out_i2c_stop_on_bus;
  178. }
  179. i2c_data_transmit(device, buffer[index]);
  180. }
  181. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  182. ret = -7;
  183. goto out_i2c_stop_on_bus;
  184. }
  185. ret = length;
  186. goto out_i2c_stop_on_bus;
  187. }
  188. //begin read
  189. i2c_start_on_bus(device);
  190. if (gd32_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
  191. ret = -8;
  192. goto out_i2c_stop_on_bus;
  193. }
  194. if (gd32_i2c_write_address(device, address, 0) < 0){
  195. ret = -9;
  196. goto out_i2c_stop_on_bus;
  197. }
  198. i2c_flag_clear(device, I2C_FLAG_ADDSEND);
  199. if (length< 3) {
  200. i2c_ack_config(device, I2C_ACK_DISABLE);
  201. } else {
  202. while (length > 3) {
  203. if (gd32_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
  204. ret = -10;
  205. goto out_i2c_stop_on_bus;
  206. }
  207. *buffer++ = i2c_data_receive(device);
  208. length--;
  209. }
  210. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  211. ret = -11;
  212. goto out_i2c_stop_on_bus;
  213. }
  214. i2c_ack_config(device, I2C_ACK_DISABLE);
  215. }
  216. while (length > 0) {
  217. if (gd32_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
  218. ret = -12;
  219. goto out_i2c_stop_on_bus;
  220. }
  221. *buffer++ = i2c_data_receive(device);
  222. length--;
  223. }
  224. if (length != 0){
  225. ret = -13;
  226. }
  227. out_i2c_stop_on_bus:
  228. i2c_stop_on_bus(device);
  229. gd32_i2c_wait_stop(device);
  230. return ret;
  231. }