gd32f10x_i2c.c 24 KB

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