i2c.c 6.3 KB

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