drv_i2c.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-27 hqfang first implementation.
  9. */
  10. #include "drv_i2c.h"
  11. #ifdef RT_USING_I2C
  12. #if !defined(BSP_USING_I2C0) && !defined(BSP_USING_I2C1)
  13. #error "Please define at least one BSP_USING_I2Cx"
  14. /* this driver can be disabled at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable I2C */
  15. #endif
  16. static struct gd32_i2c_config i2c_config[] =
  17. {
  18. #ifdef BSP_USING_I2C0
  19. {
  20. "i2c0",
  21. I2C0,
  22. 100000,
  23. },
  24. #endif
  25. #ifdef BSP_USING_I2C1
  26. {
  27. "i2c1",
  28. I2C1,
  29. 100000,
  30. },
  31. #endif
  32. };
  33. static struct gd32_i2c i2c_obj[sizeof(i2c_config) / sizeof(i2c_config[0])] = {0};
  34. #define GD32_I2C_TIMEOUT 10
  35. static int gd32_i2c_read(rt_uint32_t i2c_periph, rt_uint16_t slave_address, rt_uint8_t *p_buffer, rt_uint16_t cnt)
  36. {
  37. /* send slave address to I2C bus */
  38. i2c_master_addressing(i2c_periph, slave_address << 1, I2C_RECEIVER);
  39. /* wait until ADDSEND bit is set */
  40. while (!i2c_flag_get(i2c_periph, I2C_FLAG_ADDSEND));
  41. /* clear the ADDSEND bit */
  42. i2c_flag_clear(i2c_periph, I2C_FLAG_ADDSEND);
  43. /* while there is data to be read */
  44. while (cnt)
  45. {
  46. if (cnt == 1)
  47. {
  48. // Send NACK for last 1 byte receive
  49. i2c_ack_config(i2c_periph, I2C_ACK_DISABLE);
  50. }
  51. /* wait until the RBNE bit is set */
  52. while (i2c_flag_get(i2c_periph, I2C_FLAG_RBNE) == RESET);
  53. /* read a byte from i2c */
  54. *p_buffer = i2c_data_receive(i2c_periph);
  55. /* point to the next location where the byte read will be saved */
  56. p_buffer++;
  57. /* decrement the read bytes counter */
  58. cnt--;
  59. }
  60. return 0;
  61. }
  62. static int gd32_i2c_write(rt_uint32_t i2c_periph, uint16_t slave_address, uint8_t *p_buffer, uint16_t cnt)
  63. {
  64. /* send slave address to I2C bus */
  65. i2c_master_addressing(i2c_periph, slave_address << 1, I2C_TRANSMITTER);
  66. /* wait until ADDSEND bit is set */
  67. while (!i2c_flag_get(i2c_periph, I2C_FLAG_ADDSEND));
  68. /* clear the ADDSEND bit */
  69. i2c_flag_clear(i2c_periph, I2C_FLAG_ADDSEND);
  70. /* wait until the transmit data buffer is empty */
  71. while (SET != i2c_flag_get(i2c_periph, I2C_FLAG_TBE));
  72. /* while there is data to be read */
  73. while (cnt)
  74. {
  75. i2c_data_transmit(i2c_periph, *p_buffer);
  76. /* point to the next byte to be written */
  77. p_buffer++;
  78. /* decrement the write bytes counter */
  79. cnt--;
  80. /* wait until BTC bit is set */
  81. while (!i2c_flag_get(i2c_periph, I2C_FLAG_BTC));
  82. }
  83. return 0;
  84. }
  85. static void gd32_i2c_configure(struct gd32_i2c_config *i2c_cfg)
  86. {
  87. RT_ASSERT(i2c_cfg != RT_NULL);
  88. /* configure i2c speed to 100Khz */
  89. i2c_clock_config(i2c_cfg->i2c_periph, i2c_cfg->speed, I2C_DTCY_2);
  90. /* enable I2C */
  91. i2c_enable(i2c_cfg->i2c_periph);
  92. /* enable acknowledge */
  93. i2c_ack_config(i2c_cfg->i2c_periph, I2C_ACK_ENABLE);
  94. }
  95. static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *device, struct rt_i2c_msg msgs[], rt_uint32_t num)
  96. {
  97. struct rt_i2c_msg *msg;
  98. rt_uint32_t i;
  99. rt_err_t ret = RT_ERROR;
  100. rt_uint16_t last_flags;
  101. RT_ASSERT(device != RT_NULL);
  102. struct gd32_i2c *i2c_obj = (struct gd32_i2c *)(device);
  103. struct gd32_i2c_config *i2c_cfg = (struct gd32_i2c_config *)(i2c_obj->config);
  104. RT_ASSERT(i2c_cfg != RT_NULL);
  105. /* wait until I2C bus is idle */
  106. while (i2c_flag_get(i2c_cfg->i2c_periph, I2C_FLAG_I2CBSY));
  107. if (num)
  108. {
  109. if (msg[0].flags & RT_I2C_ADDR_10BIT)
  110. {
  111. i2c_mode_addr_config(i2c_cfg->i2c_periph, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_10BITS, 0x82);
  112. }
  113. else
  114. {
  115. i2c_mode_addr_config(i2c_cfg->i2c_periph, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0x82);
  116. }
  117. }
  118. for (i = 0; i < num; i++)
  119. {
  120. msg = &msgs[i];
  121. if (!(msg->flags & RT_I2C_NO_START))
  122. {
  123. /* send a start condition to I2C bus */
  124. i2c_start_on_bus(i2c_cfg->i2c_periph);
  125. /* wait until SBSEND bit is set */
  126. while (!i2c_flag_get(i2c_cfg->i2c_periph, I2C_FLAG_SBSEND));
  127. }
  128. if (msg->flags & RT_I2C_RD)
  129. {
  130. gd32_i2c_read(i2c_cfg->i2c_periph, msg->addr, msg->buf, msg->len);
  131. }
  132. else
  133. {
  134. gd32_i2c_write(i2c_cfg->i2c_periph, msg->addr, msg->buf, msg->len);
  135. }
  136. }
  137. if (num)
  138. {
  139. /* send a stop condition to I2C bus */
  140. i2c_stop_on_bus(i2c_cfg->i2c_periph);
  141. /* wait until the stop condition is finished */
  142. while (I2C_CTL0(i2c_cfg->i2c_periph) & I2C_CTL0_STOP);
  143. }
  144. i2c_ack_config(i2c_cfg->i2c_periph, I2C_ACK_ENABLE);
  145. ret = i;
  146. return ret;
  147. }
  148. static const struct rt_i2c_bus_device_ops i2c_ops =
  149. {
  150. gd32_i2c_xfer,
  151. RT_NULL,
  152. RT_NULL
  153. };
  154. int rt_hw_i2c_init(void)
  155. {
  156. rt_size_t obj_num;
  157. int index;
  158. rt_err_t result = 0;
  159. #ifdef BSP_USING_I2C0
  160. rcu_periph_clock_enable(RCU_I2C0);
  161. #endif
  162. #ifdef BSP_USING_I2C1
  163. rcu_periph_clock_enable(RCU_I2C1);
  164. #endif
  165. obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
  166. for (index = 0; index < obj_num; index++)
  167. {
  168. /* init i2c object */
  169. i2c_obj[index].config = &i2c_config[index];
  170. i2c_obj[index].bus.ops = &i2c_ops;
  171. /* init i2c device */
  172. gd32_i2c_configure(&i2c_config[index]);
  173. /* register i2c device */
  174. result = rt_i2c_bus_device_register(&i2c_obj[index].bus,
  175. i2c_obj[index].config->name
  176. );
  177. RT_ASSERT(result == RT_EOK);
  178. }
  179. return 0;
  180. }
  181. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  182. #endif
  183. /* end of i2c driver */