i2c.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. i2c_clk[i2c_device] = rate;
  59. rcu_periph_clock_enable(RCU_GPIOB);
  60. if (device == I2C0) {
  61. rcu_periph_clock_enable(RCU_I2C0);
  62. gd32_i2c_busy_recovery(I2C0);
  63. _i2c_deinit(I2C0);
  64. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_8|GPIO_PIN_9);
  65. gpio_mode_af(GPIOB ,GPIO_PUPD_PULLUP, GPIO_PIN_8|GPIO_PIN_9);
  66. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_8|GPIO_PIN_9);
  67. } else {
  68. rcu_periph_clock_enable(RCU_I2C1);
  69. gd32_i2c_busy_recovery(I2C1);
  70. _i2c_deinit(I2C1);
  71. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10|GPIO_PIN_11);
  72. gpio_mode_af(GPIOB ,GPIO_PUPD_PULLUP, GPIO_PIN_10|GPIO_PIN_11);
  73. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_10|GPIO_PIN_11);
  74. }
  75. i2c_clock_config(device, rate, I2C_DTCY_2);
  76. i2c_enable(device);
  77. }
  78. void gd32_i2c_deinit(uint32_t i2c_index){
  79. uint32_t device = iic_device(i2c_index);
  80. i2c_disable(device);
  81. _i2c_deinit(device);
  82. if (device == I2C0) {
  83. rcu_periph_clock_disable(RCU_I2C0);
  84. gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8|GPIO_PIN_9);
  85. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8|GPIO_PIN_9);
  86. gpio_bit_set(GPIOB, GPIO_PIN_8);
  87. gpio_bit_set(GPIOB, GPIO_PIN_9);
  88. } else {
  89. rcu_periph_clock_disable(RCU_I2C1);
  90. gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11);
  91. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10|GPIO_PIN_11);
  92. gpio_bit_set(GPIOB, GPIO_PIN_10);
  93. gpio_bit_set(GPIOB, GPIO_PIN_11);
  94. }
  95. }
  96. static int gd32_i2c_write_address(uint32_t deivce, uint8_t address, uint8_t write){
  97. uint32_t times;
  98. if (!write){
  99. address |= 1;
  100. }
  101. I2C_DATA((deivce)) = address;
  102. for (times = 500; times > 0; times--) {
  103. if (i2c_flag_get((deivce), I2C_FLAG_ADDSEND)) {
  104. return 0;
  105. }
  106. }
  107. return -1;
  108. }
  109. int gd32_i2c_read_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value){
  110. return _gd32_i2c_rw_bytes(index, address, reg, value, 1, 0);
  111. }
  112. int gd32_i2c_read_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length){
  113. return _gd32_i2c_rw_bytes(index, address, reg, buffer, length, 0);
  114. }
  115. int gd32_i2c_write_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t value){
  116. return _gd32_i2c_rw_bytes(index, address, reg, &value, 1, 1);
  117. }
  118. int gd32_i2c_write_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value, int length){
  119. return _gd32_i2c_rw_bytes(index, address, reg, value, length, 1);
  120. }
  121. static int gd32_i2c_wait_flags(uint32_t i2c_periph, i2c_flag_enum flag, FlagStatus status){
  122. int remain = 500;
  123. while (i2c_flag_get(i2c_periph, flag) != status) {
  124. if (remain-- <= 0){
  125. return -1;
  126. }
  127. }
  128. return 0;
  129. }
  130. static void gd32_i2c_wait_stop(uint32_t i2c_periph){
  131. int remain = 500;
  132. while(I2C_CTL0(i2c_periph) & 0x0200){
  133. if (remain-- <= 0){
  134. break;
  135. }
  136. };
  137. }
  138. static int _gd32_i2c_rw_bytes(uint32_t i2c_index, uint8_t address, uint8_t reg, uint8_t *buffer, int length, int write){
  139. int ret = length;
  140. uint32_t device = iic_device(i2c_index);
  141. i2c_ackpos_config(device, length == 2?I2C_ACKPOS_NEXT:I2C_ACKPOS_CURRENT);
  142. i2c_ack_config(device, I2C_ACK_ENABLE);
  143. if (gd32_i2c_wait_flags(device, I2C_FLAG_I2CBSY, RESET) < 0){
  144. i2c_busy_count[i2c_index]++;
  145. if (i2c_busy_count[i2c_index] >= 10) {
  146. gd32_i2c_deinit(i2c_index);
  147. gd32_i2c_init(i2c_index, i2c_clk[i2c_index]);
  148. i2c_busy_count[i2c_index] = 0;
  149. }
  150. return -1;
  151. }
  152. i2c_busy_count[i2c_index] = 0;
  153. //send reg
  154. i2c_start_on_bus(device);
  155. if (gd32_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
  156. ret = -2;
  157. goto out_i2c_stop_on_bus;
  158. }
  159. if (gd32_i2c_write_address(device, address, 1) < 0){
  160. ret = -3;
  161. goto out_i2c_stop_on_bus;
  162. }
  163. i2c_flag_clear(device, I2C_FLAG_ADDSEND);
  164. /* wait until the transmit data buffer is empty */
  165. if (gd32_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
  166. ret = -4;
  167. goto out_i2c_stop_on_bus;
  168. }
  169. i2c_data_transmit(device, reg);
  170. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  171. ret = -5;
  172. goto out_i2c_stop_on_bus;
  173. }
  174. if (write){
  175. int index = 0;
  176. for (; index < length; index++){
  177. if (gd32_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
  178. ret = -6;
  179. goto out_i2c_stop_on_bus;
  180. }
  181. i2c_data_transmit(device, buffer[index]);
  182. }
  183. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  184. ret = -7;
  185. goto out_i2c_stop_on_bus;
  186. }
  187. ret = length;
  188. goto out_i2c_stop_on_bus;
  189. }
  190. //begin read
  191. i2c_start_on_bus(device);
  192. if (gd32_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
  193. ret = -8;
  194. goto out_i2c_stop_on_bus;
  195. }
  196. if (gd32_i2c_write_address(device, address, 0) < 0){
  197. ret = -9;
  198. goto out_i2c_stop_on_bus;
  199. }
  200. i2c_flag_clear(device, I2C_FLAG_ADDSEND);
  201. if (length< 3) {
  202. i2c_ack_config(device, I2C_ACK_DISABLE);
  203. } else {
  204. while (length > 3) {
  205. if (gd32_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
  206. ret = -10;
  207. goto out_i2c_stop_on_bus;
  208. }
  209. *buffer++ = i2c_data_receive(device);
  210. length--;
  211. }
  212. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  213. ret = -11;
  214. goto out_i2c_stop_on_bus;
  215. }
  216. i2c_ack_config(device, I2C_ACK_DISABLE);
  217. }
  218. while (length > 0) {
  219. if (gd32_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
  220. ret = -12;
  221. goto out_i2c_stop_on_bus;
  222. }
  223. *buffer++ = i2c_data_receive(device);
  224. length--;
  225. }
  226. if (length != 0){
  227. ret = -13;
  228. }
  229. out_i2c_stop_on_bus:
  230. i2c_stop_on_bus(device);
  231. gd32_i2c_wait_stop(device);
  232. return ret;
  233. }