-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
stm32f30x_usart.c
2084 lines (1907 loc) · 84.5 KB
/
stm32f30x_usart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
* @file stm32f30x_usart.c
* @author MCD Application Team
* @version V1.1.1
* @date 04-April-2014
* @brief This file provides firmware functions to manage the following
* functionalities of the Universal synchronous asynchronous receiver
* transmitter (USART):
* + Initialization and Configuration
* + STOP Mode
* + AutoBaudRate
* + Data transfers
* + Multi-Processor Communication
* + LIN mode
* + Half-duplex mode
* + Smartcard mode
* + IrDA mode
* + RS485 mode
* + DMA transfers management
* + Interrupts and flags management
*
* @verbatim
===============================================================================
##### How to use this driver #####
===============================================================================
[..]
(#) Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE)
function for USART2, USART3, UART4 and UART5.
(#) According to the USART mode, enable the GPIO clocks using
RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS,
or and SCLK).
(#) Peripheral's alternate function:
(++) Connect the pin to the desired peripherals' Alternate
Function (AF) using GPIO_PinAFConfig() function.
(++) Configure the desired pin in alternate function by:
GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
(++) Select the type, pull-up/pull-down and output speed via
GPIO_PuPd, GPIO_OType and GPIO_Speed members.
(++) Call GPIO_Init() function.
(#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
flow control and Mode(Receiver/Transmitter) using the SPI_Init()
function.
(#) For synchronous mode, enable the clock and program the polarity,
phase and last bit using the USART_ClockInit() function.
(#) Enable the USART using the USART_Cmd() function.
(#) Enable the NVIC and the corresponding interrupt using the function
USART_ITConfig() if you need to use interrupt mode.
(#) When using the DMA mode:
(++) Configure the DMA using DMA_Init() function.
(++) Activate the needed channel Request using USART_DMACmd() function.
(#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
[..]
Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
for more details.
@endverbatim
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f30x_usart.h"
#include "stm32f30x_rcc.h"
/** @addtogroup STM32F30x_StdPeriph_Driver
* @{
*/
/** @defgroup USART
* @brief USART driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/*!< USART CR1 register clear Mask ((~(uint32_t)0xFFFFE6F3)) */
#define CR1_CLEAR_MASK ((uint32_t)(USART_CR1_M | USART_CR1_PCE | \
USART_CR1_PS | USART_CR1_TE | \
USART_CR1_RE))
/*!< USART CR2 register clock bits clear Mask ((~(uint32_t)0xFFFFF0FF)) */
#define CR2_CLOCK_CLEAR_MASK ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
USART_CR2_CPHA | USART_CR2_LBCL))
/*!< USART CR3 register clear Mask ((~(uint32_t)0xFFFFFCFF)) */
#define CR3_CLEAR_MASK ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
/*!< USART Interrupts mask */
#define IT_MASK ((uint32_t)0x000000FF)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup USART_Private_Functions
* @{
*/
/** @defgroup USART_Group1 Initialization and Configuration functions
* @brief Initialization and Configuration functions
*
@verbatim
===============================================================================
##### Initialization and Configuration functions #####
===============================================================================
[..]
This subsection provides a set of functions allowing to initialize the USART
in asynchronous and in synchronous modes.
(+) For the asynchronous mode only these parameters can be configured:
(++) Baud Rate.
(++) Word Length.
(++) Stop Bit.
(++) Parity: If the parity is enabled, then the MSB bit of the data written
in the data register is transmitted but is changed by the parity bit.
Depending on the frame length defined by the M bit (8-bits or 9-bits),
the possible USART frame formats are as listed in the following table:
[..]
+-------------------------------------------------------------+
| M bit | PCE bit | USART frame |
|---------------------|---------------------------------------|
| 0 | 0 | | SB | 8 bit data | STB | |
|---------|-----------|---------------------------------------|
| 0 | 1 | | SB | 7 bit data | PB | STB | |
|---------|-----------|---------------------------------------|
| 1 | 0 | | SB | 9 bit data | STB | |
|---------|-----------|---------------------------------------|
| 1 | 1 | | SB | 8 bit data | PB | STB | |
+-------------------------------------------------------------+
[..]
(++) Hardware flow control.
(++) Receiver/transmitter modes.
[..] The USART_Init() function follows the USART asynchronous configuration
procedure(details for the procedure are available in reference manual.
(+) For the synchronous mode in addition to the asynchronous mode parameters
these parameters should be also configured:
(++) USART Clock Enabled.
(++) USART polarity.
(++) USART phase.
(++) USART LastBit.
[..] These parameters can be configured using the USART_ClockInit() function.
@endverbatim
* @{
*/
/**
* @brief Deinitializes the USARTx peripheral registers to their default reset values.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @retval None
*/
void USART_DeInit(USART_TypeDef* USARTx)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
if (USARTx == USART1)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
}
else if (USARTx == USART2)
{
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
}
else if (USARTx == USART3)
{
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
}
else if (USARTx == UART4)
{
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
}
else
{
if (USARTx == UART5)
{
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
}
}
}
/**
* @brief Initializes the USARTx peripheral according to the specified
* parameters in the USART_InitStruct .
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param USART_InitStruct: pointer to a USART_InitTypeDef structure
* that contains the configuration information for the specified USART peripheral.
* @retval None
*/
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
{
uint32_t divider = 0, apbclock = 0, tmpreg = 0;
RCC_ClocksTypeDef RCC_ClocksStatus;
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
/* Disable USART */
USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
/*---------------------------- USART CR2 Configuration -----------------------*/
tmpreg = USARTx->CR2;
/* Clear STOP[13:12] bits */
tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
/* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
/* Set STOP[13:12] bits according to USART_StopBits value */
tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
/* Write to USART CR2 */
USARTx->CR2 = tmpreg;
/*---------------------------- USART CR1 Configuration -----------------------*/
tmpreg = USARTx->CR1;
/* Clear M, PCE, PS, TE and RE bits */
tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
/* Configure the USART Word Length, Parity and mode ----------------------- */
/* Set the M bits according to USART_WordLength value */
/* Set PCE and PS bits according to USART_Parity value */
/* Set TE and RE bits according to USART_Mode value */
tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
USART_InitStruct->USART_Mode;
/* Write to USART CR1 */
USARTx->CR1 = tmpreg;
/*---------------------------- USART CR3 Configuration -----------------------*/
tmpreg = USARTx->CR3;
/* Clear CTSE and RTSE bits */
tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
/* Configure the USART HFC -------------------------------------------------*/
/* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
/* Write to USART CR3 */
USARTx->CR3 = tmpreg;
/*---------------------------- USART BRR Configuration -----------------------*/
/* Configure the USART Baud Rate -------------------------------------------*/
RCC_GetClocksFreq(&RCC_ClocksStatus);
if (USARTx == USART1)
{
apbclock = RCC_ClocksStatus.USART1CLK_Frequency;
}
else if (USARTx == USART2)
{
apbclock = RCC_ClocksStatus.USART2CLK_Frequency;
}
else if (USARTx == USART3)
{
apbclock = RCC_ClocksStatus.USART3CLK_Frequency;
}
else if (USARTx == UART4)
{
apbclock = RCC_ClocksStatus.UART4CLK_Frequency;
}
else
{
apbclock = RCC_ClocksStatus.UART5CLK_Frequency;
}
/* Determine the integer part */
if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
{
/* (divider * 10) computing in case Oversampling mode is 8 Samples */
divider = (uint32_t)((2 * apbclock) / (USART_InitStruct->USART_BaudRate));
tmpreg = (uint32_t)((2 * apbclock) % (USART_InitStruct->USART_BaudRate));
}
else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
{
/* (divider * 10) computing in case Oversampling mode is 16 Samples */
divider = (uint32_t)((apbclock) / (USART_InitStruct->USART_BaudRate));
tmpreg = (uint32_t)((apbclock) % (USART_InitStruct->USART_BaudRate));
}
/* round the divider : if fractional part i greater than 0.5 increment divider */
if (tmpreg >= (USART_InitStruct->USART_BaudRate) / 2)
{
divider++;
}
/* Implement the divider in case Oversampling mode is 8 Samples */
if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
{
/* get the LSB of divider and shift it to the right by 1 bit */
tmpreg = (divider & (uint16_t)0x000F) >> 1;
/* update the divider value */
divider = (divider & (uint16_t)0xFFF0) | tmpreg;
}
/* Write to USART BRR */
USARTx->BRR = (uint16_t)divider;
}
/**
* @brief Fills each USART_InitStruct member with its default value.
* @param USART_InitStruct: pointer to a USART_InitTypeDef structure
* which will be initialized.
* @retval None
*/
void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
{
/* USART_InitStruct members default value */
USART_InitStruct->USART_BaudRate = 9600;
USART_InitStruct->USART_WordLength = USART_WordLength_8b;
USART_InitStruct->USART_StopBits = USART_StopBits_1;
USART_InitStruct->USART_Parity = USART_Parity_No ;
USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
}
/**
* @brief Initializes the USARTx peripheral Clock according to the
* specified parameters in the USART_ClockInitStruct.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3.
* @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
* structure that contains the configuration information for the specified
* USART peripheral.
* @retval None
*/
void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_USART_123_PERIPH(USARTx));
assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
/*---------------------------- USART CR2 Configuration -----------------------*/
tmpreg = USARTx->CR2;
/* Clear CLKEN, CPOL, CPHA, LBCL and SSM bits */
tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
/* Configure the USART Clock, CPOL, CPHA, LastBit and SSM ------------*/
/* Set CLKEN bit according to USART_Clock value */
/* Set CPOL bit according to USART_CPOL value */
/* Set CPHA bit according to USART_CPHA value */
/* Set LBCL bit according to USART_LastBit value */
tmpreg |= (uint32_t)(USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit);
/* Write to USART CR2 */
USARTx->CR2 = tmpreg;
}
/**
* @brief Fills each USART_ClockInitStruct member with its default value.
* @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
* structure which will be initialized.
* @retval None
*/
void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
{
/* USART_ClockInitStruct members default value */
USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
}
/**
* @brief Enables or disables the specified USART peripheral.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USARTx peripheral.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected USART by setting the UE bit in the CR1 register */
USARTx->CR1 |= USART_CR1_UE;
}
else
{
/* Disable the selected USART by clearing the UE bit in the CR1 register */
USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
}
}
/**
* @brief Enables or disables the USART's transmitter or receiver.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param USART_Direction: specifies the USART direction.
* This parameter can be any combination of the following values:
* @arg USART_Mode_Tx: USART Transmitter
* @arg USART_Mode_Rx: USART Receiver
* @param NewState: new state of the USART transfer direction.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_DirectionModeCmd(USART_TypeDef* USARTx, uint32_t USART_DirectionMode, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_MODE(USART_DirectionMode));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the USART's transfer interface by setting the TE and/or RE bits
in the USART CR1 register */
USARTx->CR1 |= USART_DirectionMode;
}
else
{
/* Disable the USART's transfer interface by clearing the TE and/or RE bits
in the USART CR3 register */
USARTx->CR1 &= (uint32_t)~USART_DirectionMode;
}
}
/**
* @brief Enables or disables the USART's 8x oversampling mode.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USART 8x oversampling mode.
* This parameter can be: ENABLE or DISABLE.
* @note
* This function has to be called before calling USART_Init()
* function in order to have correct baudrate Divider value.
* @retval None
*/
void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
USARTx->CR1 |= USART_CR1_OVER8;
}
else
{
/* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_OVER8);
}
}
/**
* @brief Enables or disables the USART's one bit sampling method.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USART one bit sampling method.
* This parameter can be: ENABLE or DISABLE.
* @note
* This function has to be called before calling USART_Cmd() function.
* @retval None
*/
void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the one bit method by setting the ONEBIT bit in the CR3 register */
USARTx->CR3 |= USART_CR3_ONEBIT;
}
else
{
/* Disable the one bit method by clearing the ONEBIT bit in the CR3 register */
USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_ONEBIT);
}
}
/**
* @brief Enables or disables the USART's most significant bit first
* transmitted/received following the start bit.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USART most significant bit first
* transmitted/received following the start bit.
* This parameter can be: ENABLE or DISABLE.
* @note
* This function has to be called before calling USART_Cmd() function.
* @retval None
*/
void USART_MSBFirstCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the most significant bit first transmitted/received following the
start bit by setting the MSBFIRST bit in the CR2 register */
USARTx->CR2 |= USART_CR2_MSBFIRST;
}
else
{
/* Disable the most significant bit first transmitted/received following the
start bit by clearing the MSBFIRST bit in the CR2 register */
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_MSBFIRST);
}
}
/**
* @brief Enables or disables the binary data inversion.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new defined levels for the USART data.
* This parameter can be: ENABLE or DISABLE.
* @arg ENABLE: Logical data from the data register are send/received in negative
* logic. (1=L, 0=H). The parity bit is also inverted.
* @arg DISABLE: Logical data from the data register are send/received in positive
* logic. (1=H, 0=L)
* @note
* This function has to be called before calling USART_Cmd() function.
* @retval None
*/
void USART_DataInvCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the binary data inversion feature by setting the DATAINV bit in
the CR2 register */
USARTx->CR2 |= USART_CR2_DATAINV;
}
else
{
/* Disable the binary data inversion feature by clearing the DATAINV bit in
the CR2 register */
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_DATAINV);
}
}
/**
* @brief Enables or disables the Pin(s) active level inversion.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param USART_InvPin: specifies the USART pin(s) to invert.
* This parameter can be any combination of the following values:
* @arg USART_InvPin_Tx: USART Tx pin active level inversion.
* @arg USART_InvPin_Rx: USART Rx pin active level inversion.
* @param NewState: new active level status for the USART pin(s).
* This parameter can be: ENABLE or DISABLE.
* - ENABLE: pin(s) signal values are inverted (Vdd =0, Gnd =1).
* - DISABLE: pin(s) signal works using the standard logic levels (Vdd =1, Gnd =0).
* @note
* This function has to be called before calling USART_Cmd() function.
* @retval None
*/
void USART_InvPinCmd(USART_TypeDef* USARTx, uint32_t USART_InvPin, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_INVERSTION_PIN(USART_InvPin));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the active level inversion for selected pins by setting the TXINV
and/or RXINV bits in the USART CR2 register */
USARTx->CR2 |= USART_InvPin;
}
else
{
/* Disable the active level inversion for selected requests by clearing the
TXINV and/or RXINV bits in the USART CR2 register */
USARTx->CR2 &= (uint32_t)~USART_InvPin;
}
}
/**
* @brief Enables or disables the swap Tx/Rx pins.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USARTx TX/RX pins pinout.
* This parameter can be: ENABLE or DISABLE.
* @arg ENABLE: The TX and RX pins functions are swapped.
* @arg DISABLE: TX/RX pins are used as defined in standard pinout
* @note
* This function has to be called before calling USART_Cmd() function.
* @retval None
*/
void USART_SWAPPinCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the SWAP feature by setting the SWAP bit in the CR2 register */
USARTx->CR2 |= USART_CR2_SWAP;
}
else
{
/* Disable the SWAP feature by clearing the SWAP bit in the CR2 register */
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_SWAP);
}
}
/**
* @brief Enables or disables the receiver Time Out feature.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USARTx receiver Time Out.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_ReceiverTimeOutCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the receiver time out feature by setting the RTOEN bit in the CR2
register */
USARTx->CR2 |= USART_CR2_RTOEN;
}
else
{
/* Disable the receiver time out feature by clearing the RTOEN bit in the CR2
register */
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_RTOEN);
}
}
/**
* @brief Sets the receiver Time Out value.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param USART_ReceiverTimeOut: specifies the Receiver Time Out value.
* @retval None
*/
void USART_SetReceiverTimeOut(USART_TypeDef* USARTx, uint32_t USART_ReceiverTimeOut)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_TIMEOUT(USART_ReceiverTimeOut));
/* Clear the receiver Time Out value by clearing the RTO[23:0] bits in the RTOR
register */
USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_RTO);
/* Set the receiver Time Out value by setting the RTO[23:0] bits in the RTOR
register */
USARTx->RTOR |= USART_ReceiverTimeOut;
}
/**
* @brief Sets the system clock prescaler.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param USART_Prescaler: specifies the prescaler clock.
* @note
* This function has to be called before calling USART_Cmd() function.
* @retval None
*/
void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
/* Clear the USART prescaler */
USARTx->GTPR &= USART_GTPR_GT;
/* Set the USART prescaler */
USARTx->GTPR |= USART_Prescaler;
}
/**
* @}
*/
/** @defgroup USART_Group2 STOP Mode functions
* @brief STOP Mode functions
*
@verbatim
===============================================================================
##### STOP Mode functions #####
===============================================================================
[..] This subsection provides a set of functions allowing to manage
WakeUp from STOP mode.
[..] The USART is able to WakeUp from Stop Mode if USART clock is set to HSI
or LSI.
[..] The WakeUp source is configured by calling USART_StopModeWakeUpSourceConfig()
function.
[..] After configuring the source of WakeUp and before entering in Stop Mode
USART_STOPModeCmd() function should be called to allow USART WakeUp.
@endverbatim
* @{
*/
/**
* @brief Enables or disables the specified USART peripheral in STOP Mode.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USARTx peripheral state in stop mode.
* This parameter can be: ENABLE or DISABLE.
* @note
* This function has to be called when USART clock is set to HSI or LSE.
* @retval None
*/
void USART_STOPModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected USART in STOP mode by setting the UESM bit in the CR1
register */
USARTx->CR1 |= USART_CR1_UESM;
}
else
{
/* Disable the selected USART in STOP mode by clearing the UE bit in the CR1
register */
USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UESM);
}
}
/**
* @brief Selects the USART WakeUp method form stop mode.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param USART_WakeUp: specifies the selected USART wakeup method.
* This parameter can be one of the following values:
* @arg USART_WakeUpSource_AddressMatch: WUF active on address match.
* @arg USART_WakeUpSource_StartBit: WUF active on Start bit detection.
* @arg USART_WakeUpSource_RXNE: WUF active on RXNE.
* @note
* This function has to be called before calling USART_Cmd() function.
* @retval None
*/
void USART_StopModeWakeUpSourceConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUpSource)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_STOPMODE_WAKEUPSOURCE(USART_WakeUpSource));
USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_WUS);
USARTx->CR3 |= USART_WakeUpSource;
}
/**
* @}
*/
/** @defgroup USART_Group3 AutoBaudRate functions
* @brief AutoBaudRate functions
*
@verbatim
===============================================================================
##### AutoBaudRate functions #####
===============================================================================
[..] This subsection provides a set of functions allowing to manage
the AutoBaudRate detections.
[..] Before Enabling AutoBaudRate detection using USART_AutoBaudRateCmd ()
The character patterns used to calculate baudrate must be chosen by calling
USART_AutoBaudRateConfig() function. These function take as parameter :
(#)USART_AutoBaudRate_StartBit : any character starting with a bit 1.
(#)USART_AutoBaudRate_FallingEdge : any character starting with a 10xx bit pattern.
[..] At any later time, another request for AutoBaudRate detection can be performed
using USART_RequestCmd() function.
[..] The AutoBaudRate detection is monitored by the status of ABRF flag which indicate
that the AutoBaudRate detection is completed. In addition to ABRF flag, the ABRE flag
indicate that this procedure is completed without success. USART_GetFlagStatus ()
function should be used to monitor the status of these flags.
@endverbatim
* @{
*/
/**
* @brief Enables or disables the Auto Baud Rate.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USARTx auto baud rate.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_AutoBaudRateCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the auto baud rate feature by setting the ABREN bit in the CR2
register */
USARTx->CR2 |= USART_CR2_ABREN;
}
else
{
/* Disable the auto baud rate feature by clearing the ABREN bit in the CR2
register */
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABREN);
}
}
/**
* @brief Selects the USART auto baud rate method.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param USART_AutoBaudRate: specifies the selected USART auto baud rate method.
* This parameter can be one of the following values:
* @arg USART_AutoBaudRate_StartBit: Start Bit duration measurement.
* @arg USART_AutoBaudRate_FallingEdge: Falling edge to falling edge measurement.
* @arg USART_AutoBaudRate_0x7FFrame: 0x7F frame.
* @arg USART_AutoBaudRate_0x55Frame: 0x55 frame.
* @note
* This function has to be called before calling USART_Cmd() function.
* @retval None
*/
void USART_AutoBaudRateConfig(USART_TypeDef* USARTx, uint32_t USART_AutoBaudRate)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_AUTOBAUDRATE_MODE(USART_AutoBaudRate));
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABRMODE);
USARTx->CR2 |= USART_AutoBaudRate;
}
/**
* @}
*/
/** @defgroup USART_Group4 Data transfers functions
* @brief Data transfers functions
*
@verbatim
===============================================================================
##### Data transfers functions #####
===============================================================================
[..] This subsection provides a set of functions allowing to manage
the USART data transfers.
[..] During an USART reception, data shifts in least significant bit first
through the RX pin. When a transmission is taking place, a write instruction to
the USART_TDR register stores the data in the shift register.
[..] The read access of the USART_RDR register can be done using
the USART_ReceiveData() function and returns the RDR value.
Whereas a write access to the USART_TDR can be done using USART_SendData()
function and stores the written data into TDR.
@endverbatim
* @{
*/
/**
* @brief Transmits single data through the USARTx peripheral.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param Data: the data to transmit.
* @retval None
*/
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DATA(Data));
/* Transmit Data */
USARTx->TDR = (Data & (uint16_t)0x01FF);
}
/**
* @brief Returns the most recent received data by the USARTx peripheral.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @retval The received data.
*/
uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
/* Receive Data */
return (uint16_t)(USARTx->RDR & (uint16_t)0x01FF);
}
/**
* @}
*/
/** @defgroup USART_Group5 MultiProcessor Communication functions
* @brief Multi-Processor Communication functions
*
@verbatim
===============================================================================
##### Multi-Processor Communication functions #####
===============================================================================
[..] This subsection provides a set of functions allowing to manage the USART
multiprocessor communication.
[..] For instance one of the USARTs can be the master, its TX output is
connected to the RX input of the other USART. The others are slaves,
their respective TX outputs are logically ANDed together and connected
to the RX input of the master. USART multiprocessor communication is
possible through the following procedure:
(#) Program the Baud rate, Word length = 9 bits, Stop bits, Parity,
Mode transmitter or Mode receiver and hardware flow control values
using the USART_Init() function.
(#) Configures the USART address using the USART_SetAddress() function.
(#) Configures the wake up methode (USART_WakeUp_IdleLine or
USART_WakeUp_AddressMark) using USART_WakeUpConfig() function only
for the slaves.
(#) Enable the USART using the USART_Cmd() function.
(#) Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd()
function.
[..] The USART Slave exit from mute mode when receive the wake up condition.
@endverbatim
* @{
*/
/**
* @brief Sets the address of the USART node.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param USART_Address: Indicates the address of the USART node.
* @retval None
*/
void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
/* Clear the USART address */
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADD);
/* Set the USART address node */
USARTx->CR2 |=((uint32_t)USART_Address << (uint32_t)0x18);
}
/**
* @brief Enables or disables the USART's mute mode.
* @param USARTx: Select the USART peripheral. This parameter can be one of the
* following values: USART1 or USART2 or USART3 or UART4 or UART5.
* @param NewState: new state of the USART mute mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_MuteModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)