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