gd32f10x_i2c.c 24 KB

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