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