gd32f30x_i2c.c 25 KB

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