drv_i2c.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-10-19 Nations first version
  9. */
  10. #include "drv_i2c.h"
  11. #ifdef RT_USING_I2C
  12. #define DBG_TAG "drv.I2C"
  13. #ifdef RT_I2C_DEBUG
  14. #define DBG_LVL DBG_LOG
  15. #else
  16. #define DBG_LVL DBG_INFO
  17. #endif
  18. #include <rtdbg.h>
  19. #ifdef RT_USING_I2C_BITOPS
  20. static const struct n32_soft_i2c_config soft_i2c_config[] =
  21. {
  22. #ifdef BSP_USING_I2C1
  23. I2C1_BUS_CONFIG,
  24. #endif
  25. #ifdef BSP_USING_I2C2
  26. I2C2_BUS_CONFIG,
  27. #endif
  28. #ifdef BSP_USING_I2C3
  29. I2C3_BUS_CONFIG,
  30. #endif
  31. #ifdef BSP_USING_I2C4
  32. I2C4_BUS_CONFIG,
  33. #endif
  34. };
  35. static struct n32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  36. /**
  37. *\*\name n32_i2c_gpio_init
  38. *\*\fun Initializes the i2c pin.
  39. *\*\param i2c dirver class
  40. *\*\return none
  41. **/
  42. static void n32_i2c_gpio_init(struct n32_i2c *i2c)
  43. {
  44. struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)i2c->ops.data;
  45. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  46. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  47. rt_pin_write(cfg->scl, PIN_HIGH);
  48. rt_pin_write(cfg->sda, PIN_HIGH);
  49. }
  50. static void n32_i2c_pin_init(void)
  51. {
  52. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);
  53. for(rt_size_t i = 0; i < obj_num; i++)
  54. {
  55. n32_i2c_gpio_init(&i2c_obj[i]);
  56. }
  57. }
  58. /**
  59. *\*\name n32_set_sda
  60. *\*\fun sets the sda pin.
  61. *\*\param data config class
  62. *\*\param state sda pin state
  63. *\*\return none
  64. **/
  65. static void n32_set_sda(void *data, rt_int32_t state)
  66. {
  67. struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
  68. if (state)
  69. {
  70. rt_pin_write(cfg->sda, PIN_HIGH);
  71. }
  72. else
  73. {
  74. rt_pin_write(cfg->sda, PIN_LOW);
  75. }
  76. }
  77. /**
  78. *\*\name n32_set_scl
  79. *\*\fun sets the scl pin.
  80. *\*\param data config class
  81. *\*\param state scl pin state
  82. *\*\return none
  83. **/
  84. static void n32_set_scl(void *data, rt_int32_t state)
  85. {
  86. struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
  87. if (state)
  88. {
  89. rt_pin_write(cfg->scl, PIN_HIGH);
  90. }
  91. else
  92. {
  93. rt_pin_write(cfg->scl, PIN_LOW);
  94. }
  95. }
  96. /**
  97. *\*\name n32_get_sda
  98. *\*\fun gets the sda pin state.
  99. *\*\param data config class
  100. *\*\return sda pin state
  101. **/
  102. static rt_int32_t n32_get_sda(void *data)
  103. {
  104. struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
  105. return rt_pin_read(cfg->sda);
  106. }
  107. /**
  108. *\*\name n32_get_scl
  109. *\*\fun gets the scl pin state.
  110. *\*\param data config class
  111. *\*\return scl pin state
  112. **/
  113. static rt_int32_t n32_get_scl(void *data)
  114. {
  115. struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
  116. return rt_pin_read(cfg->scl);
  117. }
  118. /**
  119. *\*\name n32_udelay
  120. *\*\fun The time delay function.
  121. *\*\param us
  122. *\*\return none
  123. **/
  124. static void n32_udelay(rt_uint32_t us)
  125. {
  126. rt_uint32_t ticks;
  127. rt_uint32_t told, tnow, tcnt = 0;
  128. rt_uint32_t reload = SysTick->LOAD;
  129. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  130. told = SysTick->VAL;
  131. while (1)
  132. {
  133. tnow = SysTick->VAL;
  134. if (tnow != told)
  135. {
  136. if (tnow < told)
  137. {
  138. tcnt += told - tnow;
  139. }
  140. else
  141. {
  142. tcnt += reload - tnow + told;
  143. }
  144. told = tnow;
  145. if (tcnt >= ticks)
  146. {
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. static const struct rt_i2c_bit_ops n32_bit_ops_default =
  153. {
  154. .data = RT_NULL,
  155. .pin_init = n32_i2c_pin_init,
  156. .set_sda = n32_set_sda,
  157. .set_scl = n32_set_scl,
  158. .get_sda = n32_get_sda,
  159. .get_scl = n32_get_scl,
  160. .udelay = n32_udelay,
  161. .delay_us = 1,
  162. .timeout = 100,
  163. .i2c_pin_init_flag = RT_FALSE
  164. };
  165. /**
  166. *\*\name n32_i2c_bus_unlock
  167. *\*\fun If i2c is locked, this function will unlock it.
  168. *\*\param cfg
  169. *\*\return RT_EOK indicates successful unlock
  170. **/
  171. static rt_err_t n32_i2c_bus_unlock(const struct n32_soft_i2c_config *cfg)
  172. {
  173. rt_int32_t i = 0;
  174. if (PIN_LOW == rt_pin_read(cfg->sda))
  175. {
  176. while (i++ < 9)
  177. {
  178. rt_pin_write(cfg->scl, PIN_HIGH);
  179. n32_udelay(100);
  180. rt_pin_write(cfg->scl, PIN_LOW);
  181. n32_udelay(100);
  182. }
  183. }
  184. if (PIN_LOW == rt_pin_read(cfg->sda))
  185. {
  186. return -RT_ERROR;
  187. }
  188. return RT_EOK;
  189. }
  190. #endif /* RT_USING_I2C_BITOPS */
  191. #ifdef RT_USING_HARDWARE_I2C
  192. #define I2CT_FLAG_TIMEOUT ((uint32_t)0x1000)
  193. #define I2CT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))
  194. static uint32_t I2CTimeout = I2CT_LONG_TIMEOUT;
  195. static int rt_i2c_read(rt_uint32_t i2c_periph, rt_uint16_t slave_address, rt_uint8_t* p_buffer, rt_uint16_t data_byte)
  196. {
  197. I2CTimeout = I2CT_LONG_TIMEOUT;
  198. /* wait until I2C bus is idle */
  199. while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_BUSY))
  200. {
  201. if ((I2CTimeout--) == 0)
  202. return 9;
  203. };
  204. I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);
  205. /** Send START condition */
  206. I2C_GenerateStart((I2C_Module*)i2c_periph, ENABLE);
  207. I2CTimeout = I2CT_LONG_TIMEOUT;
  208. /* wait until SBSEND bit is set */
  209. while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_MODE_FLAG)) // EV5
  210. {
  211. if ((I2CTimeout--) == 0)
  212. return 10;
  213. };
  214. /* send slave address to I2C bus */
  215. I2C_SendAddr7bit((I2C_Module*)i2c_periph, slave_address, I2C_DIRECTION_RECV);
  216. I2CTimeout = I2CT_LONG_TIMEOUT;
  217. while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_RXMODE_FLAG)) // EV6
  218. {
  219. if ((I2CTimeout--) == 0)
  220. return 6;
  221. };
  222. /* while there is data to be read */
  223. while (data_byte)
  224. {
  225. /* wait until the RBNE bit is set and clear it */
  226. if (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_RXDATNE))
  227. {
  228. /* read a byte*/
  229. *p_buffer = I2C_RecvData((I2C_Module*)i2c_periph);
  230. /* point to the next location where the byte read will be saved */
  231. p_buffer++;
  232. /* decrement the read bytes counter */
  233. data_byte--;
  234. if (1 == data_byte)
  235. {
  236. /* disable acknowledge */
  237. I2C_ConfigAck((I2C_Module*)i2c_periph, DISABLE);
  238. /* send a stop condition to I2C bus */
  239. I2C_GenerateStop((I2C_Module*)i2c_periph, ENABLE);
  240. }
  241. }
  242. }
  243. /* wait until the stop condition is finished */
  244. while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_STOPF))
  245. {
  246. if ((I2CTimeout--) == 0)
  247. return 7;
  248. };
  249. /* enable acknowledge */
  250. I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);
  251. I2C_ConfigNackLocation((I2C_Module*)i2c_periph,I2C_NACK_POS_CURRENT);
  252. return 0;
  253. }
  254. static int rt_i2c_write(rt_uint32_t i2c_periph, uint16_t slave_address, uint8_t* p_buffer, uint16_t data_byte)
  255. {
  256. uint8_t* sendBufferPtr = p_buffer;
  257. I2CTimeout = I2CT_LONG_TIMEOUT;
  258. while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_BUSY))
  259. {
  260. if ((I2CTimeout--) == 0)
  261. return 4;
  262. };
  263. I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);
  264. I2C_GenerateStart((I2C_Module*)i2c_periph, ENABLE);
  265. I2CTimeout = I2CT_LONG_TIMEOUT;
  266. while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_MODE_FLAG)) // EV5
  267. {
  268. if ((I2CTimeout--) == 0)
  269. return 5;
  270. };
  271. I2C_SendAddr7bit((I2C_Module*)i2c_periph, slave_address, I2C_DIRECTION_SEND);
  272. I2CTimeout = I2CT_LONG_TIMEOUT;
  273. while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_TXMODE_FLAG)) // EV6
  274. {
  275. if ((I2CTimeout--) == 0)
  276. return 6;
  277. };
  278. /* send data */
  279. while (data_byte-- > 0)
  280. {
  281. I2C_SendData((I2C_Module*)i2c_periph, *sendBufferPtr++);
  282. I2CTimeout = I2CT_LONG_TIMEOUT;
  283. while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_DATA_SENDING)) // EV8
  284. {
  285. if ((I2CTimeout--) == 0)
  286. return 7;
  287. };
  288. };
  289. I2CTimeout = I2CT_LONG_TIMEOUT;
  290. while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_DATA_SENDED)) // EV8-2
  291. {
  292. if ((I2CTimeout--) == 0)
  293. return 8;
  294. };
  295. I2C_GenerateStop((I2C_Module*)i2c_periph, ENABLE);
  296. return 0;
  297. }
  298. static rt_ssize_t rt_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  299. {
  300. struct rt_i2c_msg *msg;
  301. rt_uint32_t i;
  302. rt_err_t ret = -RT_ERROR;
  303. struct rt_i2c_bus *rt_i2c = (struct rt_i2c_bus *)bus;
  304. for (i = 0; i < num; i++)
  305. {
  306. msg = &msgs[i];
  307. if (msg->flags & RT_I2C_RD)
  308. {
  309. if (rt_i2c_read(rt_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
  310. {
  311. LOG_E("i2c bus write failed,i2c bus stop!");
  312. goto out;
  313. }
  314. }
  315. else
  316. {
  317. if (rt_i2c_write(rt_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
  318. {
  319. LOG_E("i2c bus write failed,i2c bus stop!");
  320. goto out;
  321. }
  322. }
  323. }
  324. ret = i;
  325. return ret;
  326. out:
  327. LOG_E("send stop condition\n");
  328. return ret;
  329. }
  330. static const struct rt_i2c_bus_device_ops i2c_ops =
  331. {
  332. rt_i2c_xfer,
  333. RT_NULL,
  334. RT_NULL
  335. };
  336. #endif /* RT_USING_HARDWARE_I2C */
  337. int rt_hw_i2c_init(void)
  338. {
  339. #ifdef RT_USING_I2C_BITOPS
  340. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);
  341. rt_err_t result;
  342. for(rt_size_t i = 0; i < obj_num; i++)
  343. {
  344. i2c_obj[i].ops = n32_bit_ops_default;
  345. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  346. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  347. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  348. RT_ASSERT(result == RT_EOK);
  349. n32_i2c_bus_unlock(&soft_i2c_config[i]);
  350. rt_kprintf("software simulation %s init done, pin scl: %d, pin sda %d",
  351. soft_i2c_config[i].bus_name,
  352. soft_i2c_config[i].scl,
  353. soft_i2c_config[i].sda);
  354. }
  355. #endif /* RT_USING_I2C_BITOPS */
  356. #ifdef RT_USING_HARDWARE_I2C
  357. GPIO_InitType GPIO_InitStructure;
  358. I2C_InitType I2C_InitStructure;
  359. #ifdef BSP_USING_I2C1
  360. #define I2C1_SPEED 400000
  361. static struct rt_i2c_bus i2c_bus1;
  362. #if defined(SOC_N32G45X) || defined(SOC_N32WB452)
  363. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
  364. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C1, ENABLE);
  365. GPIO_InitStruct(&GPIO_InitStructure);
  366. /* connect PB8 to I2C1_SCL, PB9 to I2C1_SDA */
  367. GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9;
  368. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  369. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  370. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  371. GPIO_ConfigPinRemap(GPIO_RMP_I2C1, ENABLE);
  372. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  373. /* Enable I2C clock */
  374. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C1, ENABLE);
  375. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
  376. GPIO_InitStruct(&GPIO_InitStructure);
  377. /* Confige I2C1_SCL(PB8) and I2C1_SDA(PB9) */
  378. GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9;
  379. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  380. GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
  381. GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_I2C1;
  382. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  383. #endif
  384. I2C_DeInit(I2C1);
  385. I2C_InitStructure.BusMode = I2C_BUSMODE_I2C;
  386. I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
  387. I2C_InitStructure.OwnAddr1 = 0xff;
  388. I2C_InitStructure.AckEnable = I2C_ACKEN;
  389. I2C_InitStructure.AddrMode = I2C_ADDR_MODE_7BIT;
  390. I2C_InitStructure.ClkSpeed = I2C1_SPEED; // 400000 400K
  391. I2C_Init(I2C1, &I2C_InitStructure);
  392. rt_memset((void *)&i2c_bus1, 0, sizeof(struct rt_i2c_bus));
  393. i2c_bus1.parent.ops = &i2c_ops;
  394. i2c_bus1.i2c_periph = (rt_uint32_t)I2C1;
  395. rt_i2c_bus_device_register(&i2c_bus1.parent, "i2c1");
  396. #endif
  397. #ifdef BSP_USING_I2C2
  398. #define I2C2_SPEED 100000
  399. static struct rt_i2c_bus i2c_bus2;
  400. #if defined(SOC_N32G45X) || defined(SOC_N32WB452)
  401. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  402. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C2, ENABLE);
  403. GPIO_InitStruct(&GPIO_InitStructure);
  404. /* connect PB10 to I2C2_SCL, PB11 to I2C2_SDA */
  405. GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;
  406. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  407. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  408. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  409. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  410. /* Enable I2C clock */
  411. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C2, ENABLE);
  412. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
  413. GPIO_InitStruct(&GPIO_InitStructure);
  414. /* Confige I2C1_SCL(PB10) and I2C1_SDA(PB11) */
  415. GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;
  416. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  417. GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
  418. GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_I2C2;
  419. GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  420. #endif
  421. I2C_DeInit(I2C2);
  422. I2C_InitStructure.BusMode = I2C_BUSMODE_I2C;
  423. I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
  424. I2C_InitStructure.OwnAddr1 = 0xff;
  425. I2C_InitStructure.AckEnable = I2C_ACKEN;
  426. I2C_InitStructure.AddrMode = I2C_ADDR_MODE_7BIT;
  427. I2C_InitStructure.ClkSpeed = I2C2_SPEED; // 100000 100K
  428. I2C_Init(I2C2, &I2C_InitStructure);
  429. rt_memset((void *)&i2c_bus2, 0, sizeof(struct rt_i2c_bus));
  430. i2c_bus2.parent.ops = &i2c_ops;
  431. i2c_bus2.i2c_periph = (rt_uint32_t)I2C2;
  432. rt_i2c_bus_device_register(&i2c_bus2.parent, "i2c2");
  433. #endif
  434. #if defined(SOC_N32G45X) || defined(SOC_N32WB452)
  435. #ifdef BSP_USING_I2C3
  436. #define I2C3_SPEED 100000
  437. static struct rt_i2c_bus i2c_bus3;
  438. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
  439. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_I2C3, ENABLE);
  440. GPIO_InitStruct(&GPIO_InitStructure);
  441. /* connect PC0 to I2C3_SCL, PC1 to I2C3_SDA */
  442. GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  443. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  444. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  445. GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
  446. I2C_DeInit(I2C3);
  447. I2C_InitStructure.BusMode = I2C_BUSMODE_I2C;
  448. I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
  449. I2C_InitStructure.OwnAddr1 = 0xff;
  450. I2C_InitStructure.AckEnable = I2C_ACKEN;
  451. I2C_InitStructure.AddrMode = I2C_ADDR_MODE_7BIT;
  452. I2C_InitStructure.ClkSpeed = I2C3_SPEED; // 100000 100K
  453. I2C_Init(I2C3, &I2C_InitStructure);
  454. rt_memset((void *)&i2c_bus3, 0, sizeof(struct rt_i2c_bus));
  455. i2c_bus3.parent.ops = &i2c_ops;
  456. i2c_bus3.i2c_periph = (rt_uint32_t)I2C3;
  457. rt_i2c_bus_device_register(&i2c_bus3.parent, "i2c3");
  458. #endif
  459. #ifdef BSP_USING_I2C4
  460. #define I2C4_SPEED 100000
  461. static struct rt_i2c_bus i2c_bus4;
  462. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
  463. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_I2C4, ENABLE);
  464. GPIO_InitStruct(&GPIO_InitStructure);
  465. /* connect PC6 to I2C4_SCL, PC7 to I2C4_SDA */
  466. GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
  467. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  468. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  469. GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
  470. I2C_DeInit(I2C4);
  471. I2C_InitStructure.BusMode = I2C_BUSMODE_I2C;
  472. I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
  473. I2C_InitStructure.OwnAddr1 = 0xff;
  474. I2C_InitStructure.AckEnable = I2C_ACKEN;
  475. I2C_InitStructure.AddrMode = I2C_ADDR_MODE_7BIT;
  476. I2C_InitStructure.ClkSpeed = I2C4_SPEED; // 100000 100K
  477. I2C_Init(I2C4, &I2C_InitStructure);
  478. rt_memset((void *)&i2c_bus4, 0, sizeof(struct rt_i2c_bus));
  479. i2c_bus4.parent.ops = &i2c_ops;
  480. i2c_bus4.i2c_periph = (rt_uint32_t)I2C4;
  481. rt_i2c_bus_device_register(&i2c_bus4.parent, "i2c4");
  482. #endif
  483. #endif
  484. #endif /* RT_USING_HARDWARE_I2C */
  485. return RT_EOK;
  486. }
  487. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  488. #endif
  489. /* end of i2c driver */