i2c.c 6.2 KB

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