drv_i2c.c 12 KB

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