drv_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. * 2017-08-08 Yang the first version
  9. * 2018-03-24 LaiYiKeTang add hardware iic
  10. * 2019-04-22 tyustli add imxrt series support
  11. *
  12. */
  13. #include <rtthread.h>
  14. #ifdef BSP_USING_I2C
  15. #define LOG_TAG "drv.i2c"
  16. #include <drv_log.h>
  17. #if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && !defined(BSP_USING_I2C3) && !defined(BSP_USING_I2C4) && !defined(BSP_USING_I2C5)&& !defined(BSP_USING_I2C6)
  18. #error "Please define at least one BSP_USING_I2Cx"
  19. #endif
  20. #include <rtdevice.h>
  21. #include "fsl_lpi2c.h"
  22. #include "drv_i2c.h"
  23. struct imxrt_i2c_bus
  24. {
  25. struct rt_i2c_bus_device parent;
  26. LPI2C_Type *I2C;
  27. struct rt_i2c_msg *msg;
  28. rt_uint32_t msg_cnt;
  29. volatile rt_uint32_t msg_ptr;
  30. volatile rt_uint32_t dptr;
  31. char *device_name;
  32. #ifdef SOC_IMXRT1170_SERIES
  33. clock_root_t clock_root;
  34. #endif
  35. };
  36. #if defined (BSP_USING_I2C1)
  37. #define I2C1BUS_NAME "i2c1"
  38. #endif /*BSP_USING_I2C1*/
  39. #if defined (BSP_USING_I2C2)
  40. #define I2C2BUS_NAME "i2c2"
  41. #endif /*BSP_USING_I2C2*/
  42. #if !defined (MIMXRT1015_SERIES) /* imxrt1015 only have two i2c bus*/
  43. #if defined (BSP_USING_I2C3)
  44. #define I2C3BUS_NAME "i2c3"
  45. #endif /*BSP_USING_I2C3*/
  46. #if defined (BSP_USING_I2C4)
  47. #define I2C4BUS_NAME "i2c4"
  48. #endif /*BSP_USING_I2C4*/
  49. #if defined (BSP_USING_I2C5)
  50. #define I2C5BUS_NAME "i2c5"
  51. #endif /*BSP_USING_I2C5*/
  52. #if defined (BSP_USING_I2C6)
  53. #define I2C6BUS_NAME "i2c6"
  54. #endif /*BSP_USING_I2C6*/
  55. #endif /* MIMXRT1015_SERIES */
  56. /* Select USB1 PLL (360 MHz) as master lpi2c clock source */
  57. #define LPI2C_CLOCK_SOURCE_SELECT (1U)
  58. #ifdef SOC_IMXRT1170_SERIES
  59. /* Clock divider for master lpi2c clock source */
  60. #define LPI2C_CLOCK_SOURCE_DIVIDER (12U)
  61. #else
  62. #define LPI2C_CLOCK_SOURCE_DIVIDER (0U)
  63. /* Get frequency of lpi2c clock */
  64. #define LPI2C_CLOCK_FREQUENCY ((CLOCK_GetFreq(kCLOCK_Usb1PllClk) / 8) / (LPI2C_CLOCK_SOURCE_DIVIDER + 1U))
  65. #endif
  66. #ifdef BSP_USING_I2C1
  67. static struct imxrt_i2c_bus lpi2c1 =
  68. {
  69. .I2C = LPI2C1,
  70. .device_name = I2C1BUS_NAME,
  71. };
  72. #endif /* RT_USING_HW_I2C1 */
  73. #ifdef BSP_USING_I2C2
  74. static struct imxrt_i2c_bus lpi2c2 =
  75. {
  76. .I2C = LPI2C2,
  77. .device_name = I2C2BUS_NAME,
  78. };
  79. #endif /* RT_USING_HW_I2C2 */
  80. #if !defined (MIMXRT1015_SERIES) /* imxrt1015 only have two i2c bus*/
  81. #ifdef BSP_USING_I2C3
  82. static struct imxrt_i2c_bus lpi2c3 =
  83. {
  84. .I2C = LPI2C3,
  85. .device_name = I2C3BUS_NAME,
  86. };
  87. #endif /* RT_USING_HW_I2C3 */
  88. #ifdef BSP_USING_I2C4
  89. static struct imxrt_i2c_bus lpi2c4 =
  90. {
  91. .I2C = LPI2C4,
  92. .device_name = I2C4BUS_NAME,
  93. };
  94. #endif /* RT_USING_HW_I2C4 */
  95. #ifdef BSP_USING_I2C5
  96. static struct imxrt_i2c_bus lpi2c5 =
  97. {
  98. .I2C = LPI2C5,
  99. .device_name = I2C5BUS_NAME,
  100. };
  101. #endif /* RT_USING_HW_I2C5 */
  102. #ifdef BSP_USING_I2C6
  103. static struct imxrt_i2c_bus lpi2c6 =
  104. {
  105. .I2C = LPI2C6,
  106. .device_name = I2C6BUS_NAME,
  107. };
  108. #endif /* RT_USING_HW_I2C6 */
  109. #endif /* MIMXRT1015_SERIES */
  110. #if (defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) || defined(BSP_USING_I2C3) || defined(BSP_USING_I2C4) ||defined(BSP_USING_I2C5) || defined(BSP_USING_I2C6))
  111. static rt_size_t imxrt_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  112. struct rt_i2c_msg msgs[],
  113. rt_uint32_t num);
  114. static rt_size_t imxrt_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
  115. struct rt_i2c_msg msgs[],
  116. rt_uint32_t num);
  117. static rt_err_t imxrt_i2c_bus_control(struct rt_i2c_bus_device *bus,
  118. rt_uint32_t,
  119. rt_uint32_t);
  120. static const struct rt_i2c_bus_device_ops imxrt_i2c_ops =
  121. {
  122. .master_xfer = imxrt_i2c_mst_xfer,
  123. .slave_xfer = imxrt_i2c_slv_xfer,
  124. .i2c_bus_control = imxrt_i2c_bus_control,
  125. };
  126. static rt_err_t imxrt_lpi2c_configure(struct imxrt_i2c_bus *bus, lpi2c_master_config_t *cfg)
  127. {
  128. RT_ASSERT(bus != RT_NULL);
  129. RT_ASSERT(cfg != RT_NULL);
  130. bus->parent.ops = &imxrt_i2c_ops;
  131. #ifdef SOC_IMXRT1170_SERIES
  132. clock_root_config_t rootCfg = {0};
  133. rootCfg.mux = LPI2C_CLOCK_SOURCE_SELECT;
  134. rootCfg.div = LPI2C_CLOCK_SOURCE_DIVIDER + 1;
  135. CLOCK_SetRootClock(bus->clock_root, &rootCfg);
  136. volatile uint32_t freq = CLOCK_GetRootClockFreq(bus->clock_root);
  137. LPI2C_MasterInit(bus->I2C, cfg, freq);
  138. #else
  139. CLOCK_SetMux(kCLOCK_Lpi2cMux, LPI2C_CLOCK_SOURCE_SELECT);
  140. CLOCK_SetDiv(kCLOCK_Lpi2cDiv, LPI2C_CLOCK_SOURCE_DIVIDER);
  141. LPI2C_MasterInit(bus->I2C, cfg, LPI2C_CLOCK_FREQUENCY);
  142. #endif
  143. return RT_EOK;
  144. }
  145. status_t LPI2C_MasterCheck(LPI2C_Type *base, uint32_t status)
  146. {
  147. status_t result = kStatus_Success;
  148. /* Check for error. These errors cause a stop to automatically be sent. We must */
  149. /* clear the errors before a new transfer can start. */
  150. status &= 0x3c00;
  151. if (status)
  152. {
  153. /* Select the correct error code. Ordered by severity, with bus issues first. */
  154. if (status & kLPI2C_MasterPinLowTimeoutFlag)
  155. {
  156. result = kStatus_LPI2C_PinLowTimeout;
  157. }
  158. else if (status & kLPI2C_MasterArbitrationLostFlag)
  159. {
  160. result = kStatus_LPI2C_ArbitrationLost;
  161. }
  162. else if (status & kLPI2C_MasterNackDetectFlag)
  163. {
  164. result = kStatus_LPI2C_Nak;
  165. }
  166. else if (status & kLPI2C_MasterFifoErrFlag)
  167. {
  168. result = kStatus_LPI2C_FifoError;
  169. }
  170. else
  171. {
  172. assert(false);
  173. }
  174. /* Clear the flags. */
  175. LPI2C_MasterClearStatusFlags(base, status);
  176. /* Reset fifos. These flags clear automatically. */
  177. base->MCR |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK;
  178. }
  179. return result;
  180. }
  181. /*!
  182. * @brief Wait until the tx fifo all empty.
  183. * @param base The LPI2C peripheral base address.
  184. * @retval #kStatus_Success
  185. * @retval #kStatus_LPI2C_PinLowTimeout
  186. * @retval #kStatus_LPI2C_ArbitrationLost
  187. * @retval #kStatus_LPI2C_Nak
  188. * @retval #kStatus_LPI2C_FifoError
  189. */
  190. static status_t LPI2C_MasterWaitForTxFifoAllEmpty(LPI2C_Type *base)
  191. {
  192. uint32_t status;
  193. size_t txCount;
  194. do
  195. {
  196. status_t result;
  197. /* Get the number of words in the tx fifo and compute empty slots. */
  198. LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
  199. /* Check for error flags. */
  200. status = LPI2C_MasterGetStatusFlags(base);
  201. result = LPI2C_MasterCheck(base, status);
  202. if (result)
  203. {
  204. return result;
  205. }
  206. }
  207. while (txCount);
  208. return kStatus_Success;
  209. }
  210. static rt_size_t imxrt_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  211. struct rt_i2c_msg msgs[],
  212. rt_uint32_t num)
  213. {
  214. struct imxrt_i2c_bus *imxrt_i2c;
  215. rt_size_t i;
  216. RT_ASSERT(bus != RT_NULL);
  217. imxrt_i2c = (struct imxrt_i2c_bus *) bus;
  218. imxrt_i2c->msg = msgs;
  219. imxrt_i2c->msg_ptr = 0;
  220. imxrt_i2c->msg_cnt = num;
  221. imxrt_i2c->dptr = 0;
  222. for (i = 0; i < num; i++)
  223. {
  224. if (imxrt_i2c->msg[i].flags & RT_I2C_RD)
  225. {
  226. if ((imxrt_i2c->msg[i].flags & RT_I2C_NO_START) != RT_I2C_NO_START)
  227. {
  228. if (LPI2C_MasterStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Write) != kStatus_Success)
  229. {
  230. i = 0;
  231. break;
  232. }
  233. while (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
  234. {
  235. }
  236. if (LPI2C_MasterRepeatedStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Read) != kStatus_Success)
  237. {
  238. i = 0;
  239. break;
  240. }
  241. }
  242. else
  243. {
  244. if (LPI2C_MasterStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Read) != kStatus_Success)
  245. {
  246. i = 0;
  247. break;
  248. }
  249. while (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
  250. {
  251. }
  252. }
  253. if (LPI2C_MasterStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Read) != kStatus_Success)
  254. {
  255. i = 0;
  256. break;
  257. }
  258. while (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
  259. {
  260. }
  261. if (LPI2C_MasterReceive(imxrt_i2c->I2C, imxrt_i2c->msg[i].buf, imxrt_i2c->msg[i].len) != kStatus_Success)
  262. {
  263. i = 0;
  264. break;
  265. }
  266. }
  267. else
  268. {
  269. if (LPI2C_MasterStart(imxrt_i2c->I2C, imxrt_i2c->msg[i].addr, kLPI2C_Write) != kStatus_Success)
  270. {
  271. i = 0;
  272. break;
  273. }
  274. // while((LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterBusBusyFlag))
  275. // {
  276. // }
  277. if(LPI2C_MasterWaitForTxFifoAllEmpty(imxrt_i2c->I2C) != kStatus_Success)
  278. {
  279. i = 0;
  280. break;
  281. }
  282. if (LPI2C_MasterGetStatusFlags(imxrt_i2c->I2C) & kLPI2C_MasterNackDetectFlag)
  283. {
  284. i = 0;
  285. break;
  286. }
  287. if (LPI2C_MasterSend(imxrt_i2c->I2C, imxrt_i2c->msg[i].buf, imxrt_i2c->msg[i].len) != kStatus_Success)
  288. {
  289. i = 0;
  290. break;
  291. }
  292. if (LPI2C_MasterWaitForTxFifoAllEmpty(imxrt_i2c->I2C) != kStatus_Success)
  293. {
  294. i = 0;
  295. break;
  296. }
  297. }
  298. if (LPI2C_MasterStop(imxrt_i2c->I2C) != kStatus_Success)
  299. {
  300. i = 0;
  301. }
  302. }
  303. imxrt_i2c->msg = RT_NULL;
  304. imxrt_i2c->msg_ptr = 0;
  305. imxrt_i2c->msg_cnt = 0;
  306. imxrt_i2c->dptr = 0;
  307. return i;
  308. }
  309. static rt_size_t imxrt_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
  310. struct rt_i2c_msg msgs[],
  311. rt_uint32_t num)
  312. {
  313. return 0;
  314. }
  315. static rt_err_t imxrt_i2c_bus_control(struct rt_i2c_bus_device *bus,
  316. rt_uint32_t cmd,
  317. rt_uint32_t arg)
  318. {
  319. return RT_ERROR;
  320. }
  321. #endif
  322. int rt_hw_i2c_init(void)
  323. {
  324. lpi2c_master_config_t masterConfig = {0};
  325. #if defined(BSP_USING_I2C1)
  326. LPI2C_MasterGetDefaultConfig(&masterConfig);
  327. #if defined(HW_I2C1_BADURATE_400kHZ)
  328. masterConfig.baudRate_Hz = 400000U;
  329. #elif defined(HW_I2C1_BADURATE_100kHZ)
  330. masterConfig.baudRate_Hz = 100000U;
  331. #endif /*HW_I2C1_BADURATE_400kHZ*/
  332. imxrt_lpi2c_configure(&lpi2c1, &masterConfig);
  333. rt_i2c_bus_device_register(&lpi2c1.parent, lpi2c1.device_name);
  334. #endif /* BSP_USING_I2C1 */
  335. #if defined(BSP_USING_I2C2)
  336. LPI2C_MasterGetDefaultConfig(&masterConfig);
  337. #if defined(HW_I2C2_BADURATE_400kHZ)
  338. masterConfig.baudRate_Hz = 400000U;
  339. #elif defined(HW_I2C2_BADURATE_100kHZ)
  340. masterConfig.baudRate_Hz = 100000U;
  341. #endif /* HW_I2C2_BADURATE_400kHZ */
  342. imxrt_lpi2c_configure(&lpi2c2, &masterConfig);
  343. rt_i2c_bus_device_register(&lpi2c2.parent, lpi2c2.device_name);
  344. #endif /* BSP_USING_I2C2 */
  345. #if !defined(MIMXRT1015_SERIES) /* imxrt1015 only have two i2c bus*/
  346. #if defined(BSP_USING_I2C3)
  347. LPI2C_MasterGetDefaultConfig(&masterConfig);
  348. #if defined(HW_I2C3_BADURATE_400kHZ)
  349. masterConfig.baudRate_Hz = 400000U;
  350. #elif defined(HW_I2C3_BADURATE_100kHZ)
  351. masterConfig.baudRate_Hz = 100000U;
  352. #endif /* HW_I2C3_BADURATE_400kHZ */
  353. imxrt_lpi2c_configure(&lpi2c3, &masterConfig);
  354. rt_i2c_bus_device_register(&lpi2c3.parent, lpi2c3.device_name);
  355. #endif /* BSP_USING_I2C3 */
  356. #if defined(BSP_USING_I2C4)
  357. LPI2C_MasterGetDefaultConfig(&masterConfig);
  358. #if defined(HW_I2C4_BADURATE_400kHZ)
  359. masterConfig.baudRate_Hz = 400000U;
  360. #elif defined(HW_I2C4_BADURATE_100kHZ)
  361. masterConfig.baudRate_Hz = 100000U;
  362. #endif /* HW_I2C4_BADURATE_400kHZ */
  363. imxrt_lpi2c_configure(&lpi2c4, &masterConfig);
  364. rt_i2c_bus_device_register(&lpi2c4.parent, lpi2c4.device_name);
  365. #endif /* BSP_USING_I2C4 */
  366. #if defined(BSP_USING_I2C5)
  367. LPI2C_MasterGetDefaultConfig(&masterConfig);
  368. #if defined(HW_I2C5_BADURATE_400kHZ)
  369. masterConfig.baudRate_Hz = 400000U;
  370. #elif defined(HW_I2C5_BADURATE_100kHZ)
  371. masterConfig.baudRate_Hz = 100000U;
  372. #endif /* HW_I2C5_BADURATE_400kHZ */
  373. lpi2c5.clock_root = kCLOCK_Root_Lpi2c5;
  374. imxrt_lpi2c_configure(&lpi2c5, &masterConfig);
  375. rt_i2c_bus_device_register(&lpi2c5.parent, lpi2c5.device_name);
  376. #endif /* BSP_USING_I2C5 */
  377. #if defined(BSP_USING_I2C6)
  378. LPI2C_MasterGetDefaultConfig(&masterConfig);
  379. #if defined(HW_I2C6_BADURATE_400kHZ)
  380. masterConfig.baudRate_Hz = 400000U;
  381. #elif defined(HW_I2C6_BADURATE_100kHZ)
  382. masterConfig.baudRate_Hz = 100000U;
  383. #endif /* HW_I2C6_BADURATE_400kHZ */
  384. lpi2c6.clock_root = kCLOCK_Root_Lpi2c6;
  385. imxrt_lpi2c_configure(&lpi2c6, &masterConfig);
  386. rt_i2c_bus_device_register(&lpi2c6.parent, lpi2c6.device_name);
  387. #endif /* BSP_USING_I2C6 */
  388. #endif /* MIMXRT1015_SERIES */
  389. return 0;
  390. }
  391. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  392. #endif /* BSP_USING_I2C */