drv_hard_i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-20 BruceOu the first version
  9. */
  10. #include "drv_hard_i2c.h"
  11. #ifdef RT_USING_I2C
  12. #define LOG_TAG "drv.i2c"
  13. #include <rtdbg.h>
  14. #if !defined(BSP_USING_HARD_I2C0) && !defined(BSP_USING_HARD_I2C1) && !defined(BSP_USING_HARD_I2C2) && !defined(BSP_USING_HARD_I2C3) && !defined(BSP_USING_HARD_I2C4) && !defined(BSP_USING_HARD_I2C5)
  15. #error "Please define at least one BSP_USING_I2Cx"
  16. /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
  17. #endif
  18. #if defined(BSP_USING_HARD_I2C0)
  19. struct rt_i2c_bus_device i2c0;
  20. #endif /* BSP_USING_I2C0 */
  21. #if defined(BSP_USING_HARD_I2C1)
  22. struct rt_i2c_bus_device i2c1;
  23. #endif /* BSP_USING_I2C1 */
  24. #if defined(BSP_USING_HARD_I2C2)
  25. struct rt_i2c_bus_device i2c2;
  26. #endif /* BSP_USING_I2C2 */
  27. #if defined(BSP_USING_HARD_I2C3)
  28. struct rt_i2c_bus_device i2c3;
  29. #endif /* BSP_USING_I2C3 */
  30. #if defined(BSP_USING_HARD_I2C4)
  31. struct rt_i2c_bus_device i2c4;
  32. #endif /* BSP_USING_I2C4 */
  33. #if defined(BSP_USING_HARD_I2C5)
  34. struct rt_i2c_bus_device i2c5;
  35. #endif /* BSP_USING_I2C5 */
  36. #if defined (SOC_SERIES_GD32F5xx)
  37. #define i2c_flag_get_gd i2c_add_flag_get
  38. #define I2C_FLAG_RBNE_GD I2C_ADD_FLAG_RBNE
  39. #define i2c_data_receive_gd i2c_add_data_receive
  40. #define I2C_STAT_GD I2C_ADD_STAT
  41. #define I2C_STAT_TBE_GD I2C_ADD_STAT_TBE
  42. #define i2c_data_transmit_gd i2c_add_data_transmit
  43. #define I2C_STAT_TI_GD I2C_ADD_STAT_TI
  44. #define i2c_address10_enable_gd i2c_add_address10_enable
  45. #define i2c_address10_disable_gd i2c_add_address10_disable
  46. #define i2c_master_addressing_gd i2c_add_master_addressing
  47. #define I2C_MASTER_RECEIVE_GD I2C_ADD_MASTER_RECEIVE
  48. #define i2c_transfer_byte_number_config_gd i2c_add_transfer_byte_number_config
  49. #define i2c_start_on_bus_gd i2c_add_start_on_bus
  50. #define I2C_MASTER_TRANSMIT_GD I2C_ADD_MASTER_TRANSMIT
  51. #define I2C_FLAG_I2CBSY_GD I2C_ADD_FLAG_I2CBSY
  52. #define I2C_FLAG_TC_GD I2C_ADD_FLAG_TC
  53. #define I2C_FLAG_TI_GD I2C_ADD_FLAG_TI
  54. #define i2c_stop_on_bus_gd i2c_add_stop_on_bus
  55. #define I2C_FLAG_STPDET_GD I2C_ADD_FLAG_STPDET
  56. #define i2c_timing_config_gd i2c_add_timing_config
  57. #define i2c_master_clock_config_gd i2c_add_master_clock_config
  58. #define i2c_enable_gd i2c_add_enable
  59. #define i2c_flag_clear_gd i2c_add_flag_clear
  60. #else
  61. #define i2c_flag_get_gd i2c_flag_get
  62. #define I2C_FLAG_RBNE_GD I2C_FLAG_RBNE
  63. #define i2c_data_receive_gd i2c_data_receive
  64. #define I2C_STAT_GD I2C_STAT
  65. #define I2C_STAT_TBE_GD I2C_STAT_TBE
  66. #define i2c_data_transmit_gd i2c_data_transmit
  67. #define I2C_STAT_TI_GD I2C_STAT_TI
  68. #define i2c_address10_enable_gd i2c_address10_enable
  69. #define i2c_address10_disable_gd i2c_address10_disable
  70. #define i2c_master_addressing_gd i2c_master_addressing
  71. #define I2C_MASTER_RECEIVE_GD I2C_MASTER_RECEIVE
  72. #define i2c_transfer_byte_number_config_gd i2c_transfer_byte_number_config
  73. #define i2c_start_on_bus_gd i2c_start_on_bus
  74. #define I2C_MASTER_TRANSMIT_GD I2C_MASTER_TRANSMIT
  75. #define I2C_FLAG_I2CBSY_GD I2C_FLAG_I2CBSY
  76. #define I2C_FLAG_TC_GD I2C_FLAG_TC
  77. #define I2C_FLAG_TI_GD I2C_FLAG_TI
  78. #define i2c_stop_on_bus_gd i2c_stop_on_bus
  79. #define I2C_FLAG_STPDET_GD I2C_FLAG_STPDET
  80. #define i2c_timing_config_gd i2c_timing_config
  81. #define i2c_master_clock_config_gd i2c_master_clock_config
  82. #define i2c_enable_gd i2c_enable
  83. #define i2c_flag_clear_gd i2c_flag_clear
  84. #endif
  85. #if defined (SOC_SERIES_GD32F5xx)
  86. #define IS_I2C_LEGACY(periph) ((periph) == I2C0 || (periph) == I2C1 || (periph) == I2C2)
  87. #elif defined (SOC_SERIES_GD32F4xx)
  88. #define IS_I2C_LEGACY(periph) (1)
  89. #elif defined (SOC_SERIES_GD32H7xx)
  90. #define IS_I2C_LEGACY(periph) (0)
  91. #endif
  92. static const struct gd32_i2c_bus gd_i2c_config[] = {
  93. #ifdef BSP_USING_HARD_I2C0
  94. {
  95. I2C0, /* uart peripheral index */
  96. RCU_I2C0, RCU_GPIOB, RCU_GPIOB, /* periph clock, scl gpio clock, sda gpio clock */
  97. GPIOB, GPIO_AF_4, GPIO_PIN_6, /* scl port, scl alternate, scl pin */
  98. GPIOB, GPIO_AF_4, GPIO_PIN_7, /* sda port, sda alternate, sda pin */
  99. &i2c0,
  100. "hwi2c0",
  101. },
  102. #endif
  103. #ifdef BSP_USING_HARD_I2C1
  104. {
  105. I2C1, /* uart peripheral index */
  106. RCU_I2C1, RCU_GPIOH, RCU_GPIOB, /* periph clock, scl gpio clock, sda gpio clock */
  107. GPIOH, GPIO_AF_4, GPIO_PIN_4, /* scl port, scl alternate, scl pin */
  108. GPIOB, GPIO_AF_4, GPIO_PIN_11, /* sda port, sda alternate, sda pin */
  109. &i2c1,
  110. "hwi2c1",
  111. },
  112. #endif
  113. #ifdef BSP_USING_HARD_I2C2
  114. {
  115. I2C2, /* uart peripheral index */
  116. RCU_I2C2, RCU_GPIOA, RCU_GPIOC, /* periph clock, scl gpio clock, sda gpio clock */
  117. GPIOA, GPIO_AF_4, GPIO_PIN_8, /* scl port, scl alternate, scl pin */
  118. GPIOC, GPIO_AF_4, GPIO_PIN_9, /* sda port, sda alternate, sda pin */
  119. &i2c2,
  120. "hwi2c2",
  121. },
  122. #endif
  123. #ifdef BSP_USING_HARD_I2C3
  124. {
  125. I2C3, /* uart peripheral index */
  126. RCU_I2C3, RCU_GPIOF, RCU_GPIOF, /* periph clock, scl gpio clock, sda gpio clock */
  127. GPIOF, GPIO_AF_4, GPIO_PIN_14, /* scl port, scl alternate, scl pin */
  128. GPIOF, GPIO_AF_4, GPIO_PIN_15, /* sda port, sda alternate, sda pin */
  129. &i2c3,
  130. "hwi2c3",
  131. },
  132. #endif
  133. #ifdef BSP_USING_HARD_I2C4
  134. {
  135. I2C4, /* uart peripheral index */
  136. RCU_I2C4, RCU_GPIOG, RCU_GPIOG, /* periph clock, scl gpio clock, sda gpio clock */
  137. GPIOG, GPIO_AF_6, GPIO_PIN_7, /* scl port, scl alternate, scl pin */
  138. GPIOG, GPIO_AF_6, GPIO_PIN_8, /* sda port, sda alternate, sda pin */
  139. &i2c4,
  140. "hwi2c4",
  141. },
  142. #endif
  143. #ifdef BSP_USING_HARD_I2C5
  144. {
  145. I2C5, /* uart peripheral index */
  146. RCU_I2C5, RCU_GPIOF, RCU_GPIOF, /* periph clock, scl gpio clock, sda gpio clock */
  147. GPIOF, GPIO_AF_4, GPIO_PIN_11, /* scl port, scl alternate, scl pin */
  148. GPIOF, GPIO_AF_4, GPIO_PIN_12, /* sda port, sda alternate, sda pin */
  149. &i2c5,
  150. "hwi2c5",
  151. }
  152. #endif
  153. };
  154. /**
  155. * @brief This function initializes the i2c pin.
  156. * @param i2c
  157. * @retval None
  158. */
  159. static void gd32_i2c_gpio_init(const struct gd32_i2c_bus *i2c)
  160. {
  161. /* enable I2C and GPIO clock */
  162. rcu_periph_clock_enable(i2c->scl_gpio_clk);
  163. rcu_periph_clock_enable(i2c->sda_gpio_clk);
  164. rcu_periph_clock_enable(i2c->per_clk);
  165. /* configure I2C_SCL as alternate function push-pull */
  166. gpio_af_set(i2c->scl_port, i2c->scl_af, i2c->scl_pin);
  167. gpio_mode_set(i2c->scl_port, GPIO_MODE_AF, GPIO_PUPD_PULLUP, i2c->scl_pin);
  168. #if defined (SOC_SERIES_GD32H7xx)
  169. gpio_output_options_set(i2c->scl_port, GPIO_OTYPE_OD, GPIO_OSPEED_60MHZ, i2c->scl_pin);
  170. /* configure I2C_SDA as alternate function push-pull */
  171. gpio_af_set(i2c->sda_port, i2c->sda_af, i2c->sda_pin);
  172. gpio_mode_set(i2c->sda_port, GPIO_MODE_AF, GPIO_PUPD_PULLUP, i2c->sda_pin);
  173. gpio_output_options_set(i2c->sda_port, GPIO_OTYPE_OD, GPIO_OSPEED_60MHZ, i2c->sda_pin);
  174. #else
  175. gpio_output_options_set(i2c->scl_port, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, i2c->scl_pin);
  176. /* configure I2C_SDA as alternate function push-pull */
  177. gpio_af_set(i2c->sda_port, i2c->sda_af, i2c->sda_pin);
  178. gpio_mode_set(i2c->sda_port, GPIO_MODE_AF, GPIO_PUPD_PULLUP, i2c->sda_pin);
  179. gpio_output_options_set(i2c->sda_port, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, i2c->sda_pin);
  180. #endif
  181. }
  182. /**
  183. * @brief read data.
  184. * @param i2c_periph
  185. * @param *p_buffer
  186. * @param data_byte
  187. * @retval None
  188. */
  189. static uint8_t gd32_i2c_read(rt_uint32_t i2c_periph, rt_uint8_t *p_buffer, rt_uint16_t data_byte)
  190. {
  191. if (data_byte == 0) return 1;
  192. /* while there is data to be read */
  193. while(data_byte)
  194. {
  195. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32F4xx)
  196. if(IS_I2C_LEGACY(i2c_periph))
  197. {
  198. if(3 == data_byte)
  199. {
  200. /* wait until BTC bit is set */
  201. while(!i2c_flag_get(i2c_periph, I2C_FLAG_BTC));
  202. /* disable acknowledge */
  203. i2c_ack_config(i2c_periph, I2C_ACK_DISABLE);
  204. }
  205. if(2 == data_byte)
  206. {
  207. /* wait until BTC bit is set */
  208. while(!i2c_flag_get(i2c_periph, I2C_FLAG_BTC));
  209. /* send a stop condition to I2C bus */
  210. i2c_stop_on_bus(i2c_periph);
  211. }
  212. /* wait until RBNE bit is set */
  213. if(i2c_flag_get(i2c_periph, I2C_FLAG_RBNE))
  214. {
  215. /* read a byte from the EEPROM */
  216. *p_buffer = i2c_data_receive(i2c_periph);
  217. /* point to the next location where the byte read will be saved */
  218. p_buffer++;
  219. /* decrement the read bytes counter */
  220. data_byte--;
  221. }
  222. }else
  223. #endif
  224. {
  225. /* wait until the RBNE bit is set */
  226. while(!i2c_flag_get_gd(i2c_periph, I2C_FLAG_RBNE_GD));
  227. /* read a byte */
  228. *p_buffer = i2c_data_receive_gd(i2c_periph);
  229. /* point to the next location where the byte read will be saved */
  230. p_buffer++;
  231. /* decrement the read bytes counter */
  232. data_byte--;
  233. }
  234. }
  235. return 0;
  236. }
  237. /**
  238. * @brief write data.
  239. * @param i2c_periph
  240. * @param *p_buffer
  241. * @param data_byte
  242. * @retval None
  243. */
  244. static uint8_t gd32_i2c_write(rt_uint32_t i2c_periph, uint8_t *p_buffer, uint16_t data_byte)
  245. {
  246. if (data_byte == 0) return 1;
  247. while(data_byte)
  248. {
  249. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32F4xx)
  250. if(IS_I2C_LEGACY(i2c_periph))
  251. {
  252. /* data transmission */
  253. i2c_data_transmit(i2c_periph, *p_buffer);
  254. /* point to the next byte to be written */
  255. p_buffer++;
  256. /* decrement the write bytes counter */
  257. data_byte--;
  258. /* wait until the TI bit is set */
  259. while(!i2c_flag_get(i2c_periph, I2C_FLAG_BTC));
  260. }
  261. else
  262. #endif
  263. {
  264. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32H7xx)
  265. /* wait until the transmit data buffer is empty */
  266. I2C_STAT_GD(i2c_periph) |= I2C_STAT_TBE_GD;
  267. while(!i2c_flag_get(i2c_periph, I2C_FLAG_TBE));
  268. while(data_byte)
  269. {
  270. /* wait until the TI bit is set */
  271. while(!i2c_flag_get(i2c_periph, I2C_FLAG_TI_GD));
  272. /* data transmission */
  273. i2c_data_transmit(i2c_periph, *p_buffer);
  274. /* point to the next byte to be written */
  275. p_buffer++;
  276. /* decrement the write bytes counter */
  277. data_byte--;
  278. }
  279. #endif
  280. }
  281. }
  282. if(data_byte != 0)
  283. {
  284. return 1;
  285. }
  286. return 0;
  287. }
  288. /**
  289. * @brief
  290. * @param
  291. * @param
  292. * @param
  293. * @retval
  294. */
  295. static rt_ssize_t gd32_i2c_master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  296. {
  297. static struct rt_i2c_msg *msg;
  298. rt_uint32_t i,w_total_byte=0,r_total_byte=0;
  299. rt_err_t ret = RT_ERROR;
  300. RT_ASSERT(bus != RT_NULL);
  301. struct gd32_i2c_bus *gd32_i2c = (struct gd32_i2c_bus *)bus->priv;
  302. for(i = 0; i < num; i++)
  303. {
  304. msg = &msgs[i];
  305. if(msg->flags & RT_I2C_RD)
  306. {
  307. r_total_byte += msg->len;
  308. }else{
  309. w_total_byte += msg->len;
  310. }
  311. }
  312. for(i = 0; i < num; i++)
  313. {
  314. msg = &msgs[i];
  315. if (!(msg->flags & RT_I2C_NO_START))
  316. {
  317. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32F4xx)
  318. if(IS_I2C_LEGACY(gd32_i2c->i2c_periph))
  319. {
  320. if(msg->flags & RT_I2C_RD)
  321. {
  322. if(i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_I2CBSY))
  323. {
  324. i2c_stop_on_bus(gd32_i2c->i2c_periph);
  325. }
  326. /* enable acknowledge */
  327. i2c_ack_config(gd32_i2c->i2c_periph, I2C_ACK_ENABLE);
  328. /* i2c master sends start signal only when the bus is idle */
  329. while(i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_I2CBSY));
  330. /* send the start signal */
  331. i2c_start_on_bus(gd32_i2c->i2c_periph);
  332. /* i2c master sends START signal successfully */
  333. while(!i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_SBSEND));
  334. i2c_master_addressing(gd32_i2c->i2c_periph, msg->addr, I2C_RECEIVER);
  335. while(!i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_ADDSEND));
  336. /* address flag set means i2c slave sends ACK */
  337. i2c_flag_clear(gd32_i2c->i2c_periph, I2C_FLAG_ADDSEND);
  338. }else {
  339. /* configure slave address */
  340. while(i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_I2CBSY));
  341. //i2c_transfer_byte_number_config(gd32_i2c->i2c_periph, w_total_byte);
  342. /* send a start condition to I2C bus */
  343. i2c_start_on_bus(gd32_i2c->i2c_periph);
  344. while(!i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_SBSEND));
  345. i2c_master_addressing(gd32_i2c->i2c_periph, msg->addr, I2C_TRANSMITTER);
  346. while(!i2c_flag_get(gd32_i2c->i2c_periph, I2C_FLAG_ADDSEND));
  347. i2c_flag_clear(gd32_i2c->i2c_periph, I2C_FLAG_ADDSEND);
  348. }
  349. }else
  350. #endif
  351. {
  352. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32H7xx)
  353. if(msg->flags & RT_I2C_ADDR_10BIT)
  354. {
  355. /* enable 10-bit addressing mode in master mode */
  356. i2c_address10_enable_gd(gd32_i2c->i2c_periph);
  357. }else {
  358. /* disable 10-bit addressing mode in master mode */
  359. i2c_address10_disable_gd(gd32_i2c->i2c_periph);
  360. }
  361. if(msg->flags & RT_I2C_RD)
  362. {
  363. /* configure slave address */
  364. i2c_master_addressing_gd(gd32_i2c->i2c_periph, msg->addr, I2C_MASTER_RECEIVE_GD);
  365. i2c_transfer_byte_number_config_gd(gd32_i2c->i2c_periph, r_total_byte);
  366. /* send a start condition to I2C bus */
  367. i2c_start_on_bus_gd(gd32_i2c->i2c_periph);
  368. }else {
  369. /* configure slave address */
  370. i2c_master_addressing_gd(gd32_i2c->i2c_periph, msg->addr, I2C_MASTER_TRANSMIT_GD);
  371. while(i2c_flag_get_gd(gd32_i2c->i2c_periph, I2C_FLAG_I2CBSY_GD));
  372. i2c_transfer_byte_number_config_gd(gd32_i2c->i2c_periph, w_total_byte);
  373. /* send a start condition to I2C bus */
  374. i2c_start_on_bus_gd(gd32_i2c->i2c_periph);
  375. }
  376. #endif
  377. }
  378. }
  379. if(msg->flags & RT_I2C_RD)
  380. {
  381. if(gd32_i2c_read(gd32_i2c->i2c_periph, msg->buf, msg->len) != 0)
  382. {
  383. LOG_E("i2c bus read failed,i2c bus stop!");
  384. goto out;
  385. }
  386. }else {
  387. if(gd32_i2c_write(gd32_i2c->i2c_periph, msg->buf, msg->len) != 0)
  388. {
  389. LOG_E("i2c bus write failed,i2c bus stop!");
  390. goto out;
  391. }
  392. }
  393. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32H7xx)
  394. if(!IS_I2C_LEGACY(gd32_i2c->i2c_periph))
  395. {
  396. if(r_total_byte != 0)
  397. {
  398. while(!i2c_flag_get_gd(gd32_i2c->i2c_periph, I2C_FLAG_TC_GD));
  399. }
  400. }
  401. #endif
  402. }
  403. ret = i;
  404. out:
  405. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32F4xx)
  406. if(IS_I2C_LEGACY(gd32_i2c->i2c_periph))
  407. {
  408. if(!(msg->flags & RT_I2C_NO_STOP))
  409. {
  410. if(msg->flags & RT_I2C_RD)
  411. {
  412. while((I2C_CTL0(gd32_i2c->i2c_periph) & I2C_CTL0_STOP));
  413. }else{
  414. /* send a stop condition to I2C bus */
  415. i2c_stop_on_bus(gd32_i2c->i2c_periph);
  416. /* wait until stop condition generate */
  417. while((I2C_CTL0(gd32_i2c->i2c_periph) & I2C_CTL0_STOP));
  418. /* clear the STPDET bit */
  419. }
  420. }
  421. }else
  422. #endif
  423. {
  424. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32H7xx)
  425. if(!(msg->flags & RT_I2C_NO_STOP))
  426. {
  427. while(!i2c_flag_get_gd(gd32_i2c->i2c_periph, I2C_FLAG_TC_GD));
  428. /* send a stop condition to I2C bus */
  429. i2c_stop_on_bus_gd(gd32_i2c->i2c_periph);
  430. /* wait until stop condition generate */
  431. while(!i2c_flag_get_gd(gd32_i2c->i2c_periph, I2C_FLAG_STPDET_GD));
  432. /* clear the STPDET bit */
  433. i2c_flag_clear_gd(gd32_i2c->i2c_periph, I2C_FLAG_STPDET_GD);
  434. }
  435. #endif
  436. }
  437. return ret;
  438. }
  439. static const struct rt_i2c_bus_device_ops i2c_ops = {
  440. .master_xfer = gd32_i2c_master_xfer,
  441. .slave_xfer = RT_NULL,
  442. .i2c_bus_control = RT_NULL
  443. };
  444. /**
  445. * @brief I2C initialization function
  446. * @param None
  447. * @retval RT_EOK indicates successful initialization.
  448. */
  449. int rt_hw_i2c_init(void)
  450. {
  451. rt_size_t obj_num = sizeof(gd_i2c_config) / sizeof(gd_i2c_config[0]);
  452. // rt_err_t result;
  453. for(int i = 0; i < obj_num; i++)
  454. {
  455. gd32_i2c_gpio_init(&gd_i2c_config[i]);
  456. /* configure I2C timing. I2C speed clock=400kHz*/
  457. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32F4xx)
  458. if(IS_I2C_LEGACY(gd_i2c_config[i].i2c_periph))
  459. {
  460. i2c_clock_config(gd_i2c_config[i].i2c_periph, 100000, I2C_DTCY_2);
  461. i2c_mode_addr_config(gd_i2c_config[i].i2c_periph, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0xa0);
  462. i2c_enable(gd_i2c_config[i].i2c_periph);
  463. i2c_ack_config(gd_i2c_config[i].i2c_periph, I2C_ACK_ENABLE);
  464. }else
  465. #endif
  466. {
  467. #if defined (SOC_SERIES_GD32F5xx) || defined (SOC_SERIES_GD32H7xx)
  468. i2c_timing_config_gd(gd_i2c_config[i].i2c_periph, 0x1, 0x7, 0);
  469. i2c_master_clock_config_gd(gd_i2c_config[i].i2c_periph, 0x2D, 0x87);
  470. /* enable I2C1 */
  471. i2c_enable_gd(gd_i2c_config[i].i2c_periph);
  472. #endif
  473. }
  474. gd_i2c_config[i].i2c_bus->ops = &i2c_ops;
  475. gd_i2c_config[i].i2c_bus->priv = (void *)&gd_i2c_config[i];
  476. rt_i2c_bus_device_register(gd_i2c_config[i].i2c_bus, gd_i2c_config[i].device_name);
  477. }
  478. return RT_EOK;
  479. }
  480. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  481. #endif /* RT_USING_I2C */