drv_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2022-3-15 Wayne First version
  10. ******************************************************************************/
  11. #include <rtconfig.h>
  12. #if defined(BSP_USING_I2C) && defined(RT_USING_I2C)
  13. #include <rtdevice.h>
  14. #include "NuMicro.h"
  15. #include <drv_i2c.h>
  16. /* Private define ---------------------------------------------------------------*/
  17. #define LOG_TAG "drv.i2c"
  18. #define DBG_ENABLE
  19. #define DBG_SECTION_NAME LOG_TAG
  20. #define DBG_LEVEL DBG_INFO
  21. #define DBG_COLOR
  22. #include <rtdbg.h>
  23. enum
  24. {
  25. I2C_START = -1,
  26. #if defined(BSP_USING_I2C0)
  27. I2C0_IDX,
  28. #endif
  29. #if defined(BSP_USING_I2C1)
  30. I2C1_IDX,
  31. #endif
  32. #if defined(BSP_USING_I2C2)
  33. I2C2_IDX,
  34. #endif
  35. #if defined(BSP_USING_I2C3)
  36. I2C3_IDX,
  37. #endif
  38. #if defined(BSP_USING_I2C4)
  39. I2C4_IDX,
  40. #endif
  41. I2C_CNT
  42. };
  43. /* Private typedef --------------------------------------------------------------*/
  44. struct nu_i2c_bus
  45. {
  46. struct rt_i2c_bus_device parent;
  47. I2C_T *I2C;
  48. struct rt_i2c_msg *msg;
  49. char *device_name;
  50. };
  51. typedef struct nu_i2c_bus *nu_i2c_bus_t;
  52. /* Private variables ------------------------------------------------------------*/
  53. static struct nu_i2c_bus nu_i2c_arr [ ] =
  54. {
  55. #if defined(BSP_USING_I2C0)
  56. {
  57. .I2C = I2C0, .device_name = "i2c0",
  58. },
  59. #endif
  60. #if defined(BSP_USING_I2C1)
  61. {
  62. .I2C = I2C1, .device_name = "i2c1",
  63. },
  64. #endif
  65. #if defined(BSP_USING_I2C2)
  66. {
  67. .I2C = I2C2, .device_name = "i2c2",
  68. },
  69. #endif
  70. #if defined(BSP_USING_I2C3)
  71. {
  72. .I2C = I2C3, .device_name = "i2c3",
  73. },
  74. #endif
  75. #if defined(BSP_USING_I2C4)
  76. {
  77. .I2C = I2C4, .device_name = "i2c4",
  78. },
  79. #endif
  80. };
  81. /* Private functions ------------------------------------------------------------*/
  82. #if defined(BSP_USING_I2C)
  83. static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  84. struct rt_i2c_msg msgs[],
  85. rt_uint32_t num);
  86. static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus,
  87. int cmd,
  88. void *args);
  89. static const struct rt_i2c_bus_device_ops nu_i2c_ops =
  90. {
  91. .master_xfer = nu_i2c_mst_xfer,
  92. .slave_xfer = NULL,
  93. .i2c_bus_control = nu_i2c_bus_control
  94. };
  95. static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
  96. {
  97. nu_i2c_bus_t nu_i2c;
  98. RT_ASSERT(bus);
  99. nu_i2c = (nu_i2c_bus_t) bus;
  100. switch (cmd)
  101. {
  102. case RT_I2C_DEV_CTRL_CLK:
  103. I2C_SetBusClockFreq(nu_i2c->I2C, *(rt_uint32_t *)args);
  104. break;
  105. default:
  106. return -RT_EIO;
  107. }
  108. return RT_EOK;
  109. }
  110. static inline rt_err_t nu_i2c_wait_ready_with_timeout(nu_i2c_bus_t bus)
  111. {
  112. rt_tick_t start = rt_tick_get();
  113. while (!(bus->I2C->CTL0 & I2C_CTL0_SI_Msk))
  114. {
  115. if ((rt_tick_get() - start) > bus->parent.timeout)
  116. {
  117. LOG_E("\ni2c: timeout!\n");
  118. return -RT_ETIMEOUT;
  119. }
  120. }
  121. return RT_EOK;
  122. }
  123. static inline rt_err_t nu_i2c_send_data(nu_i2c_bus_t nu_i2c, rt_uint8_t data)
  124. {
  125. I2C_SET_DATA(nu_i2c->I2C, data);
  126. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_SI);
  127. return nu_i2c_wait_ready_with_timeout(nu_i2c);
  128. }
  129. static rt_err_t nu_i2c_send_address(nu_i2c_bus_t nu_i2c,
  130. struct rt_i2c_msg *msg)
  131. {
  132. rt_uint16_t flags = msg->flags;
  133. rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
  134. rt_uint8_t addr1, addr2;
  135. rt_err_t ret;
  136. if (flags & RT_I2C_ADDR_10BIT)
  137. {
  138. nu_i2c->I2C->CTL1 |= I2C_CTL1_ADDR10EN_Msk;
  139. addr1 = 0xf0 | ((msg->addr >> 7) & 0x06);
  140. addr2 = msg->addr & 0xff;
  141. LOG_D("address1: %d, address2: %d\n", addr1, addr2);
  142. ret = nu_i2c_send_data(nu_i2c, addr1);
  143. if (ret != RT_EOK) /* for timeout condition */
  144. return -RT_EIO;
  145. if ((I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_TRANSMIT_ADDRESS_ACK) && !ignore_nack)
  146. {
  147. LOG_E("NACK: sending first address failed\n");
  148. return -RT_EIO;
  149. }
  150. ret = nu_i2c_send_data(nu_i2c, addr2);
  151. if (ret != RT_EOK) /* for timeout condition */
  152. return -RT_EIO;
  153. if ((I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_TRANSMIT_ADDRESS_ACK) && !ignore_nack)
  154. {
  155. LOG_E("NACK: sending second address failed\n");
  156. return -RT_EIO;
  157. }
  158. if (flags & RT_I2C_RD)
  159. {
  160. LOG_D("send repeated START signal\n");
  161. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_STA_SI);
  162. ret = nu_i2c_wait_ready_with_timeout(nu_i2c);
  163. if (ret != RT_EOK) /* for timeout condition */
  164. return -RT_EIO;
  165. if ((I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_REPEAT_START) && !ignore_nack)
  166. {
  167. //LOG_E("sending repeated START failed\n");
  168. return -RT_EIO;
  169. }
  170. addr1 |= 0x01;
  171. ret = nu_i2c_send_data(nu_i2c, addr1);
  172. if (ret != RT_EOK) /* for timeout condition */
  173. return -RT_EIO;
  174. if ((I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_RECEIVE_ADDRESS_ACK) && !ignore_nack)
  175. {
  176. LOG_E("NACK: sending read address failed\n");
  177. return -RT_EIO;
  178. }
  179. }
  180. }
  181. else
  182. {
  183. /* 7-bit addr */
  184. addr1 = msg->addr << 1;
  185. if (flags & RT_I2C_RD)
  186. addr1 |= 1;
  187. /* Send device address */
  188. ret = nu_i2c_send_data(nu_i2c, addr1); /* Send Address */
  189. if (ret != RT_EOK) /* for timeout condition */
  190. return -RT_EIO;
  191. if ((I2C_GET_STATUS(nu_i2c->I2C)
  192. != ((flags & RT_I2C_RD) ? NU_I2C_MASTER_STATUS_RECEIVE_ADDRESS_ACK : NU_I2C_MASTER_STATUS_TRANSMIT_ADDRESS_ACK))
  193. && !ignore_nack)
  194. {
  195. LOG_E("sending address failed\n");
  196. return -RT_EIO;
  197. }
  198. }
  199. return RT_EOK;
  200. }
  201. static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  202. struct rt_i2c_msg msgs[],
  203. rt_uint32_t num)
  204. {
  205. struct rt_i2c_msg *msg;
  206. nu_i2c_bus_t nu_i2c;
  207. rt_size_t i;
  208. rt_uint32_t cnt_data;
  209. rt_uint16_t ignore_nack;
  210. rt_err_t ret;
  211. RT_ASSERT(bus != RT_NULL);
  212. nu_i2c = (nu_i2c_bus_t) bus;
  213. nu_i2c->msg = msgs;
  214. nu_i2c->I2C->CTL0 |= I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk;
  215. ret = nu_i2c_wait_ready_with_timeout(nu_i2c);
  216. if (ret != RT_EOK) /* for timeout condition */
  217. {
  218. rt_set_errno(-RT_ETIMEOUT);
  219. return 0;
  220. }
  221. if (I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_START)
  222. {
  223. i = 0;
  224. LOG_E("Send START Failed");
  225. return i;
  226. }
  227. for (i = 0; i < num; i++)
  228. {
  229. msg = &msgs[i];
  230. ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
  231. if (!(msg->flags & RT_I2C_NO_START))
  232. {
  233. if (i)
  234. {
  235. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_STA_SI);
  236. ret = nu_i2c_wait_ready_with_timeout(nu_i2c);
  237. if (ret != RT_EOK) /* for timeout condition */
  238. break;
  239. if (I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_REPEAT_START)
  240. {
  241. i = 0;
  242. //LOG_E("Send repeat START Fail");
  243. break;
  244. }
  245. }
  246. if ((RT_EOK != nu_i2c_send_address(nu_i2c, msg))
  247. && !ignore_nack)
  248. {
  249. i = 0;
  250. LOG_E("Send Address Fail");
  251. break;
  252. }
  253. }
  254. if (nu_i2c->msg[i].flags & RT_I2C_RD) /* Receive Bytes */
  255. {
  256. rt_uint32_t do_rd_nack = (i == (num - 1));
  257. for (cnt_data = 0 ; cnt_data < (nu_i2c->msg[i].len) ; cnt_data++)
  258. {
  259. do_rd_nack += (cnt_data == (nu_i2c->msg[i].len - 1)); /* NACK after last byte for hardware setting */
  260. if (do_rd_nack == 2)
  261. {
  262. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_SI);
  263. }
  264. else
  265. {
  266. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_SI_AA);
  267. }
  268. ret = nu_i2c_wait_ready_with_timeout(nu_i2c);
  269. if (ret != RT_EOK) /* for timeout condition */
  270. break;
  271. if (nu_i2c->I2C->CTL0 & I2C_CTL_AA)
  272. {
  273. if (I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_RECEIVE_DATA_ACK)
  274. {
  275. i = 0;
  276. break;
  277. }
  278. }
  279. else
  280. {
  281. if (I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_RECEIVE_DATA_NACK)
  282. {
  283. i = 0;
  284. break;
  285. }
  286. }
  287. nu_i2c->msg[i].buf[cnt_data] = nu_i2c->I2C->DAT;
  288. }
  289. }
  290. else /* Send Bytes */
  291. {
  292. for (cnt_data = 0 ; cnt_data < (nu_i2c->msg[i].len) ; cnt_data++)
  293. {
  294. /* Send register number and MSB of data */
  295. ret = nu_i2c_send_data(nu_i2c, (uint8_t)(nu_i2c->msg[i].buf[cnt_data]));
  296. if (ret != RT_EOK) /* for timeout condition */
  297. break;
  298. if (I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_TRANSMIT_DATA_ACK
  299. && !ignore_nack
  300. ) /* Send aata and get Ack */
  301. {
  302. i = 0;
  303. break;
  304. }
  305. }
  306. }
  307. }
  308. I2C_STOP(nu_i2c->I2C);
  309. RT_ASSERT(I2C_GET_STATUS(nu_i2c->I2C) == NU_I2C_MASTER_STATUS_BUS_RELEASED);
  310. if (I2C_GET_STATUS(nu_i2c->I2C) != NU_I2C_MASTER_STATUS_BUS_RELEASED)
  311. {
  312. i = 0;
  313. }
  314. nu_i2c->msg = RT_NULL;
  315. nu_i2c->I2C->CTL1 = 0; /*clear all sub modes like 10 bit mode*/
  316. return i;
  317. }
  318. #endif
  319. /* Public functions -------------------------------------------------------------*/
  320. int rt_hw_i2c_init(void)
  321. {
  322. int i;
  323. rt_err_t ret = RT_EOK;
  324. for (i = (I2C_START + 1); i < I2C_CNT; i++)
  325. {
  326. /* Reset and initial IP engine. */
  327. I2C_Close(nu_i2c_arr[i].I2C);
  328. I2C_Open(nu_i2c_arr[i].I2C, 100000);
  329. nu_i2c_arr[i].parent.ops = &nu_i2c_ops;
  330. ret = rt_i2c_bus_device_register(&nu_i2c_arr[i].parent, nu_i2c_arr[i].device_name);
  331. RT_ASSERT(RT_EOK == ret);
  332. }
  333. return 0;
  334. }
  335. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  336. #endif /* BSP_USING_I2C */