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