gd32vf103_i2c.c 26 KB

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