drv_ui2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. * 2020-5-15 YCHuang12 First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if (defined(BSP_USING_UI2C) && defined(RT_USING_I2C))
  14. #include <rtdevice.h>
  15. #include <NuMicro.h>
  16. /* Private define ---------------------------------------------------------------*/
  17. #define LOG_TAG "drv.ui2c"
  18. #define DBG_ENABLE
  19. #define DBG_SECTION_NAME LOG_TAG
  20. #define DBG_LEVEL DBG_INFO
  21. #include <rtdbg.h>
  22. #define SLV_10BIT_ADDR (0x1E<<2) //1111+0xx+r/w
  23. /* Private typedef --------------------------------------------------------------*/
  24. typedef struct nu_ui2c_bus
  25. {
  26. struct rt_i2c_bus_device ui2c_dev;
  27. struct rt_i2c_msg *msg;
  28. UI2C_T *ui2c_base;
  29. char *dev_name;
  30. } nu_ui2c_bus_t;
  31. /* Private functions ------------------------------------------------------------*/
  32. static rt_size_t nu_ui2c_mst_xfer(struct rt_i2c_bus_device *ui2c_dev,
  33. struct rt_i2c_msg msgs[],
  34. rt_uint32_t num);
  35. static const struct rt_i2c_bus_device_ops nu_ui2c_ops =
  36. {
  37. .master_xfer = nu_ui2c_mst_xfer,
  38. .slave_xfer = NULL,
  39. .i2c_bus_control = NULL,
  40. };
  41. /* Private variables ------------------------------------------------------------*/
  42. #ifdef BSP_USING_UI2C0
  43. #define UI2C0BUS_NAME "ui2c0"
  44. static nu_ui2c_bus_t nu_ui2c0 =
  45. {
  46. .ui2c_base = UI2C0,
  47. .dev_name = UI2C0BUS_NAME,
  48. };
  49. #endif /* BSP_USING_UI2C0 */
  50. #ifdef BSP_USING_UI2C1
  51. #define UI2C1BUS_NAME "ui2c1"
  52. static nu_ui2c_bus_t nu_ui2c1 =
  53. {
  54. .ui2c_base = UI2C1,
  55. .dev_name = UI2C1BUS_NAME,
  56. };
  57. #endif /* BSP_USING_UI2C1 */
  58. /* Functions define ------------------------------------------------------------*/
  59. #if (defined(BSP_USING_UI2C0) || defined(BSP_USING_UI2C1))
  60. static inline rt_err_t nu_ui2c_wait_ready_with_timeout(nu_ui2c_bus_t *nu_ui2c)
  61. {
  62. rt_tick_t start = rt_tick_get();
  63. while (!(UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & (UI2C_PROTSTS_STARIF_Msk | UI2C_PROTSTS_ACKIF_Msk | UI2C_PROTSTS_NACKIF_Msk | UI2C_PROTSTS_STORIF_Msk)))
  64. {
  65. if ((rt_tick_get() - start) > nu_ui2c->ui2c_dev.timeout)
  66. {
  67. LOG_E("\nui2c: timeout!\n");
  68. return -RT_ETIMEOUT;
  69. }
  70. }
  71. return RT_EOK;
  72. }
  73. static inline rt_err_t nu_ui2c_send_data(nu_ui2c_bus_t *nu_ui2c, rt_uint8_t data)
  74. {
  75. UI2C_SET_DATA(nu_ui2c->ui2c_base, data);
  76. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, UI2C_CTL_PTRG);
  77. return nu_ui2c_wait_ready_with_timeout(nu_ui2c);
  78. }
  79. static rt_err_t nu_ui2c_send_address(nu_ui2c_bus_t *nu_ui2c,
  80. struct rt_i2c_msg *msg)
  81. {
  82. rt_uint16_t flags = msg->flags;
  83. rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
  84. rt_uint8_t addr1, addr2;
  85. rt_err_t ret;
  86. if (flags & RT_I2C_ADDR_10BIT)
  87. {
  88. UI2C_ENABLE_10BIT_ADDR_MODE(nu_ui2c->ui2c_base);
  89. /* Init Send 10-bit Addr */
  90. addr1 = ((msg->addr >> 8) | SLV_10BIT_ADDR) << 1;
  91. addr2 = msg->addr & 0xff;
  92. LOG_D("addr1: %d, addr2: %d\n", addr1, addr2);
  93. ret = nu_ui2c_send_data(nu_ui2c, addr1);
  94. if (ret != RT_EOK) //for timeout condition
  95. return -RT_EIO;
  96. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk) && !ignore_nack)
  97. {
  98. LOG_E("NACK: sending first addr\n");
  99. return -RT_EIO;
  100. }
  101. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  102. ret = nu_ui2c_send_data(nu_ui2c, addr2);
  103. if (ret != RT_EOK) //for timeout condition
  104. return -RT_EIO;
  105. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk) && !ignore_nack)
  106. {
  107. LOG_E("NACK: sending second addr\n");
  108. return -RT_EIO;
  109. }
  110. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  111. if (flags & RT_I2C_RD)
  112. {
  113. LOG_D("send repeated start condition\n");
  114. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, (UI2C_CTL_PTRG | UI2C_CTL_STA));
  115. ret = nu_ui2c_wait_ready_with_timeout(nu_ui2c);
  116. if (ret != RT_EOK) //for timeout condition
  117. return -RT_EIO;
  118. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_STARIF_Msk) != UI2C_PROTSTS_STARIF_Msk) && !ignore_nack)
  119. {
  120. LOG_E("sending repeated START fail\n");
  121. return -RT_EIO;
  122. }
  123. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_STARIF_Msk);
  124. addr1 |= RT_I2C_RD;
  125. ret = nu_ui2c_send_data(nu_ui2c, addr1);
  126. if (ret != RT_EOK) //for timeout condition
  127. return -RT_EIO;
  128. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk) && !ignore_nack)
  129. {
  130. LOG_E("NACK: sending repeated addr\n");
  131. return -RT_EIO;
  132. }
  133. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  134. }
  135. }
  136. else
  137. {
  138. /* 7-bit addr */
  139. addr1 = msg->addr << 1;
  140. if (flags & RT_I2C_RD)
  141. addr1 |= RT_I2C_RD;
  142. /* Send device address */
  143. ret = nu_ui2c_send_data(nu_ui2c, addr1); /* Send Address */
  144. if (ret != RT_EOK) //for timeout condition
  145. return -RT_EIO;
  146. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk)
  147. && !ignore_nack)
  148. {
  149. LOG_E("sending addr fail\n");
  150. return -RT_EIO;
  151. }
  152. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  153. }
  154. return RT_EOK;
  155. }
  156. static rt_size_t nu_ui2c_mst_xfer(struct rt_i2c_bus_device *bus,
  157. struct rt_i2c_msg msgs[],
  158. rt_uint32_t num)
  159. {
  160. struct rt_i2c_msg *msg;
  161. nu_ui2c_bus_t *nu_ui2c;
  162. rt_size_t i;
  163. rt_uint32_t cnt_data;
  164. rt_uint16_t ignore_nack;
  165. rt_err_t ret;
  166. RT_ASSERT(bus != RT_NULL);
  167. nu_ui2c = (nu_ui2c_bus_t *) bus;
  168. nu_ui2c->msg = msgs;
  169. UI2C_Open(nu_ui2c->ui2c_base, 100000);
  170. (nu_ui2c->ui2c_base)->PROTSTS = (nu_ui2c->ui2c_base)->PROTSTS;//Clear status
  171. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, UI2C_CTL_STA);
  172. ret = nu_ui2c_wait_ready_with_timeout(nu_ui2c);
  173. if (ret != RT_EOK) //for timeout condition
  174. {
  175. rt_set_errno(-RT_ETIMEOUT);
  176. return 0;
  177. }
  178. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_STARIF_Msk) != UI2C_PROTSTS_STARIF_Msk)) /* Check Send START */
  179. {
  180. i = 0;
  181. LOG_E("Send START Fail");
  182. return i;
  183. }
  184. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_STARIF_Msk);
  185. for (i = 0; i < num; i++)
  186. {
  187. msg = &msgs[i];
  188. ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
  189. if (!(msg->flags & RT_I2C_NO_START))
  190. {
  191. if (i)
  192. {
  193. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, (UI2C_CTL_PTRG | UI2C_CTL_STA));/* Send repeat START */
  194. ret = nu_ui2c_wait_ready_with_timeout(nu_ui2c);
  195. if (ret != RT_EOK) //for timeout condition
  196. break;
  197. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_STARIF_Msk) != UI2C_PROTSTS_STARIF_Msk)) /* Check Send repeat START */
  198. {
  199. i = 0;
  200. LOG_E("Send repeat START Fail");
  201. break;
  202. }
  203. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_STARIF_Msk);
  204. }
  205. if ((RT_EOK != nu_ui2c_send_address(nu_ui2c, msg))
  206. && !ignore_nack)
  207. {
  208. i = 0;
  209. LOG_E("Send Address Fail");
  210. break;
  211. }
  212. }
  213. if (nu_ui2c->msg[i].flags & RT_I2C_RD) /* Receive Bytes */
  214. {
  215. rt_uint32_t do_rd_nack = (i == (num - 1));
  216. for (cnt_data = 0 ; cnt_data < (nu_ui2c->msg[i].len) ; cnt_data++)
  217. {
  218. do_rd_nack += (cnt_data == (nu_ui2c->msg[i].len - 1)); /* NACK after last byte for hardware setting */
  219. if (do_rd_nack == 2)
  220. {
  221. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, UI2C_CTL_PTRG);
  222. }
  223. else
  224. {
  225. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, (UI2C_CTL_PTRG | UI2C_CTL_AA));
  226. }
  227. ret = nu_ui2c_wait_ready_with_timeout(nu_ui2c);
  228. if (ret != RT_EOK) //for timeout condition
  229. break;
  230. if (nu_ui2c->ui2c_base->PROTCTL & UI2C_CTL_AA)
  231. {
  232. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk)) /*Master Receive Data ACK*/
  233. {
  234. i = 0;
  235. break;
  236. }
  237. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  238. }
  239. else
  240. {
  241. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_NACKIF_Msk) != UI2C_PROTSTS_NACKIF_Msk)) /*Master Receive Data NACK*/
  242. {
  243. i = 0;
  244. break;
  245. }
  246. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_NACKIF_Msk);
  247. }
  248. nu_ui2c->msg[i].buf[cnt_data] = nu_ui2c->ui2c_base->RXDAT;
  249. }
  250. }
  251. else /* Send Bytes */
  252. {
  253. for (cnt_data = 0 ; cnt_data < (nu_ui2c->msg[i].len) ; cnt_data++)
  254. {
  255. /* Send register number and MSB of data */
  256. ret = nu_ui2c_send_data(nu_ui2c, (uint8_t)(nu_ui2c->msg[i].buf[cnt_data]));
  257. if (ret != RT_EOK) //for timeout condition
  258. break;
  259. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk)
  260. && !ignore_nack
  261. ) /* Send data and get Ack */
  262. {
  263. i = 0;
  264. break;
  265. }
  266. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  267. }
  268. }
  269. }
  270. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, (UI2C_CTL_PTRG | UI2C_CTL_STO)); /* Send STOP signal */
  271. ret = nu_ui2c_wait_ready_with_timeout(nu_ui2c);
  272. if (ret != RT_EOK) //for timeout condition
  273. {
  274. rt_set_errno(-RT_ETIMEOUT);
  275. return 0;
  276. }
  277. RT_ASSERT(((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_STORIF_Msk) == UI2C_PROTSTS_STORIF_Msk));
  278. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_STORIF_Msk) != UI2C_PROTSTS_STORIF_Msk)) /* Bus Free*/
  279. {
  280. i = 0;
  281. LOG_E("Send STOP Fail");
  282. }
  283. else if (nu_ui2c->msg[0].flags & RT_I2C_ADDR_10BIT)
  284. {
  285. UI2C_DISABLE_10BIT_ADDR_MODE(nu_ui2c->ui2c_base);
  286. }
  287. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_STORIF_Msk);
  288. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, UI2C_CTL_PTRG);
  289. (nu_ui2c->ui2c_base)->PROTSTS = (nu_ui2c->ui2c_base)->PROTSTS;
  290. UI2C_Close(nu_ui2c->ui2c_base);
  291. nu_ui2c->msg = RT_NULL;
  292. return i;
  293. }
  294. #endif //(defined(BSP_USING_UI2C0) || defined(BSP_USING_UI2C1))
  295. /* Public functions -------------------------------------------------------------*/
  296. int rt_hw_ui2c_init(void)
  297. {
  298. rt_err_t ret = RT_ERROR;
  299. #if defined(BSP_USING_UI2C0)
  300. /* Enable UI2C0 clock */
  301. SYS_UnlockReg();
  302. CLK_EnableModuleClock(USCI0_MODULE);
  303. SYS_ResetModule(USCI0_RST);
  304. SYS_LockReg();
  305. nu_ui2c0.ui2c_dev.ops = &nu_ui2c_ops;
  306. ret = rt_i2c_bus_device_register(&nu_ui2c0.ui2c_dev, nu_ui2c0.dev_name);
  307. RT_ASSERT(RT_EOK == ret);
  308. #endif /* BSP_USING_UI2C0 */
  309. #if defined(BSP_USING_UI2C1)
  310. /* Enable UI2C1 clock */
  311. SYS_UnlockReg();
  312. CLK_EnableModuleClock(USCI1_MODULE);
  313. SYS_ResetModule(USCI1_RST);
  314. SYS_LockReg();
  315. nu_ui2c1.ui2c_dev.ops = &nu_ui2c_ops;
  316. ret = rt_i2c_bus_device_register(&nu_ui2c1.ui2c_dev, nu_ui2c1.dev_name);
  317. RT_ASSERT(RT_EOK == ret);
  318. #endif /* BSP_USING_UI2C1 */
  319. return ret;
  320. }
  321. INIT_DEVICE_EXPORT(rt_hw_ui2c_init);
  322. #endif //#if (defined(BSP_USING_UI2C) && defined(RT_USING_I2C))