1 /* Includes ------------------------------------------------------------------*/
2 #include "air32f10x_usart.h"
3 #include "air32f10x_rcc.h"
4
5 /** @addtogroup air32f10x_StdPeriph_Driver
6 * @{
7 */
8
9 /** @defgroup USART
10 * @brief USART driver modules
11 * @{
12 */
13
14 /** @defgroup USART_Private_TypesDefinitions
15 * @{
16 */
17
18 /**
19 * @}
20 */
21
22 /** @defgroup USART_Private_Defines
23 * @{
24 */
25
26 #define CR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */
27 #define CR1_UE_Reset ((uint16_t)0xDFFF) /*!< USART Disable Mask */
28
29 #define CR1_WAKE_Mask ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
30
31 #define CR1_RWU_Set ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
32 #define CR1_RWU_Reset ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
33 #define CR1_SBK_Set ((uint16_t)0x0001) /*!< USART Break Character send Mask */
34 #define CR1_CLEAR_Mask ((uint16_t)0xE9F3) /*!< USART CR1 Mask */
35 #define CR2_Address_Mask ((uint16_t)0xFFF0) /*!< USART address Mask */
36
37 #define CR2_LINEN_Set ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
38 #define CR2_LINEN_Reset ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
39
40 #define CR2_LBDL_Mask ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
41 #define CR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) /*!< USART CR2 STOP Bits Mask */
42 #define CR2_CLOCK_CLEAR_Mask ((uint16_t)0xF0FF) /*!< USART CR2 Clock Mask */
43
44 #define CR3_SCEN_Set ((uint16_t)0x0020) /*!< USART SC Enable Mask */
45 #define CR3_SCEN_Reset ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
46
47 #define CR3_NACK_Set ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
48 #define CR3_NACK_Reset ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
49
50 #define CR3_HDSEL_Set ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
51 #define CR3_HDSEL_Reset ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
52
53 #define CR3_IRLP_Mask ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
54 #define CR3_CLEAR_Mask ((uint16_t)0xFCFF) /*!< USART CR3 Mask */
55
56 #define CR3_IREN_Set ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
57 #define CR3_IREN_Reset ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
58 #define GTPR_LSB_Mask ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
59 #define GTPR_MSB_Mask ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
60 #define IT_Mask ((uint16_t)0x001F) /*!< USART Interrupt Mask */
61
62 /* USART OverSampling-8 Mask */
63 #define CR1_OVER8_Set ((u16)0x8000) /* USART OVER8 mode Enable Mask */
64 #define CR1_OVER8_Reset ((u16)0x7FFF) /* USART OVER8 mode Disable Mask */
65
66 /* USART One Bit Sampling Mask */
67 #define CR3_ONEBITE_Set ((u16)0x0800) /* USART ONEBITE mode Enable Mask */
68 #define CR3_ONEBITE_Reset ((u16)0xF7FF) /* USART ONEBITE mode Disable Mask */
69
70 /**
71 * @}
72 */
73
74 /** @defgroup USART_Private_Macros
75 * @{
76 */
77
78 /**
79 * @}
80 */
81
82 /** @defgroup USART_Private_Variables
83 * @{
84 */
85
86 /**
87 * @}
88 */
89
90 /** @defgroup USART_Private_FunctionPrototypes
91 * @{
92 */
93
94 /**
95 * @}
96 */
97
98 /** @defgroup USART_Private_Functions
99 * @{
100 */
101
102 /**
103 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
104 * @param USARTx: Select the USART or the UART peripheral.
105 * This parameter can be one of the following values:
106 * USART1, USART2, USART3, UART4 or UART5.
107 * @retval None
108 */
USART_DeInit(USART_TypeDef * USARTx)109 void USART_DeInit(USART_TypeDef* USARTx)
110 {
111 /* Check the parameters */
112 assert_param(IS_USART_ALL_PERIPH(USARTx));
113
114 if (USARTx == USART1)
115 {
116 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
117 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
118 }
119 else if (USARTx == USART2)
120 {
121 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
122 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
123 }
124 else if (USARTx == USART3)
125 {
126 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
127 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
128 }
129 else if (USARTx == UART4)
130 {
131 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
132 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
133 }
134 else
135 {
136 if (USARTx == UART5)
137 {
138 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
139 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
140 }
141 }
142 }
143
144 /**
145 * @brief Initializes the USARTx peripheral according to the specified
146 * parameters in the USART_InitStruct .
147 * @param USARTx: Select the USART or the UART peripheral.
148 * This parameter can be one of the following values:
149 * USART1, USART2, USART3, UART4 or UART5.
150 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
151 * that contains the configuration information for the specified USART
152 * peripheral.
153 * @retval None
154 */
USART_Init(USART_TypeDef * USARTx,USART_InitTypeDef * USART_InitStruct)155 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
156 {
157 uint32_t tmpreg = 0x00, apbclock = 0x00;
158 uint32_t integerdivider = 0x00;
159 uint32_t fractionaldivider = 0x00;
160 uint32_t usartxbase = 0;
161 RCC_ClocksTypeDef RCC_ClocksStatus;
162 /* Check the parameters */
163 assert_param(IS_USART_ALL_PERIPH(USARTx));
164 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
165 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
166 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
167 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
168 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
169 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
170 /* The hardware flow control is available only for USART1, USART2 and USART3 */
171 if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
172 {
173 assert_param(IS_USART_123_PERIPH(USARTx));
174 }
175
176 usartxbase = (uint32_t)USARTx;
177
178 /*---------------------------- USART CR2 Configuration -----------------------*/
179 tmpreg = USARTx->CR2;
180 /* Clear STOP[13:12] bits */
181 tmpreg &= CR2_STOP_CLEAR_Mask;
182 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
183 /* Set STOP[13:12] bits according to USART_StopBits value */
184 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
185
186 /* Write to USART CR2 */
187 USARTx->CR2 = (uint16_t)tmpreg;
188
189 /*---------------------------- USART CR1 Configuration -----------------------*/
190 tmpreg = USARTx->CR1;
191 /* Clear M, PCE, PS, TE and RE bits */
192 tmpreg &= CR1_CLEAR_Mask;
193 /* Configure the USART Word Length, Parity and mode ----------------------- */
194 /* Set the M bits according to USART_WordLength value */
195 /* Set PCE and PS bits according to USART_Parity value */
196 /* Set TE and RE bits according to USART_Mode value */
197 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
198 USART_InitStruct->USART_Mode;
199 /* Write to USART CR1 */
200 USARTx->CR1 = (uint16_t)tmpreg;
201
202 /*---------------------------- USART CR3 Configuration -----------------------*/
203 tmpreg = USARTx->CR3;
204 /* Clear CTSE and RTSE bits */
205 tmpreg &= CR3_CLEAR_Mask;
206 /* Configure the USART HFC -------------------------------------------------*/
207 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
208 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
209 /* Write to USART CR3 */
210 USARTx->CR3 = (uint16_t)tmpreg;
211
212 /*---------------------------- USART BRR Configuration -----------------------*/
213 /* Configure the USART Baud Rate -------------------------------------------*/
214 RCC_GetClocksFreq(&RCC_ClocksStatus);
215 if (usartxbase == USART1_BASE)
216 {
217 apbclock = RCC_ClocksStatus.PCLK2_Frequency/100;
218 }
219 else
220 {
221 apbclock = RCC_ClocksStatus.PCLK1_Frequency/100;
222 }
223
224 /* Determine the integer part */
225 if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
226 {
227 /* Integer part computing in case Oversampling mode is 8 Samples */
228 integerdivider = ( (25 * apbclock)/ (2 * (USART_InitStruct->USART_BaudRate/100)));
229 }
230 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
231 {
232 /* Integer part computing in case Oversampling mode is 16 Samples */
233 integerdivider = ( (25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate/100)));
234 }
235
236 tmpreg = (integerdivider / 100) << 4;
237
238 /* Determine the fractional part */
239 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
240
241 /* Implement the fractional part in the register */
242 if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
243 {
244 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
245 }
246 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
247 {
248 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
249 }
250
251 /* Write to USART BRR */
252 USARTx->BRR = (uint16_t)tmpreg;
253 }
254
255 /**
256 * @brief Fills each USART_InitStruct member with its default value.
257 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
258 * which will be initialized.
259 * @retval None
260 */
USART_StructInit(USART_InitTypeDef * USART_InitStruct)261 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
262 {
263 /* USART_InitStruct members default value */
264 USART_InitStruct->USART_BaudRate = 9600;
265 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
266 USART_InitStruct->USART_StopBits = USART_StopBits_1;
267 USART_InitStruct->USART_Parity = USART_Parity_No ;
268 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
269 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
270 }
271
272 /**
273 * @brief Initializes the USARTx peripheral Clock according to the
274 * specified parameters in the USART_ClockInitStruct .
275 * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
276 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
277 * structure that contains the configuration information for the specified
278 * USART peripheral.
279 * @note The Smart Card and Synchronous modes are not available for UART4 and UART5.
280 * @retval None
281 */
USART_ClockInit(USART_TypeDef * USARTx,USART_ClockInitTypeDef * USART_ClockInitStruct)282 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
283 {
284 uint32_t tmpreg = 0x00;
285 /* Check the parameters */
286 assert_param(IS_USART_123_PERIPH(USARTx));
287 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
288 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
289 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
290 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
291
292 /*---------------------------- USART CR2 Configuration -----------------------*/
293 tmpreg = USARTx->CR2;
294 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
295 tmpreg &= CR2_CLOCK_CLEAR_Mask;
296 /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
297 /* Set CLKEN bit according to USART_Clock value */
298 /* Set CPOL bit according to USART_CPOL value */
299 /* Set CPHA bit according to USART_CPHA value */
300 /* Set LBCL bit according to USART_LastBit value */
301 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
302 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
303 /* Write to USART CR2 */
304 USARTx->CR2 = (uint16_t)tmpreg;
305 }
306
307 /**
308 * @brief Fills each USART_ClockInitStruct member with its default value.
309 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
310 * structure which will be initialized.
311 * @retval None
312 */
USART_ClockStructInit(USART_ClockInitTypeDef * USART_ClockInitStruct)313 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
314 {
315 /* USART_ClockInitStruct members default value */
316 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
317 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
318 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
319 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
320 }
321
322 /**
323 * @brief Enables or disables the specified USART peripheral.
324 * @param USARTx: Select the USART or the UART peripheral.
325 * This parameter can be one of the following values:
326 * USART1, USART2, USART3, UART4 or UART5.
327 * @param NewState: new state of the USARTx peripheral.
328 * This parameter can be: ENABLE or DISABLE.
329 * @retval None
330 */
USART_Cmd(USART_TypeDef * USARTx,FunctionalState NewState)331 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
332 {
333 /* Check the parameters */
334 assert_param(IS_USART_ALL_PERIPH(USARTx));
335 assert_param(IS_FUNCTIONAL_STATE(NewState));
336
337 if (NewState != DISABLE)
338 {
339 /* Enable the selected USART by setting the UE bit in the CR1 register */
340 USARTx->CR1 |= CR1_UE_Set;
341 }
342 else
343 {
344 /* Disable the selected USART by clearing the UE bit in the CR1 register */
345 USARTx->CR1 &= CR1_UE_Reset;
346 }
347 }
348
349 /**
350 * @brief Enables or disables the specified USART interrupts.
351 * @param USARTx: Select the USART or the UART peripheral.
352 * This parameter can be one of the following values:
353 * USART1, USART2, USART3, UART4 or UART5.
354 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
355 * This parameter can be one of the following values:
356 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
357 * @arg USART_IT_LBD: LIN Break detection interrupt
358 * @arg USART_IT_TXE: Transmit Data Register empty interrupt
359 * @arg USART_IT_TC: Transmission complete interrupt
360 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
361 * @arg USART_IT_IDLE: Idle line detection interrupt
362 * @arg USART_IT_PE: Parity Error interrupt
363 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
364 * @param NewState: new state of the specified USARTx interrupts.
365 * This parameter can be: ENABLE or DISABLE.
366 * @retval None
367 */
USART_ITConfig(USART_TypeDef * USARTx,uint16_t USART_IT,FunctionalState NewState)368 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
369 {
370 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
371 uint32_t usartxbase = 0x00;
372 /* Check the parameters */
373 assert_param(IS_USART_ALL_PERIPH(USARTx));
374 assert_param(IS_USART_CONFIG_IT(USART_IT));
375 assert_param(IS_FUNCTIONAL_STATE(NewState));
376 /* The CTS interrupt is not available for UART4 and UART5 */
377 if (USART_IT == USART_IT_CTS)
378 {
379 assert_param(IS_USART_123_PERIPH(USARTx));
380 }
381
382 usartxbase = (uint32_t)USARTx;
383
384 /* Get the USART register index */
385 usartreg = (((uint8_t)USART_IT) >> 0x05);
386
387 /* Get the interrupt position */
388 itpos = USART_IT & IT_Mask;
389 itmask = (((uint32_t)0x01) << itpos);
390
391 if (usartreg == 0x01) /* The IT is in CR1 register */
392 {
393 usartxbase += 0x0C;
394 }
395 else if (usartreg == 0x02) /* The IT is in CR2 register */
396 {
397 usartxbase += 0x10;
398 }
399 else /* The IT is in CR3 register */
400 {
401 usartxbase += 0x14;
402 }
403 if (NewState != DISABLE)
404 {
405 *(__IO uint32_t*)usartxbase |= itmask;
406 }
407 else
408 {
409 *(__IO uint32_t*)usartxbase &= ~itmask;
410 }
411 }
412
413 /**
414 * @brief Enables or disables the USART’s DMA interface.
415 * @param USARTx: Select the USART or the UART peripheral.
416 * This parameter can be one of the following values:
417 * USART1, USART2, USART3, UART4 or UART5.
418 * @param USART_DMAReq: specifies the DMA request.
419 * This parameter can be any combination of the following values:
420 * @arg USART_DMAReq_Tx: USART DMA transmit request
421 * @arg USART_DMAReq_Rx: USART DMA receive request
422 * @param NewState: new state of the DMA Request sources.
423 * This parameter can be: ENABLE or DISABLE.
424 * @retval None
425 */
USART_DMACmd(USART_TypeDef * USARTx,uint16_t USART_DMAReq,FunctionalState NewState)426 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
427 {
428 /* Check the parameters */
429 assert_param(IS_USART_ALL_PERIPH(USARTx));
430 assert_param(IS_USART_DMAREQ(USART_DMAReq));
431 assert_param(IS_FUNCTIONAL_STATE(NewState));
432 if (NewState != DISABLE)
433 {
434 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
435 DMAR bits in the USART CR3 register */
436 USARTx->CR3 |= USART_DMAReq;
437 }
438 else
439 {
440 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
441 DMAR bits in the USART CR3 register */
442 USARTx->CR3 &= (uint16_t)~USART_DMAReq;
443 }
444 }
445
446 /**
447 * @brief Sets the address of the USART node.
448 * @param USARTx: Select the USART or the UART peripheral.
449 * This parameter can be one of the following values:
450 * USART1, USART2, USART3, UART4 or UART5.
451 * @param USART_Address: Indicates the address of the USART node.
452 * @retval None
453 */
USART_SetAddress(USART_TypeDef * USARTx,uint8_t USART_Address)454 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
455 {
456 /* Check the parameters */
457 assert_param(IS_USART_ALL_PERIPH(USARTx));
458 assert_param(IS_USART_ADDRESS(USART_Address));
459
460 /* Clear the USART address */
461 USARTx->CR2 &= CR2_Address_Mask;
462 /* Set the USART address node */
463 USARTx->CR2 |= USART_Address;
464 }
465
466 /**
467 * @brief Selects the USART WakeUp method.
468 * @param USARTx: Select the USART or the UART peripheral.
469 * This parameter can be one of the following values:
470 * USART1, USART2, USART3, UART4 or UART5.
471 * @param USART_WakeUp: specifies the USART wakeup method.
472 * This parameter can be one of the following values:
473 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
474 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
475 * @retval None
476 */
USART_WakeUpConfig(USART_TypeDef * USARTx,uint16_t USART_WakeUp)477 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
478 {
479 /* Check the parameters */
480 assert_param(IS_USART_ALL_PERIPH(USARTx));
481 assert_param(IS_USART_WAKEUP(USART_WakeUp));
482
483 USARTx->CR1 &= CR1_WAKE_Mask;
484 USARTx->CR1 |= USART_WakeUp;
485 }
486
487 /**
488 * @brief Determines if the USART is in mute mode or not.
489 * @param USARTx: Select the USART or the UART peripheral.
490 * This parameter can be one of the following values:
491 * USART1, USART2, USART3, UART4 or UART5.
492 * @param NewState: new state of the USART mute mode.
493 * This parameter can be: ENABLE or DISABLE.
494 * @retval None
495 */
USART_ReceiverWakeUpCmd(USART_TypeDef * USARTx,FunctionalState NewState)496 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
497 {
498 /* Check the parameters */
499 assert_param(IS_USART_ALL_PERIPH(USARTx));
500 assert_param(IS_FUNCTIONAL_STATE(NewState));
501
502 if (NewState != DISABLE)
503 {
504 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
505 USARTx->CR1 |= CR1_RWU_Set;
506 }
507 else
508 {
509 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
510 USARTx->CR1 &= CR1_RWU_Reset;
511 }
512 }
513
514 /**
515 * @brief Sets the USART LIN Break detection length.
516 * @param USARTx: Select the USART or the UART peripheral.
517 * This parameter can be one of the following values:
518 * USART1, USART2, USART3, UART4 or UART5.
519 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
520 * This parameter can be one of the following values:
521 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
522 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
523 * @retval None
524 */
USART_LINBreakDetectLengthConfig(USART_TypeDef * USARTx,uint16_t USART_LINBreakDetectLength)525 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
526 {
527 /* Check the parameters */
528 assert_param(IS_USART_ALL_PERIPH(USARTx));
529 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
530
531 USARTx->CR2 &= CR2_LBDL_Mask;
532 USARTx->CR2 |= USART_LINBreakDetectLength;
533 }
534
535 /**
536 * @brief Enables or disables the USART’s LIN mode.
537 * @param USARTx: Select the USART or the UART peripheral.
538 * This parameter can be one of the following values:
539 * USART1, USART2, USART3, UART4 or UART5.
540 * @param NewState: new state of the USART LIN mode.
541 * This parameter can be: ENABLE or DISABLE.
542 * @retval None
543 */
USART_LINCmd(USART_TypeDef * USARTx,FunctionalState NewState)544 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
545 {
546 /* Check the parameters */
547 assert_param(IS_USART_ALL_PERIPH(USARTx));
548 assert_param(IS_FUNCTIONAL_STATE(NewState));
549
550 if (NewState != DISABLE)
551 {
552 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
553 USARTx->CR2 |= CR2_LINEN_Set;
554 }
555 else
556 {
557 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
558 USARTx->CR2 &= CR2_LINEN_Reset;
559 }
560 }
561
562 /**
563 * @brief Transmits single data through the USARTx peripheral.
564 * @param USARTx: Select the USART or the UART peripheral.
565 * This parameter can be one of the following values:
566 * USART1, USART2, USART3, UART4 or UART5.
567 * @param Data: the data to transmit.
568 * @retval None
569 */
USART_SendData(USART_TypeDef * USARTx,uint16_t Data)570 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
571 {
572 /* Check the parameters */
573 assert_param(IS_USART_ALL_PERIPH(USARTx));
574 assert_param(IS_USART_DATA(Data));
575
576 /* Transmit Data */
577 USARTx->DR = (Data & (uint16_t)0x01FF);
578 }
579
580 /**
581 * @brief Returns the most recent received data by the USARTx peripheral.
582 * @param USARTx: Select the USART or the UART peripheral.
583 * This parameter can be one of the following values:
584 * USART1, USART2, USART3, UART4 or UART5.
585 * @retval The received data.
586 */
USART_ReceiveData(USART_TypeDef * USARTx)587 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
588 {
589 /* Check the parameters */
590 assert_param(IS_USART_ALL_PERIPH(USARTx));
591
592 /* Receive Data */
593 return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
594 }
595
596 /**
597 * @brief Transmits break characters.
598 * @param USARTx: Select the USART or the UART peripheral.
599 * This parameter can be one of the following values:
600 * USART1, USART2, USART3, UART4 or UART5.
601 * @retval None
602 */
USART_SendBreak(USART_TypeDef * USARTx)603 void USART_SendBreak(USART_TypeDef* USARTx)
604 {
605 /* Check the parameters */
606 assert_param(IS_USART_ALL_PERIPH(USARTx));
607
608 /* Send break characters */
609 USARTx->CR1 |= CR1_SBK_Set;
610 }
611
612 /**
613 * @brief Sets the specified USART guard time.
614 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
615 * @param USART_GuardTime: specifies the guard time.
616 * @note The guard time bits are not available for UART4 and UART5.
617 * @retval None
618 */
USART_SetGuardTime(USART_TypeDef * USARTx,uint8_t USART_GuardTime)619 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
620 {
621 /* Check the parameters */
622 assert_param(IS_USART_123_PERIPH(USARTx));
623
624 /* Clear the USART Guard time */
625 USARTx->GTPR &= GTPR_LSB_Mask;
626 /* Set the USART guard time */
627 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
628 }
629
630 /**
631 * @brief Sets the system clock prescaler.
632 * @param USARTx: Select the USART or the UART peripheral.
633 * This parameter can be one of the following values:
634 * USART1, USART2, USART3, UART4 or UART5.
635 * @param USART_Prescaler: specifies the prescaler clock.
636 * @note The function is used for IrDA mode with UART4 and UART5.
637 * @retval None
638 */
USART_SetPrescaler(USART_TypeDef * USARTx,uint8_t USART_Prescaler)639 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
640 {
641 /* Check the parameters */
642 assert_param(IS_USART_ALL_PERIPH(USARTx));
643
644 /* Clear the USART prescaler */
645 USARTx->GTPR &= GTPR_MSB_Mask;
646 /* Set the USART prescaler */
647 USARTx->GTPR |= USART_Prescaler;
648 }
649
650 /**
651 * @brief Enables or disables the USART’s Smart Card mode.
652 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
653 * @param NewState: new state of the Smart Card mode.
654 * This parameter can be: ENABLE or DISABLE.
655 * @note The Smart Card mode is not available for UART4 and UART5.
656 * @retval None
657 */
USART_SmartCardCmd(USART_TypeDef * USARTx,FunctionalState NewState)658 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
659 {
660 /* Check the parameters */
661 assert_param(IS_USART_123_PERIPH(USARTx));
662 assert_param(IS_FUNCTIONAL_STATE(NewState));
663 if (NewState != DISABLE)
664 {
665 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
666 USARTx->CR3 |= CR3_SCEN_Set;
667 }
668 else
669 {
670 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
671 USARTx->CR3 &= CR3_SCEN_Reset;
672 }
673 }
674
675 /**
676 * @brief Enables or disables NACK transmission.
677 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
678 * @param NewState: new state of the NACK transmission.
679 * This parameter can be: ENABLE or DISABLE.
680 * @note The Smart Card mode is not available for UART4 and UART5.
681 * @retval None
682 */
USART_SmartCardNACKCmd(USART_TypeDef * USARTx,FunctionalState NewState)683 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
684 {
685 /* Check the parameters */
686 assert_param(IS_USART_123_PERIPH(USARTx));
687 assert_param(IS_FUNCTIONAL_STATE(NewState));
688 if (NewState != DISABLE)
689 {
690 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
691 USARTx->CR3 |= CR3_NACK_Set;
692 }
693 else
694 {
695 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
696 USARTx->CR3 &= CR3_NACK_Reset;
697 }
698 }
699
700 /**
701 * @brief Enables or disables the USART’s Half Duplex communication.
702 * @param USARTx: Select the USART or the UART peripheral.
703 * This parameter can be one of the following values:
704 * USART1, USART2, USART3, UART4 or UART5.
705 * @param NewState: new state of the USART Communication.
706 * This parameter can be: ENABLE or DISABLE.
707 * @retval None
708 */
USART_HalfDuplexCmd(USART_TypeDef * USARTx,FunctionalState NewState)709 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
710 {
711 /* Check the parameters */
712 assert_param(IS_USART_ALL_PERIPH(USARTx));
713 assert_param(IS_FUNCTIONAL_STATE(NewState));
714
715 if (NewState != DISABLE)
716 {
717 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
718 USARTx->CR3 |= CR3_HDSEL_Set;
719 }
720 else
721 {
722 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
723 USARTx->CR3 &= CR3_HDSEL_Reset;
724 }
725 }
726
727
728 /**
729 * @brief Enables or disables the USART's 8x oversampling mode.
730 * @param USARTx: Select the USART or the UART peripheral.
731 * This parameter can be one of the following values:
732 * USART1, USART2, USART3, UART4 or UART5.
733 * @param NewState: new state of the USART one bit sampling method.
734 * This parameter can be: ENABLE or DISABLE.
735 * @note
736 * This function has to be called before calling USART_Init()
737 * function in order to have correct baudrate Divider value.
738 * @retval None
739 */
USART_OverSampling8Cmd(USART_TypeDef * USARTx,FunctionalState NewState)740 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
741 {
742 /* Check the parameters */
743 assert_param(IS_USART_ALL_PERIPH(USARTx));
744 assert_param(IS_FUNCTIONAL_STATE(NewState));
745
746 if (NewState != DISABLE)
747 {
748 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
749 USARTx->CR1 |= CR1_OVER8_Set;
750 }
751 else
752 {
753 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
754 USARTx->CR1 &= CR1_OVER8_Reset;
755 }
756 }
757
758 /**
759 * @brief Enables or disables the USART's one bit sampling method.
760 * @param USARTx: Select the USART or the UART peripheral.
761 * This parameter can be one of the following values:
762 * USART1, USART2, USART3, UART4 or UART5.
763 * @param NewState: new state of the USART one bit sampling method.
764 * This parameter can be: ENABLE or DISABLE.
765 * @retval None
766 */
USART_OneBitMethodCmd(USART_TypeDef * USARTx,FunctionalState NewState)767 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
768 {
769 /* Check the parameters */
770 assert_param(IS_USART_ALL_PERIPH(USARTx));
771 assert_param(IS_FUNCTIONAL_STATE(NewState));
772
773 if (NewState != DISABLE)
774 {
775 /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
776 USARTx->CR3 |= CR3_ONEBITE_Set;
777 }
778 else
779 {
780 /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
781 USARTx->CR3 &= CR3_ONEBITE_Reset;
782 }
783 }
784
785 /**
786 * @brief Configures the USART's IrDA interface.
787 * @param USARTx: Select the USART or the UART peripheral.
788 * This parameter can be one of the following values:
789 * USART1, USART2, USART3, UART4 or UART5.
790 * @param USART_IrDAMode: specifies the IrDA mode.
791 * This parameter can be one of the following values:
792 * @arg USART_IrDAMode_LowPower
793 * @arg USART_IrDAMode_Normal
794 * @retval None
795 */
USART_IrDAConfig(USART_TypeDef * USARTx,uint16_t USART_IrDAMode)796 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
797 {
798 /* Check the parameters */
799 assert_param(IS_USART_ALL_PERIPH(USARTx));
800 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
801
802 USARTx->CR3 &= CR3_IRLP_Mask;
803 USARTx->CR3 |= USART_IrDAMode;
804 }
805
806 /**
807 * @brief Enables or disables the USART's IrDA interface.
808 * @param USARTx: Select the USART or the UART peripheral.
809 * This parameter can be one of the following values:
810 * USART1, USART2, USART3, UART4 or UART5.
811 * @param NewState: new state of the IrDA mode.
812 * This parameter can be: ENABLE or DISABLE.
813 * @retval None
814 */
USART_IrDACmd(USART_TypeDef * USARTx,FunctionalState NewState)815 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
816 {
817 /* Check the parameters */
818 assert_param(IS_USART_ALL_PERIPH(USARTx));
819 assert_param(IS_FUNCTIONAL_STATE(NewState));
820
821 if (NewState != DISABLE)
822 {
823 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
824 USARTx->CR3 |= CR3_IREN_Set;
825 }
826 else
827 {
828 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
829 USARTx->CR3 &= CR3_IREN_Reset;
830 }
831 }
832
833 /**
834 * @brief Checks whether the specified USART flag is set or not.
835 * @param USARTx: Select the USART or the UART peripheral.
836 * This parameter can be one of the following values:
837 * USART1, USART2, USART3, UART4 or UART5.
838 * @param USART_FLAG: specifies the flag to check.
839 * This parameter can be one of the following values:
840 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
841 * @arg USART_FLAG_LBD: LIN Break detection flag
842 * @arg USART_FLAG_TXE: Transmit data register empty flag
843 * @arg USART_FLAG_TC: Transmission Complete flag
844 * @arg USART_FLAG_RXNE: Receive data register not empty flag
845 * @arg USART_FLAG_IDLE: Idle Line detection flag
846 * @arg USART_FLAG_ORE: OverRun Error flag
847 * @arg USART_FLAG_NE: Noise Error flag
848 * @arg USART_FLAG_FE: Framing Error flag
849 * @arg USART_FLAG_PE: Parity Error flag
850 * @retval The new state of USART_FLAG (SET or RESET).
851 */
USART_GetFlagStatus(USART_TypeDef * USARTx,uint16_t USART_FLAG)852 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
853 {
854 FlagStatus bitstatus = RESET;
855 /* Check the parameters */
856 assert_param(IS_USART_ALL_PERIPH(USARTx));
857 assert_param(IS_USART_FLAG(USART_FLAG));
858 /* The CTS flag is not available for UART4 and UART5 */
859 if (USART_FLAG == USART_FLAG_CTS)
860 {
861 assert_param(IS_USART_123_PERIPH(USARTx));
862 }
863
864 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
865 {
866 bitstatus = SET;
867 }
868 else
869 {
870 bitstatus = RESET;
871 }
872 return bitstatus;
873 }
874
875 /**
876 * @brief Clears the USARTx's pending flags.
877 * @param USARTx: Select the USART or the UART peripheral.
878 * This parameter can be one of the following values:
879 * USART1, USART2, USART3, UART4 or UART5.
880 * @param USART_FLAG: specifies the flag to clear.
881 * This parameter can be any combination of the following values:
882 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
883 * @arg USART_FLAG_LBD: LIN Break detection flag.
884 * @arg USART_FLAG_TC: Transmission Complete flag.
885 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
886 *
887 * @note
888 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
889 * error) and IDLE (Idle line detected) flags are cleared by software
890 * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
891 * followed by a read operation to USART_DR register (USART_ReceiveData()).
892 * - RXNE flag can be also cleared by a read to the USART_DR register
893 * (USART_ReceiveData()).
894 * - TC flag can be also cleared by software sequence: a read operation to
895 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
896 * to USART_DR register (USART_SendData()).
897 * - TXE flag is cleared only by a write to the USART_DR register
898 * (USART_SendData()).
899 * @retval None
900 */
USART_ClearFlag(USART_TypeDef * USARTx,uint16_t USART_FLAG)901 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
902 {
903 /* Check the parameters */
904 assert_param(IS_USART_ALL_PERIPH(USARTx));
905 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
906 /* The CTS flag is not available for UART4 and UART5 */
907 if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
908 {
909 assert_param(IS_USART_123_PERIPH(USARTx));
910 }
911
912 USARTx->SR = (uint16_t)~USART_FLAG;
913 }
914
915 /**
916 * @brief Checks whether the specified USART interrupt has occurred or not.
917 * @param USARTx: Select the USART or the UART peripheral.
918 * This parameter can be one of the following values:
919 * USART1, USART2, USART3, UART4 or UART5.
920 * @param USART_IT: specifies the USART interrupt source to check.
921 * This parameter can be one of the following values:
922 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
923 * @arg USART_IT_LBD: LIN Break detection interrupt
924 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
925 * @arg USART_IT_TC: Transmission complete interrupt
926 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
927 * @arg USART_IT_IDLE: Idle line detection interrupt
928 * @arg USART_IT_ORE: OverRun Error interrupt
929 * @arg USART_IT_NE: Noise Error interrupt
930 * @arg USART_IT_FE: Framing Error interrupt
931 * @arg USART_IT_PE: Parity Error interrupt
932 * @retval The new state of USART_IT (SET or RESET).
933 */
USART_GetITStatus(USART_TypeDef * USARTx,uint16_t USART_IT)934 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
935 {
936 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
937 ITStatus bitstatus = RESET;
938 /* Check the parameters */
939 assert_param(IS_USART_ALL_PERIPH(USARTx));
940 assert_param(IS_USART_GET_IT(USART_IT));
941 /* The CTS interrupt is not available for UART4 and UART5 */
942 if (USART_IT == USART_IT_CTS)
943 {
944 assert_param(IS_USART_123_PERIPH(USARTx));
945 }
946
947 /* Get the USART register index */
948 usartreg = (((uint8_t)USART_IT) >> 0x05);
949 /* Get the interrupt position */
950 itmask = USART_IT & IT_Mask;
951 itmask = (uint32_t)0x01 << itmask;
952
953 if (usartreg == 0x01) /* The IT is in CR1 register */
954 {
955 itmask &= USARTx->CR1;
956 }
957 else if (usartreg == 0x02) /* The IT is in CR2 register */
958 {
959 itmask &= USARTx->CR2;
960 }
961 else /* The IT is in CR3 register */
962 {
963 itmask &= USARTx->CR3;
964 }
965
966 bitpos = USART_IT >> 0x08;
967 bitpos = (uint32_t)0x01 << bitpos;
968 bitpos &= USARTx->SR;
969 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
970 {
971 bitstatus = SET;
972 }
973 else
974 {
975 bitstatus = RESET;
976 }
977
978 return bitstatus;
979 }
980
981 /**
982 * @brief Clears the USARTx's interrupt pending bits.
983 * @param USARTx: Select the USART or the UART peripheral.
984 * This parameter can be one of the following values:
985 * USART1, USART2, USART3, UART4 or UART5.
986 * @param USART_IT: specifies the interrupt pending bit to clear.
987 * This parameter can be one of the following values:
988 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
989 * @arg USART_IT_LBD: LIN Break detection interrupt
990 * @arg USART_IT_TC: Transmission complete interrupt.
991 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
992 *
993 * @note
994 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
995 * error) and IDLE (Idle line detected) pending bits are cleared by
996 * software sequence: a read operation to USART_SR register
997 * (USART_GetITStatus()) followed by a read operation to USART_DR register
998 * (USART_ReceiveData()).
999 * - RXNE pending bit can be also cleared by a read to the USART_DR register
1000 * (USART_ReceiveData()).
1001 * - TC pending bit can be also cleared by software sequence: a read
1002 * operation to USART_SR register (USART_GetITStatus()) followed by a write
1003 * operation to USART_DR register (USART_SendData()).
1004 * - TXE pending bit is cleared only by a write to the USART_DR register
1005 * (USART_SendData()).
1006 * @retval None
1007 */
USART_ClearITPendingBit(USART_TypeDef * USARTx,uint16_t USART_IT)1008 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
1009 {
1010 uint16_t bitpos = 0x00, itmask = 0x00;
1011 /* Check the parameters */
1012 assert_param(IS_USART_ALL_PERIPH(USARTx));
1013 assert_param(IS_USART_CLEAR_IT(USART_IT));
1014 /* The CTS interrupt is not available for UART4 and UART5 */
1015 if (USART_IT == USART_IT_CTS)
1016 {
1017 assert_param(IS_USART_123_PERIPH(USARTx));
1018 }
1019
1020 bitpos = USART_IT >> 0x08;
1021 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
1022 USARTx->SR = (uint16_t)~itmask;
1023 }
1024 /**
1025 * @}
1026 */
1027
1028 /**
1029 * @}
1030 */
1031
1032 /**
1033 * @}
1034 */
1035
1036