1 /*!
2 \file gd32l23x_i2c.c
3 \brief I2C driver
4
5 \version 2021-08-04, V1.0.0, firmware for GD32L23x
6 */
7
8 /*
9 Copyright (c) 2021, GigaDevice Semiconductor Inc.
10
11 Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13
14 1. Redistributions of source code must retain the above copyright notice, this
15 list of conditions and the following disclaimer.
16 2. Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 3. Neither the name of the copyright holder nor the names of its contributors
20 may be used to endorse or promote products derived from this software without
21 specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34
35 #include "gd32l23x_i2c.h"
36
37 /* I2C register bit mask */
38 #define I2C_ADDRESS_MASK ((uint32_t)0x000003FFU) /*!< i2c address mask */
39 #define I2C_ADDRESS2_MASK ((uint32_t)0x000000FEU) /*!< the second i2c address mask */
40
41 /* I2C register bit offset */
42 #define CTL0_DNF_OFFSET ((uint32_t)0x00000008U) /*!< bit offset of DNF in I2C_CTL0 */
43 #define CTL1_BYTENUM_OFFSET ((uint32_t)0x00000010U) /*!< bit offset of BYTENUM in I2C_CTL1 */
44 #define STAT_READDR_OFFSET ((uint32_t)0x00000011U) /*!< bit offset of READDR in I2C_STAT */
45 #define TIMING_SCLL_OFFSET ((uint32_t)0x00000000U) /*!< bit offset of SCLL in I2C_TIMING */
46 #define TIMING_SCLH_OFFSET ((uint32_t)0x00000008U) /*!< bit offset of SCLH in I2C_TIMING */
47 #define TIMING_SDADELY_OFFSET ((uint32_t)0x00000010U) /*!< bit offset of SDADELY in I2C_TIMING */
48 #define TIMING_SCLDELY_OFFSET ((uint32_t)0x00000014U) /*!< bit offset of SCLDELY in I2C_TIMING */
49 #define TIMING_PSC_OFFSET ((uint32_t)0x0000001CU) /*!< bit offset of PSC in I2C_TIMING */
50 #define SADDR1_ADDMSK_OFFSET ((uint32_t)0x00000008U) /*!< bit offset of ADDMSK in I2C_SADDR1 */
51 #define TIMEOUT_BUSTOB_OFFSET ((uint32_t)0x00000010U) /*!< bit offset of BUSTOB in I2C_TIMEOUT */
52
53 /*!
54 \brief reset I2C
55 \param[in] i2c_periph: I2Cx(x=0,1,2)
56 \param[out] none
57 \retval none
58 */
i2c_deinit(uint32_t i2c_periph)59 void i2c_deinit(uint32_t i2c_periph)
60 {
61 switch(i2c_periph) {
62 case I2C0:
63 /* reset I2C0 */
64 rcu_periph_reset_enable(RCU_I2C0RST);
65 rcu_periph_reset_disable(RCU_I2C0RST);
66 break;
67 case I2C1:
68 /* reset I2C1 */
69 rcu_periph_reset_enable(RCU_I2C1RST);
70 rcu_periph_reset_disable(RCU_I2C1RST);
71 break;
72 case I2C2:
73 /* reset I2C2 */
74 rcu_periph_reset_enable(RCU_I2C2RST);
75 rcu_periph_reset_disable(RCU_I2C2RST);
76 break;
77 default:
78 break;
79 }
80 }
81
82 /*!
83 \brief configure the timing parameters
84 \param[in] i2c_periph: I2Cx(x=0,1,2)
85 \param[in] psc: 0-0x0000000F, timing prescaler
86 \param[in] scl_dely: 0-0x0000000F, data setup time
87 \param[in] sda_dely: 0-0x0000000F, data hold time
88 \param[out] none
89 \retval none
90 */
i2c_timing_config(uint32_t i2c_periph,uint32_t psc,uint32_t scl_dely,uint32_t sda_dely)91 void i2c_timing_config(uint32_t i2c_periph, uint32_t psc, uint32_t scl_dely, uint32_t sda_dely)
92 {
93 /* clear PSC, SCLDELY, SDADELY bits in I2C_TIMING register */
94 I2C_TIMING(i2c_periph) &= ~I2C_TIMING_PSC;
95 I2C_TIMING(i2c_periph) &= ~I2C_TIMING_SCLDELY;
96 I2C_TIMING(i2c_periph) &= ~I2C_TIMING_SDADELY;
97 /* mask PSC, SCLDELY, SDADELY bits in I2C_TIMING register */
98 psc = (uint32_t)(psc << TIMING_PSC_OFFSET) & I2C_TIMING_PSC;
99 scl_dely = (uint32_t)(scl_dely << TIMING_SCLDELY_OFFSET) & I2C_TIMING_SCLDELY;
100 sda_dely = (uint32_t)(sda_dely << TIMING_SDADELY_OFFSET) & I2C_TIMING_SDADELY;
101 /* write PSC, SCLDELY, SDADELY bits in I2C_TIMING register */
102 I2C_TIMING(i2c_periph) |= (psc | scl_dely | sda_dely);
103 }
104
105 /*!
106 \brief configure digital noise filter
107 \param[in] i2c_periph: I2Cx(x=0,1,2)
108 \param[in] filter_length: the length of filter spikes
109 only one parameter can be selected which is shown as below:
110 \arg FILTER_DISABLE: digital filter is disabled
111 \arg FILTER_LENGTH_1: digital filter is enabled and filter spikes with a length of up to 1 tI2CCLK
112 \arg FILTER_LENGTH_2: digital filter is enabled and filter spikes with a length of up to 2 tI2CCLK
113 \arg FILTER_LENGTH_3: digital filter is enabled and filter spikes with a length of up to 3 tI2CCLK
114 \arg FILTER_LENGTH_4: digital filter is enabled and filter spikes with a length of up to 4 tI2CCLK
115 \arg FILTER_LENGTH_5: digital filter is enabled and filter spikes with a length of up to 5 tI2CCLK
116 \arg FILTER_LENGTH_6: digital filter is enabled and filter spikes with a length of up to 6 tI2CCLK
117 \arg FILTER_LENGTH_7: digital filter is enabled and filter spikes with a length of up to 7 tI2CCLK
118 \arg FILTER_LENGTH_8: digital filter is enabled and filter spikes with a length of up to 8 tI2CCLK
119 \arg FILTER_LENGTH_9: digital filter is enabled and filter spikes with a length of up to 9 tI2CCLK
120 \arg FILTER_LENGTH_10: digital filter is enabled and filter spikes with a length of up to 10 tI2CCLK
121 \arg FILTER_LENGTH_11: digital filter is enabled and filter spikes with a length of up to 11 tI2CCLK
122 \arg FILTER_LENGTH_12: digital filter is enabled and filter spikes with a length of up to 12 tI2CCLK
123 \arg FILTER_LENGTH_13: digital filter is enabled and filter spikes with a length of up to 13 tI2CCLK
124 \arg FILTER_LENGTH_14: digital filter is enabled and filter spikes with a length of up to 14 tI2CCLK
125 \arg FILTER_LENGTH_15: digital filter is enabled and filter spikes with a length of up to 15 tI2CCLK
126 \param[out] none
127 \retval none
128 */
i2c_digital_noise_filter_config(uint32_t i2c_periph,uint32_t filter_length)129 void i2c_digital_noise_filter_config(uint32_t i2c_periph, uint32_t filter_length)
130 {
131 I2C_CTL0(i2c_periph) &= (uint32_t)(~I2C_CTL0_DNF);
132 I2C_CTL0(i2c_periph) |= (uint32_t)(filter_length << CTL0_DNF_OFFSET);
133 }
134
135 /*!
136 \brief enable analog noise filter
137 \param[in] i2c_periph: I2Cx(x=0,1,2)
138 \param[out] none
139 \retval none
140 */
i2c_analog_noise_filter_enable(uint32_t i2c_periph)141 void i2c_analog_noise_filter_enable(uint32_t i2c_periph)
142 {
143 I2C_CTL0(i2c_periph) |= I2C_CTL0_ANOFF;
144 }
145
146 /*!
147 \brief disable analog noise filter
148 \param[in] i2c_periph: I2Cx(x=0,1,2)
149 \param[out] none
150 \retval none
151 */
i2c_analog_noise_filter_disable(uint32_t i2c_periph)152 void i2c_analog_noise_filter_disable(uint32_t i2c_periph)
153 {
154 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_ANOFF;
155 }
156
157 /*!
158 \brief configure the SCL high and low period of clock in master mode
159 \param[in] i2c_periph: I2Cx(x=0,1,2)
160 \param[in] sclh: 0-0x000000FF, SCL high period
161 \param[in] scll: 0-0x000000FF, SCL low period
162 \param[out] none
163 \retval none
164 */
i2c_master_clock_config(uint32_t i2c_periph,uint32_t sclh,uint32_t scll)165 void i2c_master_clock_config(uint32_t i2c_periph, uint32_t sclh, uint32_t scll)
166 {
167 /* clear SCLH, SCLL bits in I2C_TIMING register */
168 I2C_TIMING(i2c_periph) &= ~I2C_TIMING_SCLH;
169 I2C_TIMING(i2c_periph) &= ~I2C_TIMING_SCLL;
170 /* mask SCLH, SCLL bits in I2C_TIMING register */
171 sclh = (uint32_t)(sclh << TIMING_SCLH_OFFSET) & I2C_TIMING_SCLH;
172 scll = (uint32_t)(scll << TIMING_SCLL_OFFSET) & I2C_TIMING_SCLL;
173 /* write SCLH, SCLL bits in I2C_TIMING register */
174 I2C_TIMING(i2c_periph) |= (sclh | scll);
175 }
176
177 /*!
178 \brief configure i2c slave address and transfer direction in master mode
179 \param[in] i2c_periph: I2Cx(x=0,1,2)
180 \param[in] address: 0-0x3FF except reserved address, I2C slave address to be sent
181 \param[in] trans_direction: I2C transfer direction in master mode
182 only one parameter can be selected which is shown as below:
183 \arg I2C_MASTER_TRANSMIT: master transmit
184 \arg I2C_MASTER_RECEIVE: master receive
185 \param[out] none
186 \retval none
187 */
i2c_master_addressing(uint32_t i2c_periph,uint32_t address,uint32_t trans_direction)188 void i2c_master_addressing(uint32_t i2c_periph, uint32_t address, uint32_t trans_direction)
189 {
190 /* configure slave address */
191 I2C_CTL1(i2c_periph) &= ~I2C_CTL1_SADDRESS;
192 I2C_CTL1(i2c_periph) |= address;
193 /* configure transfer direction */
194 I2C_CTL1(i2c_periph) &= ~I2C_CTL1_TRDIR;
195 I2C_CTL1(i2c_periph) |= trans_direction;
196 }
197
198 /*!
199 \brief 10-bit address header executes read direction only in master receive mode
200 \param[in] i2c_periph: I2Cx(x=0,1,2)
201 \param[out] none
202 \retval none
203 */
i2c_address10_header_enable(uint32_t i2c_periph)204 void i2c_address10_header_enable(uint32_t i2c_periph)
205 {
206 I2C_CTL1(i2c_periph) |= I2C_CTL1_HEAD10R;
207 }
208
209 /*!
210 \brief 10-bit address header executes complete sequence in master receive mode
211 \param[in] i2c_periph: I2Cx(x=0,1,2)
212 \param[out] none
213 \retval none
214 */
i2c_address10_header_disable(uint32_t i2c_periph)215 void i2c_address10_header_disable(uint32_t i2c_periph)
216 {
217 I2C_CTL1(i2c_periph) &= ~I2C_CTL1_HEAD10R;
218 }
219
220 /*!
221 \brief enable 10-bit addressing mode in master mode
222 \param[in] i2c_periph: I2Cx(x=0,1,2)
223 \param[out] none
224 \retval none
225 */
i2c_address10_enable(uint32_t i2c_periph)226 void i2c_address10_enable(uint32_t i2c_periph)
227 {
228 I2C_CTL1(i2c_periph) |= I2C_CTL1_ADD10EN;
229 }
230
231 /*!
232 \brief disable 10-bit addressing mode in master mode
233 \param[in] i2c_periph: I2Cx(x=0,1,2)
234 \param[out] none
235 \retval none
236 */
i2c_address10_disable(uint32_t i2c_periph)237 void i2c_address10_disable(uint32_t i2c_periph)
238 {
239 I2C_CTL1(i2c_periph) &= ~I2C_CTL1_ADD10EN;
240 }
241
242 /*!
243 \brief enable I2C automatic end mode in master mode
244 \param[in] i2c_periph: I2Cx(x=0,1,2)
245 \param[out] none
246 \retval none
247 */
i2c_automatic_end_enable(uint32_t i2c_periph)248 void i2c_automatic_end_enable(uint32_t i2c_periph)
249 {
250 I2C_CTL1(i2c_periph) |= I2C_CTL1_AUTOEND;
251 }
252
253 /*!
254 \brief disable I2C automatic end mode in master mode
255 \param[in] i2c_periph: I2Cx(x=0,1,2)
256 \param[out] none
257 \retval none
258 */
i2c_automatic_end_disable(uint32_t i2c_periph)259 void i2c_automatic_end_disable(uint32_t i2c_periph)
260 {
261 I2C_CTL1(i2c_periph) &= ~I2C_CTL1_AUTOEND;
262 }
263
264 /*!
265 \brief enable the response to a general call
266 \param[in] i2c_periph: I2Cx(x=0,1,2)
267 \param[in] none
268 \param[out] none
269 \retval none
270 */
i2c_slave_response_to_gcall_enable(uint32_t i2c_periph)271 void i2c_slave_response_to_gcall_enable(uint32_t i2c_periph)
272 {
273 I2C_CTL0(i2c_periph) |= I2C_CTL0_GCEN;
274 }
275
276 /*!
277 \brief disable the response to a general call
278 \param[in] i2c_periph: I2Cx(x=0,1,2)
279 \param[in] none
280 \param[out] none
281 \retval none
282 */
i2c_slave_response_to_gcall_disable(uint32_t i2c_periph)283 void i2c_slave_response_to_gcall_disable(uint32_t i2c_periph)
284 {
285 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_GCEN;
286 }
287
288 /*!
289 \brief enable to stretch SCL low when data is not ready in slave mode
290 \param[in] i2c_periph: I2Cx(x=0,1,2)
291 \param[in] none
292 \param[out] none
293 \retval none
294 */
i2c_stretch_scl_low_enable(uint32_t i2c_periph)295 void i2c_stretch_scl_low_enable(uint32_t i2c_periph)
296 {
297 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_SS;
298 }
299
300 /*!
301 \brief disable to stretch SCL low when data is not ready in slave mode
302 \param[in] i2c_periph: I2Cx(x=0,1,2)
303 \param[in] none
304 \param[out] none
305 \retval none
306 */
i2c_stretch_scl_low_disable(uint32_t i2c_periph)307 void i2c_stretch_scl_low_disable(uint32_t i2c_periph)
308 {
309 I2C_CTL0(i2c_periph) |= I2C_CTL0_SS;
310 }
311
312 /*!
313 \brief configure i2c slave address
314 \param[in] i2c_periph: I2Cx(x=0,1,2)
315 \param[in] address: I2C address
316 \param[in] addr_format: 7bits or 10bits
317 only one parameter can be selected which is shown as below:
318 \arg I2C_ADDFORMAT_7BITS: 7bits
319 \arg I2C_ADDFORMAT_10BITS: 10bits
320 \param[out] none
321 \retval none
322 */
i2c_address_config(uint32_t i2c_periph,uint32_t address,uint32_t addr_format)323 void i2c_address_config(uint32_t i2c_periph, uint32_t address, uint32_t addr_format)
324 {
325 /* configure ADDRESS[7:1] and address format */
326 address = address & I2C_ADDRESS_MASK;
327 I2C_SADDR0(i2c_periph) = (addr_format | address);
328 /* enable i2c address in slave mode */
329 I2C_SADDR0(i2c_periph) |= I2C_SADDR0_ADDRESSEN;
330 }
331
332 /*!
333 \brief define which bits of ADDRESS[7:1] need to compare with the incoming address byte
334 \param[in] i2c_periph: I2Cx(x=0,1,2)
335 \param[in] compare_bits: the bits need to compare
336 one or more parameters can be selected which are shown as below:
337 ADDRESS_BIT1_COMPARE: address bit1 needs compare
338 ADDRESS_BIT2_COMPARE: address bit2 needs compare
339 ADDRESS_BIT3_COMPARE: address bit3 needs compare
340 ADDRESS_BIT4_COMPARE: address bit4 needs compare
341 ADDRESS_BIT5_COMPARE: address bit5 needs compare
342 ADDRESS_BIT6_COMPARE: address bit6 needs compare
343 ADDRESS_BIT7_COMPARE: address bit7 needs compare
344 \param[out] none
345 \retval none
346 */
i2c_address_bit_compare_config(uint32_t i2c_periph,uint32_t compare_bits)347 void i2c_address_bit_compare_config(uint32_t i2c_periph, uint32_t compare_bits)
348 {
349 I2C_CTL2(i2c_periph) &= ~I2C_CTL2_ADDM;
350 I2C_CTL2(i2c_periph) |= compare_bits;
351 }
352
353 /*!
354 \brief disable i2c address in slave mode
355 \param[in] i2c_periph: I2Cx(x=0,1,2)
356 \param[out] none
357 \retval none
358 */
i2c_address_disable(uint32_t i2c_periph)359 void i2c_address_disable(uint32_t i2c_periph)
360 {
361 I2C_SADDR0(i2c_periph) &= ~I2C_SADDR0_ADDRESSEN;
362 }
363
364 /*!
365 \brief configure i2c second slave address
366 \param[in] i2c_periph: I2Cx(x=0,1,2)
367 \param[in] address: I2C address
368 \param[in] addr_mask: the bits not need to compare
369 one or more parameters can be selected which are shown as below:
370 \arg ADDRESS2_NO_MASK: no mask, all the bits must be compared
371 \arg ADDRESS2_MASK_BIT1: ADDRESS2[1] is masked, only ADDRESS2[7:2] are compared
372 \arg ADDRESS2_MASK_BIT1_2: ADDRESS2[2:1] is masked, only ADDRESS2[7:3] are compared
373 \arg ADDRESS2_MASK_BIT1_3: ADDRESS2[3:1] is masked, only ADDRESS2[7:4] are compared
374 \arg ADDRESS2_MASK_BIT1_4: ADDRESS2[4:1] is masked, only ADDRESS2[7:5] are compared
375 \arg ADDRESS2_MASK_BIT1_5: ADDRESS2[5:1] is masked, only ADDRESS2[7:6] are compared
376 \arg ADDRESS2_MASK_BIT1_6: ADDRESS2[6:1] is masked, only ADDRESS2[7] are compared
377 \arg ADDRESS2_MASK_ALL: all the ADDRESS2[7:1] bits are masked
378 \param[out] none
379 \retval none
380 */
i2c_second_address_config(uint32_t i2c_periph,uint32_t address,uint32_t addr_mask)381 void i2c_second_address_config(uint32_t i2c_periph, uint32_t address, uint32_t addr_mask)
382 {
383 /* configure ADDRESS2[7:1] */
384 address = address & I2C_ADDRESS2_MASK;
385 I2C_SADDR1(i2c_periph) |= address;
386 /* configure ADDRESS2[7:1] mask */
387 I2C_SADDR1(i2c_periph) &= ~I2C_SADDR1_ADDMSK2;
388 I2C_SADDR1(i2c_periph) |= (uint32_t)(addr_mask << SADDR1_ADDMSK_OFFSET);
389 /* enable i2c second address in slave mode */
390 I2C_SADDR1(i2c_periph) |= I2C_SADDR1_ADDRESS2EN;
391 }
392
393 /*!
394 \brief disable i2c second address in slave mode
395 \param[in] i2c_periph: I2Cx(x=0,1,2)
396 \param[out] none
397 \retval none
398 */
i2c_second_address_disable(uint32_t i2c_periph)399 void i2c_second_address_disable(uint32_t i2c_periph)
400 {
401 I2C_SADDR1(i2c_periph) &= ~I2C_SADDR1_ADDRESS2EN;
402 }
403
404 /*!
405 \brief get received match address in slave mode
406 \param[in] i2c_periph: I2Cx(x=0,1,2)
407 \param[out] none
408 \retval received match address
409 */
i2c_recevied_address_get(uint32_t i2c_periph)410 uint32_t i2c_recevied_address_get(uint32_t i2c_periph)
411 {
412 return (uint32_t)((I2C_STAT(i2c_periph) & I2C_STAT_READDR) >> STAT_READDR_OFFSET);
413 }
414
415 /*!
416 \brief enable slave byte control
417 \param[in] i2c_periph: I2Cx(x=0,1,2)
418 \param[in] none
419 \param[out] none
420 \retval none
421 */
i2c_slave_byte_control_enable(uint32_t i2c_periph)422 void i2c_slave_byte_control_enable(uint32_t i2c_periph)
423 {
424 I2C_CTL0(i2c_periph) |= I2C_CTL0_SBCTL;
425 }
426
427 /*!
428 \brief disable slave byte control
429 \param[in] i2c_periph: I2Cx(x=0,1,2)
430 \param[in] none
431 \param[out] none
432 \retval none
433 */
i2c_slave_byte_control_disable(uint32_t i2c_periph)434 void i2c_slave_byte_control_disable(uint32_t i2c_periph)
435 {
436 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_SBCTL;
437 }
438
439 /*!
440 \brief generate a NACK in slave mode
441 \param[in] i2c_periph: I2Cx(x=0,1,2)
442 \param[out] none
443 \retval none
444 */
i2c_nack_enable(uint32_t i2c_periph)445 void i2c_nack_enable(uint32_t i2c_periph)
446 {
447 I2C_CTL1(i2c_periph) |= I2C_CTL1_NACKEN;
448 }
449
450 /*!
451 \brief generate an ACK in slave mode
452 \param[in] i2c_periph: I2Cx(x=0,1,2)
453 \param[out] none
454 \retval none
455 */
i2c_nack_disable(uint32_t i2c_periph)456 void i2c_nack_disable(uint32_t i2c_periph)
457 {
458 I2C_CTL1(i2c_periph) &= ~I2C_CTL1_NACKEN;
459 }
460
461 /*!
462 \brief enable wakeup from deep-sleep mode
463 \param[in] i2c_periph: I2Cx(x=0,1,2)
464 \param[out] none
465 \retval none
466 */
i2c_wakeup_from_deepsleep_enable(uint32_t i2c_periph)467 void i2c_wakeup_from_deepsleep_enable(uint32_t i2c_periph)
468 {
469 I2C_CTL0(i2c_periph) |= I2C_CTL0_WUEN;
470 }
471
472 /*!
473 \brief disable wakeup from deep-sleep mode
474 \param[in] i2c_periph: I2Cx(x=0,1,2)
475 \param[out] none
476 \retval none
477 */
i2c_wakeup_from_deepsleep_disable(uint32_t i2c_periph)478 void i2c_wakeup_from_deepsleep_disable(uint32_t i2c_periph)
479 {
480 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_WUEN;
481 }
482
483 /*!
484 \brief enable I2C
485 \param[in] i2c_periph: I2Cx(x=0,1,2)
486 \param[out] none
487 \retval none
488 */
i2c_enable(uint32_t i2c_periph)489 void i2c_enable(uint32_t i2c_periph)
490 {
491 I2C_CTL0(i2c_periph) |= I2C_CTL0_I2CEN;
492 }
493
494 /*!
495 \brief disable I2C
496 \param[in] i2c_periph: I2Cx(x=0,1,2)
497 \param[out] none
498 \retval none
499 */
i2c_disable(uint32_t i2c_periph)500 void i2c_disable(uint32_t i2c_periph)
501 {
502 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_I2CEN;
503 }
504
505 /*!
506 \brief generate a START condition on I2C bus
507 \param[in] i2c_periph: I2Cx(x=0,1,2)
508 \param[out] none
509 \retval none
510 */
i2c_start_on_bus(uint32_t i2c_periph)511 void i2c_start_on_bus(uint32_t i2c_periph)
512 {
513 I2C_CTL1(i2c_periph) |= I2C_CTL1_START;
514 }
515
516 /*!
517 \brief generate a STOP condition on I2C bus
518 \param[in] i2c_periph: I2Cx(x=0,1,2)
519 \param[out] none
520 \retval none
521 */
i2c_stop_on_bus(uint32_t i2c_periph)522 void i2c_stop_on_bus(uint32_t i2c_periph)
523 {
524 I2C_CTL1(i2c_periph) |= I2C_CTL1_STOP;
525 }
526
527 /*!
528 \brief I2C transmit data
529 \param[in] i2c_periph: I2Cx(x=0,1,2)
530 \param[in] data: data to be transmitted
531 \param[out] none
532 \retval none
533 */
i2c_data_transmit(uint32_t i2c_periph,uint32_t data)534 void i2c_data_transmit(uint32_t i2c_periph, uint32_t data)
535 {
536 I2C_TDATA(i2c_periph) = (I2C_TDATA_TDATA & data);
537 }
538
539 /*!
540 \brief I2C receive data
541 \param[in] i2c_periph: I2Cx(x=0,1,2)
542 \param[out] none
543 \retval received data
544 */
i2c_data_receive(uint32_t i2c_periph)545 uint32_t i2c_data_receive(uint32_t i2c_periph)
546 {
547 return (I2C_RDATA(i2c_periph) & I2C_RDATA_RDATA);
548 }
549
550 /*!
551 \brief enable I2C reload mode
552 \param[in] i2c_periph: I2Cx(x=0,1,2)
553 \param[out] none
554 \retval none
555 */
i2c_reload_enable(uint32_t i2c_periph)556 void i2c_reload_enable(uint32_t i2c_periph)
557 {
558 I2C_CTL1(i2c_periph) |= I2C_CTL1_RELOAD;
559 }
560
561 /*!
562 \brief disable I2C reload mode
563 \param[in] i2c_periph: I2Cx(x=0,1,2)
564 \param[out] none
565 \retval none
566 */
i2c_reload_disable(uint32_t i2c_periph)567 void i2c_reload_disable(uint32_t i2c_periph)
568 {
569 I2C_CTL1(i2c_periph) &= ~I2C_CTL1_RELOAD;
570 }
571
572 /*!
573 \brief configure number of bytes to be transferred
574 \param[in] i2c_periph: I2Cx(x=0,1,2)
575 \param[in] byte_number: 0x0-0xFF, number of bytes to be transferred
576 \param[out] none
577 \retval none
578 */
i2c_transfer_byte_number_config(uint32_t i2c_periph,uint32_t byte_number)579 void i2c_transfer_byte_number_config(uint32_t i2c_periph, uint32_t byte_number)
580 {
581 I2C_CTL1(i2c_periph) &= (uint32_t)(~I2C_CTL1_BYTENUM);
582 I2C_CTL1(i2c_periph) |= (uint32_t)(byte_number << CTL1_BYTENUM_OFFSET);
583 }
584
585 /*!
586 \brief enable I2C DMA for transmission or reception
587 \param[in] i2c_periph: I2Cx(x=0,1,2)
588 \param[in] dma: I2C DMA
589 only one parameter can be selected which is shown as below:
590 \arg I2C_DMA_TRANSMIT: transmit data using DMA
591 \arg I2C_DMA_RECEIVE: receive data using DMA
592 \param[out] none
593 \retval none
594 */
i2c_dma_enable(uint32_t i2c_periph,uint8_t dma)595 void i2c_dma_enable(uint32_t i2c_periph, uint8_t dma)
596 {
597 if(I2C_DMA_TRANSMIT == dma) {
598 I2C_CTL0(i2c_periph) |= I2C_CTL0_DENT;
599 } else {
600 I2C_CTL0(i2c_periph) |= I2C_CTL0_DENR;
601 }
602 }
603
604 /*!
605 \brief disable I2C DMA for transmission or reception
606 \param[in] i2c_periph: I2Cx(x=0,1,2)
607 \param[in] dma: I2C DMA
608 only one parameter can be selected which is shown as below:
609 \arg I2C_DMA_TRANSMIT: transmit data using DMA
610 \arg I2C_DMA_RECEIVE: receive data using DMA
611 \param[out] none
612 \retval none
613 */
i2c_dma_disable(uint32_t i2c_periph,uint8_t dma)614 void i2c_dma_disable(uint32_t i2c_periph, uint8_t dma)
615 {
616 if(I2C_DMA_TRANSMIT == dma) {
617 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_DENT;
618 } else {
619 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_DENR;
620 }
621 }
622
623 /*!
624 \brief I2C transfers PEC value
625 \param[in] i2c_periph: I2Cx(x=0,1,2)
626 \param[out] none
627 \retval none
628 */
i2c_pec_transfer(uint32_t i2c_periph)629 void i2c_pec_transfer(uint32_t i2c_periph)
630 {
631 I2C_CTL1(i2c_periph) |= I2C_CTL1_PECTRANS;
632 }
633
634 /*!
635 \brief enable I2C PEC calculation
636 \param[in] i2c_periph: I2Cx(x=0,1,2)
637 \param[in] none
638 \param[out] none
639 \retval none
640 */
i2c_pec_enable(uint32_t i2c_periph)641 void i2c_pec_enable(uint32_t i2c_periph)
642 {
643 I2C_CTL0(i2c_periph) |= I2C_CTL0_PECEN;
644 }
645
646 /*!
647 \brief disable I2C PEC calculation
648 \param[in] i2c_periph: I2Cx(x=0,1,2)
649 \param[in] none
650 \param[out] none
651 \retval none
652 */
i2c_pec_disable(uint32_t i2c_periph)653 void i2c_pec_disable(uint32_t i2c_periph)
654 {
655 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_PECEN;
656 }
657
658 /*!
659 \brief get packet error checking value
660 \param[in] i2c_periph: I2Cx(x=0,1,2)
661 \param[out] none
662 \retval PEC value
663 */
i2c_pec_value_get(uint32_t i2c_periph)664 uint32_t i2c_pec_value_get(uint32_t i2c_periph)
665 {
666 return (I2C_PEC(i2c_periph) & I2C_PEC_PECV);
667 }
668
669 /*!
670 \brief enable SMBus alert
671 \param[in] i2c_periph: I2Cx(x=0,1,2)
672 \param[in] none
673 \param[out] none
674 \retval none
675 */
i2c_smbus_alert_enable(uint32_t i2c_periph)676 void i2c_smbus_alert_enable(uint32_t i2c_periph)
677 {
678 I2C_CTL0(i2c_periph) |= I2C_CTL0_SMBALTEN;
679 }
680
681 /*!
682 \brief disable SMBus alert
683 \param[in] i2c_periph: I2Cx(x=0,1,2)
684 \param[in] none
685 \param[out] none
686 \retval none
687 */
i2c_smbus_alert_disable(uint32_t i2c_periph)688 void i2c_smbus_alert_disable(uint32_t i2c_periph)
689 {
690 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_SMBALTEN;
691 }
692
693 /*!
694 \brief enable SMBus device default address
695 \param[in] i2c_periph: I2Cx(x=0,1,2)
696 \param[in] none
697 \param[out] none
698 \retval none
699 */
i2c_smbus_default_addr_enable(uint32_t i2c_periph)700 void i2c_smbus_default_addr_enable(uint32_t i2c_periph)
701 {
702 I2C_CTL0(i2c_periph) |= I2C_CTL0_SMBDAEN;
703 }
704
705 /*!
706 \brief disable SMBus device default address
707 \param[in] i2c_periph: I2Cx(x=0,1,2)
708 \param[in] none
709 \param[out] none
710 \retval none
711 */
i2c_smbus_default_addr_disable(uint32_t i2c_periph)712 void i2c_smbus_default_addr_disable(uint32_t i2c_periph)
713 {
714 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_SMBDAEN;
715 }
716
717 /*!
718 \brief enable SMBus Host address
719 \param[in] i2c_periph: I2Cx(x=0,1,2)
720 \param[in] none
721 \param[out] none
722 \retval none
723 */
i2c_smbus_host_addr_enable(uint32_t i2c_periph)724 void i2c_smbus_host_addr_enable(uint32_t i2c_periph)
725 {
726 I2C_CTL0(i2c_periph) |= I2C_CTL0_SMBHAEN;
727 }
728
729 /*!
730 \brief disable SMBus Host address
731 \param[in] i2c_periph: I2Cx(x=0,1,2)
732 \param[in] none
733 \param[out] none
734 \retval none
735 */
i2c_smbus_host_addr_disable(uint32_t i2c_periph)736 void i2c_smbus_host_addr_disable(uint32_t i2c_periph)
737 {
738 I2C_CTL0(i2c_periph) &= ~I2C_CTL0_SMBHAEN;
739 }
740
741 /*!
742 \brief enable extended clock timeout detection
743 \param[in] i2c_periph: I2Cx(x=0,1,2)
744 \param[out] none
745 \retval none
746 */
i2c_extented_clock_timeout_enable(uint32_t i2c_periph)747 void i2c_extented_clock_timeout_enable(uint32_t i2c_periph)
748 {
749 I2C_TIMEOUT(i2c_periph) |= I2C_TIMEOUT_EXTOEN;
750 }
751
752 /*!
753 \brief disable extended clock timeout detection
754 \param[in] i2c_periph: I2Cx(x=0,1,2)
755 \param[out] none
756 \retval none
757 */
i2c_extented_clock_timeout_disable(uint32_t i2c_periph)758 void i2c_extented_clock_timeout_disable(uint32_t i2c_periph)
759 {
760 I2C_TIMEOUT(i2c_periph) &= ~I2C_TIMEOUT_EXTOEN;
761 }
762
763 /*!
764 \brief enable clock timeout detection
765 \param[in] i2c_periph: I2Cx(x=0,1,2)
766 \param[out] none
767 \retval none
768 */
i2c_clock_timeout_enable(uint32_t i2c_periph)769 void i2c_clock_timeout_enable(uint32_t i2c_periph)
770 {
771 I2C_TIMEOUT(i2c_periph) |= I2C_TIMEOUT_TOEN;
772 }
773
774 /*!
775 \brief disable clock timeout detection
776 \param[in] i2c_periph: I2Cx(x=0,1,2)
777 \param[out] none
778 \retval none
779 */
i2c_clock_timeout_disable(uint32_t i2c_periph)780 void i2c_clock_timeout_disable(uint32_t i2c_periph)
781 {
782 I2C_TIMEOUT(i2c_periph) &= ~I2C_TIMEOUT_TOEN;
783 }
784
785 /*!
786 \brief configure bus timeout B
787 \param[in] i2c_periph: I2Cx(x=0,1,2)
788 \param[in] timeout: bus timeout B
789 \param[out] none
790 \retval none
791 */
i2c_bus_timeout_b_config(uint32_t i2c_periph,uint32_t timeout)792 void i2c_bus_timeout_b_config(uint32_t i2c_periph, uint32_t timeout)
793 {
794 I2C_TIMEOUT(i2c_periph) &= ~I2C_TIMEOUT_BUSTOB;
795 I2C_TIMEOUT(i2c_periph) |= (uint32_t)(timeout << TIMEOUT_BUSTOB_OFFSET);
796 }
797
798 /*!
799 \brief configure bus timeout A
800 \param[in] i2c_periph: I2Cx(x=0,1,2)
801 \param[in] timeout: bus timeout A
802 \param[out] none
803 \retval none
804 */
i2c_bus_timeout_a_config(uint32_t i2c_periph,uint32_t timeout)805 void i2c_bus_timeout_a_config(uint32_t i2c_periph, uint32_t timeout)
806 {
807 I2C_TIMEOUT(i2c_periph) &= ~I2C_TIMEOUT_BUSTOA;
808 I2C_TIMEOUT(i2c_periph) |= timeout;
809 }
810
811 /*!
812 \brief configure idle clock timeout detection
813 \param[in] i2c_periph: I2Cx(x=0,1,2)
814 \param[in] timeout: bus timeout A
815 \arg BUSTOA_DETECT_SCL_LOW: BUSTOA is used to detect SCL low timeout
816 \arg BUSTOA_DETECT_IDLE: BUSTOA is used to detect both SCL and SDA high timeout when the bus is idle
817 \param[out] none
818 \retval none
819 */
i2c_idle_clock_timeout_config(uint32_t i2c_periph,uint32_t timeout)820 void i2c_idle_clock_timeout_config(uint32_t i2c_periph, uint32_t timeout)
821 {
822 I2C_TIMEOUT(i2c_periph) &= ~I2C_TIMEOUT_TOIDLE;
823 I2C_TIMEOUT(i2c_periph) |= timeout;
824 }
825
826 /*!
827 \brief get I2C flag status
828 \param[in] i2c_periph: I2Cx(x=0,1,2)
829 \param[in] flag: I2C flags
830 only one parameter can be selected which is shown as below:
831 \arg I2C_FLAG_TBE: I2C_TDATA is empty during transmitting
832 \arg I2C_FLAG_TI: transmit interrupt
833 \arg I2C_FLAG_RBNE: I2C_RDATA is not empty during receiving
834 \arg I2C_FLAG_ADDSEND: address received matches in slave mode
835 \arg I2C_FLAG_NACK: not acknowledge flag
836 \arg I2C_FLAG_STPDET: STOP condition detected in slave mode
837 \arg I2C_FLAG_TC: transfer complete in master mode
838 \arg I2C_FLAG_TCR: transfer complete reload
839 \arg I2C_FLAG_BERR: bus error
840 \arg I2C_FLAG_LOSTARB: arbitration Lost
841 \arg I2C_FLAG_OUERR: overrun/underrun error in slave mode
842 \arg I2C_FLAG_PECERR: PEC error
843 \arg I2C_FLAG_TIMEOUT: timeout flag
844 \arg I2C_FLAG_SMBALT: SMBus Alert
845 \arg I2C_FLAG_I2CBSY: busy flag
846 \arg I2C_FLAG_TR: whether the I2C is a transmitter or a receiver in slave mode
847 \param[out] none
848 \retval FlagStatus: SET or RESET
849 */
i2c_flag_get(uint32_t i2c_periph,uint32_t flag)850 FlagStatus i2c_flag_get(uint32_t i2c_periph, uint32_t flag)
851 {
852 if(RESET != (I2C_STAT(i2c_periph) & flag)) {
853 return SET;
854 } else {
855 return RESET;
856 }
857 }
858
859 /*!
860 \brief clear I2C flag status
861 \param[in] i2c_periph: I2Cx(x=0,1,2)
862 \param[in] flag: I2C flags
863 one or more parameters can be selected which are shown as below:
864 \arg I2C_FLAG_ADDSEND: address received matches in slave mode
865 \arg I2C_FLAG_NACK: not acknowledge flag
866 \arg I2C_FLAG_STPDET: STOP condition detected in slave mode
867 \arg I2C_FLAG_BERR: bus error
868 \arg I2C_FLAG_LOSTARB: arbitration Lost
869 \arg I2C_FLAG_OUERR: overrun/underrun error in slave mode
870 \arg I2C_FLAG_PECERR: PEC error
871 \arg I2C_FLAG_TIMEOUT: timeout flag
872 \arg I2C_FLAG_SMBALT: SMBus Alert
873 \param[out] none
874 \retval none
875 */
i2c_flag_clear(uint32_t i2c_periph,uint32_t flag)876 void i2c_flag_clear(uint32_t i2c_periph, uint32_t flag)
877 {
878 I2C_STATC(i2c_periph) |= flag;
879 }
880
881 /*!
882 \brief enable I2C interrupt
883 \param[in] i2c_periph: I2Cx(x=0,1,2)
884 \param[in] interrupt: I2C interrupts
885 one or more parameters can be selected which are shown as below:
886 \arg I2C_INT_ERR: error interrupt
887 \arg I2C_INT_TC: transfer complete interrupt
888 \arg I2C_INT_STPDET: stop detection interrupt
889 \arg I2C_INT_NACK: not acknowledge received interrupt
890 \arg I2C_INT_ADDM: address match interrupt
891 \arg I2C_INT_RBNE: receive interrupt
892 \arg I2C_INT_TI: transmit interrupt
893 \param[out] none
894 \retval none
895 */
i2c_interrupt_enable(uint32_t i2c_periph,uint32_t interrupt)896 void i2c_interrupt_enable(uint32_t i2c_periph, uint32_t interrupt)
897 {
898 I2C_CTL0(i2c_periph) |= interrupt;
899 }
900
901 /*!
902 \brief disable I2C interrupt
903 \param[in] i2c_periph: I2Cx(x=0,1,2)
904 \param[in] interrupt: I2C interrupts
905 one or more parameters can be selected which are shown as below:
906 \arg I2C_INT_ERR: error interrupt
907 \arg I2C_INT_TC: transfer complete interrupt
908 \arg I2C_INT_STPDET: stop detection interrupt
909 \arg I2C_INT_NACK: not acknowledge received interrupt
910 \arg I2C_INT_ADDM: address match interrupt
911 \arg I2C_INT_RBNE: receive interrupt
912 \arg I2C_INT_TI: transmit interrupt
913 \param[out] none
914 \retval none
915 */
i2c_interrupt_disable(uint32_t i2c_periph,uint32_t interrupt)916 void i2c_interrupt_disable(uint32_t i2c_periph, uint32_t interrupt)
917 {
918 I2C_CTL0(i2c_periph) &= ~interrupt;
919 }
920
921 /*!
922 \brief get I2C interrupt flag status
923 \param[in] i2c_periph: I2Cx(x=0,1,2)
924 \param[in] int_flag: I2C interrupt flags, refer to i2c_interrupt_flag_enum
925 only one parameter can be selected which is shown as below:
926 \arg I2C_INT_FLAG_TI: transmit interrupt flag
927 \arg I2C_INT_FLAG_RBNE: I2C_RDATA is not empty during receiving interrupt flag
928 \arg I2C_INT_FLAG_ADDSEND: address received matches in slave mode interrupt flag
929 \arg I2C_INT_FLAG_NACK: not acknowledge interrupt flag
930 \arg I2C_INT_FLAG_STPDET: stop condition detected in slave mode interrupt flag
931 \arg I2C_INT_FLAG_TC: transfer complete in master mode interrupt flag
932 \arg I2C_INT_FLAG_TCR: transfer complete reload interrupt flag
933 \arg I2C_INT_FLAG_BERR: bus error interrupt flag
934 \arg I2C_INT_FLAG_LOSTARB: arbitration lost interrupt flag
935 \arg I2C_INT_FLAG_OUERR: overrun/underrun error in slave mode interrupt flag
936 \arg I2C_INT_FLAG_PECERR: PEC error interrupt flag
937 \arg I2C_INT_FLAG_TIMEOUT: timeout interrupt flag
938 \arg I2C_INT_FLAG_SMBALT: SMBus Alert interrupt flag
939 \param[out] none
940 \retval FlagStatus: SET or RESET
941 */
i2c_interrupt_flag_get(uint32_t i2c_periph,i2c_interrupt_flag_enum int_flag)942 FlagStatus i2c_interrupt_flag_get(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag)
943 {
944 uint32_t ret1 = RESET;
945 uint32_t ret2 = RESET;
946
947 /* get the status of interrupt enable bit */
948 ret1 = (I2C_REG_VAL(i2c_periph, int_flag) & BIT(I2C_BIT_POS(int_flag)));
949 /* get the status of interrupt flag */
950 ret2 = (I2C_REG_VAL2(i2c_periph, int_flag) & BIT(I2C_BIT_POS2(int_flag)));
951 if(ret1 && ret2) {
952 return SET;
953 } else {
954 return RESET;
955 }
956 }
957
958 /*!
959 \brief clear I2C interrupt flag status
960 \param[in] i2c_periph: I2Cx(x=0,1,2)
961 \param[in] int_flag: I2C interrupt flags, refer to i2c_interrupt_flag_enum
962 only one parameter can be selected which is shown as below:
963 \arg I2C_INT_FLAG_ADDSEND: address received matches in slave mode interrupt flag
964 \arg I2C_INT_FLAG_NACK: not acknowledge interrupt flag
965 \arg I2C_INT_FLAG_STPDET: stop condition detected in slave mode interrupt flag
966 \arg I2C_INT_FLAG_BERR: bus error interrupt flag
967 \arg I2C_INT_FLAG_LOSTARB: arbitration lost interrupt flag
968 \arg I2C_INT_FLAG_OUERR: overrun/underrun error in slave mode interrupt flag
969 \arg I2C_INT_FLAG_PECERR: PEC error interrupt flag
970 \arg I2C_INT_FLAG_TIMEOUT: timeout interrupt flag
971 \arg I2C_INT_FLAG_SMBALT: SMBus Alert interrupt flag
972 \param[out] none
973 \retval none
974 */
i2c_interrupt_flag_clear(uint32_t i2c_periph,i2c_interrupt_flag_enum int_flag)975 void i2c_interrupt_flag_clear(uint32_t i2c_periph, i2c_interrupt_flag_enum int_flag)
976 {
977 I2C_STATC(i2c_periph) |= BIT(I2C_BIT_POS2(int_flag));
978 }
979