nu_i2c.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /**************************************************************************//**
  2. * @file i2c.c
  3. * @version V3.00
  4. * @brief NUC980 series I2C driver source file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include "nu_i2c.h"
  10. #include "nu_sys.h"
  11. /** @addtogroup Standard_Driver Standard Driver
  12. @{
  13. */
  14. /** @addtogroup I2C_Driver I2C Driver
  15. @{
  16. */
  17. /** @addtogroup I2C_EXPORTED_FUNCTIONS I2C Exported Functions
  18. @{
  19. */
  20. /**
  21. * @brief Enable specify I2C Controller and set Clock Divider
  22. *
  23. * @param[in] i2c Specify I2C port
  24. * @param[in] u32BusClock The target I2C bus clock in Hz
  25. *
  26. * @return Actual I2C bus clock frequency
  27. *
  28. * @details The function enable the specify I2C Controller and set proper Clock Divider
  29. * in I2C CLOCK DIVIDED REGISTER (I2CLK) according to the target I2C Bus clock.
  30. * I2C Bus clock = PCLK / (4*(divider+1).
  31. *
  32. */
  33. uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock)
  34. {
  35. uint32_t u32Div;
  36. uint32_t u32Pclk;
  37. u32Pclk = (sysGetClock(SYS_PCLK01)) * 1000000;
  38. u32Div = (uint32_t)(((u32Pclk * 10U) / (u32BusClock * 4U) + 5U) / 10U - 1U); /* Compute proper divider for I2C clock */
  39. i2c->CLKDIV = u32Div;
  40. /* Enable I2C */
  41. i2c->CTL0 |= I2C_CTL0_I2CEN_Msk;
  42. return (u32Pclk / ((u32Div + 1U) << 2U));
  43. }
  44. /**
  45. * @brief Disable specify I2C Controller
  46. *
  47. * @param[in] i2c Specify I2C port
  48. *
  49. * @return None
  50. *
  51. * @details Reset I2C Controller and disable specify I2C port.
  52. *
  53. */
  54. void I2C_Close(I2C_T *i2c)
  55. {
  56. /* Reset I2C Controller */
  57. if ((uint32_t)i2c == I2C0_BA)
  58. {
  59. outp32(REG_SYS_APBIPRST1, inpw(REG_SYS_APBIPRST1) | (0x1 << 0));
  60. outp32(REG_SYS_APBIPRST1, inpw(REG_SYS_APBIPRST1) & ~(0x1 << 0));
  61. }
  62. else if ((uint32_t)i2c == I2C1_BA)
  63. {
  64. outp32(REG_SYS_APBIPRST1, inpw(REG_SYS_APBIPRST1) | (0x1 << 1));
  65. outp32(REG_SYS_APBIPRST1, inpw(REG_SYS_APBIPRST1) & ~(0x1 << 1));
  66. }
  67. else if ((uint32_t)i2c == I2C2_BA)
  68. {
  69. outp32(REG_SYS_APBIPRST1, inpw(REG_SYS_APBIPRST1) | (0x1 << 2));
  70. outp32(REG_SYS_APBIPRST1, inpw(REG_SYS_APBIPRST1) & ~(0x1 << 2));
  71. }
  72. else if ((uint32_t)i2c == I2C3_BA)
  73. {
  74. outp32(REG_SYS_APBIPRST1, inpw(REG_SYS_APBIPRST1) | (0x1 << 3));
  75. outp32(REG_SYS_APBIPRST1, inpw(REG_SYS_APBIPRST1) & ~(0x1 << 3));
  76. }
  77. /* Disable I2C */
  78. i2c->CTL0 &= ~I2C_CTL0_I2CEN_Msk;
  79. }
  80. /**
  81. * @brief Clear Time-out Counter flag
  82. *
  83. * @param[in] i2c Specify I2C port
  84. *
  85. * @return None
  86. *
  87. * @details When Time-out flag will be set, use this function to clear I2C Bus Time-out counter flag .
  88. *
  89. */
  90. void I2C_ClearTimeoutFlag(I2C_T *i2c)
  91. {
  92. i2c->TOCTL |= I2C_TOCTL_TOIF_Msk;
  93. }
  94. /**
  95. * @brief Set Control bit of I2C Controller
  96. *
  97. * @param[in] i2c Specify I2C port
  98. * @param[in] u8Start Set I2C START condition
  99. * @param[in] u8Stop Set I2C STOP condition
  100. * @param[in] u8Si Clear SI flag
  101. * @param[in] u8Ack Set I2C ACK bit
  102. *
  103. * @return None
  104. *
  105. * @details The function set I2C Control bit of I2C Bus protocol.
  106. *
  107. */
  108. void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack)
  109. {
  110. uint32_t u32Reg = 0U;
  111. if (u8Start)
  112. {
  113. u32Reg |= I2C_CTL_STA;
  114. }
  115. if (u8Stop)
  116. {
  117. u32Reg |= I2C_CTL_STO;
  118. }
  119. if (u8Si)
  120. {
  121. u32Reg |= I2C_CTL_SI;
  122. }
  123. if (u8Ack)
  124. {
  125. u32Reg |= I2C_CTL_AA;
  126. }
  127. i2c->CTL0 = (i2c->CTL0 & ~0x3CU) | u32Reg;
  128. }
  129. /**
  130. * @brief Disable Interrupt of I2C Controller
  131. *
  132. * @param[in] i2c Specify I2C port
  133. *
  134. * @return None
  135. *
  136. * @details The function is used for disable I2C interrupt
  137. *
  138. */
  139. void I2C_DisableInt(I2C_T *i2c)
  140. {
  141. i2c->CTL0 &= ~I2C_CTL0_INTEN_Msk;
  142. }
  143. /**
  144. * @brief Enable Interrupt of I2C Controller
  145. *
  146. * @param[in] i2c Specify I2C port
  147. *
  148. * @return None
  149. *
  150. * @details The function is used for enable I2C interrupt
  151. *
  152. */
  153. void I2C_EnableInt(I2C_T *i2c)
  154. {
  155. i2c->CTL0 |= I2C_CTL0_INTEN_Msk;
  156. }
  157. /**
  158. * @brief Get I2C Bus Clock
  159. *
  160. * @param[in] i2c Specify I2C port
  161. *
  162. * @return The actual I2C Bus clock in Hz
  163. *
  164. * @details To get the actual I2C Bus Clock frequency.
  165. */
  166. uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
  167. {
  168. uint32_t u32Divider = i2c->CLKDIV;
  169. uint32_t u32Pclk;
  170. u32Pclk = (sysGetClock(SYS_PCLK01)) * 1000000;
  171. return (u32Pclk / ((u32Divider + 1U) << 2U));
  172. }
  173. /**
  174. * @brief Set I2C Bus Clock
  175. *
  176. * @param[in] i2c Specify I2C port
  177. * @param[in] u32BusClock The target I2C Bus Clock in Hz
  178. *
  179. * @return The actual I2C Bus Clock in Hz
  180. *
  181. * @details To set the actual I2C Bus Clock frequency.
  182. */
  183. uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
  184. {
  185. uint32_t u32Div;
  186. uint32_t u32Pclk;
  187. u32Pclk = (sysGetClock(SYS_PCLK01)) * 1000000;
  188. u32Div = (uint32_t)(((u32Pclk * 10U) / (u32BusClock * 4U) + 5U) / 10U - 1U); /* Compute proper divider for I2C clock */
  189. i2c->CLKDIV = u32Div;
  190. return (u32Pclk / ((u32Div + 1U) << 2U));
  191. }
  192. /**
  193. * @brief Get Interrupt Flag
  194. *
  195. * @param[in] i2c Specify I2C port
  196. *
  197. * @return I2C interrupt flag status
  198. *
  199. * @details To get I2C Bus interrupt flag.
  200. */
  201. uint32_t I2C_GetIntFlag(I2C_T *i2c)
  202. {
  203. uint32_t u32Value;
  204. if ((i2c->CTL0 & I2C_CTL0_SI_Msk) == I2C_CTL0_SI_Msk)
  205. {
  206. u32Value = 1U;
  207. }
  208. else
  209. {
  210. u32Value = 0U;
  211. }
  212. return u32Value;
  213. }
  214. /**
  215. * @brief Get I2C Bus Status Code
  216. *
  217. * @param[in] i2c Specify I2C port
  218. *
  219. * @return I2C Status Code
  220. *
  221. * @details To get I2C Bus Status Code.
  222. */
  223. uint32_t I2C_GetStatus(I2C_T *i2c)
  224. {
  225. return (i2c->STATUS0);
  226. }
  227. /**
  228. * @brief Read a Byte from I2C Bus
  229. *
  230. * @param[in] i2c Specify I2C port
  231. *
  232. * @return I2C Data
  233. *
  234. * @details To read a bytes data from specify I2C port.
  235. */
  236. uint8_t I2C_GetData(I2C_T *i2c)
  237. {
  238. return (uint8_t)(i2c->DAT);
  239. }
  240. /**
  241. * @brief Send a byte to I2C Bus
  242. *
  243. * @param[in] i2c Specify I2C port
  244. * @param[in] u8Data The data to send to I2C bus
  245. *
  246. * @return None
  247. *
  248. * @details This function is used to write a byte to specified I2C port
  249. */
  250. void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
  251. {
  252. i2c->DAT = u8Data;
  253. }
  254. /**
  255. * @brief Set 7-bit Slave Address and GC Mode
  256. *
  257. * @param[in] i2c Specify I2C port
  258. * @param[in] u8SlaveNo Set the number of I2C address register (0~3)
  259. * @param[in] u8SlaveAddr 7-bit slave address
  260. * @param[in] u8GCMode Enable/Disable GC mode (I2C_GCMODE_ENABLE / I2C_GCMODE_DISABLE)
  261. *
  262. * @return None
  263. *
  264. * @details This function is used to set 7-bit slave addresses in I2C SLAVE ADDRESS REGISTER (I2CADDR0~3)
  265. * and enable GC Mode.
  266. *
  267. */
  268. void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode)
  269. {
  270. switch (u8SlaveNo)
  271. {
  272. case 1:
  273. i2c->ADDR1 = ((uint32_t)u8SlaveAddr << 1U) | u8GCMode;
  274. break;
  275. case 2:
  276. i2c->ADDR2 = ((uint32_t)u8SlaveAddr << 1U) | u8GCMode;
  277. break;
  278. case 3:
  279. i2c->ADDR3 = ((uint32_t)u8SlaveAddr << 1U) | u8GCMode;
  280. break;
  281. case 0:
  282. default:
  283. i2c->ADDR0 = ((uint32_t)u8SlaveAddr << 1U) | u8GCMode;
  284. break;
  285. }
  286. }
  287. /**
  288. * @brief Configure the mask bits of 7-bit Slave Address
  289. *
  290. * @param[in] i2c Specify I2C port
  291. * @param[in] u8SlaveNo Set the number of I2C address mask register (0~3)
  292. * @param[in] u8SlaveAddrMask A byte for slave address mask
  293. *
  294. * @return None
  295. *
  296. * @details This function is used to set 7-bit slave addresses.
  297. *
  298. */
  299. void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
  300. {
  301. switch (u8SlaveNo)
  302. {
  303. case 1:
  304. i2c->ADDRMSK1 = (uint32_t)u8SlaveAddrMask << 1U;
  305. break;
  306. case 2:
  307. i2c->ADDRMSK2 = (uint32_t)u8SlaveAddrMask << 1U;
  308. break;
  309. case 3:
  310. i2c->ADDRMSK3 = (uint32_t)u8SlaveAddrMask << 1U;
  311. break;
  312. case 0:
  313. default:
  314. i2c->ADDRMSK0 = (uint32_t)u8SlaveAddrMask << 1U;
  315. break;
  316. }
  317. }
  318. /**
  319. * @brief Enable Time-out Counter Function and support Long Time-out
  320. *
  321. * @param[in] i2c Specify I2C port
  322. * @param[in] u8LongTimeout Configure DIV4 to enable Long Time-out (0/1)
  323. *
  324. * @return None
  325. *
  326. * @details This function enable Time-out Counter function and configure DIV4 to support Long
  327. * Time-out.
  328. *
  329. */
  330. void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
  331. {
  332. if (u8LongTimeout)
  333. {
  334. i2c->TOCTL |= I2C_TOCTL_TOCDIV4_Msk;
  335. }
  336. else
  337. {
  338. i2c->TOCTL &= ~I2C_TOCTL_TOCDIV4_Msk;
  339. }
  340. i2c->TOCTL |= I2C_TOCTL_TOCEN_Msk;
  341. }
  342. /**
  343. * @brief Disable Time-out Counter Function
  344. *
  345. * @param[in] i2c Specify I2C port
  346. *
  347. * @return None
  348. *
  349. * @details To disable Time-out Counter function in I2CTOC register.
  350. *
  351. */
  352. void I2C_DisableTimeout(I2C_T *i2c)
  353. {
  354. i2c->TOCTL &= ~I2C_TOCTL_TOCEN_Msk;
  355. }
  356. /**
  357. * @brief Enable I2C Wake-up Function
  358. *
  359. * @param[in] i2c Specify I2C port
  360. *
  361. * @return None
  362. *
  363. * @details To enable Wake-up function of I2C Wake-up control register.
  364. *
  365. */
  366. void I2C_EnableWakeup(I2C_T *i2c)
  367. {
  368. i2c->WKCTL |= I2C_WKCTL_WKEN_Msk;
  369. }
  370. /**
  371. * @brief Disable I2C Wake-up Function
  372. *
  373. * @param[in] i2c Specify I2C port
  374. *
  375. * @return None
  376. *
  377. * @details To disable Wake-up function of I2C Wake-up control register.
  378. *
  379. */
  380. void I2C_DisableWakeup(I2C_T *i2c)
  381. {
  382. i2c->WKCTL &= ~I2C_WKCTL_WKEN_Msk;
  383. }
  384. /**
  385. * @brief Write a byte to Slave
  386. *
  387. * @param[in] *i2c Point to I2C peripheral
  388. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  389. * @param[in] data Write a byte data to Slave
  390. *
  391. * @retval 0 Write data success
  392. * @retval 1 Write data fail, or bus occurs error events
  393. *
  394. * @details The function is used for I2C Master write a byte data to Slave.
  395. *
  396. */
  397. uint8_t I2C_WriteByte(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data)
  398. {
  399. uint8_t u8Xfering = 1u, u8Err = 0u, u8Ctrl = 0u;
  400. I2C_START(i2c);
  401. while (u8Xfering && (u8Err == 0u))
  402. {
  403. I2C_WAIT_READY(i2c) {}
  404. switch (I2C_GET_STATUS(i2c))
  405. {
  406. case 0x08u:
  407. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  408. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  409. break;
  410. case 0x18u: /* Slave Address ACK */
  411. I2C_SET_DATA(i2c, data); /* Write data to I2CDAT */
  412. break;
  413. case 0x20u: /* Slave Address NACK */
  414. case 0x30u: /* Master transmit data NACK */
  415. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  416. u8Err = 1u;
  417. break;
  418. case 0x28u:
  419. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  420. u8Xfering = 0u;
  421. break;
  422. case 0x38u: /* Arbitration Lost */
  423. default: /* Unknow status */
  424. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  425. u8Err = 1u;
  426. break;
  427. }
  428. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  429. }
  430. return (u8Err | u8Xfering); /* return (Success)/(Fail) status */
  431. }
  432. /**
  433. * @brief Write multi bytes to Slave
  434. *
  435. * @param[in] *i2c Point to I2C peripheral
  436. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  437. * @param[in] *data Pointer to array to write data to Slave
  438. * @param[in] u32wLen How many bytes need to write to Slave
  439. *
  440. * @return A length of how many bytes have been transmitted.
  441. *
  442. * @details The function is used for I2C Master write multi bytes data to Slave.
  443. *
  444. */
  445. uint32_t I2C_WriteMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data[], uint32_t u32wLen)
  446. {
  447. uint8_t u8Xfering = 1u, u8Err = 0u, u8Ctrl = 0u;
  448. uint32_t u32txLen = 0u;
  449. I2C_START(i2c); /* Send START */
  450. while (u8Xfering && (u8Err == 0u))
  451. {
  452. I2C_WAIT_READY(i2c) {}
  453. switch (I2C_GET_STATUS(i2c))
  454. {
  455. case 0x08u:
  456. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  457. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  458. break;
  459. case 0x18u: /* Slave Address ACK */
  460. case 0x28u:
  461. if (u32txLen < u32wLen)
  462. {
  463. I2C_SET_DATA(i2c, data[u32txLen++]); /* Write Data to I2CDAT */
  464. }
  465. else
  466. {
  467. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  468. u8Xfering = 0u;
  469. }
  470. break;
  471. case 0x20u: /* Slave Address NACK */
  472. case 0x30u: /* Master transmit data NACK */
  473. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  474. u8Err = 1u;
  475. break;
  476. case 0x38u: /* Arbitration Lost */
  477. default: /* Unknow status */
  478. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  479. u8Err = 1u;
  480. break;
  481. }
  482. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  483. }
  484. return u32txLen; /* Return bytes length that have been transmitted */
  485. }
  486. /**
  487. * @brief Specify a byte register address and write a byte to Slave
  488. *
  489. * @param[in] *i2c Point to I2C peripheral
  490. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  491. * @param[in] u8DataAddr Specify a address (1 byte) of data write to
  492. * @param[in] data A byte data to write it to Slave
  493. *
  494. * @retval 0 Write data success
  495. * @retval 1 Write data fail, or bus occurs error events
  496. *
  497. * @details The function is used for I2C Master specify a address that data write to in Slave.
  498. *
  499. */
  500. uint8_t I2C_WriteByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data)
  501. {
  502. uint8_t u8Xfering = 1u, u8Err = 0u, u8Ctrl = 0u;
  503. uint32_t u32txLen = 0u;
  504. I2C_START(i2c); /* Send START */
  505. while (u8Xfering && (u8Err == 0u))
  506. {
  507. I2C_WAIT_READY(i2c) {}
  508. switch (I2C_GET_STATUS(i2c))
  509. {
  510. case 0x08u:
  511. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Send Slave address with write bit */
  512. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  513. break;
  514. case 0x18u: /* Slave Address ACK */
  515. I2C_SET_DATA(i2c, u8DataAddr); /* Write Lo byte address of register */
  516. break;
  517. case 0x20u: /* Slave Address NACK */
  518. case 0x30u: /* Master transmit data NACK */
  519. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  520. u8Err = 1u;
  521. break;
  522. case 0x28u:
  523. if (u32txLen < 1u)
  524. {
  525. I2C_SET_DATA(i2c, data);
  526. u32txLen++;
  527. }
  528. else
  529. {
  530. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  531. u8Xfering = 0u;
  532. }
  533. break;
  534. case 0x38u: /* Arbitration Lost */
  535. default: /* Unknow status */
  536. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  537. u8Err = 1u;
  538. break;
  539. }
  540. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  541. }
  542. return (u8Err | u8Xfering); /* return (Success)/(Fail) status */
  543. }
  544. /**
  545. * @brief Specify a byte register address and write multi bytes to Slave
  546. *
  547. * @param[in] *i2c Point to I2C peripheral
  548. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  549. * @param[in] u8DataAddr Specify a address (1 byte) of data write to
  550. * @param[in] *data Pointer to array to write data to Slave
  551. * @param[in] u32wLen How many bytes need to write to Slave
  552. *
  553. * @return A length of how many bytes have been transmitted.
  554. *
  555. * @details The function is used for I2C Master specify a byte address that multi data bytes write to in Slave.
  556. *
  557. */
  558. uint32_t I2C_WriteMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data[], uint32_t u32wLen)
  559. {
  560. uint8_t u8Xfering = 1u, u8Err = 0u, u8Ctrl = 0u;
  561. uint32_t u32txLen = 0u;
  562. I2C_START(i2c); /* Send START */
  563. while (u8Xfering && (u8Err == 0u))
  564. {
  565. I2C_WAIT_READY(i2c) {}
  566. switch (I2C_GET_STATUS(i2c))
  567. {
  568. case 0x08u:
  569. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  570. u8Ctrl = I2C_CTL_SI;
  571. break;
  572. case 0x18u: /* Slave Address ACK */
  573. I2C_SET_DATA(i2c, u8DataAddr); /* Write Lo byte address of register */
  574. break;
  575. case 0x20u: /* Slave Address NACK */
  576. case 0x30u: /* Master transmit data NACK */
  577. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  578. u8Err = 1u;
  579. break;
  580. case 0x28u:
  581. if (u32txLen < u32wLen)
  582. {
  583. I2C_SET_DATA(i2c, data[u32txLen++]);
  584. }
  585. else
  586. {
  587. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  588. u8Xfering = 0u;
  589. }
  590. break;
  591. case 0x38u: /* Arbitration Lost */
  592. default: /* Unknow status */
  593. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  594. u8Err = 1u;
  595. break;
  596. }
  597. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  598. }
  599. return u32txLen; /* Return bytes length that have been transmitted */
  600. }
  601. /**
  602. * @brief Specify two bytes register address and Write a byte to Slave
  603. *
  604. * @param[in] *i2c Point to I2C peripheral
  605. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  606. * @param[in] u16DataAddr Specify a address (2 byte) of data write to
  607. * @param[in] data Write a byte data to Slave
  608. *
  609. * @retval 0 Write data success
  610. * @retval 1 Write data fail, or bus occurs error events
  611. *
  612. * @details The function is used for I2C Master specify two bytes address that data write to in Slave.
  613. *
  614. */
  615. uint8_t I2C_WriteByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data)
  616. {
  617. uint8_t u8Xfering = 1u, u8Err = 0u, u8Addr = 1u, u8Ctrl = 0u;
  618. uint32_t u32txLen = 0u;
  619. I2C_START(i2c); /* Send START */
  620. while (u8Xfering && (u8Err == 0u))
  621. {
  622. I2C_WAIT_READY(i2c) {}
  623. switch (I2C_GET_STATUS(i2c))
  624. {
  625. case 0x08u:
  626. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  627. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  628. break;
  629. case 0x18u: /* Slave Address ACK */
  630. I2C_SET_DATA(i2c, (uint8_t)((u16DataAddr & 0xFF00u) >> 8u)); /* Write Hi byte address of register */
  631. break;
  632. case 0x20u: /* Slave Address NACK */
  633. case 0x30u: /* Master transmit data NACK */
  634. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  635. u8Err = 1u;
  636. break;
  637. case 0x28u:
  638. if (u8Addr)
  639. {
  640. I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFFu)); /* Write Lo byte address of register */
  641. u8Addr = 0u;
  642. }
  643. else if ((u32txLen < 1u) && (u8Addr == 0u))
  644. {
  645. I2C_SET_DATA(i2c, data);
  646. u32txLen++;
  647. }
  648. else
  649. {
  650. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  651. u8Xfering = 0u;
  652. }
  653. break;
  654. case 0x38u: /* Arbitration Lost */
  655. default: /* Unknow status */
  656. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  657. u8Err = 1u;
  658. break;
  659. }
  660. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  661. }
  662. return (u8Err | u8Xfering); /* return (Success)/(Fail) status */
  663. }
  664. /**
  665. * @brief Specify two bytes register address and write multi bytes to Slave
  666. *
  667. * @param[in] *i2c Point to I2C peripheral
  668. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  669. * @param[in] u16DataAddr Specify a address (2 bytes) of data write to
  670. * @param[in] data[] A data array for write data to Slave
  671. * @param[in] u32wLen How many bytes need to write to Slave
  672. *
  673. * @return A length of how many bytes have been transmitted.
  674. *
  675. * @details The function is used for I2C Master specify a byte address that multi data write to in Slave.
  676. *
  677. */
  678. uint32_t I2C_WriteMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data[], uint32_t u32wLen)
  679. {
  680. uint8_t u8Xfering = 1u, u8Err = 0u, u8Addr = 1u, u8Ctrl = 0u;
  681. uint32_t u32txLen = 0u;
  682. I2C_START(i2c); /* Send START */
  683. while (u8Xfering && (u8Err == 0u))
  684. {
  685. I2C_WAIT_READY(i2c) {}
  686. switch (I2C_GET_STATUS(i2c))
  687. {
  688. case 0x08u:
  689. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  690. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  691. break;
  692. case 0x18u: /* Slave Address ACK */
  693. I2C_SET_DATA(i2c, (uint8_t)((u16DataAddr & 0xFF00u) >> 8u)); /* Write Hi byte address of register */
  694. break;
  695. case 0x20u: /* Slave Address NACK */
  696. case 0x30u: /* Master transmit data NACK */
  697. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  698. u8Err = 1u;
  699. break;
  700. case 0x28u:
  701. if (u8Addr)
  702. {
  703. I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFFu)); /* Write Lo byte address of register */
  704. u8Addr = 0u;
  705. }
  706. else if ((u32txLen < u32wLen) && (u8Addr == 0u))
  707. {
  708. I2C_SET_DATA(i2c, data[u32txLen++]); /* Write data to Register I2CDAT*/
  709. }
  710. else
  711. {
  712. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  713. u8Xfering = 0u;
  714. }
  715. break;
  716. case 0x38u: /* Arbitration Lost */
  717. default: /* Unknow status */
  718. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  719. u8Err = 1u;
  720. break;
  721. }
  722. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  723. }
  724. return u32txLen; /* Return bytes length that have been transmitted */
  725. }
  726. /**
  727. * @brief Read a byte from Slave
  728. *
  729. * @param[in] *i2c Point to I2C peripheral
  730. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  731. *
  732. * @return Read a byte data from Slave
  733. *
  734. * @details The function is used for I2C Master to read a byte data from Slave.
  735. *
  736. */
  737. uint8_t I2C_ReadByte(I2C_T *i2c, uint8_t u8SlaveAddr)
  738. {
  739. uint8_t u8Xfering = 1u, u8Err = 0u, rdata = 0u, u8Ctrl = 0u;
  740. I2C_START(i2c); /* Send START */
  741. while (u8Xfering && (u8Err == 0u))
  742. {
  743. I2C_WAIT_READY(i2c) {}
  744. switch (I2C_GET_STATUS(i2c))
  745. {
  746. case 0x08u:
  747. I2C_SET_DATA(i2c, (uint8_t)((u8SlaveAddr << 1u) | 0x01u)); /* Write SLA+R to Register I2CDAT */
  748. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  749. break;
  750. case 0x40u: /* Slave Address ACK */
  751. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  752. break;
  753. case 0x48u: /* Slave Address NACK */
  754. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  755. u8Err = 1u;
  756. break;
  757. case 0x58u:
  758. rdata = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */
  759. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  760. u8Xfering = 0u;
  761. break;
  762. case 0x38u: /* Arbitration Lost */
  763. default: /* Unknow status */
  764. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  765. u8Err = 1u;
  766. break;
  767. }
  768. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  769. }
  770. if (u8Err)
  771. {
  772. rdata = 0u; /* If occurs error, return 0 */
  773. }
  774. return rdata; /* Return read data */
  775. }
  776. /**
  777. * @brief Read multi bytes from Slave
  778. *
  779. * @param[in] *i2c Point to I2C peripheral
  780. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  781. * @param[out] rdata[] A data array to store data from Slave
  782. * @param[in] u32rLen How many bytes need to read from Slave
  783. *
  784. * @return A length of how many bytes have been received
  785. *
  786. * @details The function is used for I2C Master to read multi data bytes from Slave.
  787. *
  788. *
  789. */
  790. uint32_t I2C_ReadMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t rdata[], uint32_t u32rLen)
  791. {
  792. uint8_t u8Xfering = 1u, u8Err = 0u, u8Ctrl = 0u;
  793. uint32_t u32rxLen = 0u;
  794. I2C_START(i2c); /* Send START */
  795. while (u8Xfering && (u8Err == 0u))
  796. {
  797. I2C_WAIT_READY(i2c) {}
  798. switch (I2C_GET_STATUS(i2c))
  799. {
  800. case 0x08u:
  801. I2C_SET_DATA(i2c, (uint8_t)((u8SlaveAddr << 1u) | 0x01u)); /* Write SLA+R to Register I2CDAT */
  802. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  803. break;
  804. case 0x40u: /* Slave Address ACK */
  805. u8Ctrl = I2C_CTL_SI_AA; /* Clear SI and set ACK */
  806. break;
  807. case 0x48u: /* Slave Address NACK */
  808. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  809. u8Err = 1u;
  810. break;
  811. case 0x50u:
  812. rdata[u32rxLen++] = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */
  813. if (u32rxLen < (u32rLen - 1u))
  814. {
  815. u8Ctrl = I2C_CTL_SI_AA; /* Clear SI and set ACK */
  816. }
  817. else
  818. {
  819. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  820. }
  821. break;
  822. case 0x58u:
  823. rdata[u32rxLen++] = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */
  824. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  825. u8Xfering = 0u;
  826. break;
  827. case 0x38u: /* Arbitration Lost */
  828. default: /* Unknow status */
  829. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  830. u8Err = 1u;
  831. break;
  832. }
  833. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  834. }
  835. return u32rxLen; /* Return bytes length that have been received */
  836. }
  837. /**
  838. * @brief Specify a byte register address and read a byte from Slave
  839. *
  840. * @param[in] *i2c Point to I2C peripheral
  841. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  842. * @param[in] u8DataAddr Specify a address(1 byte) of data read from
  843. *
  844. * @return Read a byte data from Slave
  845. *
  846. * @details The function is used for I2C Master specify a byte address that a data byte read from Slave.
  847. *
  848. *
  849. */
  850. uint8_t I2C_ReadByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr)
  851. {
  852. uint8_t u8Xfering = 1u, u8Err = 0u, rdata = 0u, u8Ctrl = 0u;
  853. I2C_START(i2c); /* Send START */
  854. while (u8Xfering && (u8Err == 0u))
  855. {
  856. I2C_WAIT_READY(i2c) {}
  857. switch (I2C_GET_STATUS(i2c))
  858. {
  859. case 0x08u:
  860. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  861. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  862. break;
  863. case 0x18u: /* Slave Address ACK */
  864. I2C_SET_DATA(i2c, u8DataAddr); /* Write Lo byte address of register */
  865. break;
  866. case 0x20u: /* Slave Address NACK */
  867. case 0x30u: /* Master transmit data NACK */
  868. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  869. u8Err = 1u;
  870. break;
  871. case 0x28u:
  872. u8Ctrl = I2C_CTL_STA_SI; /* Send repeat START */
  873. break;
  874. case 0x10u:
  875. I2C_SET_DATA(i2c, (uint8_t)((u8SlaveAddr << 1u) | 0x01u)); /* Write SLA+R to Register I2CDAT */
  876. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  877. break;
  878. case 0x40u: /* Slave Address ACK */
  879. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  880. break;
  881. case 0x48u: /* Slave Address NACK */
  882. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  883. u8Err = 1u;
  884. break;
  885. case 0x58u:
  886. rdata = (uint8_t) I2C_GET_DATA(i2c); /* Receive Data */
  887. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  888. u8Xfering = 0u;
  889. break;
  890. case 0x38u: /* Arbitration Lost */
  891. default: /* Unknow status */
  892. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  893. u8Err = 1u;
  894. break;
  895. }
  896. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  897. }
  898. if (u8Err)
  899. {
  900. rdata = 0u; /* If occurs error, return 0 */
  901. }
  902. return rdata; /* Return read data */
  903. }
  904. /**
  905. * @brief Specify a byte register address and read multi bytes from Slave
  906. *
  907. * @param[in] *i2c Point to I2C peripheral
  908. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  909. * @param[in] u8DataAddr Specify a address (1 bytes) of data read from
  910. * @param[out] rdata[] A data array to store data from Slave
  911. * @param[in] u32rLen How many bytes need to read from Slave
  912. *
  913. * @return A length of how many bytes have been received
  914. *
  915. * @details The function is used for I2C Master specify a byte address that multi data bytes read from Slave.
  916. *
  917. *
  918. */
  919. uint32_t I2C_ReadMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t rdata[], uint32_t u32rLen)
  920. {
  921. uint8_t u8Xfering = 1u, u8Err = 0u, u8Ctrl = 0u;
  922. uint32_t u32rxLen = 0u;
  923. I2C_START(i2c); /* Send START */
  924. while (u8Xfering && (u8Err == 0u))
  925. {
  926. I2C_WAIT_READY(i2c) {}
  927. switch (I2C_GET_STATUS(i2c))
  928. {
  929. case 0x08u:
  930. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  931. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  932. break;
  933. case 0x18u: /* Slave Address ACK */
  934. I2C_SET_DATA(i2c, u8DataAddr); /* Write Lo byte address of register */
  935. break;
  936. case 0x20u: /* Slave Address NACK */
  937. case 0x30u: /* Master transmit data NACK */
  938. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  939. u8Err = 1u;
  940. break;
  941. case 0x28u:
  942. u8Ctrl = I2C_CTL_STA_SI; /* Send repeat START */
  943. break;
  944. case 0x10u:
  945. I2C_SET_DATA(i2c, (uint8_t)((u8SlaveAddr << 1u) | 0x01u)); /* Write SLA+R to Register I2CDAT */
  946. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  947. break;
  948. case 0x40u: /* Slave Address ACK */
  949. u8Ctrl = I2C_CTL_SI_AA; /* Clear SI and set ACK */
  950. break;
  951. case 0x48u: /* Slave Address NACK */
  952. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  953. u8Err = 1u;
  954. break;
  955. case 0x50u:
  956. rdata[u32rxLen++] = (uint8_t) I2C_GET_DATA(i2c); /* Receive Data */
  957. if (u32rxLen < (u32rLen - 1u))
  958. {
  959. u8Ctrl = I2C_CTL_SI_AA; /* Clear SI and set ACK */
  960. }
  961. else
  962. {
  963. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  964. }
  965. break;
  966. case 0x58u:
  967. rdata[u32rxLen++] = (uint8_t) I2C_GET_DATA(i2c); /* Receive Data */
  968. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  969. u8Xfering = 0u;
  970. break;
  971. case 0x38u: /* Arbitration Lost */
  972. default: /* Unknow status */
  973. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  974. u8Err = 1u;
  975. break;
  976. }
  977. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  978. }
  979. return u32rxLen; /* Return bytes length that have been received */
  980. }
  981. /**
  982. * @brief Specify two bytes register address and read a byte from Slave
  983. *
  984. * @param[in] *i2c Point to I2C peripheral
  985. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  986. * @param[in] u16DataAddr Specify an address(2 bytes) of data read from
  987. *
  988. * @return Read a byte data from Slave
  989. *
  990. * @details The function is used for I2C Master specify two bytes address that a data byte read from Slave.
  991. *
  992. *
  993. */
  994. uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr)
  995. {
  996. uint8_t u8Xfering = 1u, u8Err = 0u, rdata = 0u, u8Addr = 1u, u8Ctrl = 0u;
  997. I2C_START(i2c); /* Send START */
  998. while (u8Xfering && (u8Err == 0u))
  999. {
  1000. I2C_WAIT_READY(i2c) {}
  1001. switch (I2C_GET_STATUS(i2c))
  1002. {
  1003. case 0x08u:
  1004. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  1005. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  1006. break;
  1007. case 0x18u: /* Slave Address ACK */
  1008. I2C_SET_DATA(i2c, (uint8_t)((u16DataAddr & 0xFF00u) >> 8u)); /* Write Hi byte address of register */
  1009. break;
  1010. case 0x20u: /* Slave Address NACK */
  1011. case 0x30u: /* Master transmit data NACK */
  1012. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  1013. u8Err = 1u;
  1014. break;
  1015. case 0x28u:
  1016. if (u8Addr)
  1017. {
  1018. I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFFu)); /* Write Lo byte address of register */
  1019. u8Addr = 0u;
  1020. }
  1021. else
  1022. {
  1023. u8Ctrl = I2C_CTL_STA_SI; /* Clear SI and send repeat START */
  1024. }
  1025. break;
  1026. case 0x10u:
  1027. I2C_SET_DATA(i2c, (uint8_t)((u8SlaveAddr << 1u) | 0x01u)); /* Write SLA+R to Register I2CDAT */
  1028. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  1029. break;
  1030. case 0x40u: /* Slave Address ACK */
  1031. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  1032. break;
  1033. case 0x48u: /* Slave Address NACK */
  1034. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  1035. u8Err = 1u;
  1036. break;
  1037. case 0x58u:
  1038. rdata = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */
  1039. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  1040. u8Xfering = 0u;
  1041. break;
  1042. case 0x38u: /* Arbitration Lost */
  1043. default: /* Unknow status */
  1044. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  1045. u8Err = 1u;
  1046. break;
  1047. }
  1048. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  1049. }
  1050. if (u8Err)
  1051. {
  1052. rdata = 0u; /* If occurs error, return 0 */
  1053. }
  1054. return rdata; /* Return read data */
  1055. }
  1056. /**
  1057. * @brief Specify two bytes register address and read multi bytes from Slave
  1058. *
  1059. * @param[in] *i2c Point to I2C peripheral
  1060. * @param[in] u8SlaveAddr Access Slave address(7-bit)
  1061. * @param[in] u16DataAddr Specify a address (2 bytes) of data read from
  1062. * @param[out] rdata[] A data array to store data from Slave
  1063. * @param[in] u32rLen How many bytes need to read from Slave
  1064. *
  1065. * @return A length of how many bytes have been received
  1066. *
  1067. * @details The function is used for I2C Master specify two bytes address that multi data bytes read from Slave.
  1068. *
  1069. *
  1070. */
  1071. uint32_t I2C_ReadMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t rdata[], uint32_t u32rLen)
  1072. {
  1073. uint8_t u8Xfering = 1u, u8Err = 0u, u8Addr = 1u, u8Ctrl = 0u;
  1074. uint32_t u32rxLen = 0u;
  1075. I2C_START(i2c); /* Send START */
  1076. while (u8Xfering && (u8Err == 0u))
  1077. {
  1078. I2C_WAIT_READY(i2c) {}
  1079. switch (I2C_GET_STATUS(i2c))
  1080. {
  1081. case 0x08u:
  1082. I2C_SET_DATA(i2c, (uint8_t)(u8SlaveAddr << 1u | 0x00u)); /* Write SLA+W to Register I2CDAT */
  1083. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  1084. break;
  1085. case 0x18u: /* Slave Address ACK */
  1086. I2C_SET_DATA(i2c, (uint8_t)((u16DataAddr & 0xFF00u) >> 8u)); /* Write Hi byte address of register */
  1087. break;
  1088. case 0x20u: /* Slave Address NACK */
  1089. case 0x30u: /* Master transmit data NACK */
  1090. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  1091. u8Err = 1u;
  1092. break;
  1093. case 0x28u:
  1094. if (u8Addr)
  1095. {
  1096. I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFFu)); /* Write Lo byte address of register */
  1097. u8Addr = 0u;
  1098. }
  1099. else
  1100. {
  1101. u8Ctrl = I2C_CTL_STA_SI; /* Clear SI and send repeat START */
  1102. }
  1103. break;
  1104. case 0x10u:
  1105. I2C_SET_DATA(i2c, (uint8_t)((u8SlaveAddr << 1u) | 0x01u)); /* Write SLA+R to Register I2CDAT */
  1106. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  1107. break;
  1108. case 0x40u: /* Slave Address ACK */
  1109. u8Ctrl = I2C_CTL_SI_AA; /* Clear SI and set ACK */
  1110. break;
  1111. case 0x48u: /* Slave Address NACK */
  1112. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  1113. u8Err = 1u;
  1114. break;
  1115. case 0x50u:
  1116. rdata[u32rxLen++] = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */
  1117. if (u32rxLen < (u32rLen - 1u))
  1118. {
  1119. u8Ctrl = I2C_CTL_SI_AA; /* Clear SI and set ACK */
  1120. }
  1121. else
  1122. {
  1123. u8Ctrl = I2C_CTL_SI; /* Clear SI */
  1124. }
  1125. break;
  1126. case 0x58u:
  1127. rdata[u32rxLen++] = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */
  1128. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  1129. u8Xfering = 0u;
  1130. break;
  1131. case 0x38u: /* Arbitration Lost */
  1132. default: /* Unknow status */
  1133. u8Ctrl = I2C_CTL_STO_SI; /* Clear SI and send STOP */
  1134. u8Err = 1u;
  1135. break;
  1136. }
  1137. I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */
  1138. }
  1139. return u32rxLen; /* Return bytes length that have been received */
  1140. }
  1141. /**
  1142. * @brief The macro is used to set STOP condition of I2C Bus
  1143. *
  1144. * @param[in] i2c Specify I2C port
  1145. *
  1146. * @return None
  1147. *
  1148. * @details Set the I2C bus STOP condition in I2C_CTL register.
  1149. */
  1150. void I2C_STOP(I2C_T *i2c)
  1151. {
  1152. (i2c)->CTL0 |= (I2C_CTL0_SI_Msk | I2C_CTL0_STO_Msk);
  1153. while (i2c->CTL0 & I2C_CTL0_STO_Msk)
  1154. {
  1155. }
  1156. }
  1157. /*@}*/ /* end of group I2C_EXPORTED_FUNCTIONS */
  1158. /*@}*/ /* end of group I2C_Driver */
  1159. /*@}*/ /* end of group Standard_Driver */
  1160. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/