1 /***************COPYRIGHT(C) 2019 WCH. A11 rights reserved*********************
2 * File Name : ch32f10x_i2c.c
3 * Author : WCH
4 * Version : V1.0.0
5 * Date : 2019/10/15
6 * Description : This file provides all the I2C firmware functions.
7 *******************************************************************************/
8 #include "ch32f10x_i2c.h"
9 #include "ch32f10x_rcc.h"
10
11
12 /* I2C SPE mask */
13 #define CTLR1_PE_Set ((uint16_t)0x0001)
14 #define CTLR1_PE_Reset ((uint16_t)0xFFFE)
15
16 /* I2C START mask */
17 #define CTLR1_START_Set ((uint16_t)0x0100)
18 #define CTLR1_START_Reset ((uint16_t)0xFEFF)
19
20 /* I2C STOP mask */
21 #define CTLR1_STOP_Set ((uint16_t)0x0200)
22 #define CTLR1_STOP_Reset ((uint16_t)0xFDFF)
23
24 /* I2C ACK mask */
25 #define CTLR1_ACK_Set ((uint16_t)0x0400)
26 #define CTLR1_ACK_Reset ((uint16_t)0xFBFF)
27
28 /* I2C ENGC mask */
29 #define CTLR1_ENGC_Set ((uint16_t)0x0040)
30 #define CTLR1_ENGC_Reset ((uint16_t)0xFFBF)
31
32 /* I2C SWRST mask */
33 #define CTLR1_SWRST_Set ((uint16_t)0x8000)
34 #define CTLR1_SWRST_Reset ((uint16_t)0x7FFF)
35
36 /* I2C PEC mask */
37 #define CTLR1_PEC_Set ((uint16_t)0x1000)
38 #define CTLR1_PEC_Reset ((uint16_t)0xEFFF)
39
40 /* I2C ENPEC mask */
41 #define CTLR1_ENPEC_Set ((uint16_t)0x0020)
42 #define CTLR1_ENPEC_Reset ((uint16_t)0xFFDF)
43
44 /* I2C ENARP mask */
45 #define CTLR1_ENARP_Set ((uint16_t)0x0010)
46 #define CTLR1_ENARP_Reset ((uint16_t)0xFFEF)
47
48 /* I2C NOSTRETCH mask */
49 #define CTLR1_NOSTRETCH_Set ((uint16_t)0x0080)
50 #define CTLR1_NOSTRETCH_Reset ((uint16_t)0xFF7F)
51
52 /* I2C registers Masks */
53 #define CTLR1_CLEAR_Mask ((uint16_t)0xFBF5)
54
55 /* I2C DMAEN mask */
56 #define CTLR2_DMAEN_Set ((uint16_t)0x0800)
57 #define CTLR2_DMAEN_Reset ((uint16_t)0xF7FF)
58
59 /* I2C LAST mask */
60 #define CTLR2_LAST_Set ((uint16_t)0x1000)
61 #define CTLR2_LAST_Reset ((uint16_t)0xEFFF)
62
63 /* I2C FREQ mask */
64 #define CTLR2_FREQ_Reset ((uint16_t)0xFFC0)
65
66 /* I2C ADD0 mask */
67 #define OADDR1_ADD0_Set ((uint16_t)0x0001)
68 #define OADDR1_ADD0_Reset ((uint16_t)0xFFFE)
69
70 /* I2C ENDUAL mask */
71 #define OADDR2_ENDUAL_Set ((uint16_t)0x0001)
72 #define OADDR2_ENDUAL_Reset ((uint16_t)0xFFFE)
73
74 /* I2C ADD2 mask */
75 #define OADDR2_ADD2_Reset ((uint16_t)0xFF01)
76
77 /* I2C F/S mask */
78 #define CKCFGR_FS_Set ((uint16_t)0x8000)
79
80 /* I2C CCR mask */
81 #define CKCFGR_CCR_Set ((uint16_t)0x0FFF)
82
83 /* I2C FLAG mask */
84 #define FLAG_Mask ((uint32_t)0x00FFFFFF)
85
86 /* I2C Interrupt Enable mask */
87 #define ITEN_Mask ((uint32_t)0x07000000)
88
89
90 /*******************************************************************************
91 * Function Name : I2C_DeInit
92 * Description : Deinitializes the I2Cx peripheral registers to their default
93 * reset values.
94 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
95 * Return : None
96 *******************************************************************************/
I2C_DeInit(I2C_TypeDef * I2Cx)97 void I2C_DeInit(I2C_TypeDef* I2Cx)
98 {
99 if (I2Cx == I2C1)
100 {
101 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
102 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
103 }
104 else
105 {
106 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
107 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
108 }
109 }
110
111 /*******************************************************************************
112 * Function Name : I2C_Init
113 * Description : Initializes the I2Cx peripheral according to the specified
114 * parameters in the I2C_InitStruct.
115 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
116 * I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
117 * contains the configuration information for the specified I2C peripheral.
118 * Return : None
119 *******************************************************************************/
I2C_Init(I2C_TypeDef * I2Cx,I2C_InitTypeDef * I2C_InitStruct)120 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
121 {
122 uint16_t tmpreg = 0, freqrange = 0;
123 uint16_t result = 0x04;
124 uint32_t pclk1 = 8000000;
125
126 RCC_ClocksTypeDef rcc_clocks;
127
128 /*---------------------------- I2Cx CR2 Configuration ------------------------*/
129 tmpreg = I2Cx->CTLR2;
130 tmpreg &= CTLR2_FREQ_Reset;
131 RCC_GetClocksFreq(&rcc_clocks);
132 pclk1 = rcc_clocks.PCLK1_Frequency;
133 freqrange = (uint16_t)(pclk1 / 1000000);
134 tmpreg |= freqrange;
135 I2Cx->CTLR2 = tmpreg;
136
137 /*---------------------------- I2Cx CCR Configuration ------------------------*/
138 I2Cx->CTLR1 &= CTLR1_PE_Reset;
139 tmpreg = 0;
140
141 if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
142 {
143 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
144
145 if (result < 0x04)
146 {
147 result = 0x04;
148 }
149
150 tmpreg |= result;
151 I2Cx->RTR = freqrange + 1;
152 }
153 else
154 {
155 if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
156 {
157 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));
158 }
159 else
160 {
161 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));
162 result |= I2C_DutyCycle_16_9;
163 }
164
165 if ((result & CKCFGR_CCR_Set) == 0)
166 {
167 result |= (uint16_t)0x0001;
168 }
169
170 tmpreg |= (uint16_t)(result | CKCFGR_FS_Set);
171 I2Cx->RTR = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
172 }
173
174 I2Cx->CKCFGR = tmpreg;
175 I2Cx->CTLR1 |= CTLR1_PE_Set;
176
177 /*---------------------------- I2Cx CR1 Configuration ------------------------*/
178 tmpreg = I2Cx->CTLR1;
179 tmpreg &= CTLR1_CLEAR_Mask;
180 tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
181 I2Cx->CTLR1 = tmpreg;
182
183 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
184 I2Cx->OADDR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
185 }
186
187 /*******************************************************************************
188 * Function Name : I2C_StructInit
189 * Description : Fills each I2C_InitStruct member with its default value.
190 * Input : I2C_InitStruct: pointer to an I2C_InitTypeDef structure which
191 * will be initialized.
192 * Return : None
193 *******************************************************************************/
I2C_StructInit(I2C_InitTypeDef * I2C_InitStruct)194 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
195 {
196 /*---------------- Reset I2C init structure parameters values ----------------*/
197 I2C_InitStruct->I2C_ClockSpeed = 5000;
198 I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
199 I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
200 I2C_InitStruct->I2C_OwnAddress1 = 0;
201 I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
202 I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
203 }
204
205 /*******************************************************************************
206 * Function Name : I2C_Cmd
207 * Description : Enables or disables the specified I2C peripheral.
208 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
209 * NewState: ENABLE or DISABLE.
210 * Return : None
211 *******************************************************************************/
I2C_Cmd(I2C_TypeDef * I2Cx,FunctionalState NewState)212 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
213 {
214 if (NewState != DISABLE)
215 {
216 I2Cx->CTLR1 |= CTLR1_PE_Set;
217 }
218 else
219 {
220 I2Cx->CTLR1 &= CTLR1_PE_Reset;
221 }
222 }
223
224 /*******************************************************************************
225 * Function Name : I2C_DMACmd
226 * Description : Enables or disables the specified I2C DMA requests.
227 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
228 * NewState: ENABLE or DISABLE.
229 * Return : None
230 *******************************************************************************/
I2C_DMACmd(I2C_TypeDef * I2Cx,FunctionalState NewState)231 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
232 {
233 if (NewState != DISABLE)
234 {
235 I2Cx->CTLR2 |= CTLR2_DMAEN_Set;
236 }
237 else
238 {
239 I2Cx->CTLR2 &= CTLR2_DMAEN_Reset;
240 }
241 }
242
243 /*******************************************************************************
244 * Function Name : I2C_DMALastTransferCmd
245 * Description : Specifies if the next DMA transfer will be the last one.
246 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
247 * NewState: ENABLE or DISABLE.
248 * Return : None
249 *******************************************************************************/
I2C_DMALastTransferCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)250 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
251 {
252 if (NewState != DISABLE)
253 {
254 I2Cx->CTLR2 |= CTLR2_LAST_Set;
255 }
256 else
257 {
258 I2Cx->CTLR2 &= CTLR2_LAST_Reset;
259 }
260 }
261
262 /*******************************************************************************
263 * Function Name : I2C_GenerateSTART
264 * Description : Generates I2Cx communication START condition.
265 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
266 * NewState: ENABLE or DISABLE.
267 * Return : None
268 *******************************************************************************/
I2C_GenerateSTART(I2C_TypeDef * I2Cx,FunctionalState NewState)269 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
270 {
271 if (NewState != DISABLE)
272 {
273 I2Cx->CTLR1 |= CTLR1_START_Set;
274 }
275 else
276 {
277 I2Cx->CTLR1 &= CTLR1_START_Reset;
278 }
279 }
280
281 /*******************************************************************************
282 * Function Name : I2C_GenerateSTOP
283 * Description : Generates I2Cx communication STOP condition.
284 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
285 * NewState: ENABLE or DISABLE.
286 * Return : None
287 *******************************************************************************/
I2C_GenerateSTOP(I2C_TypeDef * I2Cx,FunctionalState NewState)288 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
289 {
290 if (NewState != DISABLE)
291 {
292 I2Cx->CTLR1 |= CTLR1_STOP_Set;
293 }
294 else
295 {
296 I2Cx->CTLR1 &= CTLR1_STOP_Reset;
297 }
298 }
299
300 /*******************************************************************************
301 * Function Name : I2C_AcknowledgeConfig
302 * Description : Enables or disables the specified I2C acknowledge feature.
303 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
304 * NewState: ENABLE or DISABLE.
305 * Return : None
306 *******************************************************************************/
I2C_AcknowledgeConfig(I2C_TypeDef * I2Cx,FunctionalState NewState)307 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
308 {
309 if (NewState != DISABLE)
310 {
311 I2Cx->CTLR1 |= CTLR1_ACK_Set;
312 }
313 else
314 {
315 I2Cx->CTLR1 &= CTLR1_ACK_Reset;
316 }
317 }
318
319 /*******************************************************************************
320 * Function Name : I2C_OwnAddress2Config
321 * Description : Configures the specified I2C own address2.
322 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
323 * Address: specifies the 7bit I2C own address2.
324 * Return : None
325 *******************************************************************************/
I2C_OwnAddress2Config(I2C_TypeDef * I2Cx,uint8_t Address)326 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
327 {
328 uint16_t tmpreg = 0;
329
330 tmpreg = I2Cx->OADDR2;
331 tmpreg &= OADDR2_ADD2_Reset;
332 tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
333 I2Cx->OADDR2 = tmpreg;
334 }
335
336 /*******************************************************************************
337 * Function Name : I2C_DualAddressCmd
338 * Description : Enables or disables the specified I2C dual addressing mode.
339 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
340 * NewState: ENABLE or DISABLE.
341 * Return : None
342 *******************************************************************************/
I2C_DualAddressCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)343 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
344 {
345 if (NewState != DISABLE)
346 {
347 I2Cx->OADDR2 |= OADDR2_ENDUAL_Set;
348 }
349 else
350 {
351 I2Cx->OADDR2 &= OADDR2_ENDUAL_Reset;
352 }
353 }
354
355 /*******************************************************************************
356 * Function Name : I2C_GeneralCallCmd
357 * Description : Enables or disables the specified I2C general call feature.
358 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
359 * NewState: ENABLE or DISABLE.
360 * Return : None
361 *******************************************************************************/
I2C_GeneralCallCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)362 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
363 {
364 if (NewState != DISABLE)
365 {
366 I2Cx->CTLR1 |= CTLR1_ENGC_Set;
367 }
368 else
369 {
370 I2Cx->CTLR1 &= CTLR1_ENGC_Reset;
371 }
372 }
373
374 /*******************************************************************************
375 * Function Name : I2C_ITConfig
376 * Description : Enables or disables the specified I2C interrupts.
377 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
378 * I2C_IT: specifies the I2C interrupts sources to be enabled or disabled.
379 * I2C_IT_BUF: Buffer interrupt mask.
380 * I2C_IT_EVT: Event interrupt mask.
381 * I2C_IT_ERR: Error interrupt mask.
382 * NewState: ENABLE or DISABLE.
383 * Return : None
384 *******************************************************************************/
I2C_ITConfig(I2C_TypeDef * I2Cx,uint16_t I2C_IT,FunctionalState NewState)385 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
386 {
387 if (NewState != DISABLE)
388 {
389 I2Cx->CTLR2 |= I2C_IT;
390 }
391 else
392 {
393 I2Cx->CTLR2 &= (uint16_t)~I2C_IT;
394 }
395 }
396
397 /*******************************************************************************
398 * Function Name : I2C_SendData
399 * Description : Sends a data byte through the I2Cx peripheral.
400 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
401 * Data: Byte to be transmitted.
402 * Return : None
403 *******************************************************************************/
I2C_SendData(I2C_TypeDef * I2Cx,uint8_t Data)404 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
405 {
406 I2Cx->DATAR = Data;
407 }
408
409 /*******************************************************************************
410 * Function Name : I2C_ReceiveData
411 * Description : Returns the most recent received data by the I2Cx peripheral.
412 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
413 * Return : The value of the received data.
414 *******************************************************************************/
I2C_ReceiveData(I2C_TypeDef * I2Cx)415 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
416 {
417 return (uint8_t)I2Cx->DATAR;
418 }
419
420 /*******************************************************************************
421 * Function Name : I2C_Send7bitAddress
422 * Description : Transmits the address byte to select the slave device.
423 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
424 * Address: specifies the slave address which will be transmitted.
425 * I2C_Direction: specifies whether the I2C device will be a
426 * Transmitter or a Receiver.
427 * I2C_Direction_Transmitter: Transmitter mode.
428 * I2C_Direction_Receiver: Receiver mode.
429 * Return : None
430 *******************************************************************************/
I2C_Send7bitAddress(I2C_TypeDef * I2Cx,uint8_t Address,uint8_t I2C_Direction)431 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
432 {
433 if (I2C_Direction != I2C_Direction_Transmitter)
434 {
435 Address |= OADDR1_ADD0_Set;
436 }
437 else
438 {
439 Address &= OADDR1_ADD0_Reset;
440 }
441
442 I2Cx->DATAR = Address;
443 }
444
445 /*******************************************************************************
446 * Function Name : I2C_ReadRegister
447 * Description : Reads the specified I2C register and returns its value.
448 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
449 * I2C_Register: specifies the register to read.
450 * I2C_Register_CTLR1.
451 * I2C_Register_CTLR2.
452 * I2C_Register_OADDR1.
453 * I2C_Register_OADDR2.
454 * I2C_Register_DATAR.
455 * I2C_Register_STAR1.
456 * I2C_Register_STAR2.
457 * I2C_Register_CKCFGR.
458 * I2C_Register_RTR.
459 * Return : The value of the received data.
460 *******************************************************************************/
I2C_ReadRegister(I2C_TypeDef * I2Cx,uint8_t I2C_Register)461 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
462 {
463 __IO uint32_t tmp = 0;
464
465 tmp = (uint32_t) I2Cx;
466 tmp += I2C_Register;
467
468 return (*(__IO uint16_t *) tmp);
469 }
470
471 /*******************************************************************************
472 * Function Name : I2C_SoftwareResetCmd
473 * Description : Enables or disables the specified I2C software reset.
474 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
475 * NewState: ENABLE or DISABLE.
476 * Return : None
477 *******************************************************************************/
I2C_SoftwareResetCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)478 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
479 {
480 if (NewState != DISABLE)
481 {
482 I2Cx->CTLR1 |= CTLR1_SWRST_Set;
483 }
484 else
485 {
486 I2Cx->CTLR1 &= CTLR1_SWRST_Reset;
487 }
488 }
489
490 /*******************************************************************************
491 * Function Name : I2C_NACKPositionConfig
492 * Description : Selects the specified I2C NACK position in master receiver mode.
493 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
494 * I2C_NACKPosition: specifies the NACK position.
495 * I2C_NACKPosition_Next: indicates that the next byte will be
496 * the last received byte.
497 * I2C_NACKPosition_Current: indicates that current byte is the
498 * last received byte.
499 * Return : None
500 *******************************************************************************/
I2C_NACKPositionConfig(I2C_TypeDef * I2Cx,uint16_t I2C_NACKPosition)501 void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition)
502 {
503 if (I2C_NACKPosition == I2C_NACKPosition_Next)
504 {
505 I2Cx->CTLR1 |= I2C_NACKPosition_Next;
506 }
507 else
508 {
509 I2Cx->CTLR1 &= I2C_NACKPosition_Current;
510 }
511 }
512
513 /*******************************************************************************
514 * Function Name : I2C_SMBusAlertConfig
515 * Description : Drives the SMBusAlert pin high or low for the specified I2C.
516 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
517 * I2C_SMBusAlert: specifies SMBAlert pin level.
518 * I2C_SMBusAlert_Low: SMBAlert pin driven low.
519 * I2C_SMBusAlert_High: SMBAlert pin driven high.
520 * Return : None
521 *******************************************************************************/
I2C_SMBusAlertConfig(I2C_TypeDef * I2Cx,uint16_t I2C_SMBusAlert)522 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert)
523 {
524 if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
525 {
526 I2Cx->CTLR1 |= I2C_SMBusAlert_Low;
527 }
528 else
529 {
530 I2Cx->CTLR1 &= I2C_SMBusAlert_High;
531 }
532 }
533
534 /*******************************************************************************
535 * Function Name : I2C_TransmitPEC
536 * Description : Enables or disables the specified I2C PEC transfer.
537 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
538 * NewState: ENABLE or DISABLE.
539 * Return : None
540 *******************************************************************************/
I2C_TransmitPEC(I2C_TypeDef * I2Cx,FunctionalState NewState)541 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
542 {
543 if (NewState != DISABLE)
544 {
545 I2Cx->CTLR1 |= CTLR1_PEC_Set;
546 }
547 else
548 {
549 I2Cx->CTLR1 &= CTLR1_PEC_Reset;
550 }
551 }
552
553 /*******************************************************************************
554 * Function Name : I2C_PECPositionConfig
555 * Description : Selects the specified I2C PEC position.
556 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
557 * I2C_PECPosition: specifies the PEC position.
558 * I2C_PECPosition_Next: indicates that the next byte is PEC.
559 * I2C_PECPosition_Current: indicates that current byte is PEC.
560 * Return : None
561 *******************************************************************************/
I2C_PECPositionConfig(I2C_TypeDef * I2Cx,uint16_t I2C_PECPosition)562 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition)
563 {
564 if (I2C_PECPosition == I2C_PECPosition_Next)
565 {
566 I2Cx->CTLR1 |= I2C_PECPosition_Next;
567 }
568 else
569 {
570 I2Cx->CTLR1 &= I2C_PECPosition_Current;
571 }
572 }
573
574 /*******************************************************************************
575 * Function Name : I2C_CalculatePEC
576 * Description : Enables or disables the PEC value calculation of the transferred bytes.
577 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
578 * NewState: ENABLE or DISABLE.
579 * Return : None
580 *******************************************************************************/
I2C_CalculatePEC(I2C_TypeDef * I2Cx,FunctionalState NewState)581 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
582 {
583 if (NewState != DISABLE)
584 {
585 I2Cx->CTLR1 |= CTLR1_ENPEC_Set;
586 }
587 else
588 {
589 I2Cx->CTLR1 &= CTLR1_ENPEC_Reset;
590 }
591 }
592
593 /*******************************************************************************
594 * Function Name : I2C_GetPEC
595 * Description : Returns the PEC value for the specified I2C.
596 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
597 * Return : The PEC value.
598 *******************************************************************************/
I2C_GetPEC(I2C_TypeDef * I2Cx)599 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
600 {
601 return ((I2Cx->STAR2) >> 8);
602 }
603
604 /*******************************************************************************
605 * Function Name : I2C_ARPCmd
606 * Description : Enables or disables the specified I2C ARP.
607 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
608 * NewState: ENABLE or DISABLE.
609 * Return : The PEC value.
610 *******************************************************************************/
I2C_ARPCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)611 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
612 {
613 if (NewState != DISABLE)
614 {
615 I2Cx->CTLR1 |= CTLR1_ENARP_Set;
616 }
617 else
618 {
619 I2Cx->CTLR1 &= CTLR1_ENARP_Reset;
620 }
621 }
622
623 /*******************************************************************************
624 * Function Name : I2C_StretchClockCmd
625 * Description : Enables or disables the specified I2C Clock stretching.
626 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
627 * NewState: ENABLE or DISABLE.
628 * Return : None
629 *******************************************************************************/
I2C_StretchClockCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)630 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
631 {
632 if (NewState == DISABLE)
633 {
634 I2Cx->CTLR1 |= CTLR1_NOSTRETCH_Set;
635 }
636 else
637 {
638 I2Cx->CTLR1 &= CTLR1_NOSTRETCH_Reset;
639 }
640 }
641
642 /*******************************************************************************
643 * Function Name : I2C_FastModeDutyCycleConfig
644 * Description : Selects the specified I2C fast mode duty cycle.
645 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
646 * I2C_DutyCycle: specifies the fast mode duty cycle.
647 * I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2.
648 * I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9.
649 * Return : None
650 *******************************************************************************/
I2C_FastModeDutyCycleConfig(I2C_TypeDef * I2Cx,uint16_t I2C_DutyCycle)651 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle)
652 {
653 if (I2C_DutyCycle != I2C_DutyCycle_16_9)
654 {
655 I2Cx->CKCFGR &= I2C_DutyCycle_2;
656 }
657 else
658 {
659 I2Cx->CKCFGR |= I2C_DutyCycle_16_9;
660 }
661 }
662
663 /**
664 ****************************************************************************************
665 *
666 * I2C State Monitoring Functions
667 *
668 ****************************************************************************************
669 * This I2C driver provides three different ways for I2C state monitoring
670 * depending on the application requirements and constraints:
671 *
672 *
673 * 1) Basic state monitoring:
674 * Using I2C_CheckEvent() function:
675 * It compares the status registers (SR1 and SR2) content to a given event
676 * (can be the combination of one or more flags).
677 * It returns SUCCESS if the current status includes the given flags
678 * and returns ERROR if one or more flags are missing in the current status.
679 * - When to use:
680 * - This function is suitable for most applications as well as for startup
681 * activity since the events are fully described in the product reference manual
682 * (RM0008).
683 * - It is also suitable for users who need to define their own events.
684 * - Limitations:
685 * - If an error occurs (ie. error flags are set besides to the monitored flags),
686 * the I2C_CheckEvent() function may return SUCCESS despite the communication
687 * hold or corrupted real state.
688 * In this case, it is advised to use error interrupts to monitor the error
689 * events and handle them in the interrupt IRQ handler.
690 *
691 * @note
692 * For error management, it is advised to use the following functions:
693 * - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
694 * - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
695 * Where x is the peripheral instance (I2C1, I2C2 ...)
696 * - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into I2Cx_ER_IRQHandler()
697 * in order to determine which error occured.
698 * - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
699 * and/or I2C_GenerateStop() in order to clear the error flag and source,
700 * and return to correct communication status.
701 *
702 *
703 * 2) Advanced state monitoring:
704 * Using the function I2C_GetLastEvent() which returns the image of both status
705 * registers in a single word (uint32_t) (Status Register 2 value is shifted left
706 * by 16 bits and concatenated to Status Register 1).
707 * - When to use:
708 * - This function is suitable for the same applications above but it allows to
709 * overcome the mentioned limitation of I2C_GetFlagStatus() function.
710 * The returned value could be compared to events already defined in the
711 * library (ch32f10x_i2c.h) or to custom values defined by user.
712 * - This function is suitable when multiple flags are monitored at the same time.
713 * - At the opposite of I2C_CheckEvent() function, this function allows user to
714 * choose when an event is accepted (when all events flags are set and no
715 * other flags are set or just when the needed flags are set like
716 * I2C_CheckEvent() function).
717 * - Limitations:
718 * - User may need to define his own events.
719 * - Same remark concerning the error management is applicable for this
720 * function if user decides to check only regular communication flags (and
721 * ignores error flags).
722 *
723 *
724 * 3) Flag-based state monitoring:
725 * Using the function I2C_GetFlagStatus() which simply returns the status of
726 * one single flag (ie. I2C_FLAG_RXNE ...).
727 * - When to use:
728 * - This function could be used for specific applications or in debug phase.
729 * - It is suitable when only one flag checking is needed (most I2C events
730 * are monitored through multiple flags).
731 * - Limitations:
732 * - When calling this function, the Status register is accessed. Some flags are
733 * cleared when the status register is accessed. So checking the status
734 * of one Flag, may clear other ones.
735 * - Function may need to be called twice or more in order to monitor one
736 * single event.
737 *
738 * For detailed description of Events, please refer to section I2C_Events in
739 * ch32f10x_i2c.h file.
740 *
741 */
742
743 /**
744 *
745 * 1) Basic state monitoring
746 *******************************************************************************
747 */
748
749 /*******************************************************************************
750 * Function Name : I2C_CheckEvent
751 * Description : Checks whether the last I2Cx Event is equal to the one passed
752 * as parameter.
753 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
754 * I2C_EVENT: specifies the event to be checked.
755 * I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED : EV1.
756 * I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED : EV1.
757 * I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED : EV1.
758 * I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED : EV1.
759 * I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED : EV1.
760 * I2C_EVENT_SLAVE_BYTE_RECEIVED : EV2.
761 * (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF) : EV2.
762 * (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL) : EV2.
763 * I2C_EVENT_SLAVE_BYTE_TRANSMITTED : EV3.
764 * (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF) : EV3.
765 * (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL) : EV3.
766 * I2C_EVENT_SLAVE_ACK_FAILURE : EV3_2.
767 * I2C_EVENT_SLAVE_STOP_DETECTED : EV4.
768 * I2C_EVENT_MASTER_MODE_SELECT : EV5.
769 * I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED : EV6.
770 * I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED : EV6.
771 * I2C_EVENT_MASTER_BYTE_RECEIVED : EV7.
772 * I2C_EVENT_MASTER_BYTE_TRANSMITTING : EV8.
773 * I2C_EVENT_MASTER_BYTE_TRANSMITTED : EV8_2.
774 * I2C_EVENT_MASTER_MODE_ADDRESS10 : EV9.
775 * Return : ErrorStatus: SUCCESS or ERROR.
776 *******************************************************************************/
I2C_CheckEvent(I2C_TypeDef * I2Cx,uint32_t I2C_EVENT)777 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
778 {
779 uint32_t lastevent = 0;
780 uint32_t flag1 = 0, flag2 = 0;
781 ErrorStatus status = ERROR;
782
783 flag1 = I2Cx->STAR1;
784 flag2 = I2Cx->STAR2;
785 flag2 = flag2 << 16;
786
787 lastevent = (flag1 | flag2) & FLAG_Mask;
788
789 if ((lastevent & I2C_EVENT) == I2C_EVENT)
790 {
791 status = SUCCESS;
792 }
793 else
794 {
795 status = ERROR;
796 }
797
798 return status;
799 }
800
801 /**
802 *
803 * 2) Advanced state monitoring
804 *******************************************************************************
805 */
806
807 /*******************************************************************************
808 * Function Name : I2C_GetLastEvent
809 * Description : Returns the last I2Cx Event.
810 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
811 * Return : The last event.
812 *******************************************************************************/
I2C_GetLastEvent(I2C_TypeDef * I2Cx)813 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
814 {
815 uint32_t lastevent = 0;
816 uint32_t flag1 = 0, flag2 = 0;
817
818 flag1 = I2Cx->STAR1;
819 flag2 = I2Cx->STAR2;
820 flag2 = flag2 << 16;
821 lastevent = (flag1 | flag2) & FLAG_Mask;
822
823 return lastevent;
824 }
825
826 /**
827 *
828 * 3) Flag-based state monitoring
829 *******************************************************************************
830 */
831
832 /*******************************************************************************
833 * Function Name : I2C_GetFlagStatus
834 * Description : Checks whether the last I2Cx Event is equal to the one passed
835 * as parameter.
836 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
837 * I2C_FLAG: specifies the flag to check.
838 * I2C_FLAG_DUALF: Dual flag (Slave mode).
839 * I2C_FLAG_SMBHOST: SMBus host header (Slave mode).
840 * I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode).
841 * I2C_FLAG_GENCALL: General call header flag (Slave mode).
842 * I2C_FLAG_TRA: Transmitter/Receiver flag.
843 * I2C_FLAG_BUSY: Bus busy flag.
844 * I2C_FLAG_MSL: Master/Slave flag.
845 * I2C_FLAG_SMBALERT: SMBus Alert flag.
846 * I2C_FLAG_TIMEOUT: Timeout or Tlow error flag.
847 * I2C_FLAG_PECERR: PEC error in reception flag.
848 * I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode).
849 * I2C_FLAG_AF: Acknowledge failure flag.
850 * I2C_FLAG_ARLO: Arbitration lost flag (Master mode).
851 * I2C_FLAG_BERR: Bus error flag.
852 * I2C_FLAG_TXE: Data register empty flag (Transmitter).
853 * I2C_FLAG_RXNE: Data register not empty (Receiver) flag.
854 * I2C_FLAG_STOPF: Stop detection flag (Slave mode).
855 * I2C_FLAG_ADD10: 10-bit header sent flag (Master mode).
856 * I2C_FLAG_BTF: Byte transfer finished flag.
857 * I2C_FLAG_ADDR: Address sent flag (Master mode) "ADSL"
858 * Address matched flag (Slave mode)"ENDA".
859 * I2C_FLAG_SB: Start bit flag (Master mode).
860 * Return : FlagStatus: SET or RESET.
861 *******************************************************************************/
I2C_GetFlagStatus(I2C_TypeDef * I2Cx,uint32_t I2C_FLAG)862 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
863 {
864 FlagStatus bitstatus = RESET;
865 __IO uint32_t i2creg = 0, i2cxbase = 0;
866
867 i2cxbase = (uint32_t)I2Cx;
868 i2creg = I2C_FLAG >> 28;
869 I2C_FLAG &= FLAG_Mask;
870
871 if(i2creg != 0)
872 {
873 i2cxbase += 0x14;
874 }
875 else
876 {
877 I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
878 i2cxbase += 0x18;
879 }
880
881 if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
882 {
883 bitstatus = SET;
884 }
885 else
886 {
887 bitstatus = RESET;
888 }
889
890 return bitstatus;
891 }
892
893 /*******************************************************************************
894 * Function Name : I2C_ClearFlag
895 * Description : Clears the I2Cx's pending flags.
896 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
897 * I2C_FLAG: specifies the flag to clear.
898 * I2C_FLAG_SMBALERT: SMBus Alert flag.
899 * I2C_FLAG_TIMEOUT: Timeout or Tlow error flag.
900 * I2C_FLAG_PECERR: PEC error in reception flag.
901 * I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode).
902 * I2C_FLAG_AF: Acknowledge failure flag.
903 * I2C_FLAG_ARLO: Arbitration lost flag (Master mode).
904 * I2C_FLAG_BERR: Bus error flag.
905 * Return : None
906 *******************************************************************************/
I2C_ClearFlag(I2C_TypeDef * I2Cx,uint32_t I2C_FLAG)907 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
908 {
909 uint32_t flagpos = 0;
910
911 flagpos = I2C_FLAG & FLAG_Mask;
912 I2Cx->STAR1 = (uint16_t)~flagpos;
913 }
914
915 /*******************************************************************************
916 * Function Name : I2C_GetITStatus
917 * Description : Checks whether the specified I2C interrupt has occurred or not.
918 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
919 * II2C_IT: specifies the interrupt source to check.
920 * I2C_IT_SMBALERT: SMBus Alert flag.
921 * I2C_IT_TIMEOUT: Timeout or Tlow error flag.
922 * I2C_IT_PECERR: PEC error in reception flag.
923 * I2C_IT_OVR: Overrun/Underrun flag (Slave mode).
924 * I2C_IT_AF: Acknowledge failure flag.
925 * I2C_IT_ARLO: Arbitration lost flag (Master mode).
926 * I2C_IT_BERR: Bus error flag.
927 * I2C_IT_TXE: Data register empty flag (Transmitter).
928 * I2C_IT_RXNE: Data register not empty (Receiver) flag.
929 * I2C_IT_STOPF: Stop detection flag (Slave mode).
930 * I2C_IT_ADD10: 10-bit header sent flag (Master mode).
931 * I2C_IT_BTF: Byte transfer finished flag.
932 * I2C_IT_ADDR: Address sent flag (Master mode) "ADSL" Address matched
933 * flag (Slave mode)"ENDAD".
934 * I2C_IT_SB: Start bit flag (Master mode).
935 * Return : None
936 *******************************************************************************/
I2C_GetITStatus(I2C_TypeDef * I2Cx,uint32_t I2C_IT)937 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
938 {
939 ITStatus bitstatus = RESET;
940 uint32_t enablestatus = 0;
941
942 enablestatus = (uint32_t)(((I2C_IT & ITEN_Mask) >> 16) & (I2Cx->CTLR2)) ;
943 I2C_IT &= FLAG_Mask;
944
945 if (((I2Cx->STAR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
946 {
947 bitstatus = SET;
948 }
949 else
950 {
951 bitstatus = RESET;
952 }
953
954 return bitstatus;
955 }
956
957 /*******************************************************************************
958 * Function Name : I2C_ClearITPendingBit
959 * Description : Clears the I2Cx's interrupt pending bits.
960 * Input : I2Cx: where x can be 1 or 2 to select the I2C peripheral.
961 * I2C_IT: specifies the interrupt pending bit to clear.
962 * I2C_IT_SMBALERT: SMBus Alert interrupt.
963 * I2C_IT_TIMEOUT: Timeout or Tlow error interrupt.
964 * I2C_IT_PECERR: PEC error in reception interrupt.
965 * I2C_IT_OVR: Overrun/Underrun interrupt (Slave mode).
966 * I2C_IT_AF: Acknowledge failure interrupt.
967 * I2C_IT_ARLO: Arbitration lost interrupt (Master mode).
968 * I2C_IT_BERR: Bus error interrupt.
969 * Return : None
970 *******************************************************************************/
I2C_ClearITPendingBit(I2C_TypeDef * I2Cx,uint32_t I2C_IT)971 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
972 {
973 uint32_t flagpos = 0;
974
975 flagpos = I2C_IT & FLAG_Mask;
976 I2Cx->STAR1 = (uint16_t)~flagpos;
977 }
978
979
980
981
982
983
984
985