drv_ui2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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-1-14 klcheng 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. #define DBG_COLOR
  22. #include <rtdbg.h>
  23. #define SLV_10BIT_ADDR (0x1E<<2) //1111+0xx+r/w
  24. /* Private typedef --------------------------------------------------------------*/
  25. typedef struct nu_ui2c_bus
  26. {
  27. struct rt_i2c_bus_device ui2c_dev;
  28. struct rt_i2c_msg *msg;
  29. UI2C_T *ui2c_base;
  30. char *dev_name;
  31. } nu_ui2c_bus_t;
  32. /* Private functions ------------------------------------------------------------*/
  33. static rt_ssize_t nu_ui2c_mst_xfer(struct rt_i2c_bus_device *ui2c_dev,
  34. struct rt_i2c_msg msgs[],
  35. rt_uint32_t num);
  36. static const struct rt_i2c_bus_device_ops nu_ui2c_ops =
  37. {
  38. .master_xfer = nu_ui2c_mst_xfer,
  39. .slave_xfer = NULL,
  40. .i2c_bus_control = NULL,
  41. };
  42. /* Private variables ------------------------------------------------------------*/
  43. #ifdef BSP_USING_UI2C0
  44. #define UI2C0BUS_NAME "ui2c0"
  45. static nu_ui2c_bus_t nu_ui2c0 =
  46. {
  47. .ui2c_base = UI2C0,
  48. .dev_name = UI2C0BUS_NAME,
  49. };
  50. #endif /* BSP_USING_UI2C0 */
  51. #ifdef BSP_USING_UI2C1
  52. #define UI2C1BUS_NAME "ui2c1"
  53. static nu_ui2c_bus_t nu_ui2c1 =
  54. {
  55. .ui2c_base = UI2C1,
  56. .dev_name = UI2C1BUS_NAME,
  57. };
  58. #endif /* BSP_USING_UI2C1 */
  59. /* Functions define ------------------------------------------------------------*/
  60. #if (defined(BSP_USING_UI2C0) || defined(BSP_USING_UI2C1))
  61. static inline rt_err_t nu_ui2c_wait_ready_with_timeout(nu_ui2c_bus_t *nu_ui2c)
  62. {
  63. rt_tick_t start = rt_tick_get();
  64. 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)))
  65. {
  66. if ((rt_tick_get() - start) > nu_ui2c->ui2c_dev.timeout)
  67. {
  68. LOG_E("\nui2c: timeout!\n");
  69. return -RT_ETIMEOUT;
  70. }
  71. }
  72. return RT_EOK;
  73. }
  74. static inline rt_err_t nu_ui2c_send_data(nu_ui2c_bus_t *nu_ui2c, rt_uint8_t data)
  75. {
  76. UI2C_SET_DATA(nu_ui2c->ui2c_base, data);
  77. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, UI2C_CTL_PTRG);
  78. return nu_ui2c_wait_ready_with_timeout(nu_ui2c);
  79. }
  80. static rt_err_t nu_ui2c_send_address(nu_ui2c_bus_t *nu_ui2c,
  81. struct rt_i2c_msg *msg)
  82. {
  83. rt_uint16_t flags = msg->flags;
  84. rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
  85. rt_uint8_t addr1, addr2;
  86. rt_err_t ret;
  87. if (flags & RT_I2C_ADDR_10BIT)
  88. {
  89. UI2C_ENABLE_10BIT_ADDR_MODE(nu_ui2c->ui2c_base);
  90. /* Init Send 10-bit Addr */
  91. addr1 = ((msg->addr >> 8) | SLV_10BIT_ADDR) << 1;
  92. addr2 = msg->addr & 0xff;
  93. LOG_D("addr1: %d, addr2: %d\n", addr1, addr2);
  94. ret = nu_ui2c_send_data(nu_ui2c, addr1);
  95. if (ret != RT_EOK) //for timeout condition
  96. return -RT_EIO;
  97. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk) && !ignore_nack)
  98. {
  99. LOG_E("NACK: sending first addr\n");
  100. return -RT_EIO;
  101. }
  102. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  103. ret = nu_ui2c_send_data(nu_ui2c, addr2);
  104. if (ret != RT_EOK) //for timeout condition
  105. return -RT_EIO;
  106. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk) && !ignore_nack)
  107. {
  108. LOG_E("NACK: sending second addr\n");
  109. return -RT_EIO;
  110. }
  111. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  112. if (flags & RT_I2C_RD)
  113. {
  114. LOG_D("send repeated start condition\n");
  115. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, (UI2C_CTL_PTRG | UI2C_CTL_STA));
  116. ret = nu_ui2c_wait_ready_with_timeout(nu_ui2c);
  117. if (ret != RT_EOK) //for timeout condition
  118. return -RT_EIO;
  119. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_STARIF_Msk) != UI2C_PROTSTS_STARIF_Msk) && !ignore_nack)
  120. {
  121. LOG_E("sending repeated START fail\n");
  122. return -RT_EIO;
  123. }
  124. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_STARIF_Msk);
  125. addr1 |= RT_I2C_RD;
  126. ret = nu_ui2c_send_data(nu_ui2c, addr1);
  127. if (ret != RT_EOK) //for timeout condition
  128. return -RT_EIO;
  129. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk) && !ignore_nack)
  130. {
  131. LOG_E("NACK: sending repeated addr\n");
  132. return -RT_EIO;
  133. }
  134. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  135. }
  136. }
  137. else
  138. {
  139. /* 7-bit addr */
  140. addr1 = msg->addr << 1;
  141. if (flags & RT_I2C_RD)
  142. addr1 |= RT_I2C_RD;
  143. /* Send device address */
  144. ret = nu_ui2c_send_data(nu_ui2c, addr1); /* Send Address */
  145. if (ret != RT_EOK) //for timeout condition
  146. return -RT_EIO;
  147. if (((UI2C_GET_PROT_STATUS(nu_ui2c->ui2c_base) & UI2C_PROTSTS_ACKIF_Msk) != UI2C_PROTSTS_ACKIF_Msk)
  148. && !ignore_nack)
  149. {
  150. LOG_E("sending addr fail\n");
  151. return -RT_EIO;
  152. }
  153. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_ACKIF_Msk);
  154. }
  155. return RT_EOK;
  156. }
  157. static rt_ssize_t nu_ui2c_mst_xfer(struct rt_i2c_bus_device *bus,
  158. struct rt_i2c_msg msgs[],
  159. rt_uint32_t num)
  160. {
  161. struct rt_i2c_msg *msg;
  162. nu_ui2c_bus_t *nu_ui2c;
  163. rt_size_t i;
  164. rt_uint32_t cnt_data;
  165. rt_uint16_t ignore_nack;
  166. rt_err_t ret;
  167. RT_ASSERT(bus != RT_NULL);
  168. nu_ui2c = (nu_ui2c_bus_t *) bus;
  169. nu_ui2c->msg = msgs;
  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. UI2C_CLR_PROT_INT_FLAG(nu_ui2c->ui2c_base, UI2C_PROTSTS_STORIF_Msk);
  284. UI2C_SET_CONTROL_REG(nu_ui2c->ui2c_base, UI2C_CTL_PTRG);
  285. UI2C_DISABLE_10BIT_ADDR_MODE(nu_ui2c->ui2c_base); /*clear all sub modes like 10 bit mode*/
  286. nu_ui2c->msg = RT_NULL;
  287. return i;
  288. }
  289. #endif //(defined(BSP_USING_UI2C0) || defined(BSP_USING_UI2C1))
  290. /* Public functions -------------------------------------------------------------*/
  291. int rt_hw_ui2c_init(void)
  292. {
  293. rt_err_t ret = -RT_ERROR;
  294. #if defined(BSP_USING_UI2C0)
  295. SYS_UnlockReg();
  296. SYS_ResetModule(USCI0_RST);
  297. SYS_LockReg();
  298. nu_ui2c0.ui2c_dev.ops = &nu_ui2c_ops;
  299. UI2C_Open(nu_ui2c0.ui2c_base, 100000);
  300. ret = rt_i2c_bus_device_register(&nu_ui2c0.ui2c_dev, nu_ui2c0.dev_name);
  301. RT_ASSERT(RT_EOK == ret);
  302. #endif /* BSP_USING_UI2C0 */
  303. #if defined(BSP_USING_UI2C1)
  304. SYS_UnlockReg();
  305. SYS_ResetModule(USCI1_RST);
  306. SYS_LockReg();
  307. nu_ui2c1.ui2c_dev.ops = &nu_ui2c_ops;
  308. UI2C_Open(nu_ui2c1.ui2c_base, 100000);
  309. ret = rt_i2c_bus_device_register(&nu_ui2c1.ui2c_dev, nu_ui2c1.dev_name);
  310. RT_ASSERT(RT_EOK == ret);
  311. #endif /* BSP_USING_UI2C1 */
  312. return ret;
  313. }
  314. INIT_DEVICE_EXPORT(rt_hw_ui2c_init);
  315. #endif //#if defined(BSP_USING_UI2C)