1 /***************COPYRIGHT(C) 2019 WCH. A11 rights reserved*********************
2 * File Name : ch32f10x_usart.c
3 * Author : WCH
4 * Version : V1.0.0
5 * Date : 2019/10/15
6 * Description : This file provides all the USART firmware functions.
7 *******************************************************************************/
8 #include "ch32f10x_usart.h"
9 #include "ch32f10x_rcc.h"
10
11 /* USART_Private_Defines */
12 #define CTLR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */
13 #define CTLR1_UE_Reset ((uint16_t)0xDFFF) /*!< USART Disable Mask */
14
15 #define CTLR1_WAKE_Mask ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
16
17 #define CTLR1_RWU_Set ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
18 #define CTLR1_RWU_Reset ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
19 #define CTLR1_SBK_Set ((uint16_t)0x0001) /*!< USART Break Character send Mask */
20 #define CTLR1_CLEAR_Mask ((uint16_t)0xE9F3) /*!< USART CR1 Mask */
21 #define CTLR2_Address_Mask ((uint16_t)0xFFF0) /*!< USART address Mask */
22
23 #define CTLR2_LINEN_Set ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
24 #define CTLR2_LINEN_Reset ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
25
26 #define CTLR2_LBDL_Mask ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
27 #define CTLR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) /*!< USART CR2 STOP Bits Mask */
28 #define CTLR2_CLOCK_CLEAR_Mask ((uint16_t)0xF0FF) /*!< USART CR2 Clock Mask */
29
30 #define CTLR3_SCEN_Set ((uint16_t)0x0020) /*!< USART SC Enable Mask */
31 #define CTLR3_SCEN_Reset ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
32
33 #define CTLR3_NACK_Set ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
34 #define CTLR3_NACK_Reset ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
35
36 #define CTLR3_HDSEL_Set ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
37 #define CTLR3_HDSEL_Reset ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
38
39 #define CTLR3_IRLP_Mask ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
40 #define CTLR3_CLEAR_Mask ((uint16_t)0xFCFF) /*!< USART CR3 Mask */
41
42 #define CTLR3_IREN_Set ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
43 #define CTLR3_IREN_Reset ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
44 #define GPR_LSB_Mask ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
45 #define GPR_MSB_Mask ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
46 #define IT_Mask ((uint16_t)0x001F) /*!< USART Interrupt Mask */
47
48 /* USART OverSampling-8 Mask */
49 #define CTLR1_OVER8_Set ((uint16_t)0x8000) /* USART OVER8 mode Enable Mask */
50 #define CTLR1_OVER8_Reset ((uint16_t)0x7FFF) /* USART OVER8 mode Disable Mask */
51
52 /* USART One Bit Sampling Mask */
53 #define CTLR3_ONEBITE_Set ((uint16_t)0x0800) /* USART ONEBITE mode Enable Mask */
54 #define CTLR3_ONEBITE_Reset ((uint16_t)0xF7FF) /* USART ONEBITE mode Disable Mask */
55
56
57 /*******************************************************************************
58 * Function Name : USART_DeInit
59 * Description : Deinitializes the USARTx peripheral registers to their default
60 * reset values.
61 * Input : USARTx: where x can be 1, 2 or 3 to select the UART peripheral.
62 * Return : None
63 *******************************************************************************/
USART_DeInit(USART_TypeDef * USARTx)64 void USART_DeInit(USART_TypeDef* USARTx)
65 {
66 if (USARTx == USART1)
67 {
68 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
69 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
70 }
71 else if (USARTx == USART2)
72 {
73 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
74 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
75 }
76 else if (USARTx == USART3)
77 {
78 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
79 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
80 }
81 else if (USARTx == UART4)
82 {
83 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
84 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
85 }
86 else
87 {
88 if (USARTx == UART5)
89 {
90 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
91 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
92 }
93 }
94 }
95
96 /*******************************************************************************
97 * Function Name : USART_Init
98 * Description : Initializes the USARTx peripheral according to the specified
99 * parameters in the USART_InitStruct.
100 * Input : USARTx: where x can be 1, 2 or 3 to select the UART peripheral.
101 * USART_InitStruct: pointer to a USART_InitTypeDef structure
102 * that contains the configuration information for the specified USART peripheral.
103 * Return : None
104 *******************************************************************************/
USART_Init(USART_TypeDef * USARTx,USART_InitTypeDef * USART_InitStruct)105 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
106 {
107 uint32_t tmpreg = 0x00, apbclock = 0x00;
108 uint32_t integerdivider = 0x00;
109 uint32_t fractionaldivider = 0x00;
110 uint32_t usartxbase = 0;
111 RCC_ClocksTypeDef RCC_ClocksStatus;
112
113 if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
114 {
115 }
116
117 usartxbase = (uint32_t)USARTx;
118 tmpreg = USARTx->CTLR2;
119 tmpreg &= CTLR2_STOP_CLEAR_Mask;
120 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
121
122 USARTx->CTLR2 = (uint16_t)tmpreg;
123 tmpreg = USARTx->CTLR1;
124 tmpreg &= CTLR1_CLEAR_Mask;
125 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
126 USART_InitStruct->USART_Mode;
127 USARTx->CTLR1 = (uint16_t)tmpreg;
128
129 tmpreg = USARTx->CTLR3;
130 tmpreg &= CTLR3_CLEAR_Mask;
131 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
132 USARTx->CTLR3 = (uint16_t)tmpreg;
133
134 RCC_GetClocksFreq(&RCC_ClocksStatus);
135
136 if (usartxbase == USART1_BASE)
137 {
138 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
139 }
140 else
141 {
142 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
143 }
144
145 if ((USARTx->CTLR1 & CTLR1_OVER8_Set) != 0)
146 {
147 integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
148 }
149 else
150 {
151 integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
152 }
153 tmpreg = (integerdivider / 100) << 4;
154
155 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
156
157 if ((USARTx->CTLR1 & CTLR1_OVER8_Set) != 0)
158 {
159 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
160 }
161 else
162 {
163 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
164 }
165
166 USARTx->BRR = (uint16_t)tmpreg;
167 }
168
169 /*******************************************************************************
170 * Function Name : USART_StructInit
171 * Description : Fills each USART_InitStruct member with its default value.
172 * Input : USART_InitStruct: pointer to a USART_InitTypeDef structure
173 * which will be initialized.
174 * Return : None
175 *******************************************************************************/
USART_StructInit(USART_InitTypeDef * USART_InitStruct)176 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
177 {
178 USART_InitStruct->USART_BaudRate = 9600;
179 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
180 USART_InitStruct->USART_StopBits = USART_StopBits_1;
181 USART_InitStruct->USART_Parity = USART_Parity_No ;
182 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
183 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
184 }
185
186 /*******************************************************************************
187 * Function Name : USART_ClockInit
188 * Description : Initializes the USARTx peripheral Clock according to the
189 * specified parameters in the USART_ClockInitStruct .
190 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
191 * USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
192 * structure that contains the configuration information for the specified
193 * USART peripheral.
194 * Return : None
195 *******************************************************************************/
USART_ClockInit(USART_TypeDef * USARTx,USART_ClockInitTypeDef * USART_ClockInitStruct)196 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
197 {
198 uint32_t tmpreg = 0x00;
199
200 tmpreg = USARTx->CTLR2;
201 tmpreg &= CTLR2_CLOCK_CLEAR_Mask;
202 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
203 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
204 USARTx->CTLR2 = (uint16_t)tmpreg;
205 }
206
207 /*******************************************************************************
208 * Function Name : USART_ClockStructInit
209 * Description : Fills each USART_InitStruct member with its default value.
210 * Input : USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
211 * structure which will be initialized.
212 * Return : None
213 *******************************************************************************/
USART_ClockStructInit(USART_ClockInitTypeDef * USART_ClockInitStruct)214 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
215 {
216 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
217 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
218 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
219 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
220 }
221
222 /*******************************************************************************
223 * Function Name : USART_Cmd
224 * Description : Enables or disables the specified USART peripheral.
225 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
226 * NewState: ENABLE or DISABLE.
227 * Return : None
228 *******************************************************************************/
USART_Cmd(USART_TypeDef * USARTx,FunctionalState NewState)229 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
230 {
231 if (NewState != DISABLE)
232 {
233 USARTx->CTLR1 |= CTLR1_UE_Set;
234 }
235 else
236 {
237 USARTx->CTLR1 &= CTLR1_UE_Reset;
238 }
239 }
240
241 /*******************************************************************************
242 * Function Name : USART_ITConfig
243 * Description : Enables or disables the specified USART interrupts.
244 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
245 * USART_IT: specifies the USART interrupt sources to be enabled or disabled.
246 * USART_IT_CTS: CTS change interrupt.
247 * USART_IT_LBD: LIN Break detection interrupt.
248 * USART_IT_TXE: Transmit Data Register empty interrupt.
249 * USART_IT_TC: Transmission complete interrupt.
250 * USART_IT_RXNE: Receive Data register not empty interrupt.
251 * USART_IT_IDLE: Idle line detection interrupt.
252 * USART_IT_PE: Parity Error interrupt.
253 * USART_IT_ERR: Error interrupt.
254 * NewState: ENABLE or DISABLE.
255 * Return : None
256 *******************************************************************************/
USART_ITConfig(USART_TypeDef * USARTx,uint16_t USART_IT,FunctionalState NewState)257 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
258 {
259 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
260 uint32_t usartxbase = 0x00;
261
262 if (USART_IT == USART_IT_CTS)
263 {
264 }
265
266 usartxbase = (uint32_t)USARTx;
267 usartreg = (((uint8_t)USART_IT) >> 0x05);
268 itpos = USART_IT & IT_Mask;
269 itmask = (((uint32_t)0x01) << itpos);
270
271 if (usartreg == 0x01)
272 {
273 usartxbase += 0x0C;
274 }
275 else if (usartreg == 0x02)
276 {
277 usartxbase += 0x10;
278 }
279 else
280 {
281 usartxbase += 0x14;
282 }
283
284 if (NewState != DISABLE)
285 {
286 *(__IO uint32_t*)usartxbase |= itmask;
287 }
288 else
289 {
290 *(__IO uint32_t*)usartxbase &= ~itmask;
291 }
292 }
293
294 /*******************************************************************************
295 * Function Name : USART_DMACmd
296 * Description : Enables or disables the USART DMA interface.
297 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
298 * USART_DMAReq: specifies the DMA request.
299 * USART_DMAReq_Tx: USART DMA transmit request.
300 * USART_DMAReq_Rx: USART DMA receive request.
301 * NewState: ENABLE or DISABLE.
302 * Return : None
303 *******************************************************************************/
USART_DMACmd(USART_TypeDef * USARTx,uint16_t USART_DMAReq,FunctionalState NewState)304 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
305 {
306 if (NewState != DISABLE)
307 {
308 USARTx->CTLR3 |= USART_DMAReq;
309 }
310 else
311 {
312 USARTx->CTLR3 &= (uint16_t)~USART_DMAReq;
313 }
314 }
315
316 /*******************************************************************************
317 * Function Name : USART_SetAddress
318 * Description : Sets the address of the USART node.
319 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
320 * USART_Address: Indicates the address of the USART node.
321 * Return : None
322 *******************************************************************************/
USART_SetAddress(USART_TypeDef * USARTx,uint8_t USART_Address)323 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
324 {
325 USARTx->CTLR2 &= CTLR2_Address_Mask;
326 USARTx->CTLR2 |= USART_Address;
327 }
328
329 /*******************************************************************************
330 * Function Name : USART_WakeUpConfig
331 * Description : Selects the USART WakeUp method.
332 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
333 * USART_WakeUp: specifies the USART wakeup method.
334 * USART_WakeUp_IdleLine: WakeUp by an idle line detection.
335 * USART_WakeUp_AddressMark: WakeUp by an address mark.
336 * Return : None
337 *******************************************************************************/
USART_WakeUpConfig(USART_TypeDef * USARTx,uint16_t USART_WakeUp)338 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
339 {
340 USARTx->CTLR1 &= CTLR1_WAKE_Mask;
341 USARTx->CTLR1 |= USART_WakeUp;
342 }
343
344 /*******************************************************************************
345 * Function Name : USART_ReceiverWakeUpCmd
346 * Description : Determines if the USART is in mute mode or not.
347 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
348 * NewState: ENABLE or DISABLE.
349 * Return : None
350 *******************************************************************************/
USART_ReceiverWakeUpCmd(USART_TypeDef * USARTx,FunctionalState NewState)351 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
352 {
353 if (NewState != DISABLE)
354 {
355 USARTx->CTLR1 |= CTLR1_RWU_Set;
356 }
357 else
358 {
359 USARTx->CTLR1 &= CTLR1_RWU_Reset;
360 }
361 }
362
363 /*******************************************************************************
364 * Function Name : USART_LINBreakDetectLengthConfig
365 * Description : Sets the USART LIN Break detection length.
366 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
367 * USART_LINBreakDetectLength: specifies the LIN break detection length.
368 * USART_LINBreakDetectLength_10b: 10-bit break detection.
369 * USART_LINBreakDetectLength_11b: 11-bit break detection.
370 * Return : None
371 *******************************************************************************/
USART_LINBreakDetectLengthConfig(USART_TypeDef * USARTx,uint16_t USART_LINBreakDetectLength)372 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
373 {
374 USARTx->CTLR2 &= CTLR2_LBDL_Mask;
375 USARTx->CTLR2 |= USART_LINBreakDetectLength;
376 }
377
378 /*******************************************************************************
379 * Function Name : USART_LINCmd
380 * Description : Enables or disables the USART LIN mode.
381 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
382 * NewState: ENABLE or DISABLE.
383 * Return : None
384 *******************************************************************************/
USART_LINCmd(USART_TypeDef * USARTx,FunctionalState NewState)385 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
386 {
387 if (NewState != DISABLE)
388 {
389 USARTx->CTLR2 |= CTLR2_LINEN_Set;
390 }
391 else
392 {
393 USARTx->CTLR2 &= CTLR2_LINEN_Reset;
394 }
395 }
396
397 /*******************************************************************************
398 * Function Name : USART_SendData
399 * Description : Transmits single data through the USARTx peripheral.
400 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
401 * Data: the data to transmit..
402 * Return : None
403 *******************************************************************************/
USART_SendData(USART_TypeDef * USARTx,uint16_t Data)404 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
405 {
406 USARTx->DATAR = (Data & (uint16_t)0x01FF);
407 }
408
409 /*******************************************************************************
410 * Function Name : USART_ReceiveData
411 * Description : Returns the most recent received data by the USARTx peripheral.
412 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
413 * Return : The received data.
414 *******************************************************************************/
USART_ReceiveData(USART_TypeDef * USARTx)415 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
416 {
417 return (uint16_t)(USARTx->DATAR & (uint16_t)0x01FF);
418 }
419
420 /*******************************************************************************
421 * Function Name : USART_SendBreak
422 * Description : Transmits break characters.
423 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
424 * Return : None
425 *******************************************************************************/
USART_SendBreak(USART_TypeDef * USARTx)426 void USART_SendBreak(USART_TypeDef* USARTx)
427 {
428 USARTx->CTLR1 |= CTLR1_SBK_Set;
429 }
430
431 /*******************************************************************************
432 * Function Name : USART_SetGuardTime
433 * Description : Sets the specified USART guard time.
434 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
435 * USART_GuardTime: specifies the guard time.
436 * Return : None
437 *******************************************************************************/
USART_SetGuardTime(USART_TypeDef * USARTx,uint8_t USART_GuardTime)438 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
439 {
440 USARTx->GPR &= GPR_LSB_Mask;
441 USARTx->GPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
442 }
443
444 /*******************************************************************************
445 * Function Name : USART_SetPrescaler
446 * Description : Sets the system clock prescaler.
447 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
448 * USART_Prescaler: specifies the prescaler clock.
449 * Return : None
450 *******************************************************************************/
USART_SetPrescaler(USART_TypeDef * USARTx,uint8_t USART_Prescaler)451 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
452 {
453 USARTx->GPR &= GPR_MSB_Mask;
454 USARTx->GPR |= USART_Prescaler;
455 }
456
457 /*******************************************************************************
458 * Function Name : USART_SmartCardCmd
459 * Description : Enables or disables the USART Smart Card mode.
460 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
461 * NewState: ENABLE or DISABLE.
462 * Return : None
463 *******************************************************************************/
USART_SmartCardCmd(USART_TypeDef * USARTx,FunctionalState NewState)464 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
465 {
466 if (NewState != DISABLE)
467 {
468 USARTx->CTLR3 |= CTLR3_SCEN_Set;
469 }
470 else
471 {
472 USARTx->CTLR3 &= CTLR3_SCEN_Reset;
473 }
474 }
475
476 /*******************************************************************************
477 * Function Name : USART_SmartCardNACKCmd
478 * Description : Enables or disables NACK transmission.
479 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
480 * NewState: ENABLE or DISABLE.
481 * Return : None
482 *******************************************************************************/
USART_SmartCardNACKCmd(USART_TypeDef * USARTx,FunctionalState NewState)483 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
484 {
485 if (NewState != DISABLE)
486 {
487 USARTx->CTLR3 |= CTLR3_NACK_Set;
488 }
489 else
490 {
491 USARTx->CTLR3 &= CTLR3_NACK_Reset;
492 }
493 }
494
495 /*******************************************************************************
496 * Function Name : USART_HalfDuplexCmd
497 * Description : Enables or disables the USART Half Duplex communication.
498 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
499 * NewState: ENABLE or DISABLE.
500 * Return : None
501 *******************************************************************************/
USART_HalfDuplexCmd(USART_TypeDef * USARTx,FunctionalState NewState)502 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
503 {
504 if (NewState != DISABLE)
505 {
506 USARTx->CTLR3 |= CTLR3_HDSEL_Set;
507 }
508 else
509 {
510 USARTx->CTLR3 &= CTLR3_HDSEL_Reset;
511 }
512 }
513
514 /*******************************************************************************
515 * Function Name : USART_OverSampling8Cmd
516 * Description : Enables or disables the USART's 8x oversampling mode.
517 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
518 * NewState: ENABLE or DISABLE.
519 * Return : None
520 *******************************************************************************/
USART_OverSampling8Cmd(USART_TypeDef * USARTx,FunctionalState NewState)521 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
522 {
523 if (NewState != DISABLE)
524 {
525 USARTx->CTLR1 |= CTLR1_OVER8_Set;
526 }
527 else
528 {
529 USARTx->CTLR1 &= CTLR1_OVER8_Reset;
530 }
531 }
532
533 /*******************************************************************************
534 * Function Name : USART_OneBitMethodCmd
535 * Description : Enables or disables the USART's one bit sampling method.
536 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
537 * NewState: ENABLE or DISABLE.
538 * Return : None
539 *******************************************************************************/
USART_OneBitMethodCmd(USART_TypeDef * USARTx,FunctionalState NewState)540 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
541 {
542 if (NewState != DISABLE)
543 {
544 USARTx->CTLR3 |= CTLR3_ONEBITE_Set;
545 }
546 else
547 {
548 USARTx->CTLR3 &= CTLR3_ONEBITE_Reset;
549 }
550 }
551
552 /*******************************************************************************
553 * Function Name : USART_IrDAConfig
554 * Description : Configures the USART's IrDA interface.
555 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
556 * USART_IrDAMode: specifies the IrDA mode.
557 * USART_IrDAMode_LowPower.
558 * USART_IrDAMode_Normal.
559 * Return : None
560 *******************************************************************************/
USART_IrDAConfig(USART_TypeDef * USARTx,uint16_t USART_IrDAMode)561 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
562 {
563 USARTx->CTLR3 &= CTLR3_IRLP_Mask;
564 USARTx->CTLR3 |= USART_IrDAMode;
565 }
566
567 /*******************************************************************************
568 * Function Name : USART_IrDACmd
569 * Description : Enables or disables the USART's IrDA interface.
570 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
571 * NewState: ENABLE or DISABLE.
572 * Return : None
573 *******************************************************************************/
USART_IrDACmd(USART_TypeDef * USARTx,FunctionalState NewState)574 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
575 {
576 if (NewState != DISABLE)
577 {
578 USARTx->CTLR3 |= CTLR3_IREN_Set;
579 }
580 else
581 {
582 USARTx->CTLR3 &= CTLR3_IREN_Reset;
583 }
584 }
585
586 /*******************************************************************************
587 * Function Name : USART_GetFlagStatus
588 * Description : Checks whether the specified USART flag is set or not.
589 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
590 * USART_FLAG: specifies the flag to check.
591 * USART_FLAG_CTS: CTS Change flag.
592 * USART_FLAG_LBD: LIN Break detection flag.
593 * USART_FLAG_TXE: Transmit data register empty flag.
594 * USART_FLAG_TC: Transmission Complete flag.
595 * USART_FLAG_RXNE: Receive data register not empty flag.
596 * USART_FLAG_IDLE: Idle Line detection flag.
597 * USART_FLAG_ORE: OverRun Error flag.
598 * USART_FLAG_NE: Noise Error flag.
599 * USART_FLAG_FE: Framing Error flag.
600 * USART_FLAG_PE: Parity Error flag.
601 * Return : bitstatus: SET or RESET.
602 *******************************************************************************/
USART_GetFlagStatus(USART_TypeDef * USARTx,uint16_t USART_FLAG)603 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
604 {
605 FlagStatus bitstatus = RESET;
606
607 if (USART_FLAG == USART_FLAG_CTS)
608 {
609 }
610
611 if ((USARTx->STATR & USART_FLAG) != (uint16_t)RESET)
612 {
613 bitstatus = SET;
614 }
615 else
616 {
617 bitstatus = RESET;
618 }
619 return bitstatus;
620 }
621
622 /*******************************************************************************
623 * Function Name : USART_ClearFlag
624 * Description : Clears the USARTx's pending flags.
625 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
626 * USART_FLAG: specifies the flag to clear.
627 * USART_FLAG_CTS: CTS Change flag.
628 * USART_FLAG_LBD: LIN Break detection flag.
629 * USART_FLAG_TC: Transmission Complete flag.
630 * USART_FLAG_RXNE: Receive data register not empty flag.
631 * Return : None
632 *******************************************************************************/
USART_ClearFlag(USART_TypeDef * USARTx,uint16_t USART_FLAG)633 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
634 {
635 if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
636 {
637 }
638
639 USARTx->STATR = (uint16_t)~USART_FLAG;
640 }
641
642 /*******************************************************************************
643 * Function Name : USART_GetITStatus
644 * Description : Checks whether the specified USART interrupt has occurred or not.
645 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
646 * USART_IT: specifies the USART interrupt source to check.
647 * USART_IT_CTS: CTS change interrupt.
648 * USART_IT_LBD: LIN Break detection interrupt.
649 * USART_IT_TXE: Tansmit Data Register empty interrupt.
650 * USART_IT_TC: Transmission complete interrupt.
651 * USART_IT_RXNE: Receive Data register not empty interrupt.
652 * USART_IT_IDLE: Idle line detection interrupt.
653 * USART_IT_ORE: OverRun Error interrupt.
654 * USART_IT_NE: Noise Error interrupt.
655 * USART_IT_FE: Framing Error interrupt.
656 * USART_IT_PE: Parity Error interrupt.
657 * Return : bitstatus: SET or RESET.
658 *******************************************************************************/
USART_GetITStatus(USART_TypeDef * USARTx,uint16_t USART_IT)659 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
660 {
661 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
662 ITStatus bitstatus = RESET;
663
664 if (USART_IT == USART_IT_CTS)
665 {
666 }
667
668 usartreg = (((uint8_t)USART_IT) >> 0x05);
669 itmask = USART_IT & IT_Mask;
670 itmask = (uint32_t)0x01 << itmask;
671
672 if (usartreg == 0x01)
673 {
674 itmask &= USARTx->CTLR1;
675 }
676 else if (usartreg == 0x02)
677 {
678 itmask &= USARTx->CTLR2;
679 }
680 else
681 {
682 itmask &= USARTx->CTLR3;
683 }
684
685 bitpos = USART_IT >> 0x08;
686 bitpos = (uint32_t)0x01 << bitpos;
687 bitpos &= USARTx->STATR;
688
689 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
690 {
691 bitstatus = SET;
692 }
693 else
694 {
695 bitstatus = RESET;
696 }
697
698 return bitstatus;
699 }
700
701 /*******************************************************************************
702 * Function Name : USART_ClearITPendingBit
703 * Description : Clears the USARTx's interrupt pending bits.
704 * Input : USARTx: where x can be 1, 2, 3 to select the USART peripheral.
705 * USART_IT: specifies the interrupt pending bit to clear.
706 * USART_IT_CTS: CTS change interrupt.
707 * USART_IT_LBD: LIN Break detection interrupt.
708 * USART_IT_TC: Transmission complete interrupt.
709 * USART_IT_RXNE: Receive Data register not empty interrupt.
710 * Return : None
711 *******************************************************************************/
USART_ClearITPendingBit(USART_TypeDef * USARTx,uint16_t USART_IT)712 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
713 {
714 uint16_t bitpos = 0x00, itmask = 0x00;
715
716 if (USART_IT == USART_IT_CTS)
717 {
718
719 }
720
721 bitpos = USART_IT >> 0x08;
722 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
723 USARTx->STATR = (uint16_t)~itmask;
724 }
725
726
727
728
729
730
731
732
733