i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 void _delay_1us(uint32_t n)
  8. {
  9. while(n-->0)
  10. {
  11. int N=1000;
  12. while(N-->0);
  13. }
  14. }
  15. static void _i2c_deinit(uint32_t i2c_periph)
  16. {
  17. switch(i2c_periph){
  18. case I2C0:
  19. /* reset I2C0 */
  20. rcu_periph_reset_enable(RCU_I2C0RST);
  21. _delay_1us(10);
  22. rcu_periph_reset_disable(RCU_I2C0RST);
  23. break;
  24. case I2C1:
  25. /* reset I2C1 */
  26. rcu_periph_reset_enable(RCU_I2C1RST);
  27. _delay_1us(10);
  28. rcu_periph_reset_disable(RCU_I2C1RST);
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. void gd32_i2c_init(uint32_t i2c_device, uint32_t rate){
  35. uint32_t device = iic_device(i2c_device);
  36. rcu_periph_clock_enable(RCU_GPIOB);
  37. if (device == I2C0) {
  38. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_8|GPIO_PIN_9);
  39. gpio_mode_af(GPIOB ,GPIO_PUPD_NONE, GPIO_PIN_8|GPIO_PIN_9);
  40. rcu_periph_clock_enable(RCU_I2C0);
  41. _i2c_deinit(I2C0);
  42. } else {
  43. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10|GPIO_PIN_11);
  44. gpio_mode_af(GPIOB ,GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11);
  45. rcu_periph_clock_enable(RCU_I2C1);
  46. _i2c_deinit(I2C1);
  47. }
  48. i2c_clock_config(device, rate, I2C_DTCY_2);
  49. i2c_enable(device);
  50. }
  51. static int gd32_i2c_write_address(uint32_t deivce, uint8_t address, uint8_t write){
  52. uint32_t times;
  53. if (!write){
  54. address |= 1;
  55. }
  56. I2C_DATA((deivce)) = address;
  57. for (times = 10000; times > 0; times--) {
  58. if (i2c_flag_get((deivce), I2C_FLAG_ADDSEND)) {
  59. return 0;
  60. }
  61. }
  62. return -1;
  63. }
  64. int gd32_i2c_read_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value){
  65. return _gd32_i2c_rw_bytes(index, address, reg, value, 1, 0);
  66. }
  67. int gd32_i2c_read_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length){
  68. return _gd32_i2c_rw_bytes(index, address, reg, buffer, length, 0);
  69. }
  70. int gd32_i2c_write_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t value){
  71. return _gd32_i2c_rw_bytes(index, address, reg, &value, 1, 1);
  72. }
  73. int gd32_i2c_write_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value, int length){
  74. return _gd32_i2c_rw_bytes(index, address, reg, value, length, 1);
  75. }
  76. static int gd32_i2c_wait_flags(uint32_t i2c_periph, i2c_flag_enum flag, FlagStatus status){
  77. int remain = 100000;
  78. while (i2c_flag_get(i2c_periph, flag) != status) {
  79. if (remain-- <= 0){
  80. return -1;
  81. }
  82. }
  83. return 0;
  84. }
  85. static void gd32_i2c_wait_stop(uint32_t i2c_periph){
  86. int remain = 100000;
  87. while(I2C_CTL0(i2c_periph) & 0x0200){
  88. if (remain-- <= 0){
  89. break;
  90. }
  91. };
  92. }
  93. static int _gd32_i2c_rw_bytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length, int write){
  94. int ret = length;
  95. uint32_t device = iic_device(index);
  96. i2c_ackpos_config(device, length == 2?I2C_ACKPOS_NEXT:I2C_ACKPOS_CURRENT);
  97. i2c_ack_config(device, I2C_ACK_ENABLE);
  98. if (gd32_i2c_wait_flags(device, I2C_FLAG_I2CBSY, RESET) < 0){
  99. ret = -1;
  100. goto out_i2c_stop_on_bus;
  101. }
  102. //send reg
  103. i2c_start_on_bus(device);
  104. if (gd32_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
  105. ret = -2;
  106. goto out_i2c_stop_on_bus;
  107. }
  108. if (gd32_i2c_write_address(device, address, 1) < 0){
  109. ret = -3;
  110. goto out_i2c_stop_on_bus;
  111. }
  112. i2c_flag_clear(device, I2C_FLAG_ADDSEND);
  113. /* wait until the transmit data buffer is empty */
  114. if (gd32_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
  115. ret = -4;
  116. goto out_i2c_stop_on_bus;
  117. }
  118. i2c_data_transmit(device, reg);
  119. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  120. ret = -5;
  121. goto out_i2c_stop_on_bus;
  122. }
  123. if (write){
  124. int index = 0;
  125. for (; index < length; index++){
  126. if (gd32_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
  127. ret = -6;
  128. goto out_i2c_stop_on_bus;
  129. }
  130. i2c_data_transmit(device, buffer[index]);
  131. }
  132. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  133. ret = -7;
  134. goto out_i2c_stop_on_bus;
  135. }
  136. ret = length;
  137. goto out_i2c_stop_on_bus;
  138. }
  139. //begin read
  140. i2c_start_on_bus(device);
  141. if (gd32_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
  142. ret = -8;
  143. goto out_i2c_stop_on_bus;
  144. }
  145. if (gd32_i2c_write_address(device, address, 0) < 0){
  146. ret = -9;
  147. goto out_i2c_stop_on_bus;
  148. }
  149. i2c_flag_clear(device, I2C_FLAG_ADDSEND);
  150. if (length< 3) {
  151. i2c_ack_config(device, I2C_ACK_DISABLE);
  152. } else {
  153. while (length > 3) {
  154. if (gd32_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
  155. ret = -10;
  156. goto out_i2c_stop_on_bus;
  157. }
  158. *buffer++ = i2c_data_receive(device);
  159. length--;
  160. }
  161. if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  162. ret = -11;
  163. goto out_i2c_stop_on_bus;
  164. }
  165. i2c_ack_config(device, I2C_ACK_DISABLE);
  166. }
  167. while (length > 0) {
  168. if (gd32_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
  169. ret = -12;
  170. goto out_i2c_stop_on_bus;
  171. }
  172. *buffer++ = i2c_data_receive(device);
  173. length--;
  174. }
  175. if (length != 0){
  176. ret = -13;
  177. }
  178. out_i2c_stop_on_bus:
  179. i2c_stop_on_bus(device);
  180. gd32_i2c_wait_stop(device);
  181. return ret;
  182. }