i2c.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "bsp.h"
  2. #include "i2c.h"
  3. #include "libs/utils.h"
  4. #include "bsp/delay.h"
  5. static int _shark_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 volatile uint8_t i2c_lock[2];
  10. /*
  11. * 当读写i2c slave设备的时候,MCU复位,有概率会导致slave设备永远会锁住总线,导致永远busy,复位MCU无效
  12. * 这里,需要通过i2c的reset位配合sda,sck拉高,强行让slave释放总线
  13. */
  14. static void shark_i2c_busy_recovery(uint32_t i2c_periph){
  15. if (i2c_periph == I2C0) {
  16. gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
  17. gpio_bit_set(GPIOB, GPIO_PIN_6);
  18. gpio_bit_set(GPIOB, GPIO_PIN_7);
  19. } else {
  20. gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10 | GPIO_PIN_11);
  21. gpio_bit_set(GPIOB, GPIO_PIN_10);
  22. gpio_bit_set(GPIOB, GPIO_PIN_11);
  23. }
  24. i2c_software_reset_config(i2c_periph, 1);
  25. delay_us(10);
  26. i2c_software_reset_config(i2c_periph, 0);
  27. }
  28. void shark_i2c_init(uint32_t i2c_index, uint32_t rate){
  29. uint32_t device = iic_device(i2c_index);
  30. i2c_clk[i2c_index] = rate;
  31. i2c_lock[i2c_index] = 0;
  32. rcu_periph_clock_enable(RCU_GPIOB);
  33. if (device == I2C0) {
  34. rcu_periph_clock_enable(RCU_I2C0);
  35. shark_i2c_busy_recovery(I2C0);
  36. i2c_deinit(I2C0);
  37. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
  38. } else {
  39. rcu_periph_clock_enable(RCU_I2C1);
  40. shark_i2c_busy_recovery(I2C1);
  41. i2c_deinit(I2C1);
  42. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10 | GPIO_PIN_11);
  43. }
  44. i2c_clock_config(device, rate, I2C_DTCY_2);
  45. i2c_enable(device);
  46. }
  47. void shark_i2c_deinit(uint32_t i2c_index){
  48. uint32_t device = iic_device(i2c_index);
  49. i2c_disable(device);
  50. i2c_deinit(device);
  51. if (device == I2C0){
  52. rcu_periph_clock_disable(RCU_I2C0);
  53. }else {
  54. rcu_periph_clock_disable(RCU_I2C1);
  55. }
  56. }
  57. static int shark_i2c_write_address(uint32_t deivce, uint8_t address, uint8_t write){
  58. uint32_t times;
  59. if (!write){
  60. address |= 1;
  61. }
  62. I2C_DATA((deivce)) = address;
  63. for (times = 1000; times > 0; times--) {
  64. if (i2c_flag_get((deivce), I2C_FLAG_ADDSEND)) {
  65. return 0;
  66. }
  67. }
  68. return -1;
  69. }
  70. int shark_i2c_read_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value){
  71. return _shark_i2c_rw_bytes(index, address, reg, value, 1, 0);
  72. }
  73. int shark_i2c_read_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length){
  74. return _shark_i2c_rw_bytes(index, address, reg, buffer, length, 0);
  75. }
  76. int shark_i2c_write_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t value){
  77. return _shark_i2c_rw_bytes(index, address, reg, &value, 1, 1);
  78. }
  79. int shark_i2c_write_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value, int length){
  80. return _shark_i2c_rw_bytes(index, address, reg, value, length, 1);
  81. }
  82. static int shark_i2c_wait_flags(uint32_t i2c_periph, i2c_flag_enum flag, FlagStatus status){
  83. int remain = 1000;
  84. while (i2c_flag_get(i2c_periph, flag) != status) {
  85. if (remain-- <= 0){
  86. return -1;
  87. }
  88. }
  89. return 0;
  90. }
  91. static void shark_i2c_wait_stop(uint32_t i2c_periph){
  92. int remain = 1000;
  93. while(I2C_CTL0(i2c_periph) & 0x0200){
  94. if (remain-- <= 0){
  95. break;
  96. }
  97. };
  98. }
  99. static int lock_i2c(int i2c_index){
  100. return 0;
  101. }
  102. static int unlock_i2c(int i2c_index){
  103. return 0;
  104. }
  105. static int _shark_i2c_rw_bytes(uint32_t i2c_index, uint8_t address, uint8_t reg, uint8_t *buffer, int length, int write){
  106. int ret = length;
  107. uint32_t device = iic_device(i2c_index);
  108. if (lock_i2c(i2c_index) == 1) {
  109. return -100;
  110. }
  111. i2c_ackpos_config(device, length == 2?I2C_ACKPOS_NEXT:I2C_ACKPOS_CURRENT);
  112. i2c_ack_config(device, I2C_ACK_ENABLE);
  113. if (shark_i2c_wait_flags(device, I2C_FLAG_I2CBSY, RESET) < 0){
  114. i2c_busy_count[i2c_index]++;
  115. if (i2c_busy_count[i2c_index] >= 10) {
  116. shark_i2c_deinit(i2c_index);
  117. shark_i2c_init(i2c_index, i2c_clk[i2c_index]);
  118. i2c_busy_count[i2c_index] = 0;
  119. }
  120. ret = -1;
  121. goto out_i2c_stop_on_bus;
  122. }
  123. i2c_busy_count[i2c_index] = 0;
  124. //send reg
  125. i2c_start_on_bus(device);
  126. if (shark_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
  127. ret = -2;
  128. goto out_i2c_stop_on_bus;
  129. }
  130. if (shark_i2c_write_address(device, address, 1) < 0){
  131. ret = -3;
  132. goto out_i2c_stop_on_bus;
  133. }
  134. i2c_flag_clear(device, I2C_FLAG_ADDSEND);
  135. /* wait until the transmit data buffer is empty */
  136. if (shark_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
  137. ret = -4;
  138. goto out_i2c_stop_on_bus;
  139. }
  140. i2c_data_transmit(device, reg);
  141. if (shark_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  142. ret = -5;
  143. goto out_i2c_stop_on_bus;
  144. }
  145. if (write){
  146. int index = 0;
  147. for (; index < length; index++){
  148. if (shark_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
  149. ret = -6;
  150. goto out_i2c_stop_on_bus;
  151. }
  152. i2c_data_transmit(device, buffer[index]);
  153. }
  154. if (shark_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  155. ret = -7;
  156. goto out_i2c_stop_on_bus;
  157. }
  158. ret = length;
  159. goto out_i2c_stop_on_bus;
  160. }
  161. //begin read
  162. i2c_start_on_bus(device);
  163. if (shark_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
  164. ret = -8;
  165. goto out_i2c_stop_on_bus;
  166. }
  167. if (shark_i2c_write_address(device, address, 0) < 0){
  168. ret = -9;
  169. goto out_i2c_stop_on_bus;
  170. }
  171. i2c_flag_clear(device, I2C_FLAG_ADDSEND);
  172. if (length< 3) {
  173. i2c_ack_config(device, I2C_ACK_DISABLE);
  174. } else {
  175. while (length > 3) {
  176. if (shark_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
  177. ret = -10;
  178. goto out_i2c_stop_on_bus;
  179. }
  180. *buffer++ = i2c_data_receive(device);
  181. length--;
  182. }
  183. if (shark_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
  184. ret = -11;
  185. goto out_i2c_stop_on_bus;
  186. }
  187. i2c_ack_config(device, I2C_ACK_DISABLE);
  188. }
  189. while (length > 0) {
  190. if (shark_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
  191. ret = -12;
  192. goto out_i2c_stop_on_bus;
  193. }
  194. *buffer++ = i2c_data_receive(device);
  195. length--;
  196. }
  197. if (length != 0){
  198. ret = -13;
  199. }
  200. out_i2c_stop_on_bus:
  201. if (ret != -1) {
  202. i2c_stop_on_bus(device);
  203. shark_i2c_wait_stop(device);
  204. }
  205. unlock_i2c(i2c_index);
  206. return ret;
  207. }