drv_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 const struct rt_i2c_bus_device_ops nu_i2c_ops =
  74. {
  75. .master_xfer = nu_i2c_mst_xfer,
  76. .slave_xfer = NULL,
  77. .i2c_bus_control = NULL,
  78. };
  79. static rt_err_t nu_i2c_configure(nu_i2c_bus_t *bus)
  80. {
  81. RT_ASSERT(bus != RT_NULL);
  82. bus->parent.ops = &nu_i2c_ops;
  83. I2C_Open(bus->I2C, 100000);
  84. return RT_EOK;
  85. }
  86. static inline rt_err_t nu_i2c_wait_ready_with_timeout(nu_i2c_bus_t *bus)
  87. {
  88. rt_tick_t start = rt_tick_get();
  89. while (!(bus->I2C->CTL0 & I2C_CTL0_SI_Msk))
  90. {
  91. if ((rt_tick_get() - start) > bus->parent.timeout)
  92. {
  93. LOG_E("\ni2c: timeout!\n");
  94. return -RT_ETIMEOUT;
  95. }
  96. }
  97. return RT_EOK;
  98. }
  99. static inline rt_err_t nu_i2c_send_data(nu_i2c_bus_t *nu_i2c, rt_uint8_t data)
  100. {
  101. I2C_SET_DATA(nu_i2c->I2C, data);
  102. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_SI);
  103. return nu_i2c_wait_ready_with_timeout(nu_i2c);
  104. }
  105. static rt_err_t nu_i2c_send_address(nu_i2c_bus_t *nu_i2c,
  106. struct rt_i2c_msg *msg)
  107. {
  108. rt_uint16_t flags = msg->flags;
  109. rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
  110. rt_uint8_t addr1, addr2;
  111. rt_err_t ret;
  112. if (flags & RT_I2C_ADDR_10BIT)
  113. {
  114. nu_i2c->I2C->CTL1 |= I2C_CTL1_ADDR10EN_Msk;
  115. addr1 = 0xf0 | ((msg->addr >> 7) & 0x06);
  116. addr2 = msg->addr & 0xff;
  117. LOG_D("address1: %d, address2: %d\n", addr1, addr2);
  118. ret = nu_i2c_send_data(nu_i2c, addr1);
  119. if (ret != RT_EOK) /* for timeout condition */
  120. return -RT_EIO;
  121. if ((I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_TRANSMIT_ADDRESS_ACK) && !ignore_nack)
  122. {
  123. LOG_E("NACK: sending first address failed\n");
  124. return -RT_EIO;
  125. }
  126. ret = nu_i2c_send_data(nu_i2c, addr2);
  127. if (ret != RT_EOK) /* for timeout condition */
  128. return -RT_EIO;
  129. if ((I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_TRANSMIT_ADDRESS_ACK) && !ignore_nack)
  130. {
  131. LOG_E("NACK: sending second address failed\n");
  132. return -RT_EIO;
  133. }
  134. if (flags & RT_I2C_RD)
  135. {
  136. LOG_D("send repeated START signal\n");
  137. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_STA_SI);
  138. ret = nu_i2c_wait_ready_with_timeout(nu_i2c);
  139. if (ret != RT_EOK) /* for timeout condition */
  140. return -RT_EIO;
  141. if ((I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_REPEAT_START) && !ignore_nack)
  142. {
  143. LOG_E("sending repeated START failed\n");
  144. return -RT_EIO;
  145. }
  146. addr1 |= 0x01;
  147. ret = nu_i2c_send_data(nu_i2c, addr1);
  148. if (ret != RT_EOK) /* for timeout condition */
  149. return -RT_EIO;
  150. if ((I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_RECEIVE_ADDRESS_ACK) && !ignore_nack)
  151. {
  152. LOG_E("NACK: sending read address failed\n");
  153. return -RT_EIO;
  154. }
  155. }
  156. }
  157. else
  158. {
  159. /* 7-bit addr */
  160. addr1 = msg->addr << 1;
  161. if (flags & RT_I2C_RD)
  162. addr1 |= 1;
  163. /* Send device address */
  164. ret = nu_i2c_send_data(nu_i2c, addr1); /* Send Address */
  165. if (ret != RT_EOK) /* for timeout condition */
  166. return -RT_EIO;
  167. if ((I2C_GET_STATUS(nu_i2c->I2C)
  168. != ((flags & RT_I2C_RD) ? u32I2C_MASTER_STATUS_RECEIVE_ADDRESS_ACK : u32I2C_MASTER_STATUS_TRANSMIT_ADDRESS_ACK))
  169. && !ignore_nack)
  170. {
  171. LOG_E("sending address failed\n");
  172. return -RT_EIO;
  173. }
  174. }
  175. return RT_EOK;
  176. }
  177. static rt_size_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  178. struct rt_i2c_msg msgs[],
  179. rt_uint32_t num)
  180. {
  181. struct rt_i2c_msg *msg;
  182. nu_i2c_bus_t *nu_i2c;
  183. rt_size_t i;
  184. rt_uint32_t cnt_data;
  185. rt_uint16_t ignore_nack;
  186. rt_err_t ret;
  187. RT_ASSERT(bus != RT_NULL);
  188. nu_i2c = (nu_i2c_bus_t *) bus;
  189. nu_i2c->msg = msgs;
  190. nu_i2c->I2C->CTL0 |= I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk;
  191. ret = nu_i2c_wait_ready_with_timeout(nu_i2c);
  192. if (ret != RT_EOK) /* for timeout condition */
  193. {
  194. rt_set_errno(-RT_ETIMEOUT);
  195. return 0;
  196. }
  197. if (I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_START)
  198. {
  199. i = 0;
  200. LOG_E("Send START Failed");
  201. return i;
  202. }
  203. for (i = 0; i < num; i++)
  204. {
  205. msg = &msgs[i];
  206. ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
  207. if (!(msg->flags & RT_I2C_NO_START))
  208. {
  209. if (i)
  210. {
  211. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_STA_SI);
  212. ret = nu_i2c_wait_ready_with_timeout(nu_i2c);
  213. if (ret != RT_EOK) /* for timeout conditrion */
  214. break;
  215. if (I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_REPEAT_START)
  216. {
  217. i = 0;
  218. LOG_E("Send repeat START Fail");
  219. break;
  220. }
  221. }
  222. if ((RT_EOK != nu_i2c_send_address(nu_i2c, msg))
  223. && !ignore_nack)
  224. {
  225. i = 0;
  226. LOG_E("Send Address Fail");
  227. break;
  228. }
  229. }
  230. if (nu_i2c->msg[i].flags & RT_I2C_RD) /* Receive Bytes */
  231. {
  232. rt_uint32_t do_rd_nack = (i == (num - 1));
  233. for (cnt_data = 0 ; cnt_data < (nu_i2c->msg[i].len) ; cnt_data++)
  234. {
  235. do_rd_nack += (cnt_data == (nu_i2c->msg[i].len - 1)); /* NACK after last byte for hardware setting */
  236. if (do_rd_nack == 2)
  237. {
  238. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_SI);
  239. }
  240. else
  241. {
  242. I2C_SET_CONTROL_REG(nu_i2c->I2C, I2C_CTL_SI_AA);
  243. }
  244. ret = nu_i2c_wait_ready_with_timeout(nu_i2c);
  245. if (ret != RT_EOK) /* for timeout condition */
  246. break;
  247. if (nu_i2c->I2C->CTL0 & I2C_CTL_AA)
  248. {
  249. if (I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_RECEIVE_DATA_ACK)
  250. {
  251. i = 0;
  252. break;
  253. }
  254. }
  255. else
  256. {
  257. if (I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_RECEIVE_DATA_NACK)
  258. {
  259. i = 0;
  260. break;
  261. }
  262. }
  263. nu_i2c->msg[i].buf[cnt_data] = nu_i2c->I2C->DAT;
  264. }
  265. }
  266. else /* Send Bytes */
  267. {
  268. for (cnt_data = 0 ; cnt_data < (nu_i2c->msg[i].len) ; cnt_data++)
  269. {
  270. /* Send register number and MSB of data */
  271. ret = nu_i2c_send_data(nu_i2c, (uint8_t)(nu_i2c->msg[i].buf[cnt_data]));
  272. if (ret != RT_EOK) /* for timeout condition */
  273. break;
  274. if (I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_TRANSMIT_DATA_ACK
  275. && !ignore_nack
  276. ) /* Send aata and get Ack */
  277. {
  278. i = 0;
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. I2C_STOP(nu_i2c->I2C);
  285. RT_ASSERT(I2C_GET_STATUS(nu_i2c->I2C) == u32I2C_MASTER_STATUS_BUS_RELEASED);
  286. if (I2C_GET_STATUS(nu_i2c->I2C) != u32I2C_MASTER_STATUS_BUS_RELEASED)
  287. {
  288. i = 0;
  289. }
  290. nu_i2c->msg = RT_NULL;
  291. nu_i2c->I2C->CTL1 = 0; /*clear all sub modes like 10 bit mode*/
  292. return i;
  293. }
  294. #endif
  295. /* Public functions -------------------------------------------------------------*/
  296. int rt_hw_i2c_init(void)
  297. {
  298. rt_err_t ret = RT_ERROR;
  299. #if defined(BSP_USING_I2C0)
  300. SYS_UnlockReg();
  301. /* Enable I2C0 clock */
  302. SYS_ResetModule(I2C0_RST);
  303. SYS_LockReg();
  304. nu_i2c_configure(&nu_i2c0);
  305. ret = rt_i2c_bus_device_register(&nu_i2c0.parent, nu_i2c0.device_name);
  306. RT_ASSERT(RT_EOK == ret);
  307. #endif /* BSP_USING_I2C0 */
  308. #if defined(BSP_USING_I2C1)
  309. SYS_UnlockReg();
  310. /* Enable I2C1 clock */
  311. SYS_ResetModule(I2C1_RST);
  312. SYS_LockReg();
  313. nu_i2c_configure(&nu_i2c1);
  314. ret = rt_i2c_bus_device_register(&nu_i2c1.parent, nu_i2c1.device_name);
  315. RT_ASSERT(RT_EOK == ret);
  316. #endif /* BSP_USING_I2C1 */
  317. #if defined(BSP_USING_I2C2)
  318. SYS_UnlockReg();
  319. /* Enable I2C2 clock */
  320. SYS_ResetModule(I2C2_RST);
  321. SYS_LockReg();
  322. nu_i2c_configure(&nu_i2c2);
  323. ret = rt_i2c_bus_device_register(&nu_i2c2.parent, nu_i2c2.device_name);
  324. RT_ASSERT(RT_EOK == ret);
  325. #endif /* BSP_USING_I2C2 */
  326. return ret;
  327. }
  328. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  329. #endif /* BSP_USING_I2C */