drv_i2c.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-23 lianzhian first implementation.
  9. */
  10. #include "drv_i2c.h"
  11. #include <rtthread.h>
  12. #include "gd32f10x.h"
  13. #ifdef RT_USING_I2C
  14. #include <rtdevice.h>
  15. #define DBG_TAG "drv.I2C"
  16. #ifdef RT_I2C_DEBUG
  17. #define DBG_LVL DBG_LOG
  18. #else
  19. #define DBG_LVL DBG_INFO
  20. #endif
  21. #include <rtdbg.h>
  22. #ifdef RT_USING_I2C_BITOPS
  23. /*user can change this*/
  24. #define I2C_BUS_NAME "i2c2"
  25. /*user should change this to adapt specific board*/
  26. #define I2C_SCL_PIN GPIO_PIN_4
  27. #define I2C_SCL_PORT GPIOE
  28. #define I2C_SCL_CLK RCU_GPIOE
  29. #define I2C_SDA_PIN GPIO_PIN_5
  30. #define I2C_SDA_PORT GPIOE
  31. #define I2C_SDA_CLK RCU_GPIOE
  32. struct gd32_i2c_bit_data
  33. {
  34. struct
  35. {
  36. rcu_periph_enum clk;
  37. rt_uint32_t port;
  38. rt_uint32_t pin;
  39. }scl, sda;
  40. };
  41. static void gpio_set_sda(void *data, rt_int32_t state)
  42. {
  43. struct gd32_i2c_bit_data* bd = data;
  44. if (state)
  45. {
  46. gpio_bit_set(bd->sda.port, bd->sda.pin);
  47. }
  48. else
  49. {
  50. gpio_bit_reset(bd->sda.port, bd->sda.pin);
  51. }
  52. }
  53. static void gpio_set_scl(void *data, rt_int32_t state)
  54. {
  55. struct gd32_i2c_bit_data* bd = data;
  56. if (state)
  57. {
  58. gpio_bit_set(bd->scl.port, bd->scl.pin);
  59. }
  60. else
  61. {
  62. gpio_bit_reset(bd->scl.port, bd->scl.pin);
  63. }
  64. }
  65. static rt_int32_t gpio_get_sda(void *data)
  66. {
  67. struct gd32_i2c_bit_data* bd = data;
  68. return gpio_input_bit_get(bd->sda.port, bd->sda.pin);
  69. }
  70. static rt_int32_t gpio_get_scl(void *data)
  71. {
  72. struct gd32_i2c_bit_data* bd = data;
  73. return gpio_input_bit_get(bd->scl.port, bd->scl.pin);
  74. }
  75. static void gpio_udelay(rt_uint32_t us)
  76. {
  77. int i = ( rcu_clock_freq_get(CK_SYS) / 4000000 * us);
  78. while(i)
  79. {
  80. i--;
  81. }
  82. }
  83. static void drv_i2c_gpio_init(const struct gd32_i2c_bit_data* bd)
  84. {
  85. rcu_periph_clock_enable(bd->sda.clk);
  86. rcu_periph_clock_enable(bd->scl.clk);
  87. gpio_init(bd->sda.port, GPIO_MODE_OUT_OD, GPIO_OSPEED_10MHZ, bd->sda.pin);
  88. gpio_init(bd->scl.port, GPIO_MODE_OUT_OD, GPIO_OSPEED_10MHZ, bd->scl.pin);
  89. gpio_bit_set(bd->sda.port, bd->sda.pin);
  90. gpio_bit_set(bd->scl.port, bd->scl.pin);
  91. }
  92. #else /* use hardware i2c */
  93. struct gd32_i2c_bus
  94. {
  95. struct rt_i2c_bus_device parent;
  96. rt_uint32_t i2c_periph;
  97. };
  98. static int gd32_i2c_read(rt_uint32_t i2c_periph, rt_uint16_t slave_address, rt_uint8_t* p_buffer, rt_uint16_t data_byte)
  99. {
  100. /* wait until I2C bus is idle */
  101. while(i2c_flag_get(i2c_periph, I2C_FLAG_I2CBSY));
  102. /* send a start condition to I2C bus */
  103. i2c_start_on_bus(i2c_periph);
  104. /* wait until SBSEND bit is set */
  105. while(!i2c_flag_get(i2c_periph, I2C_FLAG_SBSEND));
  106. /* send slave address to I2C bus */
  107. i2c_master_addressing(i2c_periph, slave_address<<1, I2C_RECEIVER);
  108. /* wait until ADDSEND bit is set */
  109. while(!i2c_flag_get(i2c_periph, I2C_FLAG_ADDSEND));
  110. /* clear the ADDSEND bit */
  111. i2c_flag_clear(i2c_periph,I2C_FLAG_ADDSEND);
  112. if(1 == data_byte){
  113. /* disable acknowledge */
  114. i2c_ack_config(i2c_periph,I2C_ACK_DISABLE);
  115. /* send a stop condition to I2C bus */
  116. i2c_stop_on_bus(i2c_periph);
  117. }
  118. /* while there is data to be read */
  119. while(data_byte)
  120. {
  121. /* wait until the RBNE bit is set and clear it */
  122. if(i2c_flag_get(i2c_periph, I2C_FLAG_RBNE))
  123. {
  124. /* read a byte from the EEPROM */
  125. *p_buffer = i2c_data_receive(i2c_periph);
  126. /* point to the next location where the byte read will be saved */
  127. p_buffer++;
  128. /* decrement the read bytes counter */
  129. data_byte--;
  130. if(1 == data_byte)
  131. {
  132. /* disable acknowledge */
  133. i2c_ack_config(i2c_periph,I2C_ACK_DISABLE);
  134. /* send a stop condition to I2C bus */
  135. i2c_stop_on_bus(i2c_periph);
  136. }
  137. }
  138. }
  139. /* wait until the stop condition is finished */
  140. while(I2C_CTL0(i2c_periph)&0x0200);
  141. /* enable acknowledge */
  142. i2c_ack_config(i2c_periph,I2C_ACK_ENABLE);
  143. i2c_ackpos_config(i2c_periph,I2C_ACKPOS_CURRENT);
  144. return 0;
  145. }
  146. static int gd32_i2c_write(rt_uint32_t i2c_periph, uint16_t slave_address, uint8_t* p_buffer, uint16_t data_byte)
  147. {
  148. /* wait until I2C bus is idle */
  149. while(i2c_flag_get(i2c_periph, I2C_FLAG_I2CBSY));
  150. /* send a start condition to I2C bus */
  151. i2c_start_on_bus(i2c_periph);
  152. /* wait until SBSEND bit is set */
  153. while(!i2c_flag_get(i2c_periph, I2C_FLAG_SBSEND));
  154. /* send slave address to I2C bus */
  155. i2c_master_addressing(i2c_periph, slave_address<<1, I2C_TRANSMITTER);
  156. /* wait until ADDSEND bit is set */
  157. while(!i2c_flag_get(i2c_periph, I2C_FLAG_ADDSEND));
  158. /* clear the ADDSEND bit */
  159. i2c_flag_clear(i2c_periph,I2C_FLAG_ADDSEND);
  160. /* wait until the transmit data buffer is empty */
  161. while(SET != i2c_flag_get( i2c_periph , I2C_FLAG_TBE));
  162. /* while there is data to be read */
  163. while(data_byte)
  164. {
  165. i2c_data_transmit(i2c_periph, *p_buffer);
  166. /* point to the next byte to be written */
  167. p_buffer++;
  168. /* decrement the write bytes counter */
  169. data_byte --;
  170. /* wait until BTC bit is set */
  171. while(!i2c_flag_get(i2c_periph, I2C_FLAG_BTC));
  172. }
  173. /* send a stop condition to I2C bus */
  174. i2c_stop_on_bus(i2c_periph);
  175. /* wait until the stop condition is finished */
  176. while(I2C_CTL0(i2c_periph)&0x0200);
  177. return 0;
  178. }
  179. static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  180. {
  181. struct rt_i2c_msg *msg;
  182. rt_uint32_t i;
  183. rt_err_t ret = RT_ERROR;
  184. struct gd32_i2c_bus *gd32_i2c = (struct gd32_i2c_bus *)bus;
  185. for (i = 0; i < num; i++)
  186. {
  187. msg = &msgs[i];
  188. if (msg->flags & RT_I2C_ADDR_10BIT)
  189. {
  190. i2c_mode_addr_config(gd32_i2c->i2c_periph,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_10BITS,0);
  191. }
  192. else
  193. {
  194. i2c_mode_addr_config(gd32_i2c->i2c_periph,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,0);
  195. }
  196. if (msg->flags & RT_I2C_RD)
  197. {
  198. if (gd32_i2c_read(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
  199. {
  200. LOG_E("i2c bus write failed,i2c bus stop!");
  201. goto out;
  202. }
  203. }
  204. else
  205. {
  206. if (gd32_i2c_write(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
  207. {
  208. LOG_E("i2c bus write failed,i2c bus stop!");
  209. goto out;
  210. }
  211. }
  212. }
  213. ret = i;
  214. out:
  215. LOG_E("send stop condition\n");
  216. return ret;
  217. }
  218. static const struct rt_i2c_bus_device_ops i2c_ops =
  219. {
  220. gd32_i2c_xfer,
  221. RT_NULL,
  222. RT_NULL
  223. };
  224. #endif /* RT_USING_I2C_BITOPS */
  225. int rt_hw_i2c_init(void)
  226. {
  227. #ifdef RT_USING_I2C_BITOPS
  228. {
  229. static struct rt_i2c_bus_device i2c_device;
  230. static const struct gd32_i2c_bit_data _i2c_bdata =
  231. {
  232. /* SCL */
  233. { I2C_SCL_CLK, I2C_SCL_PORT, I2C_SCL_PIN},
  234. /* SDA */
  235. { I2C_SDA_CLK, I2C_SDA_PORT, I2C_SDA_PIN},
  236. };
  237. static const struct rt_i2c_bit_ops _i2c_bit_ops =
  238. {
  239. (void*)&_i2c_bdata,
  240. gpio_set_sda,
  241. gpio_set_scl,
  242. gpio_get_sda,
  243. gpio_get_scl,
  244. gpio_udelay,
  245. 1,
  246. 100
  247. };
  248. drv_i2c_gpio_init(&_i2c_bdata);
  249. i2c_device.priv = (void *)&_i2c_bit_ops;
  250. rt_i2c_bit_add_bus(&i2c_device, I2C_BUS_NAME);
  251. }
  252. #else /* register hardware I2C */
  253. #ifdef RT_USING_I2C0
  254. #define I2C0_SPEED 100000
  255. static struct gd32_i2c_bus gd32_i2c0;
  256. /* enable GPIOB clock */
  257. rcu_periph_clock_enable(RCU_GPIOB);
  258. /* connect PB6 to I2C0_SCL, PB7 to I2C0_SDA */
  259. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
  260. /* enable I2C clock */
  261. rcu_periph_clock_enable(RCU_I2C0);
  262. /* configure I2C clock */
  263. i2c_clock_config(I2C0,I2C0_SPEED,I2C_DTCY_2);
  264. i2c_enable(I2C0);
  265. /* enable acknowledge */
  266. i2c_ack_config(I2C0,I2C_ACK_ENABLE);
  267. rt_memset((void *)&gd32_i2c0, 0, sizeof(struct gd32_i2c_bus));
  268. gd32_i2c0.parent.ops = &i2c_ops;
  269. gd32_i2c0.i2c_periph = I2C0;
  270. rt_i2c_bus_device_register(&gd32_i2c0.parent, "i2c0");
  271. #endif
  272. #ifdef RT_USING_I2C1
  273. #define I2C1_SPEED 100000
  274. static struct gd32_i2c_bus gd32_i2c1;
  275. /* enable GPIOB clock */
  276. rcu_periph_clock_enable(RCU_GPIOB);
  277. /* connect PB10 to I2C1_SCL, PB11 to I2C1_SDA */
  278. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10 | GPIO_PIN_11);
  279. /* enable I2C clock */
  280. rcu_periph_clock_enable(RCU_I2C1);
  281. /* configure I2C clock */
  282. i2c_clock_config(I2C1,I2C1_SPEED,I2C_DTCY_2);
  283. i2c_enable(I2C1);
  284. /* enable acknowledge */
  285. i2c_ack_config(I2C1,I2C_ACK_ENABLE);
  286. rt_memset((void *)&gd32_i2c1, 0, sizeof(struct gd32_i2c_bus));
  287. gd32_i2c1.parent.ops = &i2c_ops;
  288. gd32_i2c1.i2c_periph = I2C1;
  289. rt_i2c_bus_device_register(&gd32_i2c1.parent, "i2c1");
  290. #endif
  291. #endif /* RT_USING_I2C_BITOPS */
  292. return 0;
  293. }
  294. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  295. #endif
  296. /* end of i2c driver */