gd32f30x_i2c.c 25 KB

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