i2c.c 7.3 KB

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