gd32f3x0_i2c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*!
  2. \file gd32f3x0_i2c.c
  3. \brief I2C driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0_i2c.h"
  10. #define I2CCLK_MAX 0x7fU /*!< i2cclk max value */
  11. #define I2C_FLAG_MASK 0x0000FFFFU /*!< i2c flag mask */
  12. /*!
  13. \brief reset I2C
  14. \param[in] i2c_periph: I2Cx(x=0,1,2)
  15. \param[out] none
  16. \retval none
  17. */
  18. void i2c_deinit(uint32_t i2c_periph)
  19. {
  20. switch(i2c_periph){
  21. case I2C0:
  22. /* reset I2C0 */
  23. rcu_periph_reset_enable(RCU_I2C0RST);
  24. rcu_periph_reset_disable(RCU_I2C0RST);
  25. break;
  26. case I2C1:
  27. /* reset I2C1 */
  28. rcu_periph_reset_enable(RCU_I2C1RST);
  29. rcu_periph_reset_disable(RCU_I2C1RST);
  30. break;
  31. case I2C2:
  32. /* reset I2C2 */
  33. rcu_periph_reset_enable(RCU_I2C2RST);
  34. rcu_periph_reset_disable(RCU_I2C2RST);
  35. break;
  36. default:
  37. break;
  38. }
  39. }
  40. /*!
  41. \brief configure I2C clock
  42. \param[in] i2c_periph: I2Cx(x=0,1,2)
  43. \param[in] clkspeed: I2C clock speed, supports standard mode (up to 100 kHz), fast mode (up to 400 kHz)
  44. and fast mode plus (up to 1MHz)
  45. \param[in] dutycyc: duty cycle in fast mode or fast mode plus
  46. \arg I2C_DTCY_2: T_low/T_high=2
  47. \arg I2C_DTCY_16_9: T_low/T_high=16/9
  48. \param[out] none
  49. \retval none
  50. */
  51. void i2c_clock_config(uint32_t i2c_periph, uint32_t clkspeed, uint32_t dutycyc)
  52. {
  53. uint32_t pclk1,clkc,freq,risetime;
  54. uint32_t temp;
  55. pclk1 = rcu_clock_freq_get(CK_APB1);
  56. /* I2C peripheral clock frequency */
  57. freq = (uint32_t)(pclk1/1000000U);
  58. if(freq >= I2CCLK_MAX){
  59. freq = I2CCLK_MAX;
  60. }
  61. temp = I2C_CTL1(i2c_periph);
  62. temp &= ~I2C_CTL1_I2CCLK;
  63. temp |= freq;
  64. I2C_CTL1(i2c_periph) = temp;
  65. if(100000U >= clkspeed){
  66. /* the maximum SCL rise time is 1000ns in standard mode */
  67. risetime = (uint32_t)((pclk1/1000000U)+1U);
  68. if(risetime >= I2CCLK_MAX){
  69. I2C_RT(i2c_periph) = I2CCLK_MAX;
  70. }else{
  71. I2C_RT(i2c_periph) = risetime;
  72. }
  73. clkc = (uint32_t)(pclk1/(clkspeed*2U));
  74. if(clkc < 0x04U){
  75. /* the CLKC in standard mode minmum value is 4 */
  76. clkc = 0x04U;
  77. }
  78. I2C_CKCFG(i2c_periph) |= (I2C_CKCFG_CLKC & clkc);
  79. }else if(400000U >= clkspeed){
  80. /* the maximum SCL rise time is 300ns in fast mode */
  81. I2C_RT(i2c_periph) = (uint32_t)(((freq*(uint32_t)300U)/(uint32_t)1000U)+(uint32_t)1U);
  82. if(I2C_DTCY_2 == dutycyc){
  83. /* I2C duty cycle is 2 */
  84. clkc = (uint32_t)(pclk1/(clkspeed*3U));
  85. I2C_CKCFG(i2c_periph) &= ~I2C_CKCFG_DTCY;
  86. }else{
  87. /* I2C duty cycle is 16/9 */
  88. clkc = (uint32_t)(pclk1/(clkspeed*25U));
  89. I2C_CKCFG(i2c_periph) |= I2C_CKCFG_DTCY;
  90. }
  91. if(0U == (clkc & I2C_CKCFG_CLKC)){
  92. /* the CLKC in fast mode minmum value is 1 */
  93. clkc |= 0x0001U;
  94. }
  95. I2C_CKCFG(i2c_periph) |= I2C_CKCFG_FAST;
  96. I2C_CKCFG(i2c_periph) |= clkc;
  97. }else{
  98. /* fast mode plus, the maximum SCL rise time is 120ns */
  99. I2C_RT(i2c_periph) = (uint32_t)(((freq*(uint32_t)120U)/(uint32_t)1000U)+(uint32_t)1U);
  100. if(I2C_DTCY_2 == dutycyc){
  101. /* I2C duty cycle is 2 */
  102. clkc = (uint32_t)(pclk1/(clkspeed*3U));
  103. I2C_CKCFG(i2c_periph) &= ~I2C_CKCFG_DTCY;
  104. }else{
  105. /* I2C duty cycle is 16/9 */
  106. clkc = (uint32_t)(pclk1/(clkspeed*25U));
  107. I2C_CKCFG(i2c_periph) |= I2C_CKCFG_DTCY;
  108. }
  109. /* enable fast mode */
  110. I2C_CKCFG(i2c_periph) |= I2C_CKCFG_FAST;
  111. I2C_CKCFG(i2c_periph) |= clkc;
  112. /* enable I2C fast mode plus */
  113. I2C_FMPCFG(i2c_periph) = I2C_FMPCFG_FMPEN;
  114. }
  115. }
  116. /*!
  117. \brief configure I2C address
  118. \param[in] i2c_periph: I2Cx(x=0,1)
  119. \param[in] mode:
  120. \arg I2C_I2CMODE_ENABLE: I2C mode
  121. \arg I2C_SMBUSMODE_ENABLE: SMBus mode
  122. \param[in] addformat: 7bits or 10bits
  123. \arg I2C_ADDFORMAT_7BITS: 7bits
  124. \arg I2C_ADDFORMAT_10BITS: 10bits
  125. \param[in] addr: I2C address
  126. \param[out] none
  127. \retval none
  128. */
  129. void i2c_mode_addr_config(uint32_t i2c_periph, uint32_t mode, uint32_t addformat, uint32_t addr)
  130. {
  131. /* SMBus/I2C mode selected */
  132. uint32_t ctl = 0U;
  133. ctl = I2C_CTL0(i2c_periph);
  134. ctl &= ~(I2C_CTL0_SMBEN);
  135. ctl |= mode;
  136. I2C_CTL0(i2c_periph) = ctl;
  137. /* configure address */
  138. I2C_SADDR0(i2c_periph) = (addformat | addr);
  139. }
  140. /*!
  141. \brief SMBus type selection
  142. \param[in] i2c_periph: I2Cx(x=0,1,2)
  143. \param[in] ack:
  144. \arg I2C_SMBUS_DEVICE: device
  145. \arg I2C_SMBUS_HOST: host
  146. \param[out] none
  147. \retval none
  148. */
  149. void i2c_smbus_type_config(uint32_t i2c_periph, uint32_t type)
  150. {
  151. if(I2C_SMBUS_HOST == type){
  152. I2C_CTL0(i2c_periph) |= I2C_CTL0_SMBSEL;
  153. }else{
  154. I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_SMBSEL);
  155. }
  156. }
  157. /*!
  158. \brief whether or not to send an ACK
  159. \param[in] i2c_periph: I2Cx(x=0,1,2)
  160. \param[in] ack:
  161. \arg I2C_ACK_ENABLE: ACK will be sent
  162. \arg I2C_ACK_DISABLE: ACK will not be sent
  163. \param[out] none
  164. \retval none
  165. */
  166. void i2c_ack_config(uint32_t i2c_periph, uint32_t ack)
  167. {
  168. if(I2C_ACK_ENABLE == ack){
  169. I2C_CTL0(i2c_periph) |= I2C_CTL0_ACKEN;
  170. }else{
  171. I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_ACKEN);
  172. }
  173. }
  174. /*!
  175. \brief I2C POAP position configure
  176. \param[in] i2c_periph: I2Cx(x=0,1,2)
  177. \param[in] pos:
  178. \arg I2C_ACKPOS_CURRENT: whether to send ACK or not for the current
  179. \arg I2C_ACKPOS_NEXT: whether to send ACK or not for the next byte
  180. \param[out] none
  181. \retval none
  182. */
  183. void i2c_ackpos_config(uint32_t i2c_periph, uint32_t pos)
  184. {
  185. /* configure I2C POAP position */
  186. if(I2C_ACKPOS_NEXT == pos){
  187. I2C_CTL0(i2c_periph) |= I2C_CTL0_POAP;
  188. }else{
  189. I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_POAP);
  190. }
  191. }
  192. /*!
  193. \brief master send slave address
  194. \param[in] i2c_periph: I2Cx(x=0,1,2)
  195. \param[in] addr: slave address
  196. \param[in] trandirection: transmitter or receiver
  197. \arg I2C_TRANSMITTER: transmitter
  198. \arg I2C_RECEIVER: receiver
  199. \param[out] none
  200. \retval none
  201. */
  202. void i2c_master_addressing(uint32_t i2c_periph, uint32_t addr, uint32_t trandirection)
  203. {
  204. if(I2C_TRANSMITTER == trandirection){
  205. addr = addr & I2C_TRANSMITTER;
  206. }else{
  207. addr = addr | I2C_RECEIVER;
  208. }
  209. I2C_DATA(i2c_periph) = addr;
  210. }
  211. /*!
  212. \brief dual-address mode switch
  213. \param[in] i2c_periph: I2Cx(x=0,1,2)
  214. \param[in] dualaddr:
  215. \arg I2C_DUADEN_DISABLE: disable dual-address mode
  216. \arg I2C_DUADEN_ENABLE: enable dual-address mode
  217. \param[out] none
  218. \retval none
  219. */
  220. void i2c_dualaddr_enable(uint32_t i2c_periph, uint32_t dualaddr)
  221. {
  222. if(I2C_DUADEN_ENABLE == dualaddr){
  223. I2C_SADDR1(i2c_periph) |= I2C_SADDR1_DUADEN;
  224. }else{
  225. I2C_SADDR1(i2c_periph) &= ~(I2C_SADDR1_DUADEN);
  226. }
  227. }
  228. /*!
  229. \brief enable I2C
  230. \param[in] i2c_periph: I2Cx(x=0,1,2)
  231. \param[out] none
  232. \retval none
  233. */
  234. void i2c_enable(uint32_t i2c_periph)
  235. {
  236. I2C_CTL0(i2c_periph) |= I2C_CTL0_I2CEN;
  237. }
  238. /*!
  239. \brief disable I2C
  240. \param[in] i2c_periph: I2Cx(x=0,1,2)
  241. \param[out] none
  242. \retval none
  243. */
  244. void i2c_disable(uint32_t i2c_periph)
  245. {
  246. I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_I2CEN);
  247. }
  248. /*!
  249. \brief generate a START condition on I2C bus
  250. \param[in] i2c_periph: I2Cx(x=0,1,2)
  251. \param[out] none
  252. \retval none
  253. */
  254. void i2c_start_on_bus(uint32_t i2c_periph)
  255. {
  256. I2C_CTL0(i2c_periph) |= I2C_CTL0_START;
  257. }
  258. /*!
  259. \brief generate a STOP condition on I2C bus
  260. \param[in] i2c_periph: I2Cx(x=0,1,2)
  261. \param[out] none
  262. \retval none
  263. */
  264. void i2c_stop_on_bus(uint32_t i2c_periph)
  265. {
  266. I2C_CTL0(i2c_periph) |= I2C_CTL0_STOP;
  267. }
  268. /*!
  269. \brief I2C transmit data function
  270. \param[in] i2c_periph: I2Cx(x=0,1,2)
  271. \param[in] data: data of transmission
  272. \param[out] none
  273. \retval none
  274. */
  275. void i2c_data_transmit(uint32_t i2c_periph, uint8_t data)
  276. {
  277. I2C_DATA(i2c_periph) = DATA_TRANS(data);
  278. }
  279. /*!
  280. \brief I2C receive data function
  281. \param[in] i2c_periph: I2Cx(x=0,1,2)
  282. \param[out] none
  283. \retval data of received
  284. */
  285. uint8_t i2c_data_receive(uint32_t i2c_periph)
  286. {
  287. return (uint8_t)DATA_RECV(I2C_DATA(i2c_periph));
  288. }
  289. /*!
  290. \brief enable I2C DMA mode
  291. \param[in] i2c_periph: I2Cx(x=0,1,2)
  292. \param[in] dmastate:
  293. \arg I2C_DMA_ON: DMA mode enable
  294. \arg I2C_DMA_OFF: DMA mode disable
  295. \param[out] none
  296. \retval none
  297. */
  298. void i2c_dma_enable(uint32_t i2c_periph, uint32_t dmastate)
  299. {
  300. /* configure I2C DMA function */
  301. uint32_t ctl = 0U;
  302. ctl = I2C_CTL1(i2c_periph);
  303. ctl &= ~(I2C_CTL1_DMAON);
  304. ctl |= dmastate;
  305. I2C_CTL1(i2c_periph) = ctl;
  306. }
  307. /*!
  308. \brief flag indicating DMA last transfer
  309. \param[in] i2c_periph: I2Cx(x=0,1,2)
  310. \param[in] dmalast:
  311. \arg I2C_DMALST_ON: next DMA EOT is the last transfer
  312. \arg I2C_DMALST_OFF: next DMA EOT is not the last transfer
  313. \param[out] none
  314. \retval none
  315. */
  316. void i2c_dma_last_transfer_enable(uint32_t i2c_periph, uint32_t dmalast)
  317. {
  318. /* configure DMA last transfer */
  319. uint32_t ctl = 0U;
  320. ctl = I2C_CTL1(i2c_periph);
  321. ctl &= ~(I2C_CTL1_DMALST);
  322. ctl |= dmalast;
  323. I2C_CTL1(i2c_periph) = ctl;
  324. }
  325. /*!
  326. \brief whether to stretch SCL low when data is not ready in slave mode
  327. \param[in] i2c_periph: I2Cx(x=0,1,2)
  328. \param[in] stretchpara:
  329. \arg I2C_SCLSTRETCH_ENABLE: SCL stretching is enabled
  330. \arg I2C_SCLSTRETCH_DISABLE: SCL stretching is disabled
  331. \param[out] none
  332. \retval none
  333. */
  334. void i2c_stretch_scl_low_config(uint32_t i2c_periph, uint32_t stretchpara)
  335. {
  336. /* configure I2C SCL strerching enable or disable */
  337. uint32_t ctl = 0U;
  338. ctl = I2C_CTL0(i2c_periph);
  339. ctl &= ~(I2C_CTL0_DISSTRC);
  340. ctl |= stretchpara;
  341. I2C_CTL0(i2c_periph) = ctl;
  342. }
  343. /*!
  344. \brief whether or not to response to a general call
  345. \param[in] i2c_periph: I2Cx(x=0,1,2)
  346. \param[in] gcallpara:
  347. \arg I2C_GCEN_ENABLE: slave will response to a general call
  348. \arg I2C_GCEN_DISABLE: slave will not response to a general call
  349. \param[out] none
  350. \retval none
  351. */
  352. void i2c_slave_response_to_gcall_config(uint32_t i2c_periph, uint32_t gcallpara)
  353. {
  354. /* configure slave response to a general call enable or disable */
  355. uint32_t ctl = 0U;
  356. ctl = I2C_CTL0(i2c_periph);
  357. ctl &= ~(I2C_CTL0_GCEN);
  358. ctl |= gcallpara;
  359. I2C_CTL0(i2c_periph) = ctl;
  360. }
  361. /*!
  362. \brief software reset I2C
  363. \param[in] i2c_periph: I2Cx(x=0,1,2)
  364. \param[in] sreset:
  365. \arg I2C_SRESET_SET: I2C is under reset
  366. \arg I2C_SRESET_RESET: I2C is not under reset
  367. \param[out] none
  368. \retval none
  369. */
  370. void i2c_software_reset_config(uint32_t i2c_periph, uint32_t sreset)
  371. {
  372. /* modify CTL0 and configure software reset I2C state */
  373. uint32_t ctl = 0U;
  374. ctl = I2C_CTL0(i2c_periph);
  375. ctl &= ~(I2C_CTL0_SRESET);
  376. ctl |= sreset;
  377. I2C_CTL0(i2c_periph) = ctl;
  378. }
  379. /*!
  380. \brief check I2C flag is set or not
  381. \param[in] i2c_periph: I2Cx(x=0,1,2)
  382. \param[in] flag:
  383. \arg I2C_FLAG_SBSEND: start condition send out
  384. \arg I2C_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode
  385. \arg I2C_FLAG_BTC: byte transmission finishes
  386. \arg I2C_FLAG_ADD10SEND: header of 10-bit address is sent in master mode
  387. \arg I2C_FLAG_STPDET: stop condition detected in slave mode
  388. \arg I2C_FLAG_RBNE: I2C_DATA is not Empty during receiving
  389. \arg I2C_FLAG_TBE: I2C_DATA is empty during transmitting
  390. \arg I2C_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus
  391. \arg I2C_FLAG_LOSTARB: arbitration lost in master mode
  392. \arg I2C_FLAG_AERR: acknowledge error
  393. \arg I2C_FLAG_OUERR: overrun or underrun situation occurs in slave mode
  394. \arg I2C_FLAG_PECERR: PEC error when receiving data
  395. \arg I2C_FLAG_SMBTO: timeout signal in SMBus mode
  396. \arg I2C_FLAG_SMBALT: SMBus alert status
  397. \arg I2C_FLAG_MASTER: a flag indicating whether I2C block is in master or slave mode
  398. \arg I2C_FLAG_I2CBSY: busy flag
  399. \arg I2C_FLAG_TRS: whether the I2C is a transmitter or a receiver
  400. \arg I2C_FLAG_RXGC: general call address (00h) received
  401. \arg I2C_FLAG_DEFSMB: default address of SMBus device
  402. \arg I2C_FLAG_HSTSMB: SMBus host header detected in slave mode
  403. \arg I2C_FLAG_DUMODF: dual flag in slave mode indicating which address is matched in dual-address mode
  404. \param[out] none
  405. \retval FlagStatus: SET or RESET
  406. */
  407. FlagStatus i2c_flag_get(uint32_t i2c_periph, uint32_t flag)
  408. {
  409. uint32_t reg = 0U;
  410. FlagStatus reval = RESET;
  411. /* get the flag in which register */
  412. reg = (BIT(31) & flag);
  413. if((BIT(31) == reg)){
  414. if((I2C_STAT1(i2c_periph)&(flag & I2C_FLAG_MASK))){
  415. reval = SET;
  416. }else{
  417. reval = RESET;
  418. }
  419. }else{
  420. if((I2C_STAT0(i2c_periph)&(flag & I2C_FLAG_MASK))){
  421. reval = SET;
  422. }else{
  423. reval = RESET;
  424. }
  425. }
  426. /* return the flag status */
  427. return reval;
  428. }
  429. /*!
  430. \brief clear I2C flag
  431. \param[in] i2c_periph: I2Cx(x=0,1,2)
  432. \param[in] flag: flag type
  433. \arg I2C_FLAG_SMBALT: SMBus Alert status
  434. \arg I2C_FLAG_SMBTO: timeout signal in SMBus mode
  435. \arg I2C_FLAG_PECERR: PEC error when receiving data
  436. \arg I2C_FLAG_OUERR: over-run or under-run situation occurs in slave mode
  437. \arg I2C_FLAG_AERR: acknowledge error
  438. \arg I2C_FLAG_LOSTARB: arbitration lost in master mode
  439. \arg I2C_FLAG_BERR: a bus error
  440. \arg I2C_FLAG_ADDSEND: cleared by reading I2C_STAT0 and reading I2C_STAT1
  441. \param[out] none
  442. \retval none
  443. */
  444. void i2c_flag_clear(uint32_t i2c_periph, uint32_t flag)
  445. {
  446. if(I2C_FLAG_ADDSEND == flag){
  447. /* read I2C_STAT0 and then read I2C_STAT1 to clear ADDSEND */
  448. I2C_STAT0(i2c_periph);
  449. I2C_STAT1(i2c_periph);
  450. }else{
  451. I2C_STAT0(i2c_periph) &= ~(flag);
  452. }
  453. }
  454. /*!
  455. \brief enable I2C interrupt
  456. \param[in] i2c_periph: I2Cx(x=0,1,2)
  457. \param[in] inttype: interrupt type
  458. \arg I2C_INT_ERR: error interrupt enable
  459. \arg I2C_INT_EV: event interrupt enable
  460. \arg I2C_INT_BUF: buffer interrupt enable
  461. \param[out] none
  462. \retval none
  463. */
  464. void i2c_interrupt_enable(uint32_t i2c_periph, uint32_t inttype)
  465. {
  466. I2C_CTL1(i2c_periph) |= (inttype);
  467. }
  468. /*!
  469. \brief disable I2C interrupt
  470. \param[in] i2c_periph: I2Cx(x=0,1,2)
  471. \param[in] inttype: interrupt type
  472. \arg I2C_INT_ERR: error interrupt enable
  473. \arg I2C_INT_EV: event interrupt enable
  474. \arg I2C_INT_BUF: buffer interrupt enable
  475. \param[out] none
  476. \retval none
  477. */
  478. void i2c_interrupt_disable(uint32_t i2c_periph, uint32_t inttype)
  479. {
  480. I2C_CTL1(i2c_periph) &= ~(inttype);
  481. }
  482. /*!
  483. \brief check I2C interrupt flag
  484. \param[in] i2c_periph: I2Cx(x=0,1,2)
  485. \param[in] int_flag: interrupt flag
  486. \arg I2C_INT_FLAG_SBSEND: start condition sent out in master mode interrupt flag
  487. \arg I2C_INT_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode interrupt flag
  488. \arg I2C_INT_FLAG_BTC: byte transmission finishes
  489. \arg I2C_INT_FLAG_ADD10SEND: header of 10-bit address is sent in master mode interrupt flag
  490. \arg I2C_INT_FLAG_STPDET: etop condition detected in slave mode interrupt flag
  491. \arg I2C_INT_FLAG_RBNE: I2C_DATA is not Empty during receiving interrupt flag
  492. \arg I2C_INT_FLAG_TBE: I2C_DATA is empty during transmitting interrupt flag
  493. \arg I2C_INT_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus interrupt flag
  494. \arg I2C_INT_FLAG_LOSTARB: arbitration lost in master mode interrupt flag
  495. \arg I2C_INT_FLAG_AERR: acknowledge error interrupt flag
  496. \arg I2C_INT_FLAG_OUERR: over-run or under-run situation occurs in slave mode interrupt flag
  497. \arg I2C_INT_FLAG_PECERR: PEC error when receiving data interrupt flag
  498. \arg I2C_INT_FLAG_SMBTO: timeout signal in SMBus mode interrupt flag
  499. \arg I2C_INT_FLAG_SMBALT: SMBus Alert status interrupt flag
  500. \param[out] none
  501. \retval none
  502. */
  503. FlagStatus i2c_interrupt_flag_get(uint32_t i2c_periph, uint32_t intflag)
  504. {
  505. uint32_t evie, errie, bufie;
  506. evie = I2C_CTL1(i2c_periph)&I2C_CTL1_EVIE;
  507. errie = I2C_CTL1(i2c_periph)&I2C_CTL1_ERRIE;
  508. /* check I2C event interrupt enable bit */
  509. if((intflag&0x00ffU) && evie){
  510. if(intflag&0x001fU){
  511. /* check I2C event flags except TBE and RBNE */
  512. if(intflag & I2C_STAT0(i2c_periph)){
  513. return SET;
  514. }else{
  515. return RESET;
  516. }
  517. }else{
  518. /* check I2C event flags TBE and RBNE */
  519. bufie = I2C_CTL1(i2c_periph)&I2C_CTL1_BUFIE;
  520. if(bufie){
  521. if(intflag & I2C_STAT0(i2c_periph)){
  522. return SET;
  523. }else{
  524. return RESET;
  525. }
  526. }else{
  527. return RESET;
  528. }
  529. }
  530. /* check I2C error interrupt enable bit */
  531. }else if((intflag&0xff00U) && errie){
  532. /* check I2C error flags */
  533. if(intflag & I2C_STAT0(i2c_periph)){
  534. return SET;
  535. }else{
  536. return RESET;
  537. }
  538. }else{
  539. return RESET;
  540. }
  541. }
  542. /*!
  543. \brief clear I2C interrupt flag
  544. \param[in] i2c_periph: I2Cx(x=0,1,2)
  545. \param[in] intflag: interrupt flag
  546. \arg I2C_INT_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode interrupt flag
  547. \arg I2C_INT_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus interrupt flag
  548. \arg I2C_INT_FLAG_LOSTARB: arbitration lost in master mode interrupt flag
  549. \arg I2C_INT_FLAG_AERR: acknowledge error interrupt flag
  550. \arg I2C_INT_FLAG_OUERR: over-run or under-run situation occurs in slave mode interrupt flag
  551. \arg I2C_INT_FLAG_PECERR: PEC error when receiving data interrupt flag
  552. \arg I2C_INT_FLAG_SMBTO: timeout signal in SMBus mode interrupt flag
  553. \arg I2C_INT_FLAG_SMBALT: SMBus Alert status interrupt flag
  554. \param[out] none
  555. \retval none
  556. */
  557. void i2c_interrupt_flag_clear(uint32_t i2c_periph, uint32_t intflag)
  558. {
  559. if(I2C_INT_FLAG_ADDSEND == intflag){
  560. /* read I2C_STAT0 and then read I2C_STAT1 to clear ADDSEND */
  561. I2C_STAT0(i2c_periph);
  562. I2C_STAT1(i2c_periph);
  563. }else{
  564. I2C_STAT0(i2c_periph) &= ~(intflag);
  565. }
  566. }
  567. /*!
  568. \brief I2C PEC calculation on or off
  569. \param[in] i2c_periph: I2Cx(x=0,1,2)
  570. \param[in] pecpara:
  571. \arg I2C_PEC_ENABLE: PEC calculation on
  572. \arg I2C_PEC_DISABLE: PEC calculation off
  573. \param[out] none
  574. \retval none
  575. */
  576. void i2c_pec_enable(uint32_t i2c_periph, uint32_t pecstate)
  577. {
  578. /* on/off PEC calculation */
  579. uint32_t ctl = 0U;
  580. ctl = I2C_CTL0(i2c_periph);
  581. ctl &= ~(I2C_CTL0_PECEN);
  582. ctl |= pecstate;
  583. I2C_CTL0(i2c_periph) = ctl;
  584. }
  585. /*!
  586. \brief I2C whether to transfer PEC value
  587. \param[in] i2c_periph: I2Cx(x=0,1,2)
  588. \param[in] pecpara:
  589. \arg I2C_PECTRANS_ENABLE: transfer PEC
  590. \arg I2C_PECTRANS_DISABLE: not transfer PEC
  591. \param[out] none
  592. \retval none
  593. */
  594. void i2c_pec_transfer_enable(uint32_t i2c_periph, uint32_t pecpara)
  595. {
  596. /* whether to transfer PEC */
  597. uint32_t ctl = 0U;
  598. ctl = I2C_CTL0(i2c_periph);
  599. ctl &= ~(I2C_CTL0_PECTRANS);
  600. ctl |= pecpara;
  601. I2C_CTL0(i2c_periph) = ctl;
  602. }
  603. /*!
  604. \brief get packet error checking value
  605. \param[in] i2c_periph: I2Cx(x=0,1,2)
  606. \param[out] none
  607. \retval PEC value
  608. */
  609. uint8_t i2c_pec_value_get(uint32_t i2c_periph)
  610. {
  611. return (uint8_t)((I2C_STAT1(i2c_periph) & I2C_STAT1_ECV)>>8);
  612. }
  613. /*!
  614. \brief I2C issue alert through SMBA pin
  615. \param[in] i2c_periph: I2Cx(x=0,1,2)
  616. \param[in] smbuspara:
  617. \arg I2C_SALTSEND_ENABLE: issue alert through SMBA pin
  618. \arg I2C_SALTSEND_DISABLE: not issue alert through SMBA pin
  619. \param[out] none
  620. \retval none
  621. */
  622. void i2c_smbus_issue_alert(uint32_t i2c_periph, uint32_t smbuspara)
  623. {
  624. /* issue alert through SMBA pin configure*/
  625. uint32_t ctl = 0U;
  626. ctl = I2C_CTL0(i2c_periph);
  627. ctl &= ~(I2C_CTL0_SALT);
  628. ctl |= smbuspara;
  629. I2C_CTL0(i2c_periph) = ctl;
  630. }
  631. /*!
  632. \brief enable or disable I2C ARP protocol in SMBus switch
  633. \param[in] i2c_periph: I2Cx(x=0,1,2)
  634. \param[in] smbuspara:
  635. \arg I2C_ARP_ENABLE: enable ARP
  636. \arg I2C_ARP_DISABLE: disable ARP
  637. \param[out] none
  638. \retval none
  639. */
  640. void i2c_smbus_arp_enable(uint32_t i2c_periph, uint32_t arpstate)
  641. {
  642. /* enable or disable I2C ARP protocol*/
  643. uint32_t ctl = 0U;
  644. ctl = I2C_CTL0(i2c_periph);
  645. ctl &= ~(I2C_CTL0_ARPEN);
  646. ctl |= arpstate;
  647. I2C_CTL0(i2c_periph) = ctl;
  648. }