i2c.c 6.2 KB

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