apm32s10x_i2c.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /*!
  2. * @file apm32s10x_i2c.c
  3. *
  4. * @brief This file provides all the I2C firmware functions
  5. *
  6. * @version V1.0.1
  7. *
  8. * @date 2022-12-31
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2022-2023 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be usefull and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Includes */
  26. #include "apm32s10x_i2c.h"
  27. #include "apm32s10x_rcm.h"
  28. /** @addtogroup APM32S10x_StdPeriphDriver
  29. @{
  30. */
  31. /** @addtogroup I2C_Driver I2C Driver
  32. @{
  33. */
  34. /** @defgroup I2C_Functions Functions
  35. @{
  36. */
  37. /*!
  38. * @brief Reset I2C
  39. *
  40. * @param i2c: I2C selet 1 or 2
  41. *
  42. * @retval None
  43. */
  44. void I2C_Reset(I2C_T* i2c)
  45. {
  46. if (i2c == I2C1)
  47. {
  48. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_I2C1);
  49. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_I2C1);
  50. }
  51. else
  52. {
  53. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_I2C2);
  54. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_I2C2);
  55. }
  56. }
  57. /*!
  58. * @brief Configure I2C by configuring the structure
  59. *
  60. * @param i2c: I2C selet 1 or 2
  61. *
  62. * @param i2cConfig: pointer to a I2C_Config_T structure
  63. *
  64. * @retval None
  65. */
  66. void I2C_Config(I2C_T* i2c, I2C_Config_T* i2cConfig)
  67. {
  68. uint16_t tmpreg = 0, freqrange = 0;
  69. uint32_t PCLK1 = 8000000, PCLK2 = 0;
  70. uint16_t result = 0x04;
  71. i2c->SWITCH = 0;
  72. /* I2C CTRL2 Configuration */
  73. RCM_ReadPCLKFreq(&PCLK1, &PCLK2);
  74. freqrange = PCLK1 / 1000000;
  75. i2c->CTRL2_B.CLKFCFG = freqrange;
  76. /* I2C CLKCTRL Configuration */
  77. i2c->CTRL1_B.I2CEN = BIT_RESET;
  78. if (i2cConfig->clockSpeed <= 100000)
  79. {
  80. result = (PCLK1 / (i2cConfig->clockSpeed << 1));
  81. if (result < 0x04)
  82. {
  83. result = 0x04;
  84. }
  85. i2c->RISETMAX = freqrange + 1;
  86. tmpreg |= result;
  87. }
  88. /* Configure speed in fast mode */
  89. else
  90. {
  91. if (i2cConfig->dutyCycle == I2C_DUTYCYCLE_2)
  92. {
  93. result = (PCLK1 / (i2cConfig->clockSpeed * 3));
  94. }
  95. else
  96. {
  97. result = (PCLK1 / (i2cConfig->clockSpeed * 25));
  98. result |= I2C_DUTYCYCLE_16_9;
  99. }
  100. if ((result & 0x0FFF) == 0)
  101. {
  102. result |= 0x0001;
  103. }
  104. tmpreg |= (uint16_t)(result | 0x8000);
  105. i2c->RISETMAX = ((((freqrange) * 300) / 1000) + 1);
  106. }
  107. i2c->CLKCTRL = tmpreg;
  108. i2c->CTRL1_B.I2CEN = BIT_SET;
  109. /* i2c CTRL1 Configuration */
  110. i2c->CTRL1_B.ACKEN = BIT_RESET;
  111. i2c->CTRL1_B.SMBTCFG = BIT_RESET;
  112. i2c->CTRL1_B.SMBEN = BIT_RESET;
  113. i2c->CTRL1 |= i2cConfig->mode;
  114. i2c->CTRL1_B.ACKEN = i2cConfig->ack;
  115. i2c->SADDR1 = i2cConfig->ackAddress | i2cConfig->ownAddress1;
  116. }
  117. /*!
  118. * @brief Fill each I2C_Config_T member with its default value.
  119. *
  120. * @param i2cConfig: pointer to a I2C_Config_T structure
  121. *
  122. * @retval None
  123. */
  124. void I2C_ConfigStructInit(I2C_Config_T* i2cConfig)
  125. {
  126. i2cConfig->clockSpeed = 5000;
  127. i2cConfig->mode = I2C_MODE_I2C;
  128. i2cConfig->dutyCycle = I2C_DUTYCYCLE_2;
  129. i2cConfig->ownAddress1 = 0;
  130. i2cConfig->ack = I2C_ACK_DISABLE;
  131. i2cConfig->ackAddress = I2C_ACK_ADDRESS_7BIT;
  132. }
  133. /*!
  134. * @brief Enable I2C
  135. *
  136. * @param i2c: I2C selet 1 or 2
  137. *
  138. * @retval None
  139. */
  140. void I2C_Enable(I2C_T* i2c)
  141. {
  142. i2c->CTRL1_B.I2CEN = ENABLE;
  143. }
  144. /*!
  145. * @brief Disable I2C
  146. *
  147. * @param i2c: I2C selet 1 or 2
  148. *
  149. * @retval None
  150. */
  151. void I2C_Disable(I2C_T* i2c)
  152. {
  153. i2c->CTRL1_B.I2CEN = DISABLE;
  154. }
  155. /*!
  156. * @brief Enable Generates i2c communication START condition.
  157. *
  158. * @param i2c: I2C selet 1 or 2
  159. *
  160. * @retval None
  161. */
  162. void I2C_EnableGenerateStart(I2C_T* i2c)
  163. {
  164. i2c->CTRL1_B.START = BIT_SET;
  165. }
  166. /*!
  167. * @brief Disable Generates i2c communication START condition.
  168. *
  169. * @param i2c: I2C selet 1 or 2
  170. *
  171. * @retval None
  172. */
  173. void I2C_DisableGenerateStart(I2C_T* i2c)
  174. {
  175. i2c->CTRL1_B.START = BIT_RESET;
  176. }
  177. /*!
  178. * @brief Enable Generates i2c communication STOP condition.
  179. *
  180. * @param i2c: I2C selet 1 or 2
  181. *
  182. * @retval None
  183. */
  184. void I2C_EnableGenerateStop(I2C_T* i2c)
  185. {
  186. i2c->CTRL1_B.STOP = BIT_SET;
  187. }
  188. /*!
  189. * @brief Disable Generates i2c communication STOP condition.
  190. *
  191. * @param i2c: I2C selet 1 or 2
  192. *
  193. * @retval None
  194. */
  195. void I2C_DisableGenerateStop(I2C_T* i2c)
  196. {
  197. i2c->CTRL1_B.STOP = BIT_RESET;
  198. }
  199. /*!
  200. * @brief Enable the specified I2C acknowledge feature.
  201. *
  202. * @param i2c: I2C selet 1 or 2
  203. *
  204. * @retval None
  205. */
  206. void I2C_EnableAcknowledge(I2C_T* i2c)
  207. {
  208. i2c->CTRL1_B.ACKEN = ENABLE;
  209. }
  210. /*!
  211. * @brief Disable the specified I2C acknowledge feature.
  212. *
  213. * @param i2c: I2C selet 1 or 2
  214. *
  215. * @retval None
  216. */
  217. void I2C_DisableAcknowledge(I2C_T* i2c)
  218. {
  219. i2c->CTRL1_B.ACKEN = DISABLE;
  220. }
  221. /*!
  222. * @brief Configure the specified I2C own address2.
  223. *
  224. * @param i2c: I2C selet 1 or 2
  225. *
  226. * @param address:specifies the 7bit I2C own address2.
  227. *
  228. * @retval None
  229. */
  230. void I2C_ConfigOwnAddress2(I2C_T* i2c, uint8_t address)
  231. {
  232. i2c->SADDR2_B.ADDR2 = address;
  233. }
  234. /*!
  235. * @brief Enable the specified I2C dual addressing mode.
  236. *
  237. * @param i2c: I2C selet 1 or 2
  238. *
  239. * @retval None
  240. */
  241. void I2C_EnableDualAddress(I2C_T* i2c)
  242. {
  243. i2c->SADDR2_B.ADDRNUM = ENABLE;
  244. }
  245. /*!
  246. * @brief Disable the specified I2C dual addressing mode.
  247. *
  248. * @param i2c: I2C selet 1 or 2
  249. *
  250. * @retval None
  251. */
  252. void I2C_DisableDualAddress(I2C_T* i2c)
  253. {
  254. i2c->SADDR2_B.ADDRNUM = DISABLE;
  255. }
  256. /*!
  257. * @brief Enable the specified I2C general call feature.
  258. *
  259. * @param i2c: I2C selet 1 or 2
  260. *
  261. * @retval None
  262. */
  263. void I2C_EnableGeneralCall(I2C_T* i2c)
  264. {
  265. i2c->CTRL1_B.SRBEN = ENABLE;
  266. }
  267. /*!
  268. * @brief Disable the specified I2C general call feature.
  269. *
  270. * @param i2c: I2C selet 1 or 2
  271. *
  272. * @retval None
  273. */
  274. void I2C_DisableGeneralCall(I2C_T* i2c)
  275. {
  276. i2c->CTRL1_B.SRBEN = DISABLE;
  277. }
  278. /*!
  279. * @brief Send one byte
  280. *
  281. * @param i2c: I2C selet 1 or 2
  282. *
  283. * @param data: data to send
  284. *
  285. * @retval None
  286. */
  287. void I2C_TxData(I2C_T* i2c, uint8_t data)
  288. {
  289. i2c->DATA_B.DATA = data;
  290. }
  291. /*!
  292. * @brief Return the recevie data
  293. *
  294. * @param i2c: I2C selet 1 or 2
  295. *
  296. * @retval received data
  297. */
  298. uint8_t I2C_RxData(I2C_T* i2c)
  299. {
  300. return i2c->DATA_B.DATA;
  301. }
  302. /*!
  303. * @brief Transmit the address byte to select the slave device.
  304. *
  305. * @param i2c: I2C selet 1 or 2
  306. *
  307. * @param address: slave address which will be transmitted
  308. *
  309. * @param direction: Direction mode
  310. * The parameter can be one of following values:
  311. * @arg I2C_DIRECTION_TX: Transmitter mode
  312. * @arg I2C_DIRECTION_RX: Receiver mode
  313. * @retval None
  314. */
  315. void I2C_Tx7BitAddress(I2C_T* i2c, uint8_t address, I2C_DIRECTION_T direction)
  316. {
  317. if (direction != I2C_DIRECTION_TX)
  318. {
  319. i2c->DATA_B.DATA = address | 0x0001;
  320. }
  321. else
  322. {
  323. i2c->DATA_B.DATA = address & 0xFFFE;
  324. }
  325. }
  326. /*!
  327. * @brief Reads the I2C register and returns its value.
  328. *
  329. * @param i2c: I2C selet 1 or 2
  330. *
  331. * @param i2cRegister : register to read
  332. * The parameter can be one of following values:
  333. * @arg I2C_REGISTER_CTRL1: CTRL1 register
  334. * @arg I2C_REGISTER_CTRL2: CTRL2 register
  335. * @arg I2C_REGISTER_SADDR1: SADDR1 register
  336. * @arg I2C_REGISTER_SADDR2: SADDR2 register
  337. * @arg I2C_REGISTER_DATA: DATA register
  338. * @arg I2C_REGISTER_STS1: STS1 register
  339. * @arg I2C_REGISTER_STS2: STS2 register
  340. * @arg I2C_REGISTER_CLKCTRL: CLKCTRL register
  341. * @arg I2C_REGISTER_RISETMAX: RISETMAX register
  342. * @arg I2C_REGISTER_SWITCH: SWITCH register
  343. *
  344. * @retval The value of the read register
  345. */
  346. uint16_t I2C_ReadRegister(I2C_T* i2c, I2C_REGISTER_T i2cRegister)
  347. {
  348. switch (i2cRegister)
  349. {
  350. case I2C_REGISTER_CTRL1:
  351. return i2c->CTRL1;
  352. case I2C_REGISTER_CTRL2:
  353. return i2c->CTRL2;
  354. case I2C_REGISTER_SADDR1:
  355. return i2c->SADDR1;
  356. case I2C_REGISTER_SADDR2:
  357. return i2c->SADDR2;
  358. case I2C_REGISTER_DATA:
  359. return i2c->DATA;
  360. case I2C_REGISTER_STS1:
  361. return i2c->STS1;
  362. case I2C_REGISTER_STS2:
  363. return i2c->STS2;
  364. case I2C_REGISTER_CLKCTRL:
  365. return i2c->CLKCTRL;
  366. case I2C_REGISTER_RISETMAX:
  367. return i2c->RISETMAX;
  368. case I2C_REGISTER_SWITCH:
  369. return i2c->SWITCH;
  370. default:
  371. return 0;
  372. }
  373. }
  374. /*!
  375. * @brief Enable the I2C software reset.
  376. *
  377. * @param i2c: I2C selet 1 or 2
  378. *
  379. * @retval None
  380. */
  381. void I2C_EnableSoftwareReset(I2C_T* i2c)
  382. {
  383. i2c->CTRL1_B.SWRST = ENABLE;
  384. }
  385. /*!
  386. * @brief Disable the I2C software reset.
  387. *
  388. * @param i2c: I2C selet 1 or 2
  389. *
  390. * @retval None
  391. */
  392. void I2C_DisableSoftwareReset(I2C_T* i2c)
  393. {
  394. i2c->CTRL1_B.SWRST = DISABLE;
  395. }
  396. /*!
  397. * @brief Select the specified I2C NACK position in master receiver mode.
  398. *
  399. * @param i2c: I2C selet 1 or 2
  400. *
  401. * @param NACKPosition: specifies the NACK position.
  402. *
  403. * @retval None
  404. */
  405. void I2C_ConfigNACKPosition(I2C_T* i2c, I2C_NACK_POSITION_T NACKPosition)
  406. {
  407. if (NACKPosition == I2C_NACK_POSITION_NEXT)
  408. {
  409. i2c->CTRL1_B.ACKPOS = BIT_SET;
  410. }
  411. else
  412. {
  413. i2c->CTRL1_B.ACKPOS = BIT_RESET;
  414. }
  415. }
  416. /*!
  417. * @brief Control the height of pin of SMBusAlert
  418. *
  419. * @param i2c: I2C selet 1 or 2
  420. *
  421. * @param SMBusState: SMBAlert pin level.
  422. * The parameter can be one of following values:
  423. * @arg I2C_SMBUSALER_LOW: SMBus Alert pin low
  424. * @arg I2C_SMBUSALER_HIGH: SMBus Alert pin high
  425. *
  426. * @retval None
  427. */
  428. void I2C_ConfigSMBusAlert(I2C_T* i2c, I2C_SMBUSALER_T SMBusState)
  429. {
  430. if (SMBusState == I2C_SMBUSALER_LOW)
  431. {
  432. i2c->CTRL1_B.ALERTEN = BIT_SET;
  433. }
  434. else
  435. {
  436. i2c->CTRL1_B.ALERTEN = BIT_RESET;
  437. }
  438. }
  439. /*!
  440. * @brief Enable the I2C PEC transfer.
  441. *
  442. * @param i2c: I2C selet 1 or 2
  443. *
  444. * @retval None
  445. */
  446. void I2C_EnablePECTransmit(I2C_T* i2c)
  447. {
  448. i2c->CTRL1_B.PEC = BIT_SET;
  449. }
  450. /*!
  451. * @brief Disable the I2C PEC transfer.
  452. *
  453. * @param i2c: I2C selet 1 or 2
  454. *
  455. * @retval None
  456. */
  457. void I2C_DisablePECTransmit(I2C_T* i2c)
  458. {
  459. i2c->CTRL1_B.PEC = BIT_RESET;
  460. }
  461. /*!
  462. * @brief Select the I2C PEC position.
  463. *
  464. * @param i2c: I2C selet 1 or 2
  465. *
  466. * @param PECPosition: PEC position
  467. * The parameter can be one of following values:
  468. * @arg I2C_PEC_POSITION_NEXT: indicates that the next byte is PEC
  469. * @arg I2C_PEC_POSITION_CURRENT: indicates that current byte is PEC
  470. *
  471. * @retval None
  472. */
  473. void I2C_ConfigPECPosition(I2C_T* i2c, I2C_PEC_POSITION_T PECPosition)
  474. {
  475. if (PECPosition == I2C_PEC_POSITION_NEXT)
  476. {
  477. i2c->CTRL1_B.ACKPOS = BIT_SET;
  478. }
  479. else
  480. {
  481. i2c->CTRL1_B.ACKPOS = BIT_RESET;
  482. }
  483. }
  484. /*!
  485. * @brief Enable the PEC value calculation of the transferred bytes.
  486. *
  487. * @param i2c: I2C selet 1 or 2
  488. *
  489. * @retval None
  490. */
  491. void I2C_EnablePEC(I2C_T* i2c)
  492. {
  493. i2c->CTRL1_B.PECEN = BIT_SET;
  494. }
  495. /*!
  496. * @brief Disable the PEC value calculation of the transferred bytes.
  497. *
  498. * @param i2c: I2C selet 1 or 2
  499. *
  500. * @retval None
  501. */
  502. void I2C_DisablePEC(I2C_T* i2c)
  503. {
  504. i2c->CTRL1_B.PECEN = BIT_RESET;
  505. }
  506. /*!
  507. * @brief Read the PEC value for the I2C.
  508. *
  509. * @param i2c: I2C selet 1 or 2
  510. *
  511. * @retval value of PEC
  512. */
  513. uint8_t I2C_ReadPEC(I2C_T* i2c)
  514. {
  515. return i2c->STS2_B.PECVALUE;
  516. }
  517. /*!
  518. * @brief Enable the I2C ARP.
  519. *
  520. * @param i2c: I2C selet 1 or 2
  521. *
  522. * @retval None
  523. */
  524. void I2C_EnableARP(I2C_T* i2c)
  525. {
  526. i2c->CTRL1_B.ARPEN = BIT_SET;
  527. }
  528. /*!
  529. * @brief Disable the I2C ARP.
  530. *
  531. * @param i2c: I2C selet 1 or 2
  532. *
  533. * @retval None
  534. */
  535. void I2C_DisableARP(I2C_T* i2c)
  536. {
  537. i2c->CTRL1_B.ARPEN = BIT_RESET;
  538. }
  539. /*!
  540. * @brief Enable the I2C Clock stretching.
  541. *
  542. * @param i2c: I2C selet 1 or 2
  543. *
  544. * @retval None
  545. */
  546. void I2C_EnableStretchClock(I2C_T* i2c)
  547. {
  548. i2c->CTRL1_B.CLKSTRETCHD = BIT_RESET;
  549. }
  550. /*!
  551. * @brief Disable the I2C Clock stretching.
  552. *
  553. * @param i2c: I2C selet 1 or 2
  554. *
  555. * @retval None
  556. */
  557. void I2C_DisableStretchClock(I2C_T* i2c)
  558. {
  559. i2c->CTRL1_B.CLKSTRETCHD = BIT_SET;
  560. }
  561. /*!
  562. * @brief Select the specified I2C fast mode duty cycle.
  563. *
  564. * @param i2c: I2C selet 1 or 2
  565. *
  566. * @param dutyCycle: the fast mode duty cycle.
  567. * The parameter can be one of following values:
  568. * @arg I2C_DUTYCYCLE_16_9: I2C fast mode Tlow/Thigh = 16/9
  569. * @arg I2C_DUTYCYCLE_2: I2C fast mode Tlow/Thigh = 2
  570. *
  571. * @retval None
  572. */
  573. void I2C_ConfigFastModeDutyCycle(I2C_T* i2c, I2C_DUTYCYCLE_T dutyCycle)
  574. {
  575. if (dutyCycle == I2C_DUTYCYCLE_16_9)
  576. {
  577. i2c->CLKCTRL_B.FDUTYCFG = BIT_SET;
  578. }
  579. else
  580. {
  581. i2c->CLKCTRL_B.FDUTYCFG = BIT_RESET;
  582. }
  583. }
  584. /*!
  585. * @brief Enable the specified I2C DMA requests.
  586. *
  587. * @param i2c: I2C selet 1 or 2
  588. *
  589. * @retval None
  590. */
  591. void I2C_EnableDMA(I2C_T* i2c)
  592. {
  593. i2c->CTRL2_B.DMAEN = ENABLE;
  594. }
  595. /*!
  596. * @brief Disable the specified I2C DMA requests.
  597. *
  598. * @param i2c: I2C selet 1 or 2
  599. *
  600. * @retval None
  601. */
  602. void I2C_DisableDMA(I2C_T* i2c)
  603. {
  604. i2c->CTRL2_B.DMAEN = DISABLE;
  605. }
  606. /*!
  607. * @brief Enable DMA to receive the last transfer
  608. *
  609. * @param i2c: I2C selet 1 or 2
  610. *
  611. * @retval None
  612. */
  613. void I2C_EnableDMALastTransfer(I2C_T* i2c)
  614. {
  615. i2c->CTRL2_B.LTCFG = BIT_SET;
  616. }
  617. /*!
  618. * @brief Disable DMA to receive the last transfer
  619. *
  620. * @param i2c: I2C selet 1 or 2
  621. *
  622. * @retval None
  623. */
  624. void I2C_DisableDMALastTransfer(I2C_T* i2c)
  625. {
  626. i2c->CTRL2_B.LTCFG = BIT_RESET;
  627. }
  628. /*!
  629. * @brief Enable the specified I2C interrupts.
  630. *
  631. * @param i2c: I2C selet 1 or 2
  632. *
  633. * @param interrupt:I2C interrupts sources
  634. * The parameter can be any combination of following values:
  635. * @arg I2C_INT_BUF: Buffer interrupt
  636. * @arg I2C_INT_EVT: Event interrupt
  637. * @arg I2C_INT_ERR: Error interrupt
  638. *
  639. * @retval None
  640. */
  641. void I2C_EnableInterrupt(I2C_T* i2c, uint16_t interrupt)
  642. {
  643. i2c->CTRL2 |= interrupt;
  644. }
  645. /*!
  646. * @brief Disable the specified I2C interrupts.
  647. *
  648. * @param i2c: I2C selet 1 or 2
  649. *
  650. * @param interrupt:I2C interrupts sources
  651. * The parameter can be any combination of following values:
  652. * @arg I2C_INT_BUF: Buffer interrupt
  653. * @arg I2C_INT_EVT: Event interrupt
  654. * @arg I2C_INT_ERR: Error interrupt
  655. *
  656. * @retval None
  657. */
  658. void I2C_DisableInterrupt(I2C_T* i2c, uint16_t interrupt)
  659. {
  660. i2c->CTRL2 &= ~interrupt;
  661. }
  662. /*!
  663. * @brief Check that the last event is equal to the last passed event
  664. *
  665. * @param i2c: I2C selet 1 or 2
  666. *
  667. * @param i2cEvent: the event to be checked.
  668. * The parameter can be one of the following values:
  669. * @arg I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED : EV1
  670. * @arg I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED : EV1
  671. * @arg I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED : EV1
  672. * @arg I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED : EV1
  673. * @arg I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED : EV1
  674. * @arg I2C_EVENT_SLAVE_BYTE_RECEIVED : EV2
  675. * @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED : EV3
  676. * @arg I2C_EVENT_SLAVE_ACK_FAILURE : EV3_2
  677. * @arg I2C_EVENT_SLAVE_STOP_DETECTED : EV4
  678. * @arg I2C_EVENT_MASTER_MODE_SELECT : EV5
  679. * @arg I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED : EV6
  680. * @arg I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED : EV6
  681. * @arg I2C_EVENT_MASTER_BYTE_RECEIVED : EV7
  682. * @arg I2C_EVENT_MASTER_BYTE_TRANSMITTING : EV8
  683. * @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED : EV8_2
  684. * @arg I2C_EVENT_MASTER_MODE_ADDRESS10 : EV9
  685. *
  686. * @retval Status: SUCCESS or ERROR
  687. */
  688. uint8_t I2C_ReadEventStatus(I2C_T* i2c, I2C_EVENT_T i2cEvent)
  689. {
  690. uint32_t lastevent = 0;
  691. uint32_t flag1 = 0, flag2 = 0;
  692. flag1 = i2c->STS1 & 0x0000FFFF;
  693. flag2 = i2c->STS2 & 0x0000FFFF;
  694. flag2 = flag2 << 16;
  695. lastevent = (flag1 | flag2) & 0x00FFFFFF;
  696. if ((lastevent & i2cEvent) == i2cEvent)
  697. {
  698. return SUCCESS;
  699. }
  700. return ERROR;
  701. }
  702. /*!
  703. * @brief Read the last i2c Event.
  704. *
  705. * @param i2c: I2C selet 1 or 2
  706. *
  707. * @retval The last event
  708. */
  709. uint32_t I2C_ReadLastEvent(I2C_T* i2c)
  710. {
  711. uint32_t lastevent = 0;
  712. uint32_t flag1 = 0, flag2 = 0;
  713. flag1 = i2c->STS1 & 0x0000FFFF;
  714. flag2 = i2c->STS2 & 0x0000FFFF;
  715. flag2 = flag2 << 16;
  716. lastevent = (flag1 | flag2) & 0x00FFFFFF;
  717. return lastevent;
  718. }
  719. /*!
  720. * @brief Check whether the I2C flag is set
  721. *
  722. * @param i2c: I2C selet 1 or 2
  723. *
  724. * @param flag: specify the I2C flag
  725. * The parameter can be one of the following values:
  726. * @arg I2C_FLAG_DUALADDR: Dual flag (Slave mode)
  727. * @arg I2C_FLAG_SMMHADDR: SMBus host header (Slave mode)
  728. * @arg I2C_FLAG_SMBDADDR: SMBus default header (Slave mode)
  729. * @arg I2C_FLAG_GENCALL: General call header flag (Slave mode)
  730. * @arg I2C_FLAG_TR: Transmitter/Receiver flag
  731. * @arg I2C_FLAG_BUSBSY: Bus busy flag
  732. * @arg I2C_FLAG_MS: Master/Slave flag
  733. * @arg I2C_FLAG_SMBALT: SMBus Alert flag
  734. * @arg I2C_FLAG_TTE: Timeout or Tlow error flag
  735. * @arg I2C_FLAG_PECE: PEC error in reception flag
  736. * @arg I2C_FLAG_OVRUR: Overrun/Underrun flag (Slave mode)
  737. * @arg I2C_FLAG_AE: Acknowledge error flag
  738. * @arg I2C_FLAG_AL: Arbitration lost flag (Master mode)
  739. * @arg I2C_FLAG_BERR: Bus error flag
  740. * @arg I2C_FLAG_TXBE: Transmitter data register empty flag
  741. * @arg I2C_FLAG_RXBNE: Receiver data register not empty flag
  742. * @arg I2C_FLAG_STOP: Stop detection flag (Slave mode)
  743. * @arg I2C_FLAG_ADDR10: 10-bit header sent flag (Master mode)
  744. * @arg I2C_FLAG_BTC: Byte transfer complete flag
  745. * @arg I2C_FLAG_ADDR: Address sent flag (Master mode)
  746. * @arg I2C_FLAG_START: Start bit flag (Master mode)
  747. *
  748. * @retval Status: flag SET or RESET
  749. */
  750. uint8_t I2C_ReadStatusFlag(I2C_T* i2c, I2C_FLAG_T flag)
  751. {
  752. uint8_t status = 0;
  753. switch (flag)
  754. {
  755. case I2C_FLAG_DUALADDR:
  756. status = i2c->STS2_B.DUALADDRFLG;
  757. break;
  758. case I2C_FLAG_SMMHADDR:
  759. status = i2c->STS2_B.SMMHADDR;
  760. break;
  761. case I2C_FLAG_SMBDADDR:
  762. status = i2c->STS2_B.SMBDADDRFLG;
  763. break;
  764. case I2C_FLAG_GENCALL:
  765. status = i2c->STS2_B.GENCALLFLG;
  766. break;
  767. case I2C_FLAG_TR:
  768. status = i2c->STS2_B.TRFLG;
  769. break;
  770. case I2C_FLAG_BUSBSY:
  771. status = i2c->STS2_B.BUSBSYFLG;
  772. break;
  773. case I2C_FLAG_MS:
  774. status = i2c->STS2_B.MSFLG;
  775. break;
  776. case I2C_FLAG_SMBALT:
  777. status = i2c->STS1_B.SMBALTFLG;
  778. break;
  779. case I2C_FLAG_TTE:
  780. status = i2c->STS1_B.TTEFLG;
  781. break;
  782. case I2C_FLAG_PECE:
  783. status = i2c->STS1_B.PECEFLG;
  784. break;
  785. case I2C_FLAG_OVRUR:
  786. status = i2c->STS1_B.OVRURFLG;
  787. break;
  788. case I2C_FLAG_AE:
  789. status = i2c->STS1_B.AEFLG;
  790. break;
  791. case I2C_FLAG_AL:
  792. status = i2c->STS1_B.ALFLG;
  793. break;
  794. case I2C_FLAG_BERR:
  795. status = i2c->STS1_B.BERRFLG;
  796. break;
  797. case I2C_FLAG_TXBE:
  798. status = i2c->STS1_B.TXBEFLG;
  799. break;
  800. case I2C_FLAG_RXBNE:
  801. status = i2c->STS1_B.RXBNEFLG;
  802. break;
  803. case I2C_FLAG_STOP:
  804. status = i2c->STS1_B.STOPFLG;
  805. break;
  806. case I2C_FLAG_ADDR10:
  807. status = i2c->STS1_B.ADDR10FLG;
  808. break;
  809. case I2C_FLAG_BTC:
  810. status = i2c->STS1_B.BTCFLG;
  811. break;
  812. case I2C_FLAG_ADDR:
  813. status = i2c->STS1_B.ADDRFLG;
  814. break;
  815. case I2C_FLAG_START:
  816. status = i2c->STS1_B.STARTFLG;
  817. break;
  818. default:
  819. break;
  820. }
  821. return status;
  822. }
  823. /*!
  824. * @brief Clear the I2C flag
  825. *
  826. * @param i2c: I2C selet 1 or 2
  827. *
  828. * @param flag: specify the I2C flag
  829. * The parameter can be one of the following values:
  830. * @arg I2C_FLAG_SMBALT: SMBus Alert flag
  831. * @arg I2C_FLAG_TTE: Timeout or Tlow error flag
  832. * @arg I2C_FLAG_PECE: PEC error in reception flag
  833. * @arg I2C_FLAG_OVRUR: Overrun/Underrun flag (Slave mode)
  834. * @arg I2C_FLAG_AE: Acknowledge error flag
  835. * @arg I2C_FLAG_AL: Arbitration lost flag (Master mode)
  836. * @arg I2C_FLAG_BERR: Bus error flag
  837. *
  838. * @retval None
  839. *
  840. * @note 1)I2C_FLAG_STOP: Stop detection flag is cleared by software sequence:
  841. * a read operation to I2C_STS1 register (I2C_ReadStatusFlag())
  842. * followed by a write operation to I2C_CRTL1 register (I2C_Enable()).
  843. * 2)I2C_FLAG_ADDR10: 10-bit header sent flag is cleared by software sequence:
  844. * a read operation to I2C_STS1 (I2C_ReadStatusFlag())
  845. * followed by writing the second byte of the address in I2C_DATA register.
  846. * 3)I2C_FLAG_BTC: Byte transfer complete flag is cleared by software sequence:
  847. * a read operation to I2C_STS1 register (I2C_ReadStatusFlag())
  848. * followed by a read/write to I2C_DATA register (I2C_TxData()).
  849. * 4)I2C_FLAG_ADDR: Address sent flag is cleared by software sequence:
  850. * a read operation to I2C_STS1 register (I2C_ReadStatusFlag())
  851. * followed by a read operation to I2C_STS2 register ((void)(I2Cx->STS2)).
  852. * 5)I2C_FLAG_START: Start bit flag is cleared software sequence:
  853. * a read operation to I2C_STS1 register (I2C_ReadStatusFlag())
  854. * followed by a write operation to I2C_DATA register (I2C_TxData()).
  855. */
  856. void I2C_ClearStatusFlag(I2C_T* i2c, I2C_FLAG_T flag)
  857. {
  858. switch (flag)
  859. {
  860. case I2C_FLAG_SMBALT:
  861. i2c->STS1_B.SMBALTFLG = BIT_RESET;
  862. break;
  863. case I2C_FLAG_TTE:
  864. i2c->STS1_B.TTEFLG = BIT_RESET;
  865. break;
  866. case I2C_FLAG_PECE:
  867. i2c->STS1_B.PECEFLG = BIT_RESET;
  868. break;
  869. case I2C_FLAG_OVRUR:
  870. i2c->STS1_B.OVRURFLG = BIT_RESET;
  871. break;
  872. case I2C_FLAG_AE:
  873. i2c->STS1_B.AEFLG = BIT_RESET;
  874. break;
  875. case I2C_FLAG_AL:
  876. i2c->STS1_B.ALFLG = BIT_RESET;
  877. break;
  878. case I2C_FLAG_BERR:
  879. i2c->STS1_B.BERRFLG = BIT_RESET;
  880. break;
  881. default:
  882. break;
  883. }
  884. }
  885. /*!
  886. * @brief Check whether the I2C interrupts is set
  887. *
  888. * @param i2c: I2C selet 1 or 2
  889. *
  890. * @param flag: specify the I2C interrupts
  891. * The parameter can be one of the following values:
  892. * @arg I2C_INT_FLAG_SMBALT: SMBus Alert flag
  893. * @arg I2C_INT_FLAG_TTE: Timeout or Tlow error flag
  894. * @arg I2C_INT_FLAG_PECE: PEC error in reception flag
  895. * @arg I2C_INT_FLAG_OVRUR: Overrun/Underrun flag (Slave mode)
  896. * @arg I2C_INT_FLAG_AE: Acknowledge error flag
  897. * @arg I2C_INT_FLAG_AL: Arbitration lost flag (Master mode)
  898. * @arg I2C_INT_FLAG_BERR: Bus error flag
  899. * @arg I2C_INT_FLAG_TXBE: Transmitter data register empty flag
  900. * @arg I2C_INT_FLAG_RXBNE: Receiver data register not empty flag
  901. * @arg I2C_INT_FLAG_STOP: Stop detection flag (Slave mode)
  902. * @arg I2C_INT_FLAG_ADDR10: 10-bit header sent flag (Master mode)
  903. * @arg I2C_INT_FLAG_BTC: Byte transfer complete flag
  904. * @arg I2C_INT_FLAG_ADDR: Address sent flag (Master mode)
  905. * @arg I2C_INT_FLAG_START: Start bit flag (Master mode)
  906. *
  907. * @retval Status: flag SET or RESET
  908. */
  909. uint8_t I2C_ReadIntFlag(I2C_T* i2c, I2C_INT_FLAG_T flag)
  910. {
  911. uint32_t enablestatus = 0;
  912. enablestatus = ((flag & 0x07000000) >> 16) & (i2c->CTRL2);
  913. flag &= 0x00FFFFFF;
  914. if (((i2c->STS1 & flag) != RESET) && enablestatus)
  915. {
  916. return SET;
  917. }
  918. return RESET;
  919. }
  920. /*!
  921. * @brief Clears the I2C interrupt flag bits.
  922. *
  923. * @param i2c: I2C selet 1 or 2
  924. *
  925. * @param flag: specify the I2C flag
  926. * The parameter can be any combination of the following values:
  927. * @arg I2C_INT_FLAG_SMBALT: SMBus Alert flag
  928. * @arg I2C_INT_FLAG_TTE: Timeout or Tlow error flag
  929. * @arg I2C_INT_FLAG_PECE: PEC error in reception flag
  930. * @arg I2C_INT_FLAG_OVRUR: Overrun/Underrun flag (Slave mode)
  931. * @arg I2C_INT_FLAG_AE: Acknowledge error flag
  932. * @arg I2C_INT_FLAG_AL: Arbitration lost flag (Master mode)
  933. * @arg I2C_INT_FLAG_BERR: Bus error flag
  934. *
  935. * @retval None
  936. *
  937. * @note 1)I2C_INT_FLAG_STOP: Stop detection flag is cleared by software sequence:
  938. * a read operation to I2C_STS1 register (I2C_ReadIntFlag())
  939. * followed by a write operation to I2C_CRTL1 register (I2C_Enable()).
  940. * 2)I2C_INT_FLAG_ADDR10: 10-bit header sent flag is cleared by software sequence:
  941. * a read operation to I2C_STS1 (I2C_ReadIntFlag())
  942. * followed by writing the second byte of the address in I2C_DATA register.
  943. * 3)I2C_INT_FLAG_BTC: Byte transfer complete flag is cleared by software sequence:
  944. * a read operation to I2C_STS1 register (I2C_ReadIntFlag())
  945. * followed by a read/write to I2C_DATA register (I2C_TxData()).
  946. * 4)I2C_INT_FLAG_ADDR: Address sent flag is cleared by software sequence:
  947. * a read operation to I2C_STS1 register (I2C_ReadIntFlag())
  948. * followed by a read operation to I2C_STS2 register ((void)(I2Cx->STS2)).
  949. * 5)I2C_INT_FLAG_START: Start bit flag is cleared software sequence:
  950. * a read operation to I2C_STS1 register (I2C_ReadIntFlag())
  951. * followed by a write operation to I2C_DATA register (I2C_TxData()).
  952. */
  953. void I2C_ClearIntFlag(I2C_T* i2c, uint32_t flag)
  954. {
  955. i2c->STS1 = (uint16_t)~(flag & 0x00FFFFFF);
  956. }
  957. /**@} end of group I2C_Functions */
  958. /**@} end of group I2C_Driver */
  959. /**@} end of group APM32S10x_StdPeriphDriver */