apm32f10x_sci2c.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*!
  2. * @file apm32f10x_sci2c.c
  3. *
  4. * @brief This file contains all the functions for the SCI2C peripheral
  5. *
  6. * @version V1.0.1
  7. *
  8. * @date 2021-03-23
  9. *
  10. */
  11. #include "apm32f10x_sci2c.h"
  12. #include "apm32f10x_rcm.h"
  13. /** @addtogroup Peripherals_Library Standard Peripheral Library
  14. @{
  15. */
  16. /** @addtogroup SCI2C_Driver SCI2C Driver
  17. @{
  18. */
  19. /** @addtogroup SCI2C_Fuctions Fuctions
  20. @{
  21. */
  22. /*!
  23. * @brief Set I2C peripheral registers to their default reset values
  24. *
  25. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  26. *
  27. * @retval None
  28. */
  29. void SCI2C_Reset(SCI2C_T *i2c)
  30. {
  31. if(i2c == I2C3)
  32. {
  33. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_I2C1);
  34. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_I2C1);
  35. }
  36. else
  37. {
  38. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_I2C2);
  39. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_I2C2);
  40. }
  41. i2c->SW = 0;
  42. i2c->SW = 1;
  43. i2c->INTEN = 0;
  44. }
  45. /*!
  46. * @brief Config the I2C peripheral according to the specified parameters in the sci2cConfig
  47. *
  48. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  49. *
  50. * @param sci2cConfig: pointer to a SCI2C_Config_T structure
  51. *
  52. * @retval None
  53. */
  54. void SCI2C_Config(SCI2C_T *i2c, SCI2C_Config_T *sci2cConfig)
  55. {
  56. i2c->SW = BIT_SET;
  57. i2c->CTRL2_B.I2CEN = BIT_RESET;
  58. if(sci2cConfig->mode == SCI2C_MODE_MASTER)
  59. {
  60. i2c->CTRL1_B.MST = BIT_SET;
  61. i2c->CTRL1_B.SLADIS = BIT_SET;
  62. }
  63. else
  64. {
  65. i2c->CTRL1_B.MST = BIT_RESET;
  66. }
  67. i2c->CTRL1_B.SPD = sci2cConfig->speed;
  68. i2c->CTRL1_B.RSTAEN = sci2cConfig->restart;
  69. i2c->TFT = sci2cConfig->txFifoThreshold;
  70. i2c->RFT = sci2cConfig->rxFifoThreshold;
  71. i2c->TARADDR_B.MAM = sci2cConfig->addrMode;
  72. i2c->CTRL1_B.SAM = sci2cConfig->addrMode;
  73. i2c->SLAADDR = sci2cConfig->slaveAddr;
  74. if(sci2cConfig->speed == SCI2C_SPEED_STANDARD)
  75. {
  76. i2c->SSCLC = sci2cConfig->clkLowPeriod;
  77. i2c->SSCHC = sci2cConfig->clkHighPeriod;
  78. }
  79. else if(sci2cConfig->speed == SCI2C_SPEED_FAST)
  80. {
  81. i2c->FSCLC = sci2cConfig->clkLowPeriod;
  82. i2c->FSCHC = sci2cConfig->clkHighPeriod;
  83. }
  84. else if(sci2cConfig->speed == SCI2C_SPEED_HIGH)
  85. {
  86. i2c->HSCLC = sci2cConfig->clkLowPeriod;
  87. i2c->HSCHC = sci2cConfig->clkHighPeriod;
  88. }
  89. }
  90. /*!
  91. * @brief Fills each sci2cConfig member with its default value
  92. *
  93. * @param sci2cConfig: pointer to a SCI2C_Config_T structure
  94. *
  95. * @retval None
  96. */
  97. void SCI2C_ConfigStructInit(SCI2C_Config_T *sci2cConfig)
  98. {
  99. sci2cConfig->addrMode = SCI2C_ADDR_MODE_7BIT;
  100. sci2cConfig->slaveAddr = 0x55;
  101. sci2cConfig->clkHighPeriod = 0x3C;
  102. sci2cConfig->clkLowPeriod = 0x82;
  103. sci2cConfig->mode = SCI2C_MODE_MASTER;
  104. sci2cConfig->restart = SCI2C_RESTART_ENABLE;
  105. sci2cConfig->rxFifoThreshold = 0;
  106. sci2cConfig->txFifoThreshold = 0;
  107. sci2cConfig->speed = SCI2C_SPEED_FAST;
  108. }
  109. /*!
  110. * @brief Read specified flag
  111. *
  112. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  113. *
  114. * @param flag: Specifies the flag to be checked
  115. * The parameter can be one of following values:
  116. * @arg SCI2C_FLAG_ACT: Activity flag
  117. * @arg SCI2C_FLAG_TFNF: Tx FIFO not full flag
  118. * @arg SCI2C_FLAG_TFE: TX FIFO empty flag
  119. * @arg SCI2C_FLAG_RFNE: Rx FIFO not empty flag
  120. * @arg SCI2C_FLAG_RFF: Rx FIFO full flag
  121. * @arg SCI2C_FLAG_MA: Master activity flag
  122. * @arg SCI2C_FLAG_SA: Slave activity flag
  123. * @arg SCI2C_FLAG_I2CEN: I2C enable flag
  124. * @arg SCI2C_FLAG_SDWB: Slave disable while busy flag
  125. * @arg SCI2C_FLAG_SRDL: Slave receive data lost flag
  126. *
  127. * @retval The new state of flag (SET or RESET)
  128. */
  129. uint8_t SCI2C_ReadStatusFlag(SCI2C_T *i2c, SCI2C_FLAG_T flag)
  130. {
  131. uint8_t ret = RESET;
  132. if(flag & BIT8)
  133. {
  134. ret = i2c->STS2 & flag ? SET : RESET;
  135. }
  136. else
  137. {
  138. ret = i2c->STS1 & flag ? SET : RESET;
  139. }
  140. return ret;
  141. }
  142. /*!
  143. * @brief Read specified interrupt flag
  144. *
  145. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  146. *
  147. * @param flag: Specifies the interrupt flag to be checked
  148. * The parameter can be one of following values:
  149. * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt flag
  150. * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt flag
  151. * @arg SCI2C_INT_RFF: Rx FIFO full interrupt flag
  152. * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt flag
  153. * @arg SCI2C_INT_TFE: Tx FIFO empty interrupt flag
  154. * @arg SCI2C_INT_RR: Read request interrupt flag
  155. * @arg SCI2C_INT_TA: Tx abort interrupt flag
  156. * @arg SCI2C_INT_RD: Read done interrupt flag
  157. * @arg SCI2C_INT_ACT: Activity interrupt flag
  158. * @arg SCI2C_INT_STPD: Stop detect interrupt flag
  159. * @arg SCI2C_INT_STAD: Start detect interrupt flag
  160. * @arg SCI2C_INT_GC: Gernal call interrupt flag
  161. * @arg SCI2C_INT_RSTAD: Restart detect interrupt flag
  162. * @arg SCI2C_INT_MOH: Master on hold interrupt flag
  163. *
  164. * @retval The new state of flag (SET or RESET)
  165. */
  166. uint8_t SCI2C_ReadIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag)
  167. {
  168. uint8_t ret = RESET;
  169. ret = i2c->INTSTS & flag ? SET : RESET;
  170. return ret;
  171. }
  172. /*!
  173. * @brief Clear specified interrupt flag
  174. *
  175. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  176. *
  177. * @param flag: Specifies the interrupt flag to be checked
  178. * The parameter can be one of following values:
  179. * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt flag
  180. * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt flag
  181. * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt flag
  182. * @arg SCI2C_INT_RR: Read request interrupt flag
  183. * @arg SCI2C_INT_TA: Tx abort interrupt flag
  184. * @arg SCI2C_INT_RD: Read done interrupt flag
  185. * @arg SCI2C_INT_ACT: Activity interrupt flag
  186. * @arg SCI2C_INT_STPD: Stop detect interrupt flag
  187. * @arg SCI2C_INT_STAD: Start detect interrupt flag
  188. * @arg SCI2C_INT_GC: Gernal call interrupt flag
  189. * @arg SCI2C_INT_ALL: All interrupt flag
  190. * @retval The new state of flag (SET or RESET)
  191. */
  192. void SCI2C_ClearIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag)
  193. {
  194. volatile uint32_t dummy = 0;
  195. if(flag == SCI2C_INT_ALL)
  196. {
  197. dummy = i2c->INTCLR;
  198. }
  199. else if(flag == SCI2C_INT_RFU)
  200. {
  201. dummy = i2c->RFUIC;
  202. }
  203. else if(flag == SCI2C_INT_RFO)
  204. {
  205. dummy = i2c->RFOIC;
  206. }
  207. else if(flag == SCI2C_INT_TFO)
  208. {
  209. dummy = i2c->TFOIC;
  210. }
  211. else if(flag == SCI2C_INT_RR)
  212. {
  213. dummy = i2c->RRIC;
  214. }
  215. else if(flag == SCI2C_INT_TA)
  216. {
  217. dummy = i2c->TAIC;
  218. }
  219. else if(flag == SCI2C_INT_RD)
  220. {
  221. dummy = i2c->RDIC;
  222. }
  223. else if(flag == SCI2C_INT_ACT)
  224. {
  225. dummy = i2c->AIC;
  226. }
  227. else if(flag == SCI2C_INT_STPD)
  228. {
  229. dummy = i2c->STPDIC;
  230. }
  231. else if(flag == SCI2C_INT_STAD)
  232. {
  233. dummy = i2c->STADIC;
  234. }
  235. else if(flag == SCI2C_INT_GC)
  236. {
  237. dummy = i2c->GCIC;
  238. }
  239. }
  240. /*!
  241. * @brief Read specified interrupt flag(Raw register)
  242. *
  243. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  244. *
  245. * @param flag: Specifies the interrupt flag to be checked
  246. * The parameter can be one of following values:
  247. * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt flag
  248. * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt flag
  249. * @arg SCI2C_INT_RFF: Rx FIFO full interrupt flag
  250. * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt flag
  251. * @arg SCI2C_INT_TFE: Tx FIFO empty interrupt flag
  252. * @arg SCI2C_INT_RR: Read request interrupt flag
  253. * @arg SCI2C_INT_TA: Tx abort interrupt flag
  254. * @arg SCI2C_INT_RD: Read done interrupt flag
  255. * @arg SCI2C_INT_ACT: Activity interrupt flag
  256. * @arg SCI2C_INT_STPD: Stop detect interrupt flag
  257. * @arg SCI2C_INT_STAD: Start detect interrupt flag
  258. * @arg SCI2C_INT_GC: Gernal call interrupt flag
  259. * @arg SCI2C_INT_RSTAD: Restart detect interrupt flag
  260. * @arg SCI2C_INT_MOH: Master on hold interrupt flag
  261. *
  262. * @retval The new state of flag (SET or RESET)
  263. */
  264. uint8_t SCI2C_ReadRawIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag)
  265. {
  266. uint8_t ret = RESET;
  267. ret = i2c->RIS & flag ? SET : RESET;
  268. return ret;
  269. }
  270. /*!
  271. * @brief Enable the specified interrupts
  272. *
  273. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  274. *
  275. * @param interrupt: Specifies the interrupt sources
  276. * The parameter can be any combination of following values:
  277. * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt
  278. * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt
  279. * @arg SCI2C_INT_RFF: Rx FIFO full interrupt
  280. * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt
  281. * @arg SCI2C_INT_TFE: Tx FIFO empty interrupt
  282. * @arg SCI2C_INT_RR: Read request interrupt
  283. * @arg SCI2C_INT_TA: Tx abort interrupt
  284. * @arg SCI2C_INT_RD: Read done interrupt
  285. * @arg SCI2C_INT_ACT: Activity interrupt
  286. * @arg SCI2C_INT_STPD: Stop detect interrupt
  287. * @arg SCI2C_INT_STAD: Start detect interrupt
  288. * @arg SCI2C_INT_GC: Gernal call interrupt
  289. * @arg SCI2C_INT_RSTAD: Restart detect interrupt
  290. * @arg SCI2C_INT_MOH: Master on hold interrupt
  291. *
  292. * @retval None
  293. */
  294. void SCI2C_EnableInterrupt(SCI2C_T *i2c, uint16_t interrupt)
  295. {
  296. i2c->INTEN |= interrupt;
  297. }
  298. /*!
  299. * @brief Disable the specified interrupts
  300. *
  301. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  302. *
  303. * @param interrupt: Specifies the interrupt sources
  304. * The parameter can be any combination of following values:
  305. * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt
  306. * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt
  307. * @arg SCI2C_INT_RFF: Rx FIFO full interrupt
  308. * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt
  309. * @arg SCI2C_INT_TFE: Tx FIFO empty interrupt
  310. * @arg SCI2C_INT_RR: Read request interrupt
  311. * @arg SCI2C_INT_TA: Tx abort interrupt
  312. * @arg SCI2C_INT_RD: Read done interrupt
  313. * @arg SCI2C_INT_ACT: Activity interrupt
  314. * @arg SCI2C_INT_STPD: Stop detect interrupt
  315. * @arg SCI2C_INT_STAD: Start detect interrupt
  316. * @arg SCI2C_INT_GC: Gernal call interrupt
  317. * @arg SCI2C_INT_RSTAD: Restart detect interrupt
  318. * @arg SCI2C_INT_MOH: Master on hold interrupt
  319. *
  320. * @retval None
  321. */
  322. void SCI2C_DisableInterrupt(SCI2C_T *i2c, uint16_t interrupt)
  323. {
  324. i2c->INTEN &= ~interrupt;
  325. }
  326. /*!
  327. * @brief Enable stop detected only master in activity.
  328. *
  329. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  330. */
  331. void SCI2C_EnableStopDetectMasterActivity(SCI2C_T *i2c)
  332. {
  333. i2c->CTRL1_B.DSMA = BIT_SET;
  334. }
  335. /*!
  336. * @brief Disable stop detected only master in activity.
  337. *
  338. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  339. */
  340. void SCI2C_DisableStopDetectMasterActivity(SCI2C_T *i2c)
  341. {
  342. i2c->CTRL1_B.DSMA = BIT_RESET;
  343. }
  344. /*!
  345. * @brief Enable stop detected only address is matched in slave mode.
  346. *
  347. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  348. */
  349. void SCI2C_EnableStopDetectAddressed(SCI2C_T *i2c)
  350. {
  351. i2c->CTRL1_B.DSA = BIT_SET;
  352. }
  353. /*!
  354. * @brief Disable stop detected only address is matched in slave mode.
  355. *
  356. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  357. */
  358. void SCI2C_DisableStopDetectAddressed(SCI2C_T *i2c)
  359. {
  360. i2c->CTRL1_B.DSA = BIT_RESET;
  361. }
  362. /*!
  363. * @brief Enable restart
  364. *
  365. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  366. *
  367. * @retval None
  368. */
  369. void SCI2C_EnableRestart(SCI2C_T *i2c)
  370. {
  371. i2c->CTRL1_B.RSTAEN = BIT_SET;
  372. }
  373. /*!
  374. * @brief Disable restart
  375. *
  376. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  377. *
  378. * @retval None
  379. */
  380. void SCI2C_DisableRestart(SCI2C_T *i2c)
  381. {
  382. i2c->CTRL1_B.RSTAEN = BIT_RESET;
  383. }
  384. /*!
  385. * @brief Config speed.
  386. *
  387. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  388. *
  389. * @param speed: Specifies the speed.
  390. * @arg SCI2C_SPEED_STANDARD: Standard speed.
  391. * @arg SCI2C_SPEED_FAST: Fast speed.
  392. * @arg SCI2C_SPEED_HIGH: High speed.
  393. *
  394. * @retval None
  395. */
  396. void SCI2C_ConfigSpeed(SCI2C_T *i2c, SCI2C_SPEED_T speed)
  397. {
  398. i2c->CTRL1_B.SPD = speed;
  399. }
  400. /*!
  401. * @brief Config master address.
  402. *
  403. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  404. *
  405. * @param mode: Specifies the address mode.
  406. * @arg SCI2C_ADDR_MODE_7BIT: 7-bit address mode.
  407. * @arg SCI2C_ADDR_MODE_10BIT: 10-bit address mode.
  408. *
  409. * @param addr: Specifies the address.
  410. * @retval None
  411. */
  412. void SCI2C_ConfigMasterAddr(SCI2C_T *i2c, SCI2C_ADDR_MODE_T mode, uint16_t addr)
  413. {
  414. i2c->TARADDR_B.MAM = mode;
  415. i2c->TARADDR_B.ADDR = addr;
  416. }
  417. /*!
  418. * @brief Config slave address.
  419. *
  420. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  421. *
  422. * @param mode: Specifies the address mode.
  423. * @arg SCI2C_ADDR_MODE_7BIT: 7-bit address mode.
  424. * @arg SCI2C_ADDR_MODE_10BIT: 10-bit address mode.
  425. *
  426. * @param addr: Specifies the address.
  427. * @retval None
  428. */
  429. void SCI2C_ConfigSlaveAddr(SCI2C_T *i2c, SCI2C_ADDR_MODE_T mode, uint16_t addr)
  430. {
  431. i2c->CTRL1_B.SAM = mode;
  432. i2c->SLAADDR = addr;
  433. }
  434. /*!
  435. * @brief Enable master mode
  436. *
  437. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  438. *
  439. * @retval None
  440. */
  441. void SCI2C_EnableMasterMode(SCI2C_T *i2c)
  442. {
  443. i2c->CTRL1_B.MST = BIT_SET;
  444. }
  445. /*!
  446. * @brief Disable master mode
  447. *
  448. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  449. *
  450. * @retval None
  451. */
  452. void SCI2C_DisableMasterMode(SCI2C_T *i2c)
  453. {
  454. i2c->CTRL1_B.MST = BIT_RESET;
  455. }
  456. /*!
  457. * @brief Enable slave mode
  458. *
  459. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  460. *
  461. * @retval None
  462. */
  463. void SCI2C_EnableSlaveMode(SCI2C_T *i2c)
  464. {
  465. i2c->CTRL1_B.SLADIS = BIT_RESET;
  466. }
  467. /*!
  468. * @brief Disable slave mode
  469. *
  470. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  471. *
  472. * @retval None
  473. */
  474. void SCI2C_DisableSlaveMode(SCI2C_T *i2c)
  475. {
  476. i2c->CTRL1_B.SLADIS = BIT_SET;
  477. }
  478. /*!
  479. * @brief Config master code
  480. *
  481. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  482. *
  483. * @param code: Master code
  484. *
  485. * @retval None
  486. */
  487. void SCI2C_ConfigMasterCode(SCI2C_T *i2c, uint8_t code)
  488. {
  489. i2c->HSMC = code;
  490. }
  491. /*!
  492. * @brief Config data direction
  493. *
  494. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  495. *
  496. * @param dir: Data direction
  497. * @arg SCI2C_DATA_DIR_WRITE: Write data
  498. * @arg SCI2C_DATA_DIR_READ: Read data
  499. *
  500. * @retval None
  501. */
  502. void SCI2C_ConfigDataDir(SCI2C_T *i2c, SCI2C_DATA_DIR_T dir)
  503. {
  504. i2c->DATA = (uint32_t)(dir << 8);
  505. }
  506. /*!
  507. * @brief Transmit data
  508. *
  509. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  510. *
  511. * @param data: Data to be transmited
  512. *
  513. * @retval None
  514. */
  515. void SCI2C_TxData(SCI2C_T *i2c, uint8_t data)
  516. {
  517. i2c->DATA_B.DATA = data;
  518. }
  519. /*!
  520. * @brief Returns the most recent received data
  521. *
  522. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  523. *
  524. * @retval Received data
  525. *
  526. * @note
  527. */
  528. uint8_t SCI2C_RxData(SCI2C_T *i2c)
  529. {
  530. return (uint8_t)(i2c->DATA & 0XFF);
  531. }
  532. /*!
  533. * @brief Config data register
  534. *
  535. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  536. *
  537. * @param stop: Enable or disable generate stop condition
  538. *
  539. * @param dataDir: Data direction. Read or write
  540. * @arg SCI2C_DATA_DIR_WRITE: Write data
  541. * @arg SCI2C_DATA_DIR_READ: Read data
  542. *
  543. * @param data: Data to be transmited
  544. *
  545. * @retval None
  546. */
  547. void SCI2C_ConfigDataRegister(SCI2C_T *i2c, SCI2C_STOP_T stop, SCI2C_DATA_DIR_T dataDir, uint8_t data)
  548. {
  549. i2c->DATA = (uint32_t)((stop << 9) | (dataDir << 8) | data);
  550. }
  551. /*!
  552. * @brief Read Rx FIFO data number
  553. *
  554. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  555. *
  556. * @retval None
  557. */
  558. uint8_t SCI2C_ReadRxFifoDataCnt(SCI2C_T *i2c)
  559. {
  560. return (uint8_t)i2c->RFL;
  561. }
  562. /*!
  563. * @brief Read Tx FIFO data number
  564. *
  565. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  566. *
  567. * @retval None
  568. */
  569. uint8_t SCI2C_ReadTxFifoDataCnt(SCI2C_T *i2c)
  570. {
  571. return (uint8_t)i2c->TFL;
  572. }
  573. /*!
  574. * @brief Config Rx FIFO threshold
  575. *
  576. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  577. *
  578. * @param threshold: FIFO threshold
  579. *
  580. * @retval None
  581. */
  582. void SCI2C_ConfigRxFifoThreshold(SCI2C_T *i2c, uint8_t threshold)
  583. {
  584. i2c->RFT = threshold;
  585. }
  586. /*!
  587. * @brief Config Tx FIFO threshold
  588. *
  589. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  590. *
  591. * @param threshold: FIFO threshold
  592. *
  593. * @retval None
  594. */
  595. void SCI2C_ConfigTxFifoThreshold(SCI2C_T *i2c, uint8_t threshold)
  596. {
  597. i2c->TFT = threshold;
  598. }
  599. /*!
  600. * @brief Enable I2C peripheral
  601. *
  602. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  603. *
  604. * @retval None
  605. */
  606. void SCI2C_Enable(SCI2C_T *i2c)
  607. {
  608. i2c->CTRL2_B.I2CEN = BIT_SET;
  609. }
  610. /*!
  611. * @brief Disable I2C peripheral
  612. *
  613. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  614. *
  615. * @retval None
  616. */
  617. void SCI2C_Disable(SCI2C_T *i2c)
  618. {
  619. i2c->CTRL2_B.I2CEN = BIT_RESET;
  620. }
  621. /*!
  622. * @brief Abort I2C transmit
  623. *
  624. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  625. *
  626. * @retval None
  627. */
  628. void SCI2C_Abort(SCI2C_T *i2c)
  629. {
  630. i2c->CTRL2_B.ABR = BIT_SET;
  631. }
  632. /*!
  633. * @brief Tx command block
  634. *
  635. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  636. *
  637. * @param enable: ENABLE or DISABLE
  638. *
  639. * @retval None
  640. */
  641. void SCI2C_BlockTxCmd(SCI2C_T *i2c, uint8_t enable)
  642. {
  643. i2c->CTRL2_B.TCB = enable;
  644. }
  645. /*!
  646. * @brief Config SCL high and low period
  647. *
  648. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  649. *
  650. * @param speed: Specifies the speed.
  651. * @arg SCI2C_SPEED_STANDARD: Standard speed.
  652. * @arg SCI2C_SPEED_FAST: Fast speed.
  653. * @arg SCI2C_SPEED_HIGH: High speed.
  654. *
  655. * @param highPeriod: SCL high period
  656. *
  657. * @param lowPeriod: SCL low period
  658. *
  659. * @retval None
  660. */
  661. void SCI2C_ConfigClkPeriod(SCI2C_T *i2c, SCI2C_SPEED_T speed, uint16_t highPeriod, uint16_t lowPeriod)
  662. {
  663. if(speed == SCI2C_SPEED_STANDARD)
  664. {
  665. i2c->SSCLC = lowPeriod;
  666. i2c->SSCHC = highPeriod;
  667. }
  668. else if(speed == SCI2C_SPEED_FAST)
  669. {
  670. i2c->FSCLC = lowPeriod;
  671. i2c->FSCHC = highPeriod;
  672. }
  673. else if(speed == SCI2C_SPEED_HIGH)
  674. {
  675. i2c->HSCLC = lowPeriod;
  676. i2c->HSCHC = highPeriod;
  677. }
  678. }
  679. /*!
  680. * @brief Config SDA hold time length
  681. *
  682. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  683. *
  684. * @param txHold: Tx SDA hold time length
  685. *
  686. * @param rxHold: Rx SDA hold time length
  687. *
  688. * @retval None
  689. */
  690. void SCI2C_ConfigSDAHoldTime(SCI2C_T *i2c, uint16_t txHold, uint8_t rxHold)
  691. {
  692. i2c->SDAHOLD_B.TXHOLD = txHold;
  693. i2c->SDAHOLD_B.RXHOLD = rxHold;
  694. }
  695. /*!
  696. * @brief Config SDA delay time
  697. *
  698. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  699. *
  700. * @param delay: SDA delay time
  701. *
  702. * @retval None
  703. */
  704. void SCI2C_ConfigSDADelayTime(SCI2C_T *i2c, uint8_t delay)
  705. {
  706. i2c->SDADLY = delay;
  707. }
  708. /*!
  709. * @brief Enable or disable generate gernal call ack
  710. *
  711. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  712. *
  713. * @param enable: SDA delay time
  714. *
  715. * @retval None
  716. */
  717. void SCI2C_GernalCallAck(SCI2C_T *i2c, uint8_t enable)
  718. {
  719. i2c->GCA = enable;
  720. }
  721. /*!
  722. * @brief When received data no ack generated in slave mode.
  723. *
  724. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  725. *
  726. * @param enable: ENABLE or DISABLE
  727. *
  728. * @retval None
  729. */
  730. void SCI2C_SlaveDataNackOnly(SCI2C_T *i2c, uint8_t enable)
  731. {
  732. i2c->SDNO = enable;
  733. }
  734. /*!
  735. * @brief Read Tx abort source
  736. *
  737. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  738. *
  739. * @retval Return Tx abort source
  740. */
  741. uint32_t SCI2C_ReadTxAbortSource(SCI2C_T *i2c)
  742. {
  743. return (uint32_t)i2c->TAS;
  744. }
  745. /*!
  746. * @brief Enable DMA
  747. *
  748. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  749. *
  750. * @param dma: DMA requst source
  751. * @arg SCI2C_DMA_RX: DMA RX channel
  752. * @arg SCI2C_DMA_TX: DMA TX channel
  753. *
  754. * @retval None
  755. */
  756. void SCI2C_EnableDMA(SCI2C_T *i2c, SCI2C_DMA_T dma)
  757. {
  758. i2c->DMACTRL |= dma;
  759. }
  760. /*!
  761. * @brief Disable DMA
  762. *
  763. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  764. *
  765. * @param dma: DMA requst source
  766. * @arg SCI2C_DMA_RX: DMA RX channel
  767. * @arg SCI2C_DMA_TX: DMA TX channel
  768. *
  769. * @retval None
  770. */
  771. void SCI2C_DisableDMA(SCI2C_T *i2c, SCI2C_DMA_T dma)
  772. {
  773. i2c->DMACTRL &= (uint32_t)~dma;
  774. }
  775. /*!
  776. * @brief Config DMA Tx data level
  777. *
  778. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  779. *
  780. * @param cnt: DMA Tx data level
  781. *
  782. * @retval None
  783. */
  784. void SCI2C_ConfigDMATxDataLevel(SCI2C_T *i2c, uint8_t cnt)
  785. {
  786. i2c->DTDL = cnt;
  787. }
  788. /*!
  789. * @brief Config DMA Rx data level
  790. *
  791. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  792. *
  793. * @param cnt: DMA Rx data level
  794. *
  795. * @retval None
  796. */
  797. void SCI2C_ConfigDMARxDataLevel(SCI2C_T *i2c, uint8_t cnt)
  798. {
  799. i2c->DRDL = cnt;
  800. }
  801. /*!
  802. * @brief Config spike suppressio limit
  803. *
  804. * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
  805. *
  806. * @param speed: I2C speed mode
  807. * @arg SCI2C_SPEED_STANDARD: Standard speed.
  808. * @arg SCI2C_SPEED_FAST: Fast speed.
  809. * @arg SCI2C_SPEED_HIGH: High speed.
  810. *
  811. * @param limit: Spike suppressio limit value
  812. *
  813. * @retval None
  814. */
  815. void SCI2C_ConfigSpikeSuppressionLimit(SCI2C_T *i2c, SCI2C_SPEED_T speed, uint8_t limit)
  816. {
  817. if(speed == SCI2C_SPEED_HIGH)
  818. {
  819. i2c->HSSSL = limit;
  820. }
  821. else
  822. {
  823. i2c->LSSSL = limit;
  824. }
  825. }
  826. /**@} end of group SCI2C_Fuctions*/
  827. /**@} end of group SCI2C_Driver*/
  828. /**@} end of group Peripherals_Library*/