gd32f4xx_i2c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*!
  2. \file gd32f4xx_i2c.c
  3. \brief I2C driver
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #include "gd32f4xx_i2c.h"
  10. #define I2CCLK_MAX 0x3fU /*!< i2cclk max value */
  11. #define I2C_STATE_MASK 0x0000FFFFU /*!< i2c state mask */
  12. /*!
  13. \brief reset I2C
  14. \param[in] i2c_periph: I2Cx(x=0,1,2)
  15. \param[out] none
  16. \retval none
  17. */
  18. void i2c_deinit(uint32_t i2c_periph)
  19. {
  20. switch(i2c_periph){
  21. case I2C0:
  22. rcu_periph_reset_enable(RCU_I2C0RST);
  23. rcu_periph_reset_disable(RCU_I2C0RST);
  24. break;
  25. case I2C1:
  26. rcu_periph_reset_enable(RCU_I2C1RST);
  27. rcu_periph_reset_disable(RCU_I2C1RST);
  28. break;
  29. case I2C2:
  30. rcu_periph_reset_enable(RCU_I2C2RST);
  31. rcu_periph_reset_disable(RCU_I2C2RST);
  32. break;
  33. default:
  34. break;
  35. }
  36. }
  37. /*!
  38. \brief I2C clock configure
  39. \param[in] i2c_periph: I2Cx(x=0,1,2)
  40. \param[in] clkspeed: i2c clock speed
  41. \param[in] dutycyc: duty cycle in fast mode
  42. \arg I2C_DTCY_2: T_low/T_high=2
  43. \arg I2C_DTCY_16_9: T_low/T_high=16/9
  44. \param[out] none
  45. \retval none
  46. */
  47. void i2c_clock_config(uint32_t i2c_periph,uint32_t clkspeed,uint32_t dutycyc)
  48. {
  49. uint32_t pclk1,clkc,i2cclk,risetime;
  50. pclk1 = rcu_clock_freq_get(CK_APB1);
  51. /* I2C Peripheral clock frequency */
  52. i2cclk=((pclk1)/(uint32_t)(1000000));
  53. if(i2cclk >= I2CCLK_MAX){
  54. i2cclk = I2CCLK_MAX;
  55. }
  56. I2C_CTL1(i2c_periph) |= (I2C_CTL1_I2CCLK & i2cclk) ;
  57. if(100000U >= clkspeed){
  58. /* standard mode the maximum SCL rise time in standard mode is 1000ns */
  59. risetime = (uint32_t)((pclk1/1000000U)+1U);
  60. if(risetime >= I2CCLK_MAX){
  61. I2C_RT(i2c_periph) |= I2CCLK_MAX;
  62. }else{
  63. I2C_RT(i2c_periph) |= (uint32_t)((pclk1/1000000U)+1U);
  64. }
  65. clkc = (uint32_t)(pclk1/(clkspeed*2U));
  66. if(clkc < 0x04U){
  67. /* The CLKC in standard mode minmum value is 4*/
  68. clkc = 0x04U;
  69. }
  70. I2C_CKCFG(i2c_periph) |= (I2C_CKCFG_CLKC & clkc);
  71. }else{
  72. /* fast mode the maximum SCL rise time in standard mode is 300ns */
  73. I2C_RT(i2c_periph) |= (uint16_t)(((i2cclk*(uint16_t)300)/(uint16_t)1000)+(uint16_t)1);
  74. if(I2C_DTCY_2 == dutycyc){
  75. /* I2C_DutyCycle == 2 */
  76. clkc = (uint16_t)(pclk1/(clkspeed*3U));
  77. } else{
  78. /* I2C_DutyCycle == 16/9 */
  79. clkc = (uint16_t)(pclk1/(clkspeed*25U));
  80. I2C_CKCFG(i2c_periph) |= I2C_CKCFG_DTCY;
  81. }
  82. if((clkc & I2C_CKCFG_CLKC) == 0U){
  83. /* The CLKC in standard mode minmum value is 1*/
  84. clkc |= (uint16_t)0x0001;
  85. }
  86. I2C_CKCFG(i2c_periph) |= clkc;
  87. }
  88. }
  89. /*!
  90. \brief I2C address configure
  91. \param[in] i2c_periph: I2Cx(x=0,1,2)
  92. \param[in] i2cmod:
  93. \arg I2C_I2CMODE_ENABLE: I2C mode
  94. \arg I2C_SMBUSMODE_ENABLE: SMBus mode
  95. \param[in] addformat: 7bits or 10bits
  96. \arg I2C_ADDFORMAT_7BITS: 7bits
  97. \arg I2C_ADDFORMAT_10BITS: 10bits
  98. \param[in] addr: I2C address
  99. \param[out] none
  100. \retval none
  101. */
  102. void i2c_mode_addr_config(uint32_t i2c_periph,uint32_t i2cmod,uint32_t addformat,uint32_t addr)
  103. {
  104. /* SMBus/I2C mode selected */
  105. uint32_t ctl = 0U;
  106. ctl = I2C_CTL0(i2c_periph);
  107. ctl &= ~(I2C_CTL0_SMBEN);
  108. ctl |= i2cmod;
  109. I2C_CTL0(i2c_periph) = ctl;
  110. /* configure address */
  111. I2C_SADDR0(i2c_periph) = (addformat|addr);
  112. }
  113. /*!
  114. \brief SMBus type selection
  115. \param[in] i2c_periph: I2Cx(x=0,1,2)
  116. \param[in] ack:
  117. \arg I2C_SMBUS_DEVICE: device
  118. \arg I2C_SMBUS_HOST: host
  119. \param[out] none
  120. \retval none
  121. */
  122. void i2c_smbus_type_config(uint32_t i2c_periph,uint32_t type)
  123. {
  124. if(I2C_SMBUS_HOST == type){
  125. I2C_CTL0(i2c_periph) |= I2C_CTL0_SMBSEL;
  126. }else{
  127. I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_SMBSEL);
  128. }
  129. }
  130. /*!
  131. \brief whether or not to send an ACK
  132. \param[in] i2c_periph: I2Cx(x=0,1,2)
  133. \param[in] ack:
  134. \arg I2C_ACK_ENABLE: ACK will be sent
  135. \arg I2C_ACK_DISABLE: ACK will not be sent
  136. \param[out] none
  137. \retval none
  138. */
  139. void i2c_ack_config(uint32_t i2c_periph,uint8_t ack)
  140. {
  141. if(I2C_ACK_ENABLE == ack){
  142. I2C_CTL0(i2c_periph) |= I2C_CTL0_ACKEN;
  143. }else{
  144. I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_ACKEN);
  145. }
  146. }
  147. /*!
  148. \brief I2C POAP position configure
  149. \param[in] i2c_periph: I2Cx(x=0,1,2)
  150. \param[in] pos:
  151. \arg I2C_ACK_ENABLE: ACK will be sent
  152. \arg I2C_ACK_DISABLE: ACK will not be sent
  153. \param[out] none
  154. \retval none
  155. */
  156. void i2c_ackpos_config(uint32_t i2c_periph,uint8_t pos)
  157. {
  158. /* configure i2c POAP position */
  159. if(I2C_ACKPOS_NEXT == pos){
  160. I2C_CTL0(i2c_periph) |= I2C_CTL0_POAP;
  161. }else{
  162. I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_POAP);
  163. }
  164. }
  165. /*!
  166. \brief master send slave address
  167. \param[in] i2c_periph: I2Cx(x=0,1,2)
  168. \param[in] addr: slave address
  169. \param[in] trandirection: transmitter or receiver
  170. \arg I2C_TRANSMITTER: transmitter
  171. \arg I2C_RECEIVER: receiver
  172. \param[out] none
  173. \retval none
  174. */
  175. void i2c_master_addressing(uint32_t i2c_periph,uint8_t addr,uint32_t trandirection)
  176. {
  177. if(I2C_TRANSMITTER==trandirection){
  178. addr = (uint8_t)((uint32_t)addr & I2C_TRANSMITTER);
  179. }else{
  180. addr = (uint8_t)((uint32_t)addr|I2C_RECEIVER);
  181. }
  182. I2C_DATA(i2c_periph) = addr;
  183. }
  184. /*!
  185. \brief dual-address mode switch
  186. \param[in] i2c_periph: I2Cx(x=0,1,2)
  187. \param[in] dualaddr:
  188. \arg I2C_DUADEN_DISABLE: dual-address mode disabled
  189. \arg I2C_DUADEN_ENABLE: dual-address mode enabled
  190. \param[out] none
  191. \retval none
  192. */
  193. void i2c_dualaddr_enable(uint32_t i2c_periph,uint8_t dualaddr)
  194. {
  195. if(I2C_DUADEN_ENABLE == dualaddr){
  196. I2C_SADDR1(i2c_periph) |= I2C_SADDR1_DUADEN;
  197. }else{
  198. I2C_SADDR1(i2c_periph) &= ~(I2C_SADDR1_DUADEN);
  199. }
  200. }
  201. /*!
  202. \brief enable i2c
  203. \param[in] i2c_periph: I2Cx(x=0,1,2)
  204. \param[out] none
  205. \retval none
  206. */
  207. void i2c_enable(uint32_t i2c_periph)
  208. {
  209. I2C_CTL0(i2c_periph) |= I2C_CTL0_I2CEN;
  210. }
  211. /*!
  212. \brief disable i2c
  213. \param[in] i2c_periph: I2Cx(x=0,1,2)
  214. \param[out] none
  215. \retval none
  216. */
  217. void i2c_disable(uint32_t i2c_periph)
  218. {
  219. I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_I2CEN);
  220. }
  221. /*!
  222. \brief generate a START condition on I2C bus
  223. \param[in] i2c_periph: I2Cx(x=0,1,2)
  224. \param[out] none
  225. \retval none
  226. */
  227. void i2c_start_on_bus(uint32_t i2c_periph)
  228. {
  229. I2C_CTL0(i2c_periph) |= I2C_CTL0_START;
  230. }
  231. /*!
  232. \brief generate a STOP condition on I2C bus
  233. \param[in] i2c_periph: I2Cx(x=0,1,2)
  234. \param[out] none
  235. \retval none
  236. */
  237. void i2c_stop_on_bus(uint32_t i2c_periph)
  238. {
  239. I2C_CTL0(i2c_periph) |= I2C_CTL0_STOP;
  240. }
  241. /*!
  242. \brief i2c transmit data function
  243. \param[in] i2c_periph: I2Cx(x=0,1,2)
  244. \param[in] data: data of transmission
  245. \param[out] none
  246. \retval none
  247. */
  248. void i2c_transmit_data(uint32_t i2c_periph,uint8_t data)
  249. {
  250. I2C_DATA(i2c_periph) = data;
  251. }
  252. /*!
  253. \brief i2c receive data function
  254. \param[in] i2c_periph: I2Cx(x=0,1,2)
  255. \param[out] none
  256. \retval data of received
  257. */
  258. uint8_t i2c_receive_data(uint32_t i2c_periph)
  259. {
  260. return (uint8_t)I2C_DATA(i2c_periph);
  261. }
  262. /*!
  263. \brief I2C DMA mode enable
  264. \param[in] i2c_periph: I2Cx(x=0,1,2)
  265. \param[in] dmastste:
  266. \arg I2C_DMA_ON: DMA mode enabled
  267. \arg I2C_DMA_OFF: DMA mode disabled
  268. \param[out] none
  269. \retval none
  270. */
  271. void i2c_dma_enable(uint32_t i2c_periph,uint32_t dmastste)
  272. {
  273. /* configure i2c DMA function */
  274. uint32_t ctl = 0U;
  275. ctl = I2C_CTL1(i2c_periph);
  276. ctl &= ~(I2C_CTL1_DMAON);
  277. ctl |= dmastste;
  278. I2C_CTL1(i2c_periph) = ctl;
  279. }
  280. /*!
  281. \brief flag indicating DMA last transfer
  282. \param[in] i2c_periph: I2Cx(x=0,1,2)
  283. \param[in] dmastste:
  284. \arg I2C_DMALST_ON: next DMA EOT is the last transfer
  285. \arg I2C_DMALST_OFF: next DMA EOT is not the last transfer
  286. \param[out] none
  287. \retval none
  288. */
  289. void i2c_dma_last_transfer_enable(uint32_t i2c_periph,uint32_t dmalast)
  290. {
  291. /* configure DMA last transfer */
  292. uint32_t ctl = 0U;
  293. ctl = I2C_CTL1(i2c_periph);
  294. ctl &= ~(I2C_CTL1_DMALST);
  295. ctl |= dmalast;
  296. I2C_CTL1(i2c_periph) = ctl;
  297. }
  298. /*!
  299. \brief whether to stretch SCL low when data is not ready in slave mode
  300. \param[in] i2c_periph: I2Cx(x=0,1,2)
  301. \param[in] stretchpara:
  302. \arg I2C_SCLSTRETCH_ENABLE: SCL stretching is enabled
  303. \arg I2C_SCLSTRETCH_DISABLE: SCL stretching is disabled
  304. \param[out] none
  305. \retval none
  306. */
  307. void i2c_stretch_scl_low_config(uint32_t i2c_periph,uint32_t stretchpara)
  308. {
  309. /* configure I2C SCL strerching enable or disable */
  310. uint32_t ctl = 0U;
  311. ctl = I2C_CTL0(i2c_periph);
  312. ctl &= ~(I2C_CTL0_DISSTRC);
  313. ctl |= stretchpara;
  314. I2C_CTL0(i2c_periph) = ctl;
  315. }
  316. /*!
  317. \brief whether or not to response to a general Cal
  318. \param[in] i2c_periph: I2Cx(x=0,1,2)
  319. \param[in] gcallpara:
  320. \arg I2C_GCEN_ENABLE: slave will response to a general call
  321. \arg I2C_GCEN_DISABLE: slave will not response to a general call
  322. \param[out] none
  323. \retval none
  324. */
  325. void i2c_slave_response_to_gcall_config(uint32_t i2c_periph, uint32_t gcallpara)
  326. {
  327. /* configure slave response to a general call enable or disable */
  328. uint32_t ctl = 0U;
  329. ctl = I2C_CTL0(i2c_periph);
  330. ctl &= ~(I2C_CTL0_GCEN);
  331. ctl |= gcallpara;
  332. I2C_CTL0(i2c_periph) = ctl;
  333. }
  334. /*!
  335. \brief software reset I2C
  336. \param[in] i2c_periph: I2Cx(x=0,1,2)
  337. \param[in] sreset:
  338. \arg I2C_SRESET_SET: I2C is under reset
  339. \arg I2C_SRESET_RESET: I2C is not under reset
  340. \param[out] none
  341. \retval none
  342. */
  343. void i2c_software_reset_config(uint32_t i2c_periph, uint32_t sreset)
  344. {
  345. /* modify CTL0 and configure software reset I2C state */
  346. uint32_t ctl = 0U;
  347. ctl = I2C_CTL0(i2c_periph);
  348. ctl &= ~(I2C_CTL0_SRESET);
  349. ctl |= sreset;
  350. I2C_CTL0(i2c_periph) = ctl;
  351. }
  352. /*!
  353. \brief check i2c state
  354. \param[in] i2c_periph: I2Cx(x=0,1,2)
  355. \param[in] state:
  356. \arg I2C_SBSEND: start condition send out
  357. \arg I2C_ADDSEND: address is sent in master mode or received and matches in slave mode
  358. \arg I2C_BTC: byte transmission finishes
  359. \arg I2C_ADD10SEND: header of 10-bit address is sent in master mode
  360. \arg I2C_STPDET: etop condition detected in slave mode
  361. \arg I2C_RBNE: I2C_DATA is not Empty during receiving
  362. \arg I2C_TBE: I2C_DATA is empty during transmitting
  363. \arg I2C_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus
  364. \arg I2C_LOSTARB: arbitration lost in master mode
  365. \arg I2C_AERR: acknowledge error
  366. \arg I2C_OUERR: over-run or under-run situation occurs in slave mode
  367. \arg I2C_PECERR: PEC error when receiving data
  368. \arg I2C_SMBTO: timeout signal in SMBus mode
  369. \arg I2C_SMBALT: SMBus alert status
  370. \arg I2C_MASTER: a flag indicating whether I2C block is in master or slave mode
  371. \arg I2C_I2CBSY: busy flag
  372. \arg I2C_TRS: whether the I2C is a transmitter or a receiver
  373. \arg I2C_RXGC: general call address (00h) received
  374. \arg I2C_DEFSMB: default address of SMBus device
  375. \arg I2C_HSTSMB: SMBus host header detected in slave mode
  376. \arg I2C_DUMODF: dual flag in slave mode indicating which address is matched in dual-address mode
  377. \param[out] none
  378. \retval state of i2c
  379. */
  380. FlagStatus i2c_flag_get(uint32_t i2c_periph,uint32_t state )
  381. {
  382. uint32_t reg = 0U;
  383. FlagStatus regstate = RESET;
  384. /* get the state in which register */
  385. reg = (BIT(31) & state);
  386. if((BIT(31) == reg)){
  387. if((I2C_STAT1(i2c_periph)&(state & I2C_STATE_MASK))){
  388. regstate = SET;
  389. }else{
  390. regstate = RESET;
  391. }
  392. }else{
  393. if((I2C_STAT0(i2c_periph)&(state & I2C_STATE_MASK))){
  394. regstate = SET;
  395. }else{
  396. regstate = RESET;
  397. }
  398. }
  399. /* return the state */
  400. return regstate;
  401. }
  402. /*!
  403. \brief clear i2c state
  404. \param[in] i2c_periph: I2Cx(x=0,1,2)
  405. \param[in] state: state type
  406. \@arg I2C_STAT0_SMBALT: SMBus Alert status
  407. \@arg I2C_STAT0_SMBTO: timeout signal in SMBus mode
  408. \@arg I2C_STAT0_PECERR: PEC error when receiving data
  409. \@arg I2C_STAT0_OUERR: over-run or under-run situation occurs in slave mode
  410. \@arg I2C_STAT0_AERR: acknowledge error
  411. \@arg I2C_STAT0_LOSTARB: arbitration lost in master mode
  412. \@arg I2C_STAT0_BERR: a bus error
  413. \@arg I2C_STAT0_ADDSEND: cleared by reading I2C_STAT0 and reading I2C_STAT1
  414. \param[out] none
  415. \retval none
  416. */
  417. void i2c_flag_clear(uint32_t i2c_periph,uint32_t state)
  418. {
  419. if(I2C_STAT0_ADDSEND == state){
  420. /* read I2C_STAT0 and then read I2C_STAT1 to clear ADDSEND */
  421. I2C_STAT0(i2c_periph);
  422. I2C_STAT1(i2c_periph);
  423. }else{
  424. I2C_STAT0(i2c_periph) &= ~(state);
  425. }
  426. }
  427. /*!
  428. \brief enable i2c interrupt
  429. \param[in] i2c_periph: I2Cx(x=0,1,2)
  430. \param[in] inttype: interrupt type
  431. \arg I2C_CTL1_ERRIE: error interrupt enable
  432. \arg I2C_CTL1_EVIE: event interrupt enable
  433. \arg I2C_CTL1_BUFIE: buffer interrupt enable
  434. \param[out] none
  435. \retval none
  436. */
  437. void i2c_interrupt_enable(uint32_t i2c_periph,uint32_t inttype)
  438. {
  439. I2C_CTL1(i2c_periph) |= (inttype);
  440. }
  441. /*!
  442. \brief disable i2c interrupt
  443. \param[in] i2c_periph: I2Cx(x=0,1,2)
  444. \param[in] inttype: interrupt type
  445. \arg I2C_CTL1_ERRIE: error interrupt enable
  446. \arg I2C_CTL1_EVIE: event interrupt enable
  447. \arg I2C_CTL1_BUFIE: buffer interrupt enable
  448. \param[out] none
  449. \retval none
  450. */
  451. void i2c_interrupt_disable(uint32_t i2c_periph,uint32_t inttype)
  452. {
  453. I2C_CTL1(i2c_periph) &= ~(inttype);
  454. }
  455. /*!
  456. \brief I2C PEC calculation on or off
  457. \param[in] i2c_periph: I2Cx(x=0,1,2)
  458. \param[in] pecpara:
  459. \arg I2C_PEC_ENABLE: PEC calculation on
  460. \arg I2C_PEC_DISABLE: PEC calculation off
  461. \param[out] none
  462. \retval none
  463. */
  464. void i2c_pec_enable(uint32_t i2c_periph,uint32_t pecstate)
  465. {
  466. /* on/off PEC calculation */
  467. uint32_t ctl = 0U;
  468. ctl = I2C_CTL0(i2c_periph);
  469. ctl &= ~(I2C_CTL0_PECEN);
  470. ctl |= pecstate;
  471. I2C_CTL0(i2c_periph) = ctl;
  472. }
  473. /*!
  474. \brief I2C whether to transfer PEC value
  475. \param[in] i2c_periph: I2Cx(x=0,1,2)
  476. \param[in] pecpara:
  477. \arg I2C_PECTRANS_ENABLE: transfer PEC
  478. \arg I2C_PECTRANS_DISABLE: not transfer PEC
  479. \param[out] none
  480. \retval none
  481. */
  482. void i2c_pec_transfer_enable(uint32_t i2c_periph,uint32_t pecpara)
  483. {
  484. /* whether to transfer PEC */
  485. uint32_t ctl = 0U;
  486. ctl = I2C_CTL0(i2c_periph);
  487. ctl &= ~(I2C_CTL0_PECTRANS);
  488. ctl |= pecpara;
  489. I2C_CTL0(i2c_periph) = ctl;
  490. }
  491. /*!
  492. \brief packet error checking value
  493. \param[in] i2c_periph: I2Cx(x=0,1,2)
  494. \param[out] none
  495. \retval PEC value
  496. */
  497. uint8_t i2c_pec_value(uint32_t i2c_periph)
  498. {
  499. return (uint8_t)((I2C_STAT1(i2c_periph) &I2C_STAT1_ECV)>>8);
  500. }
  501. /*!
  502. \brief I2C issue alert through SMBA pin
  503. \param[in] i2c_periph: I2Cx(x=0,1,2)
  504. \param[in] smbuspara:
  505. \arg I2C_SALTSEND_ENABLE: issue alert through SMBA pin
  506. \arg I2C_SALTSEND_DISABLE: not issue alert through SMBA pin
  507. \param[out] none
  508. \retval none
  509. */
  510. void i2c_smbus_alert_issue(uint32_t i2c_periph,uint32_t smbuspara)
  511. {
  512. /* issue alert through SMBA pin configure*/
  513. uint32_t ctl = 0U;
  514. ctl = I2C_CTL0(i2c_periph);
  515. ctl &= ~(I2C_CTL0_SALT);
  516. ctl |= smbuspara;
  517. I2C_CTL0(i2c_periph) = ctl;
  518. }
  519. /*!
  520. \brief I2C ARP protocol in SMBus switch enable or disable
  521. \param[in] i2c_periph: I2Cx(x=0,1,2)
  522. \param[in] smbuspara:
  523. \arg I2C_ARP_ENABLE: ARP is enabled
  524. \arg I2C_ARP_DISABLE: ARP is disabled
  525. \param[out] none
  526. \retval none
  527. */
  528. void i2c_smbus_arp_enable(uint32_t i2c_periph,uint32_t arpstate)
  529. {
  530. /* enable or disable I2C ARP protocol*/
  531. uint32_t ctl = 0U;
  532. ctl = I2C_CTL0(i2c_periph);
  533. ctl &= ~(I2C_CTL0_ARPEN);
  534. ctl |= arpstate;
  535. I2C_CTL0(i2c_periph) = ctl;
  536. }
  537. /*!
  538. \brief analog noise filter disable
  539. \param[in] i2c_periph: I2Cx(x=0,1,2)
  540. \param[out] none
  541. \retval none
  542. */
  543. void i2c_analog_noise_filter_disable(uint32_t i2c_periph)
  544. {
  545. I2C_FCTL(i2c_periph) |= I2C_FCTL_AFD;
  546. }
  547. /*!
  548. \brief analog noise filter enable
  549. \param[in] i2c_periph: I2Cx(x=0,1,2)
  550. \param[out] none
  551. \retval none
  552. */
  553. void i2c_analog_noise_filter_enable(uint32_t i2c_periph)
  554. {
  555. I2C_FCTL(i2c_periph) &= ~(I2C_FCTL_AFD);
  556. }
  557. /*!
  558. \brief digital noise filter configuration
  559. \param[in] i2c_periph: I2Cx(x=0,1,2)
  560. \param[in] dfilterpara: refer to enum i2c_gcall_config_enum
  561. \param[out] none
  562. \retval none
  563. */
  564. void i2c_digital_noise_filter_config(uint32_t i2c_periph,i2c_digital_filter_enum dfilterpara)
  565. {
  566. I2C_FCTL(i2c_periph) |= dfilterpara;
  567. }
  568. /*!
  569. \brief enable SAM_V interface
  570. \param[in] i2c_periph: I2Cx(x=0,1,2)
  571. \param[out] none
  572. \retval none
  573. */
  574. void i2c_sam_enable(uint32_t i2c_periph)
  575. {
  576. I2C_SAMCS(i2c_periph) |= I2C_SAMCS_SAMEN;
  577. }
  578. /*!
  579. \brief disable SAM_V interface
  580. \param[in] i2c_periph: I2Cx(x=0,1,2)
  581. \param[out] none
  582. \retval none
  583. */
  584. void i2c_sam_disable(uint32_t i2c_periph)
  585. {
  586. I2C_SAMCS(i2c_periph) &= ~(I2C_SAMCS_SAMEN);
  587. }
  588. /*!
  589. \brief enable SAM_V interface timeout detect
  590. \param[in] i2c_periph: I2Cx(x=0,1,2)
  591. \param[out] none
  592. \retval none
  593. */
  594. void i2c_sam_timeout_enable(uint32_t i2c_periph)
  595. {
  596. I2C_SAMCS(i2c_periph) |= I2C_SAMCS_STOEN;
  597. }
  598. /*!
  599. \brief disable SAM_V interface timeout detect
  600. \param[in] i2c_periph: I2Cx(x=0,1,2)
  601. \param[out] none
  602. \retval none
  603. */
  604. void i2c_sam_timeout_disable(uint32_t i2c_periph)
  605. {
  606. I2C_SAMCS(i2c_periph) &= ~(I2C_SAMCS_STOEN);
  607. }
  608. /*!
  609. \brief enable the specified I2C SAM interrupt
  610. \param[in] i2c_periph: I2Cx(x=0,1,2)
  611. \param[in] inttype: interrupt type
  612. \@arg I2C_SAMCS_TFFIE: txframe fall interrupt
  613. \@arg I2C_SAMCS_TFRIE: txframe rise interrupt
  614. \@arg I2C_SAMCS_RFFIE: rxframe fall interrupt
  615. \@arg I2C_SAMCS_RFRIE: rxframe rise interrupt
  616. \param[out] none
  617. \retval none
  618. */
  619. void i2c_sam_interrupt_enable(uint32_t i2c_periph,uint32_t inttype)
  620. {
  621. I2C_SAMCS(i2c_periph) |= (inttype);
  622. }
  623. /*!
  624. \brief disable i2c interrupt
  625. \param[in] i2c_periph: I2Cx(x=0,1,2)
  626. \param[in] inttype: interrupt type
  627. \@arg I2C_SAMCS_TFFIE: txframe fall interrupt
  628. \@arg I2C_SAMCS_TFRIE: txframe rise interrupt
  629. \@arg I2C_SAMCS_RFFIE: rxframe fall interrupt
  630. \@arg I2C_SAMCS_RFRIE: rxframe rise interrupt
  631. \param[out] none
  632. \retval none
  633. */
  634. void i2c_sam_interrupt_disable(uint32_t i2c_periph,uint32_t inttype)
  635. {
  636. I2C_SAMCS(i2c_periph) &= ~(inttype);
  637. }
  638. /*!
  639. \brief check i2c SAM state
  640. \param[in] i2c_periph: I2Cx(x=0,1,2)
  641. \param[in] samstate: state type
  642. \@arg I2C_SAMCS_TXF: level of txframe signal
  643. \@arg I2C_SAMCS_RXF: level of rxframe signal
  644. \@arg I2C_SAMCS_TFF: txframe fall flag
  645. \@arg I2C_SAMCS_TFR: txframe rise flag
  646. \@arg I2C_SAMCS_RFF: rxframe fall flag
  647. \@arg I2C_SAMCS_RFR: rxframe rise flag
  648. \param[out] none
  649. \retval state of i2c SAM
  650. */
  651. FlagStatus i2c_sam_flag_get(uint32_t i2c_periph,uint32_t samstate)
  652. {
  653. FlagStatus reg = RESET;
  654. if(I2C_SAMCS(i2c_periph)&samstate){
  655. reg =SET;
  656. }else{
  657. reg =RESET;
  658. }
  659. return reg;
  660. }
  661. /*!
  662. \brief clear i2c SAM state
  663. \param[in] i2c_periph: I2Cx(x=0,1,2)
  664. \param[in] samstate: state type
  665. \@arg I2C_SAMCS_TFF: txframe fall flag
  666. \@arg I2C_SAMCS_TFR: txframe rise flag
  667. \@arg I2C_SAMCS_RFF: rxframe fall flag
  668. \@arg I2C_SAMCS_RFR: rxframe rise flag
  669. \param[out] none
  670. \retval none
  671. */
  672. void i2c_sam_flag_clear(uint32_t i2c_periph,uint32_t samstate)
  673. {
  674. I2C_SAMCS(i2c_periph) &= ~(samstate);
  675. }