1 /*!
2     \file    gd32e50x_i2c.c
3     \brief   I2C driver
4 
5     \version 2020-03-10, V1.0.0, firmware for GD32E50x
6     \version 2020-08-26, V1.1.0, firmware for GD32E50x
7     \version 2021-03-23, V1.2.0, firmware for GD32E50x
8 */
9 
10 /*
11     Copyright (c) 2021, GigaDevice Semiconductor Inc.
12 
13     Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15 
16     1. Redistributions of source code must retain the above copyright notice, this
17        list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19        this list of conditions and the following disclaimer in the documentation
20        and/or other materials provided with the distribution.
21     3. Neither the name of the copyright holder nor the names of its contributors
22        may be used to endorse or promote products derived from this software without
23        specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36 
37 #include "gd32e50x_i2c.h"
38 
39 #define I2C_ERROR_HANDLE(s)           do{}while(1)
40 
41 /* I2C register bit mask */
42 #define I2C_FLAG_MASK                 ((uint32_t)0x0000FFFFU)             /*!< i2c flag mask */
43 #define I2C_ADDRESS_MASK              ((uint32_t)0x000003FFU)             /*!< i2c address mask */
44 #define I2C_ADDRESS2_MASK             ((uint32_t)0x000000FEU)             /*!< the second i2c address mask */
45 
46 /* I2C register bit offset */
47 #define STAT1_PECV_OFFSET             ((uint32_t)8U)                      /* bit offset of PECV in I2C_STAT1 */
48 
49 /* I2C2 register bit offset */
50 #define CTL0_DNF_OFFSET               ((uint32_t)0x00000008U)             /*!< bit offset of DNF in I2C2_CTL0 */
51 #define CTL1_BYTENUM_OFFSET           ((uint32_t)0x00000010U)             /*!< bit offset of BYTENUM in I2C2_CTL1 */
52 #define STAT_READDR_OFFSET            ((uint32_t)0x00000011U)             /*!< bit offset of READDR in I2C2_STAT */
53 #define TIMING_SCLL_OFFSET            ((uint32_t)0x00000000U)             /*!< bit offset of SCLL in I2C2_TIMING */
54 #define TIMING_SCLH_OFFSET            ((uint32_t)0x00000008U)             /*!< bit offset of SCLH in I2C2_TIMING */
55 #define TIMING_SDADELY_OFFSET         ((uint32_t)0x00000010U)             /*!< bit offset of SDADELY in I2C2_TIMING */
56 #define TIMING_SCLDELY_OFFSET         ((uint32_t)0x00000014U)             /*!< bit offset of SCLDELY in I2C2_TIMING */
57 #define TIMING_PSC_OFFSET             ((uint32_t)0x0000001CU)             /*!< bit offset of PSC in I2C2_TIMING */
58 #define SADDR1_ADDMSK_OFFSET          ((uint32_t)0x00000008U)             /*!< bit offset of ADDMSK in I2C2_SADDR1 */
59 #define TIMEOUT_BUSTOB_OFFSET         ((uint32_t)0x00000010U)             /*!< bit offset of BUSTOB in I2C2_TIMEOUT */
60 
61 /*!
62     \brief      reset I2C
63     \param[in]  i2c_periph: I2Cx(x=0,1,2)
64     \param[out] none
65     \retval     none
66 */
i2c_deinit(uint32_t i2c_periph)67 void i2c_deinit(uint32_t i2c_periph)
68 {
69     switch(i2c_periph){
70     case I2C0:
71         /* reset I2C0 */
72         rcu_periph_reset_enable(RCU_I2C0RST);
73         rcu_periph_reset_disable(RCU_I2C0RST);
74         break;
75     case I2C1:
76         /* reset I2C1 */
77         rcu_periph_reset_enable(RCU_I2C1RST);
78         rcu_periph_reset_disable(RCU_I2C1RST);
79         break;
80     case I2C2:
81         /* reset I2C2 */
82         rcu_periph_reset_enable(RCU_I2C2RST);
83         rcu_periph_reset_disable(RCU_I2C2RST);
84         break;
85     default:
86         break;
87     }
88 }
89 
90 /*!
91     \brief      enable I2C
92     \param[in]  i2c_periph: I2Cx(x=0,1,2)
93     \param[out] none
94     \retval     none
95 */
i2c_enable(uint32_t i2c_periph)96 void i2c_enable(uint32_t i2c_periph)
97 {
98     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
99         I2C_CTL0(i2c_periph) |= I2C_CTL0_I2CEN;
100     }else{
101         I2C2_CTL0(i2c_periph) |= I2C2_CTL0_I2CEN;
102     }
103 }
104 
105 /*!
106     \brief      disable I2C
107     \param[in]  i2c_periph: I2Cx(x=0,1,2)
108     \param[out] none
109     \retval     none
110 */
i2c_disable(uint32_t i2c_periph)111 void i2c_disable(uint32_t i2c_periph)
112 {
113     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
114         I2C_CTL0(i2c_periph) &= ~I2C_CTL0_I2CEN;
115     }else{
116         I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_I2CEN;
117     }
118 }
119 
120 /*!
121     \brief      generate a START condition on I2C bus
122     \param[in]  i2c_periph: I2Cx(x=0,1,2)
123     \param[out] none
124     \retval     none
125 */
i2c_start_on_bus(uint32_t i2c_periph)126 void i2c_start_on_bus(uint32_t i2c_periph)
127 {
128     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
129         I2C_CTL0(i2c_periph) |= I2C_CTL0_START;
130     }else{
131         I2C2_CTL1(i2c_periph) |= I2C2_CTL1_START;
132     }
133 }
134 
135 /*!
136     \brief      generate a STOP condition on I2C bus
137     \param[in]  i2c_periph: I2Cx(x=0,1,2)
138     \param[out] none
139     \retval     none
140 */
i2c_stop_on_bus(uint32_t i2c_periph)141 void i2c_stop_on_bus(uint32_t i2c_periph)
142 {
143     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
144         I2C_CTL0(i2c_periph) |= I2C_CTL0_STOP;
145     }else{
146         I2C2_CTL1(i2c_periph) |= I2C2_CTL1_STOP;
147     }
148 }
149 
150 /*!
151     \brief      enable the response to a general call
152     \param[in]  i2c_periph: I2Cx(x=0,1,2)
153     \param[in]  none
154     \param[out] none
155     \retval     none
156 */
i2c_slave_response_to_gcall_enable(uint32_t i2c_periph)157 void i2c_slave_response_to_gcall_enable(uint32_t i2c_periph)
158 {
159     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
160         I2C_CTL0(i2c_periph) |= I2C_CTL0_GCEN;
161     }else{
162         I2C2_CTL0(i2c_periph) |= I2C2_CTL0_GCEN;
163     }
164 }
165 
166 /*!
167     \brief      disable the response to a general call
168     \param[in]  i2c_periph: I2Cx(x=0,1,2)
169     \param[in]  none
170     \param[out] none
171     \retval     none
172 */
i2c_slave_response_to_gcall_disable(uint32_t i2c_periph)173 void i2c_slave_response_to_gcall_disable(uint32_t i2c_periph)
174 {
175     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
176         I2C_CTL0(i2c_periph) &= ~I2C_CTL0_GCEN;
177     }else{
178         I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_GCEN;
179     }
180 }
181 
182 /*!
183     \brief      enable to stretch SCL low when data is not ready in slave mode
184     \param[in]  i2c_periph: I2Cx(x=0,1,2)
185     \param[in]  none
186     \param[out] none
187     \retval     none
188 */
i2c_stretch_scl_low_enable(uint32_t i2c_periph)189 void i2c_stretch_scl_low_enable(uint32_t i2c_periph)
190 {
191     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
192         I2C_CTL0(i2c_periph) &= ~I2C_CTL0_SS;
193     }else{
194         I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_SS;
195     }
196 }
197 
198 /*!
199     \brief      disable to stretch SCL low when data is not ready in slave mode
200     \param[in]  i2c_periph: I2Cx(x=0,1,2)
201     \param[in]  none
202     \param[out] none
203     \retval     none
204 */
i2c_stretch_scl_low_disable(uint32_t i2c_periph)205 void i2c_stretch_scl_low_disable(uint32_t i2c_periph)
206 {
207     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
208         I2C_CTL0(i2c_periph) |= I2C_CTL0_SS;
209     }else{
210         I2C2_CTL0(i2c_periph) |= I2C2_CTL0_SS;
211     }
212 }
213 
214 /*!
215     \brief      I2C transmit data function
216     \param[in]  i2c_periph: I2Cx(x=0,1,2)
217     \param[in]  data: data of transmission
218     \param[out] none
219     \retval     none
220 */
i2c_data_transmit(uint32_t i2c_periph,uint32_t data)221 void i2c_data_transmit(uint32_t i2c_periph, uint32_t data)
222 {
223     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
224         I2C_DATA(i2c_periph) = (I2C_DATA_TRB & data);
225     }else{
226         I2C2_TDATA(i2c_periph) = (I2C2_TDATA_TDATA & data);
227     }
228 }
229 
230 /*!
231     \brief      I2C receive data function
232     \param[in]  i2c_periph: I2Cx(x=0,1,2)
233     \param[out] none
234     \retval     data of received
235 */
i2c_data_receive(uint32_t i2c_periph)236 uint32_t i2c_data_receive(uint32_t i2c_periph)
237 {
238     uint32_t data = 0U;
239 
240     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
241         data = I2C_DATA(i2c_periph) & I2C_DATA_TRB;
242     }else{
243         data = I2C2_RDATA(i2c_periph) & I2C2_RDATA_RDATA;
244     }
245     return data;
246 }
247 
248 /*!
249     \brief      I2C transfers PEC value
250     \param[in]  i2c_periph: I2Cx(x=0,1,2)
251     \param[out] none
252     \retval     none
253 */
i2c_pec_transfer(uint32_t i2c_periph)254 void i2c_pec_transfer(uint32_t i2c_periph)
255 {
256     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
257         I2C_CTL0(i2c_periph) |= I2C_CTL0_PECTRANS;
258     }else{
259         I2C2_CTL1(i2c_periph) |= I2C2_CTL1_PECTRANS;
260     }
261 }
262 
263 /*!
264     \brief      enable I2C PEC calculation
265     \param[in]  i2c_periph: I2Cx(x=0,1,2)
266     \param[in]  none
267     \param[out] none
268     \retval     none
269 */
i2c_pec_enable(uint32_t i2c_periph)270 void i2c_pec_enable(uint32_t i2c_periph)
271 {
272     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
273         I2C_CTL0(i2c_periph) |= I2C_CTL0_PECEN;
274     }else{
275         I2C2_CTL0(i2c_periph) |= I2C2_CTL0_PECEN;
276     }
277 }
278 
279 /*!
280     \brief      disable I2C PEC calculation
281     \param[in]  i2c_periph: I2Cx(x=0,1,2)
282     \param[in]  none
283     \param[out] none
284     \retval     none
285 */
i2c_pec_disable(uint32_t i2c_periph)286 void i2c_pec_disable(uint32_t i2c_periph)
287 {
288     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
289         I2C_CTL0(i2c_periph) &= ~I2C_CTL0_PECEN;
290     }else{
291         I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_PECEN;
292     }
293 }
294 
295 /*!
296     \brief      get packet error checking value
297     \param[in]  i2c_periph: I2Cx(x=0,1,2)
298     \param[out] none
299     \retval     PEC value
300 */
i2c_pec_value_get(uint32_t i2c_periph)301 uint32_t i2c_pec_value_get(uint32_t i2c_periph)
302 {
303     uint32_t value = 0U;
304 
305     if((I2C0 == i2c_periph)||(I2C1 == i2c_periph)){
306         value = (I2C_STAT1(i2c_periph) & I2C_STAT1_PECV)>>STAT1_PECV_OFFSET;
307     }else{
308         value = I2C2_PEC(i2c_periph) & I2C2_PEC_PECV;
309     }
310     return value;
311 }
312 
313 /*!
314     \brief      configure I2C clock
315     \param[in]  i2c_periph: I2Cx(x=0,1)
316     \param[in]  clkspeed: I2C clock speed, supports standard mode (up to 100 kHz), fast mode (up to 400 kHz)
317                           and fast mode plus (up to 1MHz)
318     \param[in]  dutycyc: duty cycle in fast mode or fast mode plus
319                 only one parameter can be selected which is shown as below:
320       \arg        I2C_DTCY_2: T_low/T_high=2
321       \arg        I2C_DTCY_16_9: T_low/T_high=16/9
322     \param[out] none
323     \retval     none
324 */
i2c_clock_config(uint32_t i2c_periph,uint32_t clkspeed,uint32_t dutycyc)325 void i2c_clock_config(uint32_t i2c_periph, uint32_t clkspeed, uint32_t dutycyc)
326 {
327     uint32_t pclk1, clkc, freq, risetime;
328     uint32_t temp;
329 
330     if(0U == clkspeed){
331         I2C_ERROR_HANDLE("the parameter can not be 0 \r\n");
332     }
333 
334     pclk1 = rcu_clock_freq_get(CK_APB1);
335     /* I2C peripheral clock frequency */
336     freq = (uint32_t)(pclk1/1000000U);
337     if(freq >= I2CCLK_MAX){
338         freq = I2CCLK_MAX;
339     }
340     temp = I2C_CTL1(i2c_periph);
341     temp &= ~I2C_CTL1_I2CCLK;
342     temp |= freq;
343 
344     I2C_CTL1(i2c_periph) = temp;
345 
346     if(100000U >= clkspeed){
347         /* the maximum SCL rise time is 1000ns in standard mode */
348         risetime = (uint32_t)((pclk1/1000000U)+1U);
349         if(risetime >= I2CCLK_MAX){
350             I2C_RT(i2c_periph) = I2CCLK_MAX;
351         }else if(risetime <= I2CCLK_MIN){
352             I2C_RT(i2c_periph) = I2CCLK_MIN;
353         }else{
354             I2C_RT(i2c_periph) = risetime;
355         }
356         clkc = (uint32_t)(pclk1/(clkspeed*2U));
357         if(clkc < 0x04U){
358             /* the CLKC in standard mode minmum value is 4 */
359             clkc = 0x04U;
360         }
361         I2C_CKCFG(i2c_periph) |= (I2C_CKCFG_CLKC & clkc);
362 
363     }else if(400000U >= clkspeed){
364         /* the maximum SCL rise time is 300ns in fast mode */
365         I2C_RT(i2c_periph) = (uint32_t)(((freq*(uint32_t)300U)/(uint32_t)1000U)+(uint32_t)1U);
366         if(I2C_DTCY_2 == dutycyc){
367             /* I2C duty cycle is 2 */
368             clkc = (uint32_t)(pclk1/(clkspeed*3U));
369             I2C_CKCFG(i2c_periph) &= ~I2C_CKCFG_DTCY;
370         }else{
371             /* I2C duty cycle is 16/9 */
372             clkc = (uint32_t)(pclk1/(clkspeed*25U));
373             I2C_CKCFG(i2c_periph) |= I2C_CKCFG_DTCY;
374         }
375         if(0U == (clkc & I2C_CKCFG_CLKC)){
376             /* the CLKC in fast mode minmum value is 1 */
377             clkc |= 0x0001U;
378         }
379         I2C_CKCFG(i2c_periph) |= I2C_CKCFG_FAST;
380         I2C_CKCFG(i2c_periph) |= clkc;
381     }else{
382         /* fast mode plus, the maximum SCL rise time is 120ns */
383         I2C_RT(i2c_periph) = (uint32_t)(((freq*(uint32_t)120U)/(uint32_t)1000U)+(uint32_t)1U);
384         if(I2C_DTCY_2 == dutycyc){
385             /* I2C duty cycle is 2 */
386             clkc = (uint32_t)(pclk1/(clkspeed*3U));
387             I2C_CKCFG(i2c_periph) &= ~I2C_CKCFG_DTCY;
388         }else{
389             /* I2C duty cycle is 16/9 */
390             clkc = (uint32_t)(pclk1/(clkspeed*25U));
391             I2C_CKCFG(i2c_periph) |= I2C_CKCFG_DTCY;
392         }
393         /* enable fast mode */
394         I2C_CKCFG(i2c_periph) |= I2C_CKCFG_FAST;
395         I2C_CKCFG(i2c_periph) |= clkc;
396         /* enable I2C fast mode plus */
397         I2C_CTL2(i2c_periph)  |= I2C_FAST_MODE_PLUS_ENABLE;
398     }
399 }
400 
401 /*!
402     \brief      configure I2C address
403     \param[in]  i2c_periph: I2Cx(x=0,1)
404     \param[in]  mode:
405                 only one parameter can be selected which is shown as below:
406       \arg        I2C_I2CMODE_ENABLE: I2C mode
407       \arg        I2C_SMBUSMODE_ENABLE: SMBus mode
408     \param[in]  addformat: 7bits or 10bits
409                 only one parameter can be selected which is shown as below:
410       \arg        I2C_ADDFORMAT_7BITS: 7bits
411       \arg        I2C_ADDFORMAT_10BITS: 10bits
412     \param[in]  addr: I2C address
413     \param[out] none
414     \retval     none
415 */
i2c_mode_addr_config(uint32_t i2c_periph,uint32_t mode,uint32_t addformat,uint32_t addr)416 void i2c_mode_addr_config(uint32_t i2c_periph, uint32_t mode, uint32_t addformat, uint32_t addr)
417 {
418     /* SMBus/I2C mode selected */
419     uint32_t ctl = 0U;
420 
421     ctl = I2C_CTL0(i2c_periph);
422     ctl &= ~(I2C_CTL0_SMBEN);
423     ctl |= mode;
424     I2C_CTL0(i2c_periph) = ctl;
425     /* configure address */
426     addr = addr & I2C_ADDRESS_MASK;
427     I2C_SADDR0(i2c_periph) = (addformat | addr);
428 }
429 
430 /*!
431     \brief      SMBus type selection
432     \param[in]  i2c_periph: I2Cx(x=0,1)
433     \param[in]  type:
434                 only one parameter can be selected which is shown as below:
435       \arg        I2C_SMBUS_DEVICE: device
436       \arg        I2C_SMBUS_HOST: host
437     \param[out] none
438     \retval     none
439 */
i2c_smbus_type_config(uint32_t i2c_periph,uint32_t type)440 void i2c_smbus_type_config(uint32_t i2c_periph, uint32_t type)
441 {
442     if(I2C_SMBUS_HOST == type){
443         I2C_CTL0(i2c_periph) |= I2C_CTL0_SMBSEL;
444     }else{
445         I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_SMBSEL);
446     }
447 }
448 
449 /*!
450     \brief      whether or not to send an ACK
451     \param[in]  i2c_periph: I2Cx(x=0,1)
452     \param[in]  ack:
453                 only one parameter can be selected which is shown as below:
454       \arg        I2C_ACK_ENABLE: ACK will be sent
455       \arg        I2C_ACK_DISABLE: ACK will not be sent
456     \param[out] none
457     \retval     none
458 */
i2c_ack_config(uint32_t i2c_periph,uint32_t ack)459 void i2c_ack_config(uint32_t i2c_periph, uint32_t ack)
460 {
461     if(I2C_ACK_ENABLE == ack){
462         I2C_CTL0(i2c_periph) |= I2C_CTL0_ACKEN;
463     }else{
464         I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_ACKEN);
465     }
466 }
467 
468 /*!
469     \brief      configure I2C POAP position
470     \param[in]  i2c_periph: I2Cx(x=0,1)
471     \param[in]  pos:
472                 only one parameter can be selected which is shown as below:
473       \arg        I2C_ACKPOS_CURRENT: whether to send ACK or not for the current
474       \arg        I2C_ACKPOS_NEXT: whether to send ACK or not for the next byte
475     \param[out] none
476     \retval     none
477 */
i2c_ackpos_config(uint32_t i2c_periph,uint32_t pos)478 void i2c_ackpos_config(uint32_t i2c_periph, uint32_t pos)
479 {
480     /* configure I2C POAP position */
481     if(I2C_ACKPOS_NEXT == pos){
482         I2C_CTL0(i2c_periph) |= I2C_CTL0_POAP;
483     }else{
484         I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_POAP);
485     }
486 }
487 
488 /*!
489     \brief      master sends slave address
490     \param[in]  i2c_periph: I2Cx(x=0,1)
491     \param[in]  addr: slave address
492     \param[in]  trandirection: transmitter or receiver
493                 only one parameter can be selected which is shown as below:
494       \arg        I2C_TRANSMITTER: transmitter
495       \arg        I2C_RECEIVER:    receiver
496     \param[out] none
497     \retval     none
498 */
i2c_master_addressing(uint32_t i2c_periph,uint32_t addr,uint32_t trandirection)499 void i2c_master_addressing(uint32_t i2c_periph, uint32_t addr, uint32_t trandirection)
500 {
501     /* master is a transmitter or a receiver */
502     if(I2C_TRANSMITTER == trandirection){
503         addr = addr & I2C_TRANSMITTER;
504     }else{
505         addr = addr | I2C_RECEIVER;
506     }
507     /* send slave address */
508     I2C_DATA(i2c_periph) = addr;
509 }
510 
511 /*!
512     \brief      enable dual-address mode
513     \param[in]  i2c_periph: I2Cx(x=0,1)
514     \param[in]  addr: the second address in dual-address mode
515     \param[out] none
516     \retval     none
517 */
i2c_dualaddr_enable(uint32_t i2c_periph,uint32_t addr)518 void i2c_dualaddr_enable(uint32_t i2c_periph, uint32_t addr)
519 {
520     /* configure address */
521     addr = addr & I2C_ADDRESS2_MASK;
522     I2C_SADDR1(i2c_periph) = (I2C_SADDR1_DUADEN | addr);
523 }
524 
525 /*!
526     \brief      disable dual-address mode
527     \param[in]  i2c_periph: I2Cx(x=0,1)
528     \param[out] none
529     \retval     none
530 */
i2c_dualaddr_disable(uint32_t i2c_periph)531 void i2c_dualaddr_disable(uint32_t i2c_periph)
532 {
533     I2C_SADDR1(i2c_periph) &= ~(I2C_SADDR1_DUADEN);
534 }
535 
536 /*!
537     \brief      enable I2C DMA mode
538     \param[in]  i2c_periph: I2Cx(x=0,1)
539     \param[in]  dmastate:
540                 only one parameter can be selected which is shown as below:
541       \arg        I2C_DMA_ON: DMA mode enable
542       \arg        I2C_DMA_OFF: DMA mode disable
543     \param[out] none
544     \retval     none
545 */
i2c_dma_enable(uint32_t i2c_periph,uint32_t dmastate)546 void i2c_dma_enable(uint32_t i2c_periph, uint32_t dmastate)
547 {
548     /* configure I2C DMA function */
549     uint32_t ctl = 0U;
550 
551     ctl = I2C_CTL1(i2c_periph);
552     ctl &= ~(I2C_CTL1_DMAON);
553     ctl |= dmastate;
554     I2C_CTL1(i2c_periph) = ctl;
555 }
556 
557 /*!
558     \brief      configure whether next DMA EOT is DMA last transfer or not
559     \param[in]  i2c_periph: I2Cx(x=0,1)
560     \param[in]  dmalast:
561                 only one parameter can be selected which is shown as below:
562       \arg        I2C_DMALST_ON: next DMA EOT is the last transfer
563       \arg        I2C_DMALST_OFF: next DMA EOT is not the last transfer
564     \param[out] none
565     \retval     none
566 */
i2c_dma_last_transfer_config(uint32_t i2c_periph,uint32_t dmalast)567 void i2c_dma_last_transfer_config(uint32_t i2c_periph, uint32_t dmalast)
568 {
569     /* configure DMA last transfer */
570     uint32_t ctl = 0U;
571 
572     ctl = I2C_CTL1(i2c_periph);
573     ctl &= ~(I2C_CTL1_DMALST);
574     ctl |= dmalast;
575     I2C_CTL1(i2c_periph) = ctl;
576 }
577 
578 /*!
579     \brief      software reset I2C
580     \param[in]  i2c_periph: I2Cx(x=0,1)
581     \param[in]  sreset:
582                 only one parameter can be selected which is shown as below:
583       \arg        I2C_SRESET_SET: I2C is under reset
584       \arg        I2C_SRESET_RESET: I2C is not under reset
585     \param[out] none
586     \retval     none
587 */
i2c_software_reset_config(uint32_t i2c_periph,uint32_t sreset)588 void i2c_software_reset_config(uint32_t i2c_periph, uint32_t sreset)
589 {
590     /* modify CTL0 and configure software reset I2C state */
591     uint32_t ctl = 0U;
592 
593     ctl = I2C_CTL0(i2c_periph);
594     ctl &= ~(I2C_CTL0_SRESET);
595     ctl |= sreset;
596     I2C_CTL0(i2c_periph) = ctl;
597 }
598 
599 /*!
600     \brief      I2C issue alert through SMBA pin
601     \param[in]  i2c_periph: I2Cx(x=0,1)
602     \param[in]  smbuspara:
603                 only one parameter can be selected which is shown as below:
604       \arg        I2C_SALTSEND_ENABLE: issue alert through SMBA pin
605       \arg        I2C_SALTSEND_DISABLE: not issue alert through SMBA pin
606     \param[out] none
607     \retval     none
608 */
i2c_smbus_issue_alert(uint32_t i2c_periph,uint32_t smbuspara)609 void i2c_smbus_issue_alert(uint32_t i2c_periph, uint32_t smbuspara)
610 {
611     /* issue alert through SMBA pin configure*/
612     uint32_t ctl = 0U;
613 
614     ctl = I2C_CTL0(i2c_periph);
615     ctl &= ~(I2C_CTL0_SALT);
616     ctl |= smbuspara;
617     I2C_CTL0(i2c_periph) = ctl;
618 }
619 
620 /*!
621     \brief      I2C ARP protocol in SMBus switch
622     \param[in]  i2c_periph: I2Cx(x=0,1)
623     \param[in]  arpstate:
624                 only one parameter can be selected which is shown as below:
625       \arg        I2C_ARP_ENABLE: enable ARP
626       \arg        I2C_ARP_DISABLE: disable ARP
627     \param[out] none
628     \retval     none
629 */
i2c_smbus_arp_enable(uint32_t i2c_periph,uint32_t arpstate)630 void i2c_smbus_arp_enable(uint32_t i2c_periph, uint32_t arpstate)
631 {
632     /* enable or disable I2C ARP protocol*/
633     uint32_t ctl = 0U;
634 
635     ctl = I2C_CTL0(i2c_periph);
636     ctl &= ~(I2C_CTL0_ARPEN);
637     ctl |= arpstate;
638     I2C_CTL0(i2c_periph) = ctl;
639 }
640 
641 /*!
642     \brief      enable SAM_V interface
643     \param[in]  i2c_periph: I2Cx(x=0,1)
644     \param[out] none
645     \retval     none
646 */
i2c_sam_enable(uint32_t i2c_periph)647 void i2c_sam_enable(uint32_t i2c_periph)
648 {
649     I2C_SAMCS(i2c_periph) |= I2C_SAMCS_SAMEN;
650 }
651 
652 /*!
653     \brief      disable SAM_V interface
654     \param[in]  i2c_periph: I2Cx(x=0,1)
655     \param[out] none
656     \retval     none
657 */
i2c_sam_disable(uint32_t i2c_periph)658 void i2c_sam_disable(uint32_t i2c_periph)
659 {
660     I2C_SAMCS(i2c_periph) &= ~(I2C_SAMCS_SAMEN);
661 }
662 
663 /*!
664     \brief      enable SAM_V interface timeout detect
665     \param[in]  i2c_periph: I2Cx(x=0,1)
666     \param[out] none
667     \retval     none
668 */
i2c_sam_timeout_enable(uint32_t i2c_periph)669 void i2c_sam_timeout_enable(uint32_t i2c_periph)
670 {
671     I2C_SAMCS(i2c_periph) |= I2C_SAMCS_STOEN;
672 }
673 
674 /*!
675     \brief      disable SAM_V interface timeout detect
676     \param[in]  i2c_periph: I2Cx(x=0,1)
677     \param[out] none
678     \retval     none
679 */
i2c_sam_timeout_disable(uint32_t i2c_periph)680 void i2c_sam_timeout_disable(uint32_t i2c_periph)
681 {
682     I2C_SAMCS(i2c_periph) &= ~(I2C_SAMCS_STOEN);
683 }
684 
685 /*!
686     \brief      configure i2c start early termination mode
687     \param[in]  i2c_periph: I2Cx(x=0,1)
688     \param[in]  mode:
689                 only one parameter can be selected which is shown as below:
690                 STANDARD_I2C_PROTOCOL_MODE: do as the standard i2c protocol
691                 ARBITRATION_LOST_MODE: do the same thing as arbitration lost
692     \param[out] none
693     \retval     none
694 */
i2c_start_early_termination_mode_config(uint32_t i2c_periph,uint32_t mode)695 void i2c_start_early_termination_mode_config(uint32_t i2c_periph, uint32_t mode)
696 {
697     I2C_CTL2(i2c_periph) &= ~(I2C_CTL2_SETM);
698     I2C_CTL2(i2c_periph) |= mode;
699 }
700 
701 /*!
702     \brief      enable I2C timeout calculation
703     \param[in]  i2c_periph: I2Cx(x=0,1)
704     \param[out] none
705     \retval     none
706 */
i2c_timeout_calculation_enable(uint32_t i2c_periph)707 void i2c_timeout_calculation_enable(uint32_t i2c_periph)
708 {
709     I2C_CTL2(i2c_periph) |= I2C_CTL2_TOEN;
710 }
711 
712 /*!
713     \brief      disable I2C timeout calculation
714     \param[in]  i2c_periph: I2Cx(x=0,1)
715     \param[out] none
716     \retval     none
717 */
i2c_timeout_calculation_disable(uint32_t i2c_periph)718 void i2c_timeout_calculation_disable(uint32_t i2c_periph)
719 {
720     I2C_CTL2(i2c_periph) &= ~(I2C_CTL2_TOEN);
721 }
722 
723 /*!
724     \brief      enable i2c record the received slave address to the transfer buffer register
725     \param[in]  i2c_periph: I2Cx(x=0,1)
726     \param[out] none
727     \retval     none
728 */
i2c_record_received_slave_address_enable(uint32_t i2c_periph)729 void i2c_record_received_slave_address_enable(uint32_t i2c_periph)
730 {
731     I2C_CTL2(i2c_periph) |= I2C_CTL2_RADD;
732 }
733 
734 /*!
735     \brief      disable i2c record the received slave address to the transfer buffer register
736     \param[in]  i2c_periph: I2Cx(x=0,1)
737     \param[out] none
738     \retval     none
739 */
i2c_record_received_slave_address_disable(uint32_t i2c_periph)740 void i2c_record_received_slave_address_disable(uint32_t i2c_periph)
741 {
742     I2C_CTL2(i2c_periph) &= ~(I2C_CTL2_RADD);
743 }
744 
745 /*!
746     \brief      define which bits of ADDRESS[7:1] need to compare with the incoming address byte
747     \param[in]  i2c_periph: I2Cx(x=0,1)
748     \param[in]  compare_bits: the bits need to compare
749                 one or more parameters can be selected which are shown as below:
750                 ADDRESS_BIT1_COMPARE: address bit1 needs compare
751                 ADDRESS_BIT2_COMPARE: address bit2 needs compare
752                 ADDRESS_BIT3_COMPARE: address bit3 needs compare
753                 ADDRESS_BIT4_COMPARE: address bit4 needs compare
754                 ADDRESS_BIT5_COMPARE: address bit5 needs compare
755                 ADDRESS_BIT6_COMPARE: address bit6 needs compare
756                 ADDRESS_BIT7_COMPARE: address bit7 needs compare
757     \param[out] none
758     \retval     none
759 */
i2c_address_bit_compare_config(uint32_t i2c_periph,uint16_t compare_bits)760 void i2c_address_bit_compare_config(uint32_t i2c_periph, uint16_t compare_bits)
761 {
762     I2C_CTL2(i2c_periph) &= ~(I2C_CTL2_ADDM);
763     I2C_CTL2(i2c_periph) |= (uint32_t)compare_bits;
764 }
765 
766 /*!
767     \brief      enable I2C status register clear
768     \param[in]  i2c_periph: I2Cx(x=0,1)
769     \param[out] none
770     \retval     none
771 */
i2c_status_clear_enable(uint32_t i2c_periph)772 void i2c_status_clear_enable(uint32_t i2c_periph)
773 {
774     I2C_STATC(i2c_periph) |= I2C_STATC_SRCEN;
775 }
776 
777 /*!
778     \brief      disable I2C status register clear
779     \param[in]  i2c_periph: I2Cx(x=0,1)
780     \param[out] none
781     \retval     none
782 */
i2c_status_clear_disable(uint32_t i2c_periph)783 void i2c_status_clear_disable(uint32_t i2c_periph)
784 {
785     I2C_STATC(i2c_periph) &= ~(I2C_STATC_SRCEN);
786 }
787 
788 /*!
789     \brief      clear I2C status in I2C_STAT0 register
790     \param[in]  i2c_periph: I2Cx(x=0,1)
791     \param[in]  clear_bit: which bit needs to clear
792                 one or more parameters can be selected which are shown as below:
793                 CLEAR_STPDET: clear STPDET bit in I2C_STAT0
794                 CLEAR_ADD10SEND: clear ADD10SEND bit in I2C_STAT0
795                 CLEAR_BTC: clear BTC bit in I2C_STAT0
796                 CLEAR_ADDSEND: clear ADDSEND bit in I2C_STAT0
797                 CLEAR_SBSEND: clear SBSEND bit in I2C_STAT0
798     \param[out] none
799     \retval     none
800 */
i2c_status_bit_clear(uint32_t i2c_periph,uint32_t clear_bit)801 void i2c_status_bit_clear(uint32_t i2c_periph, uint32_t clear_bit)
802 {
803     I2C_STATC(i2c_periph) |= clear_bit;
804 }
805 
806 /*!
807     \brief      check I2C flag is set or not
808     \param[in]  i2c_periph: I2Cx(x=0,1)
809     \param[in]  flag: I2C flags, refer to i2c_flag_enum
810                 only one parameter can be selected which is shown as below:
811       \arg        I2C_FLAG_SBSEND: start condition send out
812       \arg        I2C_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode
813       \arg        I2C_FLAG_BTC: byte transmission finishes
814       \arg        I2C_FLAG_ADD10SEND: header of 10-bit address is sent in master mode
815       \arg        I2C_FLAG_STPDET: stop condition detected in slave mode
816       \arg        I2C_FLAG_RBNE: I2C_DATA is not Empty during receiving
817       \arg        I2C_FLAG_TBE: I2C_DATA is empty during transmitting
818       \arg        I2C_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus
819       \arg        I2C_FLAG_LOSTARB: arbitration lost in master mode
820       \arg        I2C_FLAG_AERR: acknowledge error
821       \arg        I2C_FLAG_OUERR: overrun or underrun situation occurs in slave mode
822       \arg        I2C_FLAG_PECERR: PEC error when receiving data
823       \arg        I2C_FLAG_SMBTO: timeout signal in SMBus mode
824       \arg        I2C_FLAG_SMBALT: SMBus alert status
825       \arg        I2C_FLAG_MASTER: a flag indicating whether I2C block is in master or slave mode
826       \arg        I2C_FLAG_I2CBSY: busy flag
827       \arg        I2C_FLAG_TR: whether the I2C is a transmitter or a receiver
828       \arg        I2C_FLAG_RXGC: general call address (00h) received
829       \arg        I2C_FLAG_DEFSMB: default address of SMBus device
830       \arg        I2C_FLAG_HSTSMB: SMBus host header detected in slave mode
831       \arg        I2C_FLAG_DUMOD: dual flag in slave mode indicating which address is matched in dual-address mode
832       \arg        I2C_FLAG_TFF: txframe fall flag
833       \arg        I2C_FLAG_TFR: txframe rise flag
834       \arg        I2C_FLAG_RFF: rxframe fall flag
835       \arg        I2C_FLAG_RFR: rxframe rise flag
836       \arg        I2C_FLAG_STLO: start lost flag
837       \arg        I2C_FLAG_STPSEND: stop condition sent flag
838     \param[out] none
839     \retval     FlagStatus: SET or RESET
840 */
i2c_flag_get(uint32_t i2c_periph,i2c_flag_enum flag)841 FlagStatus i2c_flag_get(uint32_t i2c_periph, i2c_flag_enum flag)
842 {
843     if(RESET != (I2C_REG_VAL(i2c_periph, flag) & BIT(I2C_BIT_POS(flag)))){
844         return SET;
845     }else{
846         return RESET;
847     }
848 }
849 
850 /*!
851     \brief      clear I2C flag
852     \param[in]  i2c_periph: I2Cx(x=0,1)
853     \param[in]  flag: I2C flags, refer to i2c_flag_enum
854                 only one parameter can be selected which is shown as below:
855       \arg       I2C_FLAG_SMBALT: SMBus Alert status
856       \arg       I2C_FLAG_SMBTO: timeout signal in SMBus mode
857       \arg       I2C_FLAG_PECERR: PEC error when receiving data
858       \arg       I2C_FLAG_OUERR: over-run or under-run situation occurs in slave mode
859       \arg       I2C_FLAG_AERR: acknowledge error
860       \arg       I2C_FLAG_LOSTARB: arbitration lost in master mode
861       \arg       I2C_FLAG_BERR: a bus error
862       \arg       I2C_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode flag
863       \arg       I2C_FLAG_TFF: txframe fall flag
864       \arg       I2C_FLAG_TFR: txframe rise flag
865       \arg       I2C_FLAG_RFF: rxframe fall flag
866       \arg       I2C_FLAG_RFR: rxframe rise flag
867       \arg       I2C_FLAG_STLO: start lost flag
868       \arg       I2C_FLAG_STPSEND: stop condition sent flag
869     \param[out] none
870     \retval     none
871 */
i2c_flag_clear(uint32_t i2c_periph,i2c_flag_enum flag)872 void i2c_flag_clear(uint32_t i2c_periph, i2c_flag_enum flag)
873 {
874     if(I2C_FLAG_ADDSEND == flag){
875         /* read I2C_STAT0 and then read I2C_STAT1 to clear ADDSEND */
876         I2C_STAT0(i2c_periph);
877         I2C_STAT1(i2c_periph);
878     }else{
879         I2C_REG_VAL(i2c_periph, flag) &= ~BIT(I2C_BIT_POS(flag));
880     }
881 }
882 
883 /*!
884     \brief      enable I2C interrupt
885     \param[in]  i2c_periph: I2Cx(x=0,1)
886     \param[in]  interrupt: I2C interrupts, refer to i2c_interrupt_enum
887                 only one parameter can be selected which is shown as below:
888       \arg        I2C_INT_ERR: error interrupt enable
889       \arg        I2C_INT_EV: event interrupt enable
890       \arg        I2C_INT_BUF: buffer interrupt enable
891       \arg        I2C_INT_TFF: txframe fall interrupt enable
892       \arg        I2C_INT_TFR: txframe rise interrupt enable
893       \arg        I2C_INT_RFF: rxframe fall interrupt enable
894       \arg        I2C_INT_RFR: rxframe rise interrupt enable
895       \arg        I2C_INT_STLO: start lost interrupt enable
896       \arg        I2C_INT_STPSEND: stop condition sent interrupt enable
897     \param[out] none
898     \retval     none
899 */
i2c_interrupt_enable(uint32_t i2c_periph,i2c_interrupt_enum interrupt)900 void i2c_interrupt_enable(uint32_t i2c_periph, i2c_interrupt_enum interrupt)
901 {
902     I2C_REG_VAL(i2c_periph, interrupt) |= BIT(I2C_BIT_POS(interrupt));
903 }
904 
905 /*!
906     \brief      disable I2C interrupt
907     \param[in]  i2c_periph: I2Cx(x=0,1)
908     \param[in]  interrupt: I2C interrupts, refer to i2c_flag_enum
909                 only one parameter can be selected which is shown as below:
910       \arg        I2C_INT_ERR: error interrupt enable
911       \arg        I2C_INT_EV: event interrupt enable
912       \arg        I2C_INT_BUF: buffer interrupt enable
913       \arg        I2C_INT_TFF: txframe fall interrupt enable
914       \arg        I2C_INT_TFR: txframe rise interrupt enable
915       \arg        I2C_INT_RFF: rxframe fall interrupt enable
916       \arg        I2C_INT_RFR: rxframe rise interrupt enable
917       \arg        I2C_INT_STLO: start lost interrupt enable
918       \arg        I2C_INT_STPSEND: stop condition sent interrupt enable
919     \param[out] none
920     \retval     none
921 */
i2c_interrupt_disable(uint32_t i2c_periph,i2c_interrupt_enum interrupt)922 void i2c_interrupt_disable(uint32_t i2c_periph, i2c_interrupt_enum interrupt)
923 {
924     I2C_REG_VAL(i2c_periph, interrupt) &= ~BIT(I2C_BIT_POS(interrupt));
925 }
926 
927 /*!
928     \brief      check I2C interrupt flag
929     \param[in]  i2c_periph: I2Cx(x=0,1)
930     \param[in]  int_flag: I2C interrupt flags, refer to i2c_interrupt_flag_enum
931                 only one parameter can be selected which is shown as below:
932       \arg        I2C_INT_FLAG_SBSEND: start condition sent out in master mode interrupt flag
933       \arg        I2C_INT_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode interrupt flag
934       \arg        I2C_INT_FLAG_BTC: byte transmission finishes
935       \arg        I2C_INT_FLAG_ADD10SEND: header of 10-bit address is sent in master mode interrupt flag
936       \arg        I2C_INT_FLAG_STPDET: etop condition detected in slave mode interrupt flag
937       \arg        I2C_INT_FLAG_RBNE: I2C_DATA is not Empty during receiving interrupt flag
938       \arg        I2C_INT_FLAG_TBE: I2C_DATA is empty during transmitting interrupt flag
939       \arg        I2C_INT_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus interrupt flag
940       \arg        I2C_INT_FLAG_LOSTARB: arbitration lost in master mode interrupt flag
941       \arg        I2C_INT_FLAG_AERR: acknowledge error interrupt flag
942       \arg        I2C_INT_FLAG_OUERR: over-run or under-run situation occurs in slave mode interrupt flag
943       \arg        I2C_INT_FLAG_PECERR: PEC error when receiving data interrupt flag
944       \arg        I2C_INT_FLAG_SMBTO: timeout signal in SMBus mode interrupt flag
945       \arg        I2C_INT_FLAG_SMBALT: SMBus Alert status interrupt flag
946       \arg        I2C_INT_FLAG_TFF: txframe fall interrupt flag
947       \arg        I2C_INT_FLAG_TFR: txframe rise interrupt flag
948       \arg        I2C_INT_FLAG_RFF: rxframe fall interrupt flag
949       \arg        I2C_INT_FLAG_RFR: rxframe rise interrupt flag
950       \arg        I2C_INT_FLAG_STLO: start lost interrupt flag
951       \arg        I2C_INT_FLAG_STPSEND: stop condition sent interrupt flag
952     \param[out] none
953     \retval     FlagStatus: SET or RESET
954 */
i2c_interrupt_flag_get(uint32_t i2c_periph,i2c_interrupt_flag_enum int_flag)955 FlagStatus i2c_interrupt_flag_get(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag)
956 {
957     uint32_t intenable = 0U, flagstatus = 0U, bufie;
958 
959     /* check BUFIE */
960     bufie = I2C_CTL1(i2c_periph)&I2C_CTL1_BUFIE;
961 
962     /* get the interrupt enable bit status */
963     intenable = (I2C_REG_VAL(i2c_periph, int_flag) & BIT(I2C_BIT_POS(int_flag)));
964     /* get the corresponding flag bit status */
965     flagstatus = (I2C_REG_VAL2(i2c_periph, int_flag) & BIT(I2C_BIT_POS2(int_flag)));
966 
967     if((I2C_INT_FLAG_RBNE == int_flag) || (I2C_INT_FLAG_TBE == int_flag)){
968         if(intenable && bufie){
969             intenable = 1U;
970         }else{
971             intenable = 0U;
972         }
973     }
974     if((0U != flagstatus) && (0U != intenable)){
975         return SET;
976     }else{
977         return RESET;
978     }
979 }
980 
981 /*!
982     \brief      clear I2C interrupt flag
983     \param[in]  i2c_periph: I2Cx(x=0,1)
984     \param[in]  intflag: I2C interrupt flags, refer to i2c_interrupt_flag_enum
985                 only one parameter can be selected which is shown as below:
986       \arg        I2C_INT_FLAG_ADDSEND: address is sent in master mode or received and matches in slave mode interrupt flag
987       \arg        I2C_INT_FLAG_BERR: a bus error occurs indication a unexpected start or stop condition on I2C bus interrupt flag
988       \arg        I2C_INT_FLAG_LOSTARB: arbitration lost in master mode interrupt flag
989       \arg        I2C_INT_FLAG_AERR: acknowledge error interrupt flag
990       \arg        I2C_INT_FLAG_OUERR: over-run or under-run situation occurs in slave mode interrupt flag
991       \arg        I2C_INT_FLAG_PECERR: PEC error when receiving data interrupt flag
992       \arg        I2C_INT_FLAG_SMBTO: timeout signal in SMBus mode interrupt flag
993       \arg        I2C_INT_FLAG_SMBALT: SMBus Alert status interrupt flag
994       \arg        I2C_INT_FLAG_TFF: txframe fall interrupt flag
995       \arg        I2C_INT_FLAG_TFR: txframe rise interrupt flag
996       \arg        I2C_INT_FLAG_RFF: rxframe fall interrupt flag
997       \arg        I2C_INT_FLAG_RFR: rxframe rise interrupt flag
998       \arg        I2C_INT_FLAG_STLO: start lost interrupt flag
999       \arg        I2C_INT_FLAG_STPSEND: stop condition sent interrupt flag
1000     \param[out] none
1001     \retval     none
1002 */
i2c_interrupt_flag_clear(uint32_t i2c_periph,i2c_interrupt_flag_enum int_flag)1003 void i2c_interrupt_flag_clear(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag)
1004 {
1005     if(I2C_INT_FLAG_ADDSEND == int_flag){
1006         /* read I2C_STAT0 and then read I2C_STAT1 to clear ADDSEND */
1007         I2C_STAT0(i2c_periph);
1008         I2C_STAT1(i2c_periph);
1009     }else{
1010         I2C_REG_VAL2(i2c_periph, int_flag) &= ~BIT(I2C_BIT_POS2(int_flag));
1011     }
1012 }
1013 
1014 /*!
1015     \brief      configure the timing parameters
1016     \param[in]  i2c_periph: I2Cx(x=2)
1017     \param[in]  psc: 0-0x0000000F, timing prescaler
1018     \param[in]  scl_dely: 0-0x0000000F, data setup time
1019     \param[in]  sda_dely: 0-0x0000000F, data hold time
1020     \param[out] none
1021     \retval     none
1022 */
i2c_timing_config(uint32_t i2c_periph,uint32_t psc,uint32_t scl_dely,uint32_t sda_dely)1023 void i2c_timing_config(uint32_t i2c_periph, uint32_t psc, uint32_t scl_dely, uint32_t sda_dely)
1024 {
1025     /* clear PSC, SCLDELY, SDADELY bits in I2C2_TIMING register */
1026     I2C2_TIMING(i2c_periph) &= ~I2C2_TIMING_PSC;
1027     I2C2_TIMING(i2c_periph) &= ~I2C2_TIMING_SCLDELY;
1028     I2C2_TIMING(i2c_periph) &= ~I2C2_TIMING_SDADELY;
1029     /* mask PSC, SCLDELY, SDADELY bits in I2C2_TIMING register */
1030     psc = (uint32_t)(psc << TIMING_PSC_OFFSET) & I2C2_TIMING_PSC;
1031     scl_dely = (uint32_t)(scl_dely << TIMING_SCLDELY_OFFSET) & I2C2_TIMING_SCLDELY;
1032     sda_dely = (uint32_t)(sda_dely << TIMING_SDADELY_OFFSET) & I2C2_TIMING_SDADELY;
1033     /* write PSC, SCLDELY, SDADELY bits in I2C2_TIMING register */
1034     I2C2_TIMING(i2c_periph) |= (psc | scl_dely |sda_dely);
1035 }
1036 
1037 /*!
1038     \brief      configure digital noise filter
1039     \param[in]  i2c_periph: I2Cx(x=2)
1040     \param[in]  filter_length: the length of filter spikes
1041                 only one parameter can be selected which is shown as below:
1042       \arg        FILTER_DISABLE: digital filter is disabled
1043       \arg        FILTER_LENGTH_1: digital filter is enabled and filter spikes with a length of up to 1 tI2CCLK
1044       \arg        FILTER_LENGTH_2: digital filter is enabled and filter spikes with a length of up to 2 tI2CCLK
1045       \arg        FILTER_LENGTH_3: digital filter is enabled and filter spikes with a length of up to 3 tI2CCLK
1046       \arg        FILTER_LENGTH_4: digital filter is enabled and filter spikes with a length of up to 4 tI2CCLK
1047       \arg        FILTER_LENGTH_5: digital filter is enabled and filter spikes with a length of up to 5 tI2CCLK
1048       \arg        FILTER_LENGTH_6: digital filter is enabled and filter spikes with a length of up to 6 tI2CCLK
1049       \arg        FILTER_LENGTH_7: digital filter is enabled and filter spikes with a length of up to 7 tI2CCLK
1050       \arg        FILTER_LENGTH_8: digital filter is enabled and filter spikes with a length of up to 8 tI2CCLK
1051       \arg        FILTER_LENGTH_9: digital filter is enabled and filter spikes with a length of up to 9 tI2CCLK
1052       \arg        FILTER_LENGTH_10: digital filter is enabled and filter spikes with a length of up to 10 tI2CCLK
1053       \arg        FILTER_LENGTH_11: digital filter is enabled and filter spikes with a length of up to 11 tI2CCLK
1054       \arg        FILTER_LENGTH_12: digital filter is enabled and filter spikes with a length of up to 12 tI2CCLK
1055       \arg        FILTER_LENGTH_13: digital filter is enabled and filter spikes with a length of up to 13 tI2CCLK
1056       \arg        FILTER_LENGTH_14: digital filter is enabled and filter spikes with a length of up to 14 tI2CCLK
1057       \arg        FILTER_LENGTH_15: digital filter is enabled and filter spikes with a length of up to 15 tI2CCLK
1058     \param[out] none
1059     \retval     none
1060 */
i2c_digital_noise_filter_config(uint32_t i2c_periph,uint32_t filter_length)1061 void i2c_digital_noise_filter_config(uint32_t i2c_periph, uint32_t filter_length)
1062 {
1063     I2C2_CTL0(i2c_periph) &= (uint32_t)(~I2C2_CTL0_DNF);
1064     I2C2_CTL0(i2c_periph) |= (uint32_t)(filter_length << CTL0_DNF_OFFSET);
1065 }
1066 
1067 /*!
1068     \brief      enable analog noise filter
1069     \param[in]  i2c_periph: I2Cx(x=2)
1070     \param[out] none
1071     \retval     none
1072 */
i2c_analog_noise_filter_enable(uint32_t i2c_periph)1073 void i2c_analog_noise_filter_enable(uint32_t i2c_periph)
1074 {
1075     I2C2_CTL0(i2c_periph) |= I2C2_CTL0_ANOFF;
1076 }
1077 
1078 /*!
1079     \brief      disable analog noise filter
1080     \param[in]  i2c_periph: I2Cx(x=2)
1081     \param[out] none
1082     \retval     none
1083 */
i2c_analog_noise_filter_disable(uint32_t i2c_periph)1084 void i2c_analog_noise_filter_disable(uint32_t i2c_periph)
1085 {
1086     I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_ANOFF;
1087 }
1088 
1089 /*!
1090     \brief      enable wakeup from deep-sleep mode
1091     \param[in]  i2c_periph: I2Cx(x=2)
1092     \param[out] none
1093     \retval     none
1094 */
i2c_wakeup_from_deepsleep_enable(uint32_t i2c_periph)1095 void i2c_wakeup_from_deepsleep_enable(uint32_t i2c_periph)
1096 {
1097     I2C2_CTL0(i2c_periph) |= I2C2_CTL0_WUEN;
1098 }
1099 
1100 /*!
1101     \brief      disable wakeup from deep-sleep mode
1102     \param[in]  i2c_periph: I2Cx(x=2)
1103     \param[out] none
1104     \retval     none
1105 */
i2c_wakeup_from_deepsleep_disable(uint32_t i2c_periph)1106 void i2c_wakeup_from_deepsleep_disable(uint32_t i2c_periph)
1107 {
1108     I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_WUEN;
1109 }
1110 
1111 /*!
1112     \brief      configure the SCL high and low period of clock in master mode
1113     \param[in]  i2c_periph: I2Cx(x=2)
1114     \param[in]  sclh: 0-0x000000FF, SCL high period
1115     \param[in]  scll: 0-0x000000FF, SCL low period
1116     \param[out] none
1117     \retval     none
1118 */
i2c_master_clock_config(uint32_t i2c_periph,uint32_t sclh,uint32_t scll)1119 void i2c_master_clock_config(uint32_t i2c_periph, uint32_t sclh, uint32_t scll)
1120 {
1121     /* clear SCLH, SCLL bits in I2C2_TIMING register */
1122     I2C2_TIMING(i2c_periph) &= ~I2C2_TIMING_SCLH;
1123     I2C2_TIMING(i2c_periph) &= ~I2C2_TIMING_SCLL;
1124     /* mask SCLH, SCLL bits in I2C2_TIMING register */
1125     sclh = (uint32_t)(sclh << TIMING_SCLH_OFFSET) & I2C2_TIMING_SCLH;
1126     scll = (uint32_t)(scll<< TIMING_SCLL_OFFSET) & I2C2_TIMING_SCLL;
1127     /* write SCLH, SCLL bits in I2C2_TIMING register */
1128     I2C2_TIMING(i2c_periph) |= (sclh | scll);
1129 }
1130 
1131 /*!
1132     \brief      configure i2c slave address and transfer direction in master mode
1133     \param[in]  i2c_periph: I2Cx(x=2)
1134     \param[in]  address: 0-0x3FF except reserved address, I2C slave address to be sent
1135     \param[in]  trans_direction: I2C transfer direction in master mode
1136                 only one parameter can be selected which is shown as below:
1137       \arg        I2C2_MASTER_TRANSMIT: master transmit
1138       \arg        I2C2_MASTER_RECEIVE: master receive
1139     \param[out] none
1140     \retval     none
1141 */
i2c2_master_addressing(uint32_t i2c_periph,uint32_t address,uint32_t trans_direction)1142 void i2c2_master_addressing(uint32_t i2c_periph, uint32_t address, uint32_t trans_direction)
1143 {
1144     /* configure slave address */
1145     I2C2_CTL1(i2c_periph) &= ~I2C2_CTL1_SADDRESS;
1146     I2C2_CTL1(i2c_periph) |= address;
1147     /* configure transfer direction */
1148     I2C2_CTL1(i2c_periph) &= ~I2C2_CTL1_TRDIR;
1149     I2C2_CTL1(i2c_periph) |= trans_direction;
1150 }
1151 
1152 /*!
1153     \brief      10-bit address header executes read direction only in master receive mode
1154     \param[in]  i2c_periph: I2Cx(x=2)
1155     \param[out] none
1156     \retval     none
1157 */
i2c_address10_header_enable(uint32_t i2c_periph)1158 void i2c_address10_header_enable(uint32_t i2c_periph)
1159 {
1160     I2C2_CTL1(i2c_periph) |= I2C2_CTL1_HEAD10R;
1161 }
1162 
1163 /*!
1164     \brief      10-bit address header executes complete sequence in master receive mode
1165     \param[in]  i2c_periph: I2Cx(x=2)
1166     \param[out] none
1167     \retval     none
1168 */
i2c_address10_header_disable(uint32_t i2c_periph)1169 void i2c_address10_header_disable(uint32_t i2c_periph)
1170 {
1171     I2C2_CTL1(i2c_periph) &= ~I2C2_CTL1_HEAD10R;
1172 }
1173 
1174 /*!
1175     \brief      enable 10-bit addressing mode in master mode
1176     \param[in]  i2c_periph: I2Cx(x=2)
1177     \param[out] none
1178     \retval     none
1179 */
i2c_address10_enable(uint32_t i2c_periph)1180 void i2c_address10_enable(uint32_t i2c_periph)
1181 {
1182     I2C2_CTL1(i2c_periph) |= I2C2_CTL1_ADD10EN;
1183 }
1184 
1185 /*!
1186     \brief      disable 10-bit addressing mode in master mode
1187     \param[in]  i2c_periph: I2Cx(x=2)
1188     \param[out] none
1189     \retval     none
1190 */
i2c_address10_disable(uint32_t i2c_periph)1191 void i2c_address10_disable(uint32_t i2c_periph)
1192 {
1193     I2C2_CTL1(i2c_periph) &= ~I2C2_CTL1_ADD10EN;
1194 }
1195 
1196 /*!
1197     \brief      enable I2C automatic end mode in master mode
1198     \param[in]  i2c_periph: I2Cx(x=2)
1199     \param[out] none
1200     \retval     none
1201 */
i2c_automatic_end_enable(uint32_t i2c_periph)1202 void i2c_automatic_end_enable(uint32_t i2c_periph)
1203 {
1204     I2C2_CTL1(i2c_periph) |= I2C2_CTL1_AUTOEND;
1205 }
1206 
1207 /*!
1208     \brief      disable I2C automatic end mode in master mode
1209     \param[in]  i2c_periph: I2Cx(x=2)
1210     \param[out] none
1211     \retval     none
1212 */
i2c_automatic_end_disable(uint32_t i2c_periph)1213 void i2c_automatic_end_disable(uint32_t i2c_periph)
1214 {
1215     I2C2_CTL1(i2c_periph) &= ~I2C2_CTL1_AUTOEND;
1216 }
1217 
1218 /*!
1219     \brief      configure i2c slave address
1220     \param[in]  i2c_periph: I2Cx(x=2)
1221     \param[in]  address: I2C address
1222     \param[in]  addr_format: 7bits or 10bits
1223                 only one parameter can be selected which is shown as below:
1224       \arg        I2C2_ADDFORMAT_7BITS: 7bits
1225       \arg        I2C2_ADDFORMAT_10BITS: 10bits
1226     \param[out] none
1227     \retval     none
1228 */
i2c_address_config(uint32_t i2c_periph,uint32_t address,uint32_t addr_format)1229 void i2c_address_config(uint32_t i2c_periph, uint32_t address, uint32_t addr_format)
1230 {
1231     /* configure ADDRESS[7:1] and address format */
1232     address = address & I2C_ADDRESS_MASK;
1233     I2C2_SADDR0(i2c_periph) = (addr_format | address);
1234     /* enable i2c address in slave mode */
1235     I2C2_SADDR0(i2c_periph) |= I2C2_SADDR0_ADDRESSEN;
1236 
1237 }
1238 
1239 /*!
1240     \brief      disable i2c address in slave mode
1241     \param[in]  i2c_periph: I2Cx(x=2)
1242     \param[out] none
1243     \retval     none
1244 */
i2c_address_disable(uint32_t i2c_periph)1245 void i2c_address_disable(uint32_t i2c_periph)
1246 {
1247     I2C2_SADDR0(i2c_periph) &= ~I2C2_SADDR0_ADDRESSEN;
1248 }
1249 
1250 /*!
1251     \brief      configure i2c second slave address
1252     \param[in]  i2c_periph: I2Cx(x=2)
1253     \param[in]  address: I2C address
1254     \param[in]  addr_mask: the bits not need to compare
1255                 one or more parameters can be selected which are shown as below:
1256       \arg        ADDRESS2_NO_MASK: no mask, all the bits must be compared
1257       \arg        ADDRESS2_MASK_BIT1: ADDRESS2[1] is masked, only ADDRESS2[7:2] are compared
1258       \arg        ADDRESS2_MASK_BIT1_2: ADDRESS2[2:1] is masked, only ADDRESS2[7:3] are compared
1259       \arg        ADDRESS2_MASK_BIT1_3: ADDRESS2[3:1] is masked, only ADDRESS2[7:4] are compared
1260       \arg        ADDRESS2_MASK_BIT1_4: ADDRESS2[4:1] is masked, only ADDRESS2[7:5] are compared
1261       \arg        ADDRESS2_MASK_BIT1_5: ADDRESS2[5:1] is masked, only ADDRESS2[7:6] are compared
1262       \arg        ADDRESS2_MASK_BIT1_6: ADDRESS2[6:1] is masked, only ADDRESS2[7] are compared
1263       \arg        ADDRESS2_MASK_ALL: all the ADDRESS2[7:1] bits are masked
1264     \param[out] none
1265     \retval     none
1266 */
i2c_second_address_config(uint32_t i2c_periph,uint32_t address,uint32_t addr_mask)1267 void i2c_second_address_config(uint32_t i2c_periph, uint32_t address, uint32_t addr_mask)
1268 {
1269     /* configure ADDRESS2[7:1] */
1270     address = address & I2C_ADDRESS2_MASK;
1271     I2C2_SADDR1(i2c_periph) |= address;
1272     /* configure ADDRESS2[7:1] mask */
1273     I2C2_SADDR1(i2c_periph) &= ~I2C2_SADDR1_ADDMSK2;
1274     I2C2_SADDR1(i2c_periph) |= (uint32_t)(addr_mask << SADDR1_ADDMSK_OFFSET);
1275     /* enable i2c second address in slave mode */
1276     I2C2_SADDR1(i2c_periph) |= I2C2_SADDR1_ADDRESS2EN;
1277 }
1278 
1279 /*!
1280     \brief      disable i2c second address in slave mode
1281     \param[in]  i2c_periph: I2Cx(x=2)
1282     \param[out] none
1283     \retval     none
1284 */
i2c_second_address_disable(uint32_t i2c_periph)1285 void i2c_second_address_disable(uint32_t i2c_periph)
1286 {
1287     I2C2_SADDR1(i2c_periph) &= ~I2C2_SADDR1_ADDRESS2EN;
1288 }
1289 
1290 /*!
1291     \brief      get received match address in slave mode
1292     \param[in]  i2c_periph: I2Cx(x=2)
1293     \param[out] none
1294     \retval     received match address
1295 */
i2c_recevied_address_get(uint32_t i2c_periph)1296 uint32_t i2c_recevied_address_get(uint32_t i2c_periph)
1297 {
1298     return (uint32_t)((I2C2_STAT(i2c_periph) & I2C2_STAT_READDR) >> STAT_READDR_OFFSET);
1299 }
1300 
1301 /*!
1302     \brief      enable slave byte control
1303     \param[in]  i2c_periph: I2Cx(x=2)
1304     \param[in]  none
1305     \param[out] none
1306     \retval     none
1307 */
i2c_slave_byte_control_enable(uint32_t i2c_periph)1308 void i2c_slave_byte_control_enable(uint32_t i2c_periph)
1309 {
1310     I2C2_CTL0(i2c_periph) |= I2C2_CTL0_SBCTL;
1311 }
1312 
1313 /*!
1314     \brief      disable slave byte control
1315     \param[in]  i2c_periph: I2Cx(x=2)
1316     \param[in]  none
1317     \param[out] none
1318     \retval     none
1319 */
i2c_slave_byte_control_disable(uint32_t i2c_periph)1320 void i2c_slave_byte_control_disable(uint32_t i2c_periph)
1321 {
1322     I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_SBCTL;
1323 }
1324 
1325 /*!
1326     \brief      generate a NACK in slave mode
1327     \param[in]  i2c_periph: I2Cx(x=2)
1328     \param[out] none
1329     \retval     none
1330 */
i2c_nack_enable(uint32_t i2c_periph)1331 void i2c_nack_enable(uint32_t i2c_periph)
1332 {
1333     I2C2_CTL1(i2c_periph) |= I2C2_CTL1_NACKEN;
1334 }
1335 
1336 /*!
1337     \brief      generate an ACK in slave mode
1338     \param[in]  i2c_periph: I2Cx(x=2)
1339     \param[out] none
1340     \retval     none
1341 */
i2c_nack_disable(uint32_t i2c_periph)1342 void i2c_nack_disable(uint32_t i2c_periph)
1343 {
1344     I2C2_CTL1(i2c_periph) &= ~I2C2_CTL1_NACKEN;
1345 }
1346 
1347 /*!
1348     \brief      enable I2C reload mode
1349     \param[in]  i2c_periph: I2Cx(x=2)
1350     \param[out] none
1351     \retval     none
1352 */
i2c_reload_enable(uint32_t i2c_periph)1353 void i2c_reload_enable(uint32_t i2c_periph)
1354 {
1355     I2C2_CTL1(i2c_periph) |= I2C2_CTL1_RELOAD;
1356 }
1357 
1358 /*!
1359     \brief      disable I2C reload mode
1360     \param[in]  i2c_periph: I2Cx(x=2)
1361     \param[out] none
1362     \retval     none
1363 */
i2c_reload_disable(uint32_t i2c_periph)1364 void i2c_reload_disable(uint32_t i2c_periph)
1365 {
1366     I2C2_CTL1(i2c_periph) &= ~I2C2_CTL1_RELOAD;
1367 }
1368 
1369 /*!
1370     \brief      configure number of bytes to be transferred
1371     \param[in]  i2c_periph: I2Cx(x=2)
1372     \param[in]  byte_number: 0x0-0xFF, number of bytes to be transferred
1373     \param[out] none
1374     \retval     none
1375 */
i2c_transfer_byte_number_config(uint32_t i2c_periph,uint32_t byte_number)1376 void i2c_transfer_byte_number_config(uint32_t i2c_periph, uint32_t byte_number)
1377 {
1378     I2C2_CTL1(i2c_periph) &= (uint32_t)(~I2C2_CTL1_BYTENUM);
1379     I2C2_CTL1(i2c_periph) |= (uint32_t)(byte_number << CTL1_BYTENUM_OFFSET);
1380 }
1381 
1382 /*!
1383     \brief      enable I2C DMA for transmission or reception
1384     \param[in]  i2c_periph: I2Cx(x=2)
1385     \param[in]  dma: I2C DMA
1386                 only one parameter can be selected which is shown as below:
1387       \arg        I2C2_DMA_TRANSMIT: transmit data using DMA
1388       \arg        I2C2_DMA_RECEIVE: receive data using DMA
1389     \param[out] none
1390     \retval     none
1391 */
i2c2_dma_enable(uint32_t i2c_periph,uint8_t dma)1392 void i2c2_dma_enable(uint32_t i2c_periph, uint8_t dma)
1393 {
1394     if(I2C2_DMA_TRANSMIT == dma){
1395         I2C2_CTL0(i2c_periph) |= I2C2_CTL0_DENT;
1396     }else{
1397         I2C2_CTL0(i2c_periph) |= I2C2_CTL0_DENR;
1398     }
1399 }
1400 
1401 /*!
1402     \brief      disable I2C DMA for transmission or reception
1403     \param[in]  i2c_periph: I2Cx(x=2)
1404     \param[in]  dma: I2C DMA
1405                 only one parameter can be selected which is shown as below:
1406       \arg        I2C2_DMA_TRANSMIT: transmit data using DMA
1407       \arg        I2C2_DMA_RECEIVE: receive data using DMA
1408     \param[out] none
1409     \retval     none
1410 */
i2c2_dma_disable(uint32_t i2c_periph,uint8_t dma)1411 void i2c2_dma_disable(uint32_t i2c_periph, uint8_t dma)
1412 {
1413     if(I2C2_DMA_TRANSMIT == dma){
1414         I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_DENT;
1415     }else{
1416         I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_DENR;
1417     }
1418 }
1419 
1420 /*!
1421     \brief      enable SMBus Alert
1422     \param[in]  i2c_periph: I2Cx(x=2)
1423     \param[in]  none
1424     \param[out] none
1425     \retval     none
1426 */
i2c_smbus_alert_enable(uint32_t i2c_periph)1427 void i2c_smbus_alert_enable(uint32_t i2c_periph)
1428 {
1429     I2C2_CTL0(i2c_periph) |= I2C2_CTL0_SMBALTEN;
1430 }
1431 
1432 /*!
1433     \brief      disable SMBus Alert
1434     \param[in]  i2c_periph: I2Cx(x=2)
1435     \param[in]  none
1436     \param[out] none
1437     \retval     none
1438 */
i2c_smbus_alert_disable(uint32_t i2c_periph)1439 void i2c_smbus_alert_disable(uint32_t i2c_periph)
1440 {
1441     I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_SMBALTEN;
1442 }
1443 
1444 /*!
1445     \brief      enable SMBus device default address
1446     \param[in]  i2c_periph: I2Cx(x=2)
1447     \param[in]  none
1448     \param[out] none
1449     \retval     none
1450 */
i2c_smbus_default_addr_enable(uint32_t i2c_periph)1451 void i2c_smbus_default_addr_enable(uint32_t i2c_periph)
1452 {
1453     I2C2_CTL0(i2c_periph) |= I2C2_CTL0_SMBDAEN;
1454 }
1455 
1456 /*!
1457     \brief      disable SMBus device default address
1458     \param[in]  i2c_periph: I2Cx(x=2)
1459     \param[in]  none
1460     \param[out] none
1461     \retval     none
1462 */
i2c_smbus_default_addr_disable(uint32_t i2c_periph)1463 void i2c_smbus_default_addr_disable(uint32_t i2c_periph)
1464 {
1465     I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_SMBDAEN;
1466 }
1467 
1468 /*!
1469     \brief      enable SMBus Host address
1470     \param[in]  i2c_periph: I2Cx(x=2)
1471     \param[in]  none
1472     \param[out] none
1473     \retval     none
1474 */
i2c_smbus_host_addr_enable(uint32_t i2c_periph)1475 void i2c_smbus_host_addr_enable(uint32_t i2c_periph)
1476 {
1477     I2C2_CTL0(i2c_periph) |= I2C2_CTL0_SMBHAEN;
1478 }
1479 
1480 /*!
1481     \brief      disable SMBus Host address
1482     \param[in]  i2c_periph: I2Cx(x=2)
1483     \param[in]  none
1484     \param[out] none
1485     \retval     none
1486 */
i2c_smbus_host_addr_disable(uint32_t i2c_periph)1487 void i2c_smbus_host_addr_disable(uint32_t i2c_periph)
1488 {
1489     I2C2_CTL0(i2c_periph) &= ~I2C2_CTL0_SMBHAEN;
1490 }
1491 
1492 /*!
1493     \brief      enable extended clock timeout detection
1494     \param[in]  i2c_periph: I2Cx(x=2)
1495     \param[out] none
1496     \retval     none
1497 */
i2c_extented_clock_timeout_enable(uint32_t i2c_periph)1498 void i2c_extented_clock_timeout_enable(uint32_t i2c_periph)
1499 {
1500     I2C2_TIMEOUT(i2c_periph) |= I2C2_TIMEOUT_EXTOEN;
1501 }
1502 
1503 /*!
1504     \brief      disable extended clock timeout detection
1505     \param[in]  i2c_periph: I2Cx(x=2)
1506     \param[out] none
1507     \retval     none
1508 */
i2c_extented_clock_timeout_disable(uint32_t i2c_periph)1509 void i2c_extented_clock_timeout_disable(uint32_t i2c_periph)
1510 {
1511     I2C2_TIMEOUT(i2c_periph) &= ~I2C2_TIMEOUT_EXTOEN;
1512 }
1513 
1514 /*!
1515     \brief      enable clock timeout detection
1516     \param[in]  i2c_periph: I2Cx(x=2)
1517     \param[out] none
1518     \retval     none
1519 */
i2c_clock_timeout_enable(uint32_t i2c_periph)1520 void i2c_clock_timeout_enable(uint32_t i2c_periph)
1521 {
1522     I2C2_TIMEOUT(i2c_periph) |= I2C2_TIMEOUT_TOEN;
1523 }
1524 
1525 /*!
1526     \brief      disable clock timeout detection
1527     \param[in]  i2c_periph: I2Cx(x=2)
1528     \param[out] none
1529     \retval     none
1530 */
i2c_clock_timeout_disable(uint32_t i2c_periph)1531 void i2c_clock_timeout_disable(uint32_t i2c_periph)
1532 {
1533     I2C2_TIMEOUT(i2c_periph) &= ~I2C2_TIMEOUT_TOEN;
1534 }
1535 
1536 /*!
1537     \brief      configure bus timeout B
1538     \param[in]  i2c_periph: I2Cx(x=2)
1539     \param[in]  timeout: bus timeout B
1540     \param[out] none
1541     \retval     none
1542 */
i2c_bus_timeout_b_config(uint32_t i2c_periph,uint32_t timeout)1543 void i2c_bus_timeout_b_config(uint32_t i2c_periph, uint32_t timeout)
1544 {
1545     I2C2_TIMEOUT(i2c_periph) &= ~I2C2_TIMEOUT_BUSTOB;
1546     I2C2_TIMEOUT(i2c_periph) |= (uint32_t)(timeout << TIMEOUT_BUSTOB_OFFSET);
1547 }
1548 
1549 /*!
1550     \brief      configure bus timeout A
1551     \param[in]  i2c_periph: I2Cx(x=2)
1552     \param[in]  timeout: bus timeout A
1553     \param[out] none
1554     \retval     none
1555 */
i2c_bus_timeout_a_config(uint32_t i2c_periph,uint32_t timeout)1556 void i2c_bus_timeout_a_config(uint32_t i2c_periph, uint32_t timeout)
1557 {
1558     I2C2_TIMEOUT(i2c_periph) &= ~I2C2_TIMEOUT_BUSTOA;
1559     I2C2_TIMEOUT(i2c_periph) |= timeout;
1560 }
1561 
1562 /*!
1563     \brief      configure idle clock timeout detection
1564     \param[in]  i2c_periph: I2Cx(x=2)
1565     \param[in]  timeout: bus timeout A
1566       \arg        BUSTOA_DETECT_SCL_LOW: BUSTOA is used to detect SCL low timeout
1567       \arg        BUSTOA_DETECT_IDLE: BUSTOA is used to detect both SCL and SDA high timeout when the bus is idle
1568     \param[out] none
1569     \retval     none
1570 */
i2c_idle_clock_timeout_config(uint32_t i2c_periph,uint32_t timeout)1571 void i2c_idle_clock_timeout_config(uint32_t i2c_periph, uint32_t timeout)
1572 {
1573     I2C2_TIMEOUT(i2c_periph) &= ~I2C2_TIMEOUT_TOIDLE;
1574     I2C2_TIMEOUT(i2c_periph) |= timeout;
1575 }
1576 
1577 /*!
1578     \brief      get I2C flag status
1579     \param[in]  i2c_periph: I2Cx(x=2)
1580     \param[in]  flag: I2C flags
1581                 only one parameter can be selected which is shown as below:
1582       \arg        I2C2_FLAG_TBE: I2C2_TDATA is empty during transmitting
1583       \arg        I2C2_FLAG_TI: transmit interrupt
1584       \arg        I2C2_FLAG_RBNE: I2C2_RDATA is not empty during receiving
1585       \arg        I2C2_FLAG_ADDSEND: address received matches in slave mode
1586       \arg        I2C2_FLAG_NACK: not acknowledge flag
1587       \arg        I2C2_FLAG_STPDET: STOP condition detected in slave mode
1588       \arg        I2C2_FLAG_TC: transfer complete in master mode
1589       \arg        I2C2_FLAG_TCR: transfer complete reload
1590       \arg        I2C2_FLAG_BERR: bus error
1591       \arg        I2C2_FLAG_LOSTARB: arbitration Lost
1592       \arg        I2C2_FLAG_OUERR: overrun/underrun error in slave mode
1593       \arg        I2C2_FLAG_PECERR: PEC error
1594       \arg        I2C2_FLAG_TIMEOUT: timeout flag
1595       \arg        I2C2_FLAG_SMBALT: SMBus Alert
1596       \arg        I2C2_FLAG_I2CBSY: busy flag
1597       \arg        I2C2_FLAG_TR: whether the I2C is a transmitter or a receiver in slave mode
1598     \param[out] none
1599     \retval     FlagStatus: SET or RESET
1600 */
i2c2_flag_get(uint32_t i2c_periph,uint32_t flag)1601 FlagStatus i2c2_flag_get(uint32_t i2c_periph, uint32_t flag)
1602 {
1603     if(RESET != (I2C2_STAT(i2c_periph) & flag)){
1604         return SET;
1605     }else{
1606         return RESET;
1607     }
1608 }
1609 
1610 /*!
1611     \brief      clear I2C flag status
1612     \param[in]  i2c_periph: I2Cx(x=2)
1613     \param[in]  flag: I2C flags
1614                 one or more parameters can be selected which are shown as below:
1615       \arg        I2C2_FLAG_ADDSEND: address received matches in slave mode
1616       \arg        I2C2_FLAG_NACK: not acknowledge flag
1617       \arg        I2C2_FLAG_STPDET: STOP condition detected in slave mode
1618       \arg        I2C2_FLAG_BERR: bus error
1619       \arg        I2C2_FLAG_LOSTARB: arbitration Lost
1620       \arg        I2C2_FLAG_OUERR: overrun/underrun error in slave mode
1621       \arg        I2C2_FLAG_PECERR: PEC error
1622       \arg        I2C2_FLAG_TIMEOUT: timeout flag
1623       \arg        I2C2_FLAG_SMBALT: SMBus Alert
1624     \param[out] none
1625     \retval     none
1626 */
i2c2_flag_clear(uint32_t i2c_periph,uint32_t flag)1627 void i2c2_flag_clear(uint32_t i2c_periph, uint32_t flag)
1628 {
1629     I2C2_STATC(i2c_periph) |= flag;
1630 }
1631 
1632 /*!
1633     \brief      enable I2C interrupt
1634     \param[in]  i2c_periph: I2Cx(x=2)
1635     \param[in]  interrupt: I2C interrupts
1636                 one or more parameters can be selected which are shown as below:
1637       \arg        I2C2_INT_ERR: error interrupt
1638       \arg        I2C2_INT_TC: transfer complete interrupt
1639       \arg        I2C2_INT_STPDET: stop detection interrupt
1640       \arg        I2C2_INT_NACK: not acknowledge received interrupt
1641       \arg        I2C2_INT_ADDM: address match interrupt
1642       \arg        I2C2_INT_RBNE: receive interrupt
1643       \arg        I2C2_INT_TI: transmit interrupt
1644     \param[out] none
1645     \retval     none
1646 */
i2c2_interrupt_enable(uint32_t i2c_periph,uint32_t interrupt)1647 void i2c2_interrupt_enable(uint32_t i2c_periph, uint32_t interrupt)
1648 {
1649     I2C2_CTL0(i2c_periph) |= interrupt;
1650 }
1651 
1652 /*!
1653     \brief      disable I2C interrupt
1654     \param[in]  i2c_periph: I2Cx(x=2)
1655     \param[in]  interrupt: I2C interrupts
1656                 one or more parameters can be selected which are shown as below:
1657       \arg        I2C2_INT_ERR: error interrupt
1658       \arg        I2C2_INT_TC: transfer complete interrupt
1659       \arg        I2C2_INT_STPDET: stop detection interrupt
1660       \arg        I2C2_INT_NACK: not acknowledge received interrupt
1661       \arg        I2C2_INT_ADDM: address match interrupt
1662       \arg        I2C2_INT_RBNE: receive interrupt
1663       \arg        I2C2_INT_TI: transmit interrupt
1664     \param[out] none
1665     \retval     none
1666 */
i2c2_interrupt_disable(uint32_t i2c_periph,uint32_t interrupt)1667 void i2c2_interrupt_disable(uint32_t i2c_periph, uint32_t interrupt)
1668 {
1669     I2C2_CTL0(i2c_periph) &= ~interrupt;
1670 }
1671 
1672 /*!
1673     \brief      get I2C interrupt flag status
1674     \param[in]  i2c_periph: I2Cx(x=2)
1675     \param[in]  int_flag: I2C interrupt flags, refer to i2c2_interrupt_flag_enum
1676                 only one parameter can be selected which is shown as below:
1677       \arg        I2C2_INT_FLAG_TI: transmit interrupt flag
1678       \arg        I2C2_INT_FLAG_RBNE: I2C2_RDATA is not empty during receiving interrupt flag
1679       \arg        I2C2_INT_FLAG_ADDSEND: address received matches in slave mode interrupt flag
1680       \arg        I2C2_INT_FLAG_NACK: not acknowledge interrupt flag
1681       \arg        I2C2_INT_FLAG_STPDET: stop condition detected in slave mode interrupt flag
1682       \arg        I2C2_INT_FLAG_TC: transfer complete in master mode interrupt flag
1683       \arg        I2C2_INT_FLAG_TCR: transfer complete reload interrupt flag
1684       \arg        I2C2_INT_FLAG_BERR: bus error interrupt flag
1685       \arg        I2C2_INT_FLAG_LOSTARB: arbitration lost interrupt flag
1686       \arg        I2C2_INT_FLAG_OUERR: overrun/underrun error in slave mode interrupt flag
1687       \arg        I2C2_INT_FLAG_PECERR: PEC error interrupt flag
1688       \arg        I2C2_INT_FLAG_TIMEOUT: timeout interrupt flag
1689       \arg        I2C2_INT_FLAG_SMBALT: SMBus Alert interrupt flag
1690     \param[out] none
1691     \retval     FlagStatus: SET or RESET
1692 */
i2c2_interrupt_flag_get(uint32_t i2c_periph,i2c2_interrupt_flag_enum int_flag)1693 FlagStatus i2c2_interrupt_flag_get(uint32_t i2c_periph, i2c2_interrupt_flag_enum int_flag)
1694 {
1695     uint32_t ret1 = RESET;
1696     uint32_t ret2 = RESET;
1697 
1698     /* get the status of interrupt enable bit */
1699     ret1 = (I2C_REG_VAL(i2c_periph, int_flag) & BIT(I2C_BIT_POS(int_flag)));
1700     /* get the status of interrupt flag */
1701     ret2 = (I2C_REG_VAL2(i2c_periph, int_flag) & BIT(I2C_BIT_POS2(int_flag)));
1702     if(ret1 && ret2){
1703         return SET;
1704     }else{
1705         return RESET;
1706     }
1707 }
1708 
1709 /*!
1710     \brief      clear I2C interrupt flag status
1711     \param[in]  i2c_periph: I2Cx(x=2)
1712     \param[in]  int_flag: I2C interrupt flags, refer to i2c2_interrupt_flag_enum
1713                 only one parameter can be selected which is shown as below:
1714       \arg        I2C2_INT_FLAG_ADDSEND: address received matches in slave mode interrupt flag
1715       \arg        I2C2_INT_FLAG_NACK: not acknowledge interrupt flag
1716       \arg        I2C2_INT_FLAG_STPDET: stop condition detected in slave mode interrupt flag
1717       \arg        I2C2_INT_FLAG_BERR: bus error interrupt flag
1718       \arg        I2C2_INT_FLAG_LOSTARB: arbitration lost interrupt flag
1719       \arg        I2C2_INT_FLAG_OUERR: overrun/underrun error in slave mode interrupt flag
1720       \arg        I2C2_INT_FLAG_PECERR: PEC error interrupt flag
1721       \arg        I2C2_INT_FLAG_TIMEOUT: timeout interrupt flag
1722       \arg        I2C2_INT_FLAG_SMBALT: SMBus Alert interrupt flag
1723     \param[out] none
1724     \retval     none
1725 */
i2c2_interrupt_flag_clear(uint32_t i2c_periph,i2c2_interrupt_flag_enum int_flag)1726 void i2c2_interrupt_flag_clear(uint32_t i2c_periph, i2c2_interrupt_flag_enum int_flag)
1727 {
1728     I2C2_STATC(i2c_periph) |= BIT(I2C_BIT_POS2(int_flag));
1729 }
1730