drv_i2c.c 9.2 KB

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