drv_i2c.c 9.8 KB

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