gd32e23x_i2c.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*!
  2. \file gd32e23x_i2c.c
  3. \brief I2C driver
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  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 "gd32e23x_i2c.h"
  31. /* I2C register bit mask */
  32. #define I2CCLK_MAX ((uint32_t)0x0000007FU) /*!< 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)0x00000008U) /* 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 in fast mode or fast mode plus
  70. \arg I2C_DTCY_16_9: T_low/T_high = 16/9 in fast mode or fast mode plus
  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: address format is 7 bits
  151. \arg I2C_ADDFORMAT_10BITS: address format is 10 bits
  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 select SMBus type
  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: SMBus mode device type
  174. \arg I2C_SMBUS_HOST: SMBus mode host type
  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. uint32_t ctl = 0U;
  199. ctl = I2C_CTL0(i2c_periph);
  200. ctl &= ~(I2C_CTL0_ACKEN);
  201. ctl |= ack;
  202. I2C_CTL0(i2c_periph) = ctl;
  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: ACKEN bit decides whether or not to send ACK or not for the current byte
  210. \arg I2C_ACKPOS_NEXT: ACKEN bit decides whether or not to send ACK 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. uint32_t ctl = 0U;
  217. /* configure I2C POAP position */
  218. ctl = I2C_CTL0(i2c_periph);
  219. ctl &= ~(I2C_CTL0_POAP);
  220. ctl |= pos;
  221. I2C_CTL0(i2c_periph) = ctl;
  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] addr: 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 addr)
  253. {
  254. /* configure address */
  255. addr = addr & I2C_ADDRESS2_MASK;
  256. I2C_SADDR1(i2c_periph) = (I2C_SADDR1_DUADEN | addr);
  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 configure 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: enable DMA mode
  335. \arg I2C_DMA_OFF: disable DMA mode
  336. \param[out] none
  337. \retval none
  338. */
  339. void i2c_dma_config(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: enable SCL stretching
  373. \arg I2C_SCLSTRETCH_DISABLE: disable SCL stretching
  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 */
  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 configure software reset of 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 configure I2C PEC calculation
  426. \param[in] i2c_periph: I2Cx(x=0,1)
  427. \param[in] pecstate:
  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_config(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 configure 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 value
  449. \arg I2C_PECTRANS_DISABLE: not transfer PEC value
  450. \param[out] none
  451. \retval none
  452. */
  453. void i2c_pec_transfer_config(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 configure I2C 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_alert_config(uint32_t i2c_periph, uint32_t smbuspara)
  483. {
  484. /* configure smubus alert through SMBA pin */
  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 configure I2C ARP protocol in SMBus
  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_config(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 enable SAM_V interface
  512. \param[in] i2c_periph: I2Cx(x=0,1)
  513. \param[out] none
  514. \retval none
  515. */
  516. void i2c_sam_enable(uint32_t i2c_periph)
  517. {
  518. I2C_SAMCS(i2c_periph) |= I2C_SAMCS_SAMEN;
  519. }
  520. /*!
  521. \brief disable SAM_V interface
  522. \param[in] i2c_periph: I2Cx(x=0,1)
  523. \param[out] none
  524. \retval none
  525. */
  526. void i2c_sam_disable(uint32_t i2c_periph)
  527. {
  528. I2C_SAMCS(i2c_periph) &= ~(I2C_SAMCS_SAMEN);
  529. }
  530. /*!
  531. \brief enable SAM_V interface timeout detect
  532. \param[in] i2c_periph: I2Cx(x=0,1)
  533. \param[out] none
  534. \retval none
  535. */
  536. void i2c_sam_timeout_enable(uint32_t i2c_periph)
  537. {
  538. I2C_SAMCS(i2c_periph) |= I2C_SAMCS_STOEN;
  539. }
  540. /*!
  541. \brief disable SAM_V interface timeout detect
  542. \param[in] i2c_periph: I2Cx(x=0,1)
  543. \param[out] none
  544. \retval none
  545. */
  546. void i2c_sam_timeout_disable(uint32_t i2c_periph)
  547. {
  548. I2C_SAMCS(i2c_periph) &= ~(I2C_SAMCS_STOEN);
  549. }
  550. /*!
  551. \brief get I2C flag status
  552. \param[in] i2c_periph: I2Cx(x=0,1)
  553. \param[in] flag: I2C flags, refer to i2c_flag_enum
  554. only one parameter can be selected which is shown as below:
  555. \arg I2C_FLAG_SBSEND: start condition sent out in master mode
  556. \arg I2C_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode
  557. \arg I2C_FLAG_BTC: byte transmission finishes
  558. \arg I2C_FLAG_ADD10SEND: header of 10-bit address is sent in master mode
  559. \arg I2C_FLAG_STPDET: stop condition detected in slave mode
  560. \arg I2C_FLAG_RBNE: I2C_DATA is not empty during receiving
  561. \arg I2C_FLAG_TBE: I2C_DATA is empty during transmitting
  562. \arg I2C_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus
  563. \arg I2C_FLAG_LOSTARB: arbitration lost in master mode
  564. \arg I2C_FLAG_AERR: acknowledge error
  565. \arg I2C_FLAG_OUERR: over-run or under-run situation occurs in slave mode
  566. \arg I2C_FLAG_PECERR: PEC error when receiving data
  567. \arg I2C_FLAG_SMBTO: timeout signal in SMBus mode
  568. \arg I2C_FLAG_SMBALT: SMBus alert status
  569. \arg I2C_FLAG_MASTER: a flag indicating whether I2C block is in master or slave mode
  570. \arg I2C_FLAG_I2CBSY: busy flag
  571. \arg I2C_FLAG_TR: whether the I2C is a transmitter or a receiver
  572. \arg I2C_FLAG_RXGC: general call address (00h) received
  573. \arg I2C_FLAG_DEFSMB: default address of SMBus device
  574. \arg I2C_FLAG_HSTSMB: SMBus host header detected in slave mode
  575. \arg I2C_FLAG_DUMOD: dual flag in slave mode indicating which address is matched in dual-address mode
  576. \arg I2C_FLAG_TFF: txframe fall flag
  577. \arg I2C_FLAG_TFR: txframe rise flag
  578. \arg I2C_FLAG_RFF: rxframe fall flag
  579. \arg I2C_FLAG_RFR: rxframe rise flag
  580. \param[out] none
  581. \retval FlagStatus: SET or RESET
  582. */
  583. FlagStatus i2c_flag_get(uint32_t i2c_periph, i2c_flag_enum flag)
  584. {
  585. if(RESET != (I2C_REG_VAL(i2c_periph, flag) & BIT(I2C_BIT_POS(flag)))) {
  586. return SET;
  587. } else {
  588. return RESET;
  589. }
  590. }
  591. /*!
  592. \brief clear I2C flag status
  593. \param[in] i2c_periph: I2Cx(x=0,1)
  594. \param[in] flag: I2C flags, refer to i2c_flag_enum
  595. only one parameter can be selected which is shown as below:
  596. \arg I2C_FLAG_SMBALT: SMBus alert status
  597. \arg I2C_FLAG_SMBTO: timeout signal in SMBus mode
  598. \arg I2C_FLAG_PECERR: PEC error when receiving data
  599. \arg I2C_FLAG_OUERR: over-run or under-run situation occurs in slave mode
  600. \arg I2C_FLAG_AERR: acknowledge error
  601. \arg I2C_FLAG_LOSTARB: arbitration lost in master mode
  602. \arg I2C_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus
  603. \arg I2C_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode
  604. \arg I2C_FLAG_TFF: txframe fall flag
  605. \arg I2C_FLAG_TFR: txframe rise flag
  606. \arg I2C_FLAG_RFF: rxframe fall flag
  607. \arg I2C_FLAG_RFR: rxframe rise flag
  608. \param[out] none
  609. \retval none
  610. */
  611. void i2c_flag_clear(uint32_t i2c_periph, i2c_flag_enum flag)
  612. {
  613. if(I2C_FLAG_ADDSEND == flag) {
  614. /* read I2C_STAT0 and then read I2C_STAT1 to clear ADDSEND */
  615. I2C_STAT0(i2c_periph);
  616. I2C_STAT1(i2c_periph);
  617. } else {
  618. I2C_REG_VAL(i2c_periph, flag) &= ~BIT(I2C_BIT_POS(flag));
  619. }
  620. }
  621. /*!
  622. \brief enable I2C interrupt
  623. \param[in] i2c_periph: I2Cx(x=0,1)
  624. \param[in] interrupt: I2C interrupts, refer to i2c_interrupt_enum
  625. only one parameter can be selected which is shown as below:
  626. \arg I2C_INT_ERR: error interrupt
  627. \arg I2C_INT_EV: event interrupt
  628. \arg I2C_INT_BUF: buffer interrupt
  629. \arg I2C_INT_TFF: txframe fall interrupt
  630. \arg I2C_INT_TFR: txframe rise interrupt
  631. \arg I2C_INT_RFF: rxframe fall interrupt
  632. \arg I2C_INT_RFR: rxframe rise interrupt
  633. \param[out] none
  634. \retval none
  635. */
  636. void i2c_interrupt_enable(uint32_t i2c_periph, i2c_interrupt_enum interrupt)
  637. {
  638. I2C_REG_VAL(i2c_periph, interrupt) |= BIT(I2C_BIT_POS(interrupt));
  639. }
  640. /*!
  641. \brief disable I2C interrupt
  642. \param[in] i2c_periph: I2Cx(x=0,1)
  643. \param[in] interrupt: I2C interrupts, refer to i2c_interrupt_enum
  644. only one parameter can be selected which is shown as below:
  645. \arg I2C_INT_ERR: error interrupt
  646. \arg I2C_INT_EV: event interrupt
  647. \arg I2C_INT_BUF: buffer interrupt
  648. \arg I2C_INT_TFF: txframe fall interrupt
  649. \arg I2C_INT_TFR: txframe rise interrupt
  650. \arg I2C_INT_RFF: rxframe fall interrupt
  651. \arg I2C_INT_RFR: rxframe rise interrupt
  652. \param[out] none
  653. \retval none
  654. */
  655. void i2c_interrupt_disable(uint32_t i2c_periph, i2c_interrupt_enum interrupt)
  656. {
  657. I2C_REG_VAL(i2c_periph, interrupt) &= ~BIT(I2C_BIT_POS(interrupt));
  658. }
  659. /*!
  660. \brief get I2C interrupt flag status
  661. \param[in] i2c_periph: I2Cx(x=0,1)
  662. \param[in] int_flag: I2C interrupt flags, refer to i2c_interrupt_flag_enum
  663. only one parameter can be selected which is shown as below:
  664. \arg I2C_INT_FLAG_SBSEND: start condition sent out in master mode interrupt flag
  665. \arg I2C_INT_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode interrupt flag
  666. \arg I2C_INT_FLAG_BTC: byte transmission finishes interrupt flag
  667. \arg I2C_INT_FLAG_ADD10SEND: header of 10-bit address is sent in master mode interrupt flag
  668. \arg I2C_INT_FLAG_STPDET: stop condition detected in slave mode interrupt flag
  669. \arg I2C_INT_FLAG_RBNE: I2C_DATA is not Empty during receiving interrupt flag
  670. \arg I2C_INT_FLAG_TBE: I2C_DATA is empty during transmitting interrupt flag
  671. \arg I2C_INT_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus interrupt flag
  672. \arg I2C_INT_FLAG_LOSTARB: arbitration lost in master mode interrupt flag
  673. \arg I2C_INT_FLAG_AERR: acknowledge error interrupt flag
  674. \arg I2C_INT_FLAG_OUERR: over-run or under-run situation occurs in slave mode interrupt flag
  675. \arg I2C_INT_FLAG_PECERR: PEC error when receiving data interrupt flag
  676. \arg I2C_INT_FLAG_SMBTO: timeout signal in SMBus mode interrupt flag
  677. \arg I2C_INT_FLAG_SMBALT: SMBus alert status interrupt flag
  678. \arg I2C_INT_FLAG_TFF: txframe fall interrupt flag
  679. \arg I2C_INT_FLAG_TFR: txframe rise interrupt flag
  680. \arg I2C_INT_FLAG_RFF: rxframe fall interrupt flag
  681. \arg I2C_INT_FLAG_RFR: rxframe rise interrupt flag
  682. \param[out] none
  683. \retval FlagStatus: SET or RESET
  684. */
  685. FlagStatus i2c_interrupt_flag_get(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag)
  686. {
  687. uint32_t intenable = 0U, flagstatus = 0U, bufie;
  688. /* check BUFIE */
  689. bufie = I2C_CTL1(i2c_periph)&I2C_CTL1_BUFIE;
  690. /* get the interrupt enable bit status */
  691. intenable = (I2C_REG_VAL(i2c_periph, int_flag) & BIT(I2C_BIT_POS(int_flag)));
  692. /* get the corresponding flag bit status */
  693. flagstatus = (I2C_REG_VAL2(i2c_periph, int_flag) & BIT(I2C_BIT_POS2(int_flag)));
  694. if((I2C_INT_FLAG_RBNE == int_flag) || (I2C_INT_FLAG_TBE == int_flag)) {
  695. if(intenable && bufie) {
  696. intenable = 1U;
  697. } else {
  698. intenable = 0U;
  699. }
  700. }
  701. if((0U != flagstatus) && (0U != intenable)) {
  702. return SET;
  703. } else {
  704. return RESET;
  705. }
  706. }
  707. /*!
  708. \brief clear I2C interrupt flag status
  709. \param[in] i2c_periph: I2Cx(x=0,1)
  710. \param[in] int_flag: I2C interrupt flags, refer to i2c_interrupt_flag_enum
  711. only one parameter can be selected which is shown as below:
  712. \arg I2C_INT_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode interrupt flag
  713. \arg I2C_INT_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus interrupt flag
  714. \arg I2C_INT_FLAG_LOSTARB: arbitration lost in master mode interrupt flag
  715. \arg I2C_INT_FLAG_AERR: acknowledge error interrupt flag
  716. \arg I2C_INT_FLAG_OUERR: over-run or under-run situation occurs in slave mode interrupt flag
  717. \arg I2C_INT_FLAG_PECERR: PEC error when receiving data interrupt flag
  718. \arg I2C_INT_FLAG_SMBTO: timeout signal in SMBus mode interrupt flag
  719. \arg I2C_INT_FLAG_SMBALT: SMBus alert status interrupt flag
  720. \arg I2C_INT_FLAG_TFF: txframe fall interrupt flag
  721. \arg I2C_INT_FLAG_TFR: txframe rise interrupt flag
  722. \arg I2C_INT_FLAG_RFF: rxframe fall interrupt flag
  723. \arg I2C_INT_FLAG_RFR: rxframe rise interrupt flag
  724. \param[out] none
  725. \retval none
  726. */
  727. void i2c_interrupt_flag_clear(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag)
  728. {
  729. if(I2C_INT_FLAG_ADDSEND == int_flag) {
  730. /* read I2C_STAT0 and then read I2C_STAT1 to clear ADDSEND */
  731. I2C_STAT0(i2c_periph);
  732. I2C_STAT1(i2c_periph);
  733. } else {
  734. I2C_REG_VAL2(i2c_periph, int_flag) &= ~BIT(I2C_BIT_POS2(int_flag));
  735. }
  736. }