gd32e23x_i2c.c 28 KB

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