n32g45x_i2c.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_i2c.c
  29. * @author Nations
  30. * @version v1.0.2
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_i2c.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup I2C
  40. * @brief I2C driver modules
  41. * @{
  42. */
  43. /** @addtogroup I2C_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup I2C_Private_Defines
  50. * @{
  51. */
  52. /* I2C SPE mask */
  53. #define CTRL1_SPEN_SET ((uint16_t)0x0001)
  54. #define CTRL1_SPEN_RESET ((uint16_t)0xFFFE)
  55. /* I2C START mask */
  56. #define CTRL1_START_SET ((uint16_t)0x0100)
  57. #define CTRL1_START_RESET ((uint16_t)0xFEFF)
  58. /* I2C STOP mask */
  59. #define CTRL1_STOP_SET ((uint16_t)0x0200)
  60. #define CTRL1_STOP_RESET ((uint16_t)0xFDFF)
  61. /* I2C ACK mask */
  62. #define CTRL1_ACK_SET ((uint16_t)0x0400)
  63. #define CTRL1_ACK_RESET ((uint16_t)0xFBFF)
  64. /* I2C ENGC mask */
  65. #define CTRL1_GCEN_SET ((uint16_t)0x0040)
  66. #define CTRL1_GCEN_RESET ((uint16_t)0xFFBF)
  67. /* I2C SWRST mask */
  68. #define CTRL1_SWRESET_SET ((uint16_t)0x8000)
  69. #define CTRL1_SWRESET_RESET ((uint16_t)0x7FFF)
  70. /* I2C PEC mask */
  71. #define CTRL1_PEC_SET ((uint16_t)0x1000)
  72. #define CTRL1_PEC_RESET ((uint16_t)0xEFFF)
  73. /* I2C ENPEC mask */
  74. #define CTRL1_PECEN_SET ((uint16_t)0x0020)
  75. #define CTRL1_PECEN_RESET ((uint16_t)0xFFDF)
  76. /* I2C ENARP mask */
  77. #define CTRL1_ARPEN_SET ((uint16_t)0x0010)
  78. #define CTRL1_ARPEN_RESET ((uint16_t)0xFFEF)
  79. /* I2C NOSTRETCH mask */
  80. #define CTRL1_NOEXTEND_SET ((uint16_t)0x0080)
  81. #define CTRL1_NOEXTEND_RESET ((uint16_t)0xFF7F)
  82. /* I2C registers Masks */
  83. #define CTRL1_CLR_MASK ((uint16_t)0xFBF5)
  84. /* I2C DMAEN mask */
  85. #define CTRL2_DMAEN_SET ((uint16_t)0x0800)
  86. #define CTRL2_DMAEN_RESET ((uint16_t)0xF7FF)
  87. /* I2C LAST mask */
  88. #define CTRL2_DMALAST_SET ((uint16_t)0x1000)
  89. #define CTRL2_DMALAST_RESET ((uint16_t)0xEFFF)
  90. /* I2C FREQ mask */
  91. #define CTRL2_CLKFREQ_RESET ((uint16_t)0xFFC0)
  92. /* I2C ADD0 mask */
  93. #define OADDR1_ADDR0_SET ((uint16_t)0x0001)
  94. #define OADDR1_ADDR0_RESET ((uint16_t)0xFFFE)
  95. /* I2C ENDUAL mask */
  96. #define OADDR2_DUALEN_SET ((uint16_t)0x0001)
  97. #define OADDR2_DUALEN_RESET ((uint16_t)0xFFFE)
  98. /* I2C ADD2 mask */
  99. #define OADDR2_ADDR2_RESET ((uint16_t)0xFF01)
  100. /* I2C F/S mask */
  101. #define CLKCTRL_FSMODE_SET ((uint16_t)0x8000)
  102. /* I2C CHCFG mask */
  103. #define CLKCTRL_CLKCTRL_SET ((uint16_t)0x0FFF)
  104. /* I2C FLAG mask */
  105. #define FLAG_MASK ((uint32_t)0x00FFFFFF)
  106. /* I2C Interrupt Enable mask */
  107. #define INTEN_MASK ((uint32_t)0x07000000)
  108. /**
  109. * @}
  110. */
  111. /** @addtogroup I2C_Private_Macros
  112. * @{
  113. */
  114. /**
  115. * @}
  116. */
  117. /** @addtogroup I2C_Private_Variables
  118. * @{
  119. */
  120. /**
  121. * @}
  122. */
  123. /** @addtogroup I2C_Private_FunctionPrototypes
  124. * @{
  125. */
  126. /**
  127. * @}
  128. */
  129. /** @addtogroup I2C_Private_Functions
  130. * @{
  131. */
  132. /**
  133. * @brief Deinitializes the I2Cx peripheral registers to their default reset values.
  134. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  135. */
  136. void I2C_DeInit(I2C_Module* I2Cx)
  137. {
  138. /* Check the parameters */
  139. assert_param(IS_I2C_PERIPH(I2Cx));
  140. if (I2Cx == I2C1)
  141. {
  142. /* Enable I2C1 reset state */
  143. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_I2C1, ENABLE);
  144. /* Release I2C1 from reset state */
  145. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_I2C1, DISABLE);
  146. }
  147. else if (I2Cx == I2C2)
  148. {
  149. /* Enable I2C2 reset state */
  150. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_I2C2, ENABLE);
  151. /* Release I2C2 from reset state */
  152. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_I2C2, DISABLE);
  153. }
  154. else if (I2Cx == I2C3)
  155. {
  156. /* Enable I2C2 reset state */
  157. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_I2C3, ENABLE);
  158. /* Release I2C2 from reset state */
  159. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_I2C3, DISABLE);
  160. }
  161. else if (I2Cx == I2C4)
  162. {
  163. /* Enable I2C4 reset state */
  164. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_I2C4, ENABLE);
  165. /* Release I2C4 from reset state */
  166. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_I2C4, DISABLE);
  167. }
  168. else
  169. {
  170. }
  171. }
  172. /**
  173. * @brief Initializes the I2Cx peripheral according to the specified
  174. * parameters in the I2C_InitStruct.
  175. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  176. * @param I2C_InitStruct pointer to a I2C_InitType structure that
  177. * contains the configuration information for the specified I2C peripheral.
  178. */
  179. void I2C_Init(I2C_Module* I2Cx, I2C_InitType* I2C_InitStruct)
  180. {
  181. uint16_t tmpregister = 0, freqrange = 0;
  182. uint16_t result = 0x04;
  183. uint32_t pclk = 8000000;
  184. RCC_ClocksType rcc_clocks;
  185. /* Check the parameters */
  186. assert_param(IS_I2C_PERIPH(I2Cx));
  187. assert_param(IS_I2C_CLK_SPEED(I2C_InitStruct->ClkSpeed));
  188. assert_param(IS_I2C_BUS_MODE(I2C_InitStruct->BusMode));
  189. assert_param(IS_I2C_FM_DUTY_CYCLE(I2C_InitStruct->FmDutyCycle));
  190. assert_param(IS_I2C_OWN_ADDR1(I2C_InitStruct->OwnAddr1));
  191. assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->AckEnable));
  192. assert_param(IS_I2C_ADDR_MODE(I2C_InitStruct->AddrMode));
  193. /*---------------------------- I2Cx CTRL2 Configuration ------------------------*/
  194. /* Get the I2Cx CTRL2 value */
  195. tmpregister = I2Cx->CTRL2;
  196. /* Clear frequency FREQ[5:0] bits */
  197. tmpregister &= CTRL2_CLKFREQ_RESET;
  198. /* Get APB1/2 frequency value */
  199. RCC_GetClocksFreqValue(&rcc_clocks);
  200. if ((I2Cx == I2C1) || (I2Cx == I2C2))
  201. {
  202. pclk = rcc_clocks.Pclk1Freq;
  203. }
  204. else if ((I2Cx == I2C3) || (I2Cx == I2C4))
  205. {
  206. pclk = rcc_clocks.Pclk2Freq;
  207. }
  208. /* Set frequency bits depending on pclk1 value */
  209. freqrange = (uint16_t)(pclk / 1000000);
  210. if (freqrange > 36)
  211. {
  212. freqrange = 36;
  213. }
  214. /* Write to I2Cx CTRL2 */
  215. tmpregister |= freqrange;
  216. I2Cx->CTRL2 = tmpregister;
  217. /*---------------------------- I2Cx CHCFG Configuration ------------------------*/
  218. /* Disable the selected I2C peripheral to configure TMRISE */
  219. I2Cx->CTRL1 &= CTRL1_SPEN_RESET;
  220. /* Reset tmpregister value */
  221. /* Clear F/S, DUTY and CHCFG[11:0] bits */
  222. tmpregister = 0;
  223. /* Configure speed in standard mode */
  224. if (I2C_InitStruct->ClkSpeed <= 100000)
  225. {
  226. /* Standard mode speed calculate */
  227. result = (uint16_t)(pclk / (I2C_InitStruct->ClkSpeed << 1));
  228. /* Test if CHCFG value is under 0x4*/
  229. if (result < 0x04)
  230. {
  231. /* Set minimum allowed value */
  232. result = 0x04;
  233. }
  234. /* Set speed value for standard mode */
  235. tmpregister |= result;
  236. /* Set Maximum Rise Time for standard mode */
  237. I2Cx->TMRISE = freqrange + 1;
  238. }
  239. /* Configure speed in fast mode */
  240. else
  241. {
  242. if (I2C_InitStruct->FmDutyCycle == I2C_FMDUTYCYCLE_2)
  243. {
  244. /* Fast mode speed calculate: Tlow/Thigh = 2 */
  245. result = (uint16_t)(pclk / (I2C_InitStruct->ClkSpeed * 3));
  246. }
  247. else /*I2C_InitStruct->FmDutyCycle == I2C_FMDUTYCYCLE_16_9*/
  248. {
  249. /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
  250. result = (uint16_t)(pclk / (I2C_InitStruct->ClkSpeed * 25));
  251. /* Set DUTY bit */
  252. result |= I2C_FMDUTYCYCLE_16_9;
  253. }
  254. /* Test if CHCFG value is under 0x1*/
  255. if ((result & CLKCTRL_CLKCTRL_SET) == 0)
  256. {
  257. /* Set minimum allowed value */
  258. result |= (uint16_t)0x0001;
  259. }
  260. /* Set speed value and set F/S bit for fast mode */
  261. tmpregister |= (uint16_t)(result | CLKCTRL_FSMODE_SET);
  262. /* Set Maximum Rise Time for fast mode */
  263. I2Cx->TMRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
  264. }
  265. /* Write to I2Cx CHCFG */
  266. I2Cx->CLKCTRL = tmpregister;
  267. /* Enable the selected I2C peripheral */
  268. I2Cx->CTRL1 |= CTRL1_SPEN_SET;
  269. /*---------------------------- I2Cx CTRL1 Configuration ------------------------*/
  270. /* Get the I2Cx CTRL1 value */
  271. tmpregister = I2Cx->CTRL1;
  272. /* Clear ACK, SMBTYPE and SMBUS bits */
  273. tmpregister &= CTRL1_CLR_MASK;
  274. /* Configure I2Cx: mode and acknowledgement */
  275. /* Set SMBTYPE and SMBUS bits according to BusMode value */
  276. /* Set ACK bit according to AckEnable value */
  277. tmpregister |= (uint16_t)((uint32_t)I2C_InitStruct->BusMode | I2C_InitStruct->AckEnable);
  278. /* Write to I2Cx CTRL1 */
  279. I2Cx->CTRL1 = tmpregister;
  280. /*---------------------------- I2Cx OADDR1 Configuration -----------------------*/
  281. /* Set I2Cx Own Address1 and acknowledged address */
  282. I2Cx->OADDR1 = (I2C_InitStruct->AddrMode | I2C_InitStruct->OwnAddr1);
  283. }
  284. /**
  285. * @brief Fills each I2C_InitStruct member with its default value.
  286. * @param I2C_InitStruct pointer to an I2C_InitType structure which will be initialized.
  287. */
  288. void I2C_InitStruct(I2C_InitType* I2C_InitStruct)
  289. {
  290. /*---------------- Reset I2C init structure parameters values ----------------*/
  291. /* initialize the ClkSpeed member */
  292. I2C_InitStruct->ClkSpeed = 5000;
  293. /* Initialize the BusMode member */
  294. I2C_InitStruct->BusMode = I2C_BUSMODE_I2C;
  295. /* Initialize the FmDutyCycle member */
  296. I2C_InitStruct->FmDutyCycle = I2C_FMDUTYCYCLE_2;
  297. /* Initialize the OwnAddr1 member */
  298. I2C_InitStruct->OwnAddr1 = 0;
  299. /* Initialize the AckEnable member */
  300. I2C_InitStruct->AckEnable = I2C_ACKDIS;
  301. /* Initialize the AddrMode member */
  302. I2C_InitStruct->AddrMode = I2C_ADDR_MODE_7BIT;
  303. }
  304. /**
  305. * @brief Enables or disables the specified I2C peripheral.
  306. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  307. * @param Cmd new state of the I2Cx peripheral.
  308. * This parameter can be: ENABLE or DISABLE.
  309. */
  310. void I2C_Enable(I2C_Module* I2Cx, FunctionalState Cmd)
  311. {
  312. /* Check the parameters */
  313. assert_param(IS_I2C_PERIPH(I2Cx));
  314. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  315. if (Cmd != DISABLE)
  316. {
  317. /* Enable the selected I2C peripheral */
  318. I2Cx->CTRL1 |= CTRL1_SPEN_SET;
  319. }
  320. else
  321. {
  322. /* Disable the selected I2C peripheral */
  323. I2Cx->CTRL1 &= CTRL1_SPEN_RESET;
  324. }
  325. }
  326. /**
  327. * @brief Enables or disables the specified I2C DMA requests.
  328. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  329. * @param Cmd new state of the I2C DMA transfer.
  330. * This parameter can be: ENABLE or DISABLE.
  331. */
  332. void I2C_EnableDMA(I2C_Module* I2Cx, FunctionalState Cmd)
  333. {
  334. /* Check the parameters */
  335. assert_param(IS_I2C_PERIPH(I2Cx));
  336. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  337. if (Cmd != DISABLE)
  338. {
  339. /* Enable the selected I2C DMA requests */
  340. I2Cx->CTRL2 |= CTRL2_DMAEN_SET;
  341. }
  342. else
  343. {
  344. /* Disable the selected I2C DMA requests */
  345. I2Cx->CTRL2 &= CTRL2_DMAEN_RESET;
  346. }
  347. }
  348. /**
  349. * @brief Specifies if the next DMA transfer will be the last one.
  350. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  351. * @param Cmd new state of the I2C DMA last transfer.
  352. * This parameter can be: ENABLE or DISABLE.
  353. */
  354. void I2C_EnableDmaLastSend(I2C_Module* I2Cx, FunctionalState Cmd)
  355. {
  356. /* Check the parameters */
  357. assert_param(IS_I2C_PERIPH(I2Cx));
  358. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  359. if (Cmd != DISABLE)
  360. {
  361. /* Next DMA transfer is the last transfer */
  362. I2Cx->CTRL2 |= CTRL2_DMALAST_SET;
  363. }
  364. else
  365. {
  366. /* Next DMA transfer is not the last transfer */
  367. I2Cx->CTRL2 &= CTRL2_DMALAST_RESET;
  368. }
  369. }
  370. /**
  371. * @brief Generates I2Cx communication START condition.
  372. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  373. * @param Cmd new state of the I2C START condition generation.
  374. * This parameter can be: ENABLE or DISABLE.
  375. */
  376. void I2C_GenerateStart(I2C_Module* I2Cx, FunctionalState Cmd)
  377. {
  378. /* Check the parameters */
  379. assert_param(IS_I2C_PERIPH(I2Cx));
  380. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  381. if (Cmd != DISABLE)
  382. {
  383. /* Generate a START condition */
  384. I2Cx->CTRL1 |= CTRL1_START_SET;
  385. }
  386. else
  387. {
  388. /* Disable the START condition generation */
  389. I2Cx->CTRL1 &= CTRL1_START_RESET;
  390. }
  391. }
  392. /**
  393. * @brief Generates I2Cx communication STOP condition.
  394. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  395. * @param Cmd new state of the I2C STOP condition generation.
  396. * This parameter can be: ENABLE or DISABLE.
  397. */
  398. void I2C_GenerateStop(I2C_Module* I2Cx, FunctionalState Cmd)
  399. {
  400. /* Check the parameters */
  401. assert_param(IS_I2C_PERIPH(I2Cx));
  402. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  403. if (Cmd != DISABLE)
  404. {
  405. /* Generate a STOP condition */
  406. I2Cx->CTRL1 |= CTRL1_STOP_SET;
  407. }
  408. else
  409. {
  410. /* Disable the STOP condition generation */
  411. I2Cx->CTRL1 &= CTRL1_STOP_RESET;
  412. }
  413. }
  414. /**
  415. * @brief Enables or disables the specified I2C acknowledge feature.
  416. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  417. * @param Cmd new state of the I2C Acknowledgement.
  418. * This parameter can be: ENABLE or DISABLE.
  419. */
  420. void I2C_ConfigAck(I2C_Module* I2Cx, FunctionalState Cmd)
  421. {
  422. /* Check the parameters */
  423. assert_param(IS_I2C_PERIPH(I2Cx));
  424. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  425. if (Cmd != DISABLE)
  426. {
  427. /* Enable the acknowledgement */
  428. I2Cx->CTRL1 |= CTRL1_ACK_SET;
  429. }
  430. else
  431. {
  432. /* Disable the acknowledgement */
  433. I2Cx->CTRL1 &= CTRL1_ACK_RESET;
  434. }
  435. }
  436. /**
  437. * @brief Configures the specified I2C own address2.
  438. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  439. * @param Address specifies the 7bit I2C own address2.
  440. */
  441. void I2C_ConfigOwnAddr2(I2C_Module* I2Cx, uint8_t Address)
  442. {
  443. uint16_t tmpregister = 0;
  444. /* Check the parameters */
  445. assert_param(IS_I2C_PERIPH(I2Cx));
  446. /* Get the old register value */
  447. tmpregister = I2Cx->OADDR2;
  448. /* Reset I2Cx Own address2 bit [7:1] */
  449. tmpregister &= OADDR2_ADDR2_RESET;
  450. /* Set I2Cx Own address2 */
  451. tmpregister |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
  452. /* Store the new register value */
  453. I2Cx->OADDR2 = tmpregister;
  454. }
  455. /**
  456. * @brief Enables or disables the specified I2C dual addressing mode.
  457. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  458. * @param Cmd new state of the I2C dual addressing mode.
  459. * This parameter can be: ENABLE or DISABLE.
  460. */
  461. void I2C_EnableDualAddr(I2C_Module* I2Cx, FunctionalState Cmd)
  462. {
  463. /* Check the parameters */
  464. assert_param(IS_I2C_PERIPH(I2Cx));
  465. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  466. if (Cmd != DISABLE)
  467. {
  468. /* Enable dual addressing mode */
  469. I2Cx->OADDR2 |= OADDR2_DUALEN_SET;
  470. }
  471. else
  472. {
  473. /* Disable dual addressing mode */
  474. I2Cx->OADDR2 &= OADDR2_DUALEN_RESET;
  475. }
  476. }
  477. /**
  478. * @brief Enables or disables the specified I2C general call feature.
  479. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  480. * @param Cmd new state of the I2C General call.
  481. * This parameter can be: ENABLE or DISABLE.
  482. */
  483. void I2C_EnableGeneralCall(I2C_Module* I2Cx, FunctionalState Cmd)
  484. {
  485. /* Check the parameters */
  486. assert_param(IS_I2C_PERIPH(I2Cx));
  487. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  488. if (Cmd != DISABLE)
  489. {
  490. /* Enable generall call */
  491. I2Cx->CTRL1 |= CTRL1_GCEN_SET;
  492. }
  493. else
  494. {
  495. /* Disable generall call */
  496. I2Cx->CTRL1 &= CTRL1_GCEN_RESET;
  497. }
  498. }
  499. /**
  500. * @brief Enables or disables the specified I2C interrupts.
  501. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  502. * @param I2C_IT specifies the I2C interrupts sources to be enabled or disabled.
  503. * This parameter can be any combination of the following values:
  504. * @arg I2C_INT_BUF Buffer interrupt mask
  505. * @arg I2C_INT_EVENT Event interrupt mask
  506. * @arg I2C_INT_ERR Error interrupt mask
  507. * @param Cmd new state of the specified I2C interrupts.
  508. * This parameter can be: ENABLE or DISABLE.
  509. */
  510. void I2C_ConfigInt(I2C_Module* I2Cx, uint16_t I2C_IT, FunctionalState Cmd)
  511. {
  512. /* Check the parameters */
  513. assert_param(IS_I2C_PERIPH(I2Cx));
  514. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  515. assert_param(IS_I2C_CFG_INT(I2C_IT));
  516. if (Cmd != DISABLE)
  517. {
  518. /* Enable the selected I2C interrupts */
  519. I2Cx->CTRL2 |= I2C_IT;
  520. }
  521. else
  522. {
  523. /* Disable the selected I2C interrupts */
  524. I2Cx->CTRL2 &= (uint16_t)~I2C_IT;
  525. }
  526. }
  527. /**
  528. * @brief Sends a data byte through the I2Cx peripheral.
  529. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  530. * @param Data Byte to be transmitted..
  531. */
  532. void I2C_SendData(I2C_Module* I2Cx, uint8_t Data)
  533. {
  534. /* Check the parameters */
  535. assert_param(IS_I2C_PERIPH(I2Cx));
  536. /* Write in the DAT register the data to be sent */
  537. I2Cx->DAT = Data;
  538. }
  539. /**
  540. * @brief Returns the most recent received data by the I2Cx peripheral.
  541. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  542. * @return The value of the received data.
  543. */
  544. uint8_t I2C_RecvData(I2C_Module* I2Cx)
  545. {
  546. /* Check the parameters */
  547. assert_param(IS_I2C_PERIPH(I2Cx));
  548. /* Return the data in the DAT register */
  549. return (uint8_t)I2Cx->DAT;
  550. }
  551. /**
  552. * @brief Transmits the address byte to select the slave device.
  553. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  554. * @param Address specifies the slave address which will be transmitted
  555. * @param I2C_Direction specifies whether the I2C device will be a
  556. * Transmitter or a Receiver. This parameter can be one of the following values
  557. * @arg I2C_DIRECTION_SEND Transmitter mode
  558. * @arg I2C_DIRECTION_RECV Receiver mode
  559. */
  560. void I2C_SendAddr7bit(I2C_Module* I2Cx, uint8_t Address, uint8_t I2C_Direction)
  561. {
  562. /* Check the parameters */
  563. assert_param(IS_I2C_PERIPH(I2Cx));
  564. assert_param(IS_I2C_DIRECTION(I2C_Direction));
  565. /* Test on the direction to set/reset the read/write bit */
  566. if (I2C_Direction != I2C_DIRECTION_SEND)
  567. {
  568. /* Set the address bit0 for read */
  569. Address |= OADDR1_ADDR0_SET;
  570. }
  571. else
  572. {
  573. /* Reset the address bit0 for write */
  574. Address &= OADDR1_ADDR0_RESET;
  575. }
  576. /* Send the address */
  577. I2Cx->DAT = Address;
  578. }
  579. /**
  580. * @brief Reads the specified I2C register and returns its value.
  581. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  582. * @param I2C_Register specifies the register to read.
  583. * This parameter can be one of the following values:
  584. * @arg I2C_REG_CTRL1 CTRL1 register.
  585. * @arg I2C_REG_CTRL2 CTRL2 register.
  586. * @arg I2C_REG_OADDR1 OADDR1 register.
  587. * @arg I2C_REG_OADDR2 OADDR2 register.
  588. * @arg I2C_REG_DAT DAT register.
  589. * @arg I2C_REG_STS1 STS1 register.
  590. * @arg I2C_REG_STS2 STS2 register.
  591. * @arg I2C_REG_CLKCTRL CHCFG register.
  592. * @arg I2C_REG_TMRISE TMRISE register.
  593. * @return The value of the read register.
  594. */
  595. uint16_t I2C_GetRegister(I2C_Module* I2Cx, uint8_t I2C_Register)
  596. {
  597. __IO uint32_t tmp = 0;
  598. /* Check the parameters */
  599. assert_param(IS_I2C_PERIPH(I2Cx));
  600. assert_param(IS_I2C_REG(I2C_Register));
  601. tmp = (uint32_t)I2Cx;
  602. tmp += I2C_Register;
  603. /* Return the selected register value */
  604. return (*(__IO uint16_t*)tmp);
  605. }
  606. /**
  607. * @brief Enables or disables the specified I2C software reset.
  608. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  609. * @param Cmd new state of the I2C software reset.
  610. * This parameter can be: ENABLE or DISABLE.
  611. */
  612. void I2C_EnableSoftwareReset(I2C_Module* I2Cx, FunctionalState Cmd)
  613. {
  614. /* Check the parameters */
  615. assert_param(IS_I2C_PERIPH(I2Cx));
  616. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  617. if (Cmd != DISABLE)
  618. {
  619. /* Peripheral under reset */
  620. I2Cx->CTRL1 |= CTRL1_SWRESET_SET;
  621. }
  622. else
  623. {
  624. /* Peripheral not under reset */
  625. I2Cx->CTRL1 &= CTRL1_SWRESET_RESET;
  626. }
  627. }
  628. /**
  629. * @brief Selects the specified I2C NACK position in master receiver mode.
  630. * This function is useful in I2C Master Receiver mode when the number
  631. * of data to be received is equal to 2. In this case, this function
  632. * should be called (with parameter I2C_NACK_POS_NEXT) before data
  633. * reception starts,as described in the 2-byte reception procedure
  634. * recommended in Reference Manual in Section: Master receiver.
  635. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  636. * @param I2C_NACKPosition specifies the NACK position.
  637. * This parameter can be one of the following values:
  638. * @arg I2C_NACK_POS_NEXT indicates that the next byte will be the last
  639. * received byte.
  640. * @arg I2C_NACK_POS_CURRENT indicates that current byte is the last
  641. * received byte.
  642. *
  643. * @note This function configures the same bit (POS) as I2C_ConfigPecLocation()
  644. * but is intended to be used in I2C mode while I2C_ConfigPecLocation()
  645. * is intended to used in SMBUS mode.
  646. *
  647. */
  648. void I2C_ConfigNackLocation(I2C_Module* I2Cx, uint16_t I2C_NACKPosition)
  649. {
  650. /* Check the parameters */
  651. assert_param(IS_I2C_PERIPH(I2Cx));
  652. assert_param(IS_I2C_NACK_POS(I2C_NACKPosition));
  653. /* Check the input parameter */
  654. if (I2C_NACKPosition == I2C_NACK_POS_NEXT)
  655. {
  656. /* Next byte in shift register is the last received byte */
  657. I2Cx->CTRL1 |= I2C_NACK_POS_NEXT;
  658. }
  659. else
  660. {
  661. /* Current byte in shift register is the last received byte */
  662. I2Cx->CTRL1 &= I2C_NACK_POS_CURRENT;
  663. }
  664. }
  665. /**
  666. * @brief Drives the SMBusAlert pin high or low for the specified I2C.
  667. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  668. * @param I2C_SMBusAlert specifies SMBAlert pin level.
  669. * This parameter can be one of the following values:
  670. * @arg I2C_SMBALERT_LOW SMBAlert pin driven low
  671. * @arg I2C_SMBALERT_HIGH SMBAlert pin driven high
  672. */
  673. void I2C_ConfigSmbusAlert(I2C_Module* I2Cx, uint16_t I2C_SMBusAlert)
  674. {
  675. /* Check the parameters */
  676. assert_param(IS_I2C_PERIPH(I2Cx));
  677. assert_param(IS_I2C_SMB_ALERT(I2C_SMBusAlert));
  678. if (I2C_SMBusAlert == I2C_SMBALERT_LOW)
  679. {
  680. /* Drive the SMBusAlert pin Low */
  681. I2Cx->CTRL1 |= I2C_SMBALERT_LOW;
  682. }
  683. else
  684. {
  685. /* Drive the SMBusAlert pin High */
  686. I2Cx->CTRL1 &= I2C_SMBALERT_HIGH;
  687. }
  688. }
  689. /**
  690. * @brief Enables or disables the specified I2C PEC transfer.
  691. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  692. * @param Cmd new state of the I2C PEC transmission.
  693. * This parameter can be: ENABLE or DISABLE.
  694. */
  695. void I2C_SendPEC(I2C_Module* I2Cx, FunctionalState Cmd)
  696. {
  697. /* Check the parameters */
  698. assert_param(IS_I2C_PERIPH(I2Cx));
  699. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  700. if (Cmd != DISABLE)
  701. {
  702. /* Enable the selected I2C PEC transmission */
  703. I2Cx->CTRL1 |= CTRL1_PEC_SET;
  704. }
  705. else
  706. {
  707. /* Disable the selected I2C PEC transmission */
  708. I2Cx->CTRL1 &= CTRL1_PEC_RESET;
  709. }
  710. }
  711. /**
  712. * @brief Selects the specified I2C PEC position.
  713. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  714. * @param I2C_PECPosition specifies the PEC position.
  715. * This parameter can be one of the following values:
  716. * @arg I2C_PEC_POS_NEXT indicates that the next byte is PEC
  717. * @arg I2C_PEC_POS_CURRENT indicates that current byte is PEC
  718. *
  719. * @note This function configures the same bit (POS) as I2C_ConfigNackLocation()
  720. * but is intended to be used in SMBUS mode while I2C_ConfigNackLocation()
  721. * is intended to used in I2C mode.
  722. *
  723. */
  724. void I2C_ConfigPecLocation(I2C_Module* I2Cx, uint16_t I2C_PECPosition)
  725. {
  726. /* Check the parameters */
  727. assert_param(IS_I2C_PERIPH(I2Cx));
  728. assert_param(IS_I2C_PEC_POS(I2C_PECPosition));
  729. if (I2C_PECPosition == I2C_PEC_POS_NEXT)
  730. {
  731. /* Next byte in shift register is PEC */
  732. I2Cx->CTRL1 |= I2C_PEC_POS_NEXT;
  733. }
  734. else
  735. {
  736. /* Current byte in shift register is PEC */
  737. I2Cx->CTRL1 &= I2C_PEC_POS_CURRENT;
  738. }
  739. }
  740. /**
  741. * @brief Enables or disables the PEC value calculation of the transferred bytes.
  742. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  743. * @param Cmd new state of the I2Cx PEC value calculation.
  744. * This parameter can be: ENABLE or DISABLE.
  745. */
  746. void I2C_ComputePec(I2C_Module* I2Cx, FunctionalState Cmd)
  747. {
  748. /* Check the parameters */
  749. assert_param(IS_I2C_PERIPH(I2Cx));
  750. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  751. if (Cmd != DISABLE)
  752. {
  753. /* Enable the selected I2C PEC calculation */
  754. I2Cx->CTRL1 |= CTRL1_PECEN_SET;
  755. }
  756. else
  757. {
  758. /* Disable the selected I2C PEC calculation */
  759. I2Cx->CTRL1 &= CTRL1_PECEN_RESET;
  760. }
  761. }
  762. /**
  763. * @brief Returns the PEC value for the specified I2C.
  764. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  765. * @return The PEC value.
  766. */
  767. uint8_t I2C_GetPec(I2C_Module* I2Cx)
  768. {
  769. /* Check the parameters */
  770. assert_param(IS_I2C_PERIPH(I2Cx));
  771. /* Return the selected I2C PEC value */
  772. return ((I2Cx->STS2) >> 8);
  773. }
  774. /**
  775. * @brief Enables or disables the specified I2C ARP.
  776. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  777. * @param Cmd new state of the I2Cx ARP.
  778. * This parameter can be: ENABLE or DISABLE.
  779. */
  780. void I2C_EnableArp(I2C_Module* I2Cx, FunctionalState Cmd)
  781. {
  782. /* Check the parameters */
  783. assert_param(IS_I2C_PERIPH(I2Cx));
  784. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  785. if (Cmd != DISABLE)
  786. {
  787. /* Enable the selected I2C ARP */
  788. I2Cx->CTRL1 |= CTRL1_ARPEN_SET;
  789. }
  790. else
  791. {
  792. /* Disable the selected I2C ARP */
  793. I2Cx->CTRL1 &= CTRL1_ARPEN_RESET;
  794. }
  795. }
  796. /**
  797. * @brief Enables or disables the specified I2C Clock stretching.
  798. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  799. * @param Cmd new state of the I2Cx Clock stretching.
  800. * This parameter can be: ENABLE or DISABLE.
  801. */
  802. void I2C_EnableExtendClk(I2C_Module* I2Cx, FunctionalState Cmd)
  803. {
  804. /* Check the parameters */
  805. assert_param(IS_I2C_PERIPH(I2Cx));
  806. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  807. if (Cmd == DISABLE)
  808. {
  809. /* Enable the selected I2C Clock stretching */
  810. I2Cx->CTRL1 |= CTRL1_NOEXTEND_SET;
  811. }
  812. else
  813. {
  814. /* Disable the selected I2C Clock stretching */
  815. I2Cx->CTRL1 &= CTRL1_NOEXTEND_RESET;
  816. }
  817. }
  818. /**
  819. * @brief Selects the specified I2C fast mode duty cycle.
  820. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  821. * @param FmDutyCycle specifies the fast mode duty cycle.
  822. * This parameter can be one of the following values:
  823. * @arg I2C_FMDUTYCYCLE_2 I2C fast mode Tlow/Thigh = 2
  824. * @arg I2C_FMDUTYCYCLE_16_9 I2C fast mode Tlow/Thigh = 16/9
  825. */
  826. void I2C_ConfigFastModeDutyCycle(I2C_Module* I2Cx, uint16_t FmDutyCycle)
  827. {
  828. /* Check the parameters */
  829. assert_param(IS_I2C_PERIPH(I2Cx));
  830. assert_param(IS_I2C_FM_DUTY_CYCLE(FmDutyCycle));
  831. if (FmDutyCycle != I2C_FMDUTYCYCLE_16_9)
  832. {
  833. /* I2C fast mode Tlow/Thigh=2 */
  834. I2Cx->CLKCTRL &= I2C_FMDUTYCYCLE_2;
  835. }
  836. else
  837. {
  838. /* I2C fast mode Tlow/Thigh=16/9 */
  839. I2Cx->CLKCTRL |= I2C_FMDUTYCYCLE_16_9;
  840. }
  841. }
  842. /**
  843. * @brief
  844. ****************************************************************************************
  845. *
  846. * I2C State Monitoring Functions
  847. *
  848. ****************************************************************************************
  849. * This I2C driver provides three different ways for I2C state monitoring
  850. * depending on the application requirements and constraints:
  851. *
  852. *
  853. * 1) Basic state monitoring:
  854. * Using I2C_CheckEvent() function:
  855. * It compares the status registers (STS1 and STS2) content to a given event
  856. * (can be the combination of one or more flags).
  857. * It returns SUCCESS if the current status includes the given flags
  858. * and returns ERROR if one or more flags are missing in the current status.
  859. * - When to use:
  860. * - This function is suitable for most applications as well as for startup
  861. * activity since the events are fully described in the product reference manual
  862. * (RM0008).
  863. * - It is also suitable for users who need to define their own events.
  864. * - Limitations:
  865. * - If an error occurs (ie. error flags are set besides to the monitored flags),
  866. * the I2C_CheckEvent() function may return SUCCESS despite the communication
  867. * hold or corrupted real state.
  868. * In this case, it is advised to use error interrupts to monitor the error
  869. * events and handle them in the interrupt IRQ handler.
  870. *
  871. * @note
  872. * For error management, it is advised to use the following functions:
  873. * - I2C_ConfigInt() to configure and enable the error interrupts (I2C_INT_ERR).
  874. * - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
  875. * Where x is the peripheral instance (I2C1, I2C2 ...)
  876. * - I2C_GetFlag() or I2C_GetIntStatus() to be called into I2Cx_ER_IRQHandler()
  877. * in order to determine which error occured.
  878. * - I2C_ClrFlag() or I2C_ClrIntPendingBit() and/or I2C_EnableSoftwareReset()
  879. * and/or I2C_GenerateStop() in order to clear the error flag and source,
  880. * and return to correct communication status.
  881. *
  882. *
  883. * 2) Advanced state monitoring:
  884. * Using the function I2C_GetLastEvent() which returns the image of both status
  885. * registers in a single word (uint32_t) (Status Register 2 value is shifted left
  886. * by 16 bits and concatenated to Status Register 1).
  887. * - When to use:
  888. * - This function is suitable for the same applications above but it allows to
  889. * overcome the mentioned limitation of I2C_GetFlag() function.
  890. * The returned value could be compared to events already defined in the
  891. * library (n32g45x_i2c.h) or to custom values defined by user.
  892. * - This function is suitable when multiple flags are monitored at the same time.
  893. * - At the opposite of I2C_CheckEvent() function, this function allows user to
  894. * choose when an event is accepted (when all events flags are set and no
  895. * other flags are set or just when the needed flags are set like
  896. * I2C_CheckEvent() function).
  897. * - Limitations:
  898. * - User may need to define his own events.
  899. * - Same remark concerning the error management is applicable for this
  900. * function if user decides to check only regular communication flags (and
  901. * ignores error flags).
  902. *
  903. *
  904. * 3) Flag-based state monitoring:
  905. * Using the function I2C_GetFlag() which simply returns the status of
  906. * one single flag (ie. I2C_FLAG_RXDATNE ...).
  907. * - When to use:
  908. * - This function could be used for specific applications or in debug phase.
  909. * - It is suitable when only one flag checking is needed (most I2C events
  910. * are monitored through multiple flags).
  911. * - Limitations:
  912. * - When calling this function, the Status register is accessed. Some flags are
  913. * cleared when the status register is accessed. So checking the status
  914. * of one Flag, may clear other ones.
  915. * - Function may need to be called twice or more in order to monitor one
  916. * single event.
  917. *
  918. * For detailed description of Events, please refer to section I2C_Events in
  919. * n32g45x_i2c.h file.
  920. *
  921. */
  922. /**
  923. * @brief Checks whether the last I2Cx Event is equal to the one passed
  924. * as parameter.
  925. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  926. * @param I2C_EVENT specifies the event to be checked.
  927. * This parameter can be one of the following values:
  928. * @arg I2C_EVT_SLAVE_SEND_ADDR_MATCHED EV1
  929. * @arg I2C_EVT_SLAVE_RECV_ADDR_MATCHED EV1
  930. * @arg I2C_EVT_SLAVE_SEND_ADDR2_MATCHED EV1
  931. * @arg I2C_EVT_SLAVE_RECV_ADDR2_MATCHED EV1
  932. * @arg I2C_EVT_SLAVE_GCALLADDR_MATCHED EV1
  933. * @arg I2C_EVT_SLAVE_DATA_RECVD EV2
  934. * @arg (I2C_EVT_SLAVE_DATA_RECVD | I2C_FLAG_DUALFLAG) EV2
  935. * @arg (I2C_EVT_SLAVE_DATA_RECVD | I2C_FLAG_GCALLADDR) EV2
  936. * @arg I2C_EVT_SLAVE_DATA_SENDED EV3
  937. * @arg (I2C_EVT_SLAVE_DATA_SENDED | I2C_FLAG_DUALFLAG) EV3
  938. * @arg (I2C_EVT_SLAVE_DATA_SENDED | I2C_FLAG_GCALLADDR) EV3
  939. * @arg I2C_EVT_SLAVE_ACK_MISS EV3_2
  940. * @arg I2C_EVT_SLAVE_STOP_RECVD EV4
  941. * @arg I2C_EVT_MASTER_MODE_FLAG EV5
  942. * @arg I2C_EVT_MASTER_TXMODE_FLAG EV6
  943. * @arg I2C_EVT_MASTER_RXMODE_FLAG EV6
  944. * @arg I2C_EVT_MASTER_DATA_RECVD_FLAG EV7
  945. * @arg I2C_EVT_MASTER_DATA_SENDING EV8
  946. * @arg I2C_EVT_MASTER_DATA_SENDED EV8_2
  947. * @arg I2C_EVT_MASTER_MODE_ADDRESS10_FLAG EV9
  948. *
  949. * @note: For detailed description of Events, please refer to section
  950. * I2C_Events in n32g45x_i2c.h file.
  951. *
  952. * @return An ErrorStatus enumeration value:
  953. * - SUCCESS: Last event is equal to the I2C_EVENT
  954. * - ERROR: Last event is different from the I2C_EVENT
  955. */
  956. ErrorStatus I2C_CheckEvent(I2C_Module* I2Cx, uint32_t I2C_EVENT)
  957. {
  958. uint32_t lastevent = 0;
  959. uint32_t flag1 = 0, flag2 = 0;
  960. ErrorStatus status = ERROR;
  961. /* Check the parameters */
  962. assert_param(IS_I2C_PERIPH(I2Cx));
  963. assert_param(IS_I2C_EVT(I2C_EVENT));
  964. /* Read the I2Cx status register */
  965. flag1 = I2Cx->STS1;
  966. flag2 = I2Cx->STS2;
  967. flag2 = flag2 << 16;
  968. /* Get the last event value from I2C status register */
  969. lastevent = (flag1 | flag2) & FLAG_MASK;
  970. /* Check whether the last event contains the I2C_EVENT */
  971. if ((lastevent & I2C_EVENT) == I2C_EVENT)
  972. {
  973. /* SUCCESS: last event is equal to I2C_EVENT */
  974. status = SUCCESS;
  975. }
  976. else
  977. {
  978. /* ERROR: last event is different from I2C_EVENT */
  979. status = ERROR;
  980. }
  981. /* Return status */
  982. return status;
  983. }
  984. /**
  985. * @brief Returns the last I2Cx Event.
  986. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  987. *
  988. * @note: For detailed description of Events, please refer to section
  989. * I2C_Events in n32g45x_i2c.h file.
  990. *
  991. * @return The last event
  992. */
  993. uint32_t I2C_GetLastEvent(I2C_Module* I2Cx)
  994. {
  995. uint32_t lastevent = 0;
  996. uint32_t flag1 = 0, flag2 = 0;
  997. /* Check the parameters */
  998. assert_param(IS_I2C_PERIPH(I2Cx));
  999. /* Read the I2Cx status register */
  1000. flag1 = I2Cx->STS1;
  1001. flag2 = I2Cx->STS2;
  1002. flag2 = flag2 << 16;
  1003. /* Get the last event value from I2C status register */
  1004. lastevent = (flag1 | flag2) & FLAG_MASK;
  1005. /* Return status */
  1006. return lastevent;
  1007. }
  1008. /**
  1009. * @brief Checks whether the specified I2C flag is set or not.
  1010. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  1011. * @param I2C_FLAG specifies the flag to check.
  1012. * This parameter can be one of the following values:
  1013. * @arg I2C_FLAG_DUALFLAG Dual flag (Slave mode)
  1014. * @arg I2C_FLAG_SMBHADDR SMBus host header (Slave mode)
  1015. * @arg I2C_FLAG_SMBDADDR SMBus default header (Slave mode)
  1016. * @arg I2C_FLAG_GCALLADDR General call header flag (Slave mode)
  1017. * @arg I2C_FLAG_TRF Transmitter/Receiver flag
  1018. * @arg I2C_FLAG_BUSY Bus busy flag
  1019. * @arg I2C_FLAG_MSMODE Master/Slave flag
  1020. * @arg I2C_FLAG_SMBALERT SMBus Alert flag
  1021. * @arg I2C_FLAG_TIMOUT Timeout or Tlow error flag
  1022. * @arg I2C_FLAG_PECERR PEC error in reception flag
  1023. * @arg I2C_FLAG_OVERRUN Overrun/Underrun flag (Slave mode)
  1024. * @arg I2C_FLAG_ACKFAIL Acknowledge failure flag
  1025. * @arg I2C_FLAG_ARLOST Arbitration lost flag (Master mode)
  1026. * @arg I2C_FLAG_BUSERR Bus error flag
  1027. * @arg I2C_FLAG_TXDATE Data register empty flag (Transmitter)
  1028. * @arg I2C_FLAG_RXDATNE Data register not empty (Receiver) flag
  1029. * @arg I2C_FLAG_STOPF Stop detection flag (Slave mode)
  1030. * @arg I2C_FLAG_ADDR10F 10-bit header sent flag (Master mode)
  1031. * @arg I2C_FLAG_BYTEF Byte transfer finished flag
  1032. * @arg I2C_FLAG_ADDRF Address sent flag (Master mode) "ADSL"
  1033. * Address matched flag (Slave mode)"ENDA"
  1034. * @arg I2C_FLAG_STARTBF Start bit flag (Master mode)
  1035. * @return The new state of I2C_FLAG (SET or RESET).
  1036. */
  1037. FlagStatus I2C_GetFlag(I2C_Module* I2Cx, uint32_t I2C_FLAG)
  1038. {
  1039. FlagStatus bitstatus = RESET;
  1040. __IO uint32_t i2creg = 0, i2cxbase = 0;
  1041. /* Check the parameters */
  1042. assert_param(IS_I2C_PERIPH(I2Cx));
  1043. assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
  1044. /* Get the I2Cx peripheral base address */
  1045. i2cxbase = (uint32_t)I2Cx;
  1046. /* Read flag register index */
  1047. i2creg = I2C_FLAG >> 28;
  1048. /* Get bit[23:0] of the flag */
  1049. I2C_FLAG &= FLAG_MASK;
  1050. if (i2creg != 0)
  1051. {
  1052. /* Get the I2Cx STS1 register address */
  1053. i2cxbase += 0x14;
  1054. }
  1055. else
  1056. {
  1057. /* Flag in I2Cx STS2 Register */
  1058. I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
  1059. /* Get the I2Cx STS2 register address */
  1060. i2cxbase += 0x18;
  1061. }
  1062. if (((*(__IO uint32_t*)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
  1063. {
  1064. /* I2C_FLAG is set */
  1065. bitstatus = SET;
  1066. }
  1067. else
  1068. {
  1069. /* I2C_FLAG is reset */
  1070. bitstatus = RESET;
  1071. }
  1072. /* Return the I2C_FLAG status */
  1073. return bitstatus;
  1074. }
  1075. /**
  1076. * @brief Clears the I2Cx's pending flags.
  1077. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  1078. * @param I2C_FLAG specifies the flag to clear.
  1079. * This parameter can be any combination of the following values:
  1080. * @arg I2C_FLAG_SMBALERT SMBus Alert flag
  1081. * @arg I2C_FLAG_TIMOUT Timeout or Tlow error flag
  1082. * @arg I2C_FLAG_PECERR PEC error in reception flag
  1083. * @arg I2C_FLAG_OVERRUN Overrun/Underrun flag (Slave mode)
  1084. * @arg I2C_FLAG_ACKFAIL Acknowledge failure flag
  1085. * @arg I2C_FLAG_ARLOST Arbitration lost flag (Master mode)
  1086. * @arg I2C_FLAG_BUSERR Bus error flag
  1087. *
  1088. * @note
  1089. * - STOPF (STOP detection) is cleared by software sequence: a read operation
  1090. * to I2C_STS1 register (I2C_GetFlag()) followed by a write operation
  1091. * to I2C_CTRL1 register (I2C_Enable() to re-enable the I2C peripheral).
  1092. * - ADD10 (10-bit header sent) is cleared by software sequence: a read
  1093. * operation to I2C_STS1 (I2C_GetFlag()) followed by writing the
  1094. * second byte of the address in DAT register.
  1095. * - BTF (Byte Transfer Finished) is cleared by software sequence: a read
  1096. * operation to I2C_STS1 register (I2C_GetFlag()) followed by a
  1097. * read/write to I2C_DAT register (I2C_SendData()).
  1098. * - ADDR (Address sent) is cleared by software sequence: a read operation to
  1099. * I2C_STS1 register (I2C_GetFlag()) followed by a read operation to
  1100. * I2C_STS2 register ((void)(I2Cx->STS2)).
  1101. * - SB (Start Bit) is cleared software sequence: a read operation to I2C_STS1
  1102. * register (I2C_GetFlag()) followed by a write operation to I2C_DAT
  1103. * register (I2C_SendData()).
  1104. */
  1105. void I2C_ClrFlag(I2C_Module* I2Cx, uint32_t I2C_FLAG)
  1106. {
  1107. uint32_t flagpos = 0;
  1108. /* Check the parameters */
  1109. assert_param(IS_I2C_PERIPH(I2Cx));
  1110. assert_param(IS_I2C_CLR_FLAG(I2C_FLAG));
  1111. /* Get the I2C flag position */
  1112. flagpos = I2C_FLAG & FLAG_MASK;
  1113. /* Clear the selected I2C flag */
  1114. I2Cx->STS1 = (uint16_t)~flagpos;
  1115. }
  1116. /**
  1117. * @brief Checks whether the specified I2C interrupt has occurred or not.
  1118. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  1119. * @param I2C_IT specifies the interrupt source to check.
  1120. * This parameter can be one of the following values:
  1121. * @arg I2C_INT_SMBALERT SMBus Alert flag
  1122. * @arg I2C_INT_TIMOUT Timeout or Tlow error flag
  1123. * @arg I2C_INT_PECERR PEC error in reception flag
  1124. * @arg I2C_INT_OVERRUN Overrun/Underrun flag (Slave mode)
  1125. * @arg I2C_INT_ACKFAIL Acknowledge failure flag
  1126. * @arg I2C_INT_ARLOST Arbitration lost flag (Master mode)
  1127. * @arg I2C_INT_BUSERR Bus error flag
  1128. * @arg I2C_INT_TXDATE Data register empty flag (Transmitter)
  1129. * @arg I2C_INT_RXDATNE Data register not empty (Receiver) flag
  1130. * @arg I2C_INT_STOPF Stop detection flag (Slave mode)
  1131. * @arg I2C_INT_ADDR10F 10-bit header sent flag (Master mode)
  1132. * @arg I2C_INT_BYTEF Byte transfer finished flag
  1133. * @arg I2C_INT_ADDRF Address sent flag (Master mode) "ADSL"
  1134. * Address matched flag (Slave mode)"ENDAD"
  1135. * @arg I2C_INT_STARTBF Start bit flag (Master mode)
  1136. * @return The new state of I2C_IT (SET or RESET).
  1137. */
  1138. INTStatus I2C_GetIntStatus(I2C_Module* I2Cx, uint32_t I2C_IT)
  1139. {
  1140. INTStatus bitstatus = RESET;
  1141. uint32_t enablestatus = 0;
  1142. /* Check the parameters */
  1143. assert_param(IS_I2C_PERIPH(I2Cx));
  1144. assert_param(IS_I2C_GET_INT(I2C_IT));
  1145. /* Check if the interrupt source is enabled or not */
  1146. enablestatus = (uint32_t)(((I2C_IT & INTEN_MASK) >> 16) & (I2Cx->CTRL2));
  1147. /* Get bit[23:0] of the flag */
  1148. I2C_IT &= FLAG_MASK;
  1149. /* Check the status of the specified I2C flag */
  1150. if (((I2Cx->STS1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
  1151. {
  1152. /* I2C_IT is set */
  1153. bitstatus = SET;
  1154. }
  1155. else
  1156. {
  1157. /* I2C_IT is reset */
  1158. bitstatus = RESET;
  1159. }
  1160. /* Return the I2C_IT status */
  1161. return bitstatus;
  1162. }
  1163. /**
  1164. * @brief Clears the I2Cx's interrupt pending bits.
  1165. * @param I2Cx where x can be 1 or 2 to select the I2C peripheral.
  1166. * @param I2C_IT specifies the interrupt pending bit to clear.
  1167. * This parameter can be any combination of the following values:
  1168. * @arg I2C_INT_SMBALERT SMBus Alert interrupt
  1169. * @arg I2C_INT_TIMOUT Timeout or Tlow error interrupt
  1170. * @arg I2C_INT_PECERR PEC error in reception interrupt
  1171. * @arg I2C_INT_OVERRUN Overrun/Underrun interrupt (Slave mode)
  1172. * @arg I2C_INT_ACKFAIL Acknowledge failure interrupt
  1173. * @arg I2C_INT_ARLOST Arbitration lost interrupt (Master mode)
  1174. * @arg I2C_INT_BUSERR Bus error interrupt
  1175. *
  1176. * @note
  1177. * - STOPF (STOP detection) is cleared by software sequence: a read operation
  1178. * to I2C_STS1 register (I2C_GetIntStatus()) followed by a write operation to
  1179. * I2C_CTRL1 register (I2C_Enable() to re-enable the I2C peripheral).
  1180. * - ADD10 (10-bit header sent) is cleared by software sequence: a read
  1181. * operation to I2C_STS1 (I2C_GetIntStatus()) followed by writing the second
  1182. * byte of the address in I2C_DAT register.
  1183. * - BTF (Byte Transfer Finished) is cleared by software sequence: a read
  1184. * operation to I2C_STS1 register (I2C_GetIntStatus()) followed by a
  1185. * read/write to I2C_DAT register (I2C_SendData()).
  1186. * - ADDR (Address sent) is cleared by software sequence: a read operation to
  1187. * I2C_STS1 register (I2C_GetIntStatus()) followed by a read operation to
  1188. * I2C_STS2 register ((void)(I2Cx->STS2)).
  1189. * - SB (Start Bit) is cleared by software sequence: a read operation to
  1190. * I2C_STS1 register (I2C_GetIntStatus()) followed by a write operation to
  1191. * I2C_DAT register (I2C_SendData()).
  1192. */
  1193. void I2C_ClrIntPendingBit(I2C_Module* I2Cx, uint32_t I2C_IT)
  1194. {
  1195. uint32_t flagpos = 0;
  1196. /* Check the parameters */
  1197. assert_param(IS_I2C_PERIPH(I2Cx));
  1198. assert_param(IS_I2C_CLR_INT(I2C_IT));
  1199. /* Get the I2C flag position */
  1200. flagpos = I2C_IT & FLAG_MASK;
  1201. /* Clear the selected I2C flag */
  1202. I2Cx->STS1 = (uint16_t)~flagpos;
  1203. }
  1204. /**
  1205. * @}
  1206. */
  1207. /**
  1208. * @}
  1209. */
  1210. /**
  1211. * @}
  1212. */