gd32f1x0_i2c.c 28 KB

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