-
Notifications
You must be signed in to change notification settings - Fork 32
/
tm_stm32_usart.c
1443 lines (1326 loc) · 40.1 KB
/
tm_stm32_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
/**
* |----------------------------------------------------------------------
* | Copyright (c) 2020 Tilen MAJERLE
* |
* | Permission is hereby granted, free of charge, to any person
* | obtaining a copy of this software and associated documentation
* | files (the "Software"), to deal in the Software without restriction,
* | including without limitation the rights to use, copy, modify, merge,
* | publish, distribute, sublicense, and/or sell copies of the Software,
* | and to permit persons to whom the Software is furnished to do so,
* | subject to the following conditions:
* |
* | The above copyright notice and this permission notice shall be
* | included in all copies or substantial portions of the Software.
* |
* | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* | AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* | OTHER DEALINGS IN THE SOFTWARE.
* |----------------------------------------------------------------------
*/
#include "tm_stm32_usart.h"
#if defined(USART_DISABLE_USART1) && defined(USART1)
#undef USART1
#endif
#if defined(USART_DISABLE_USART2) && defined(USART1)
#undef USART2
#endif
#if defined(USART_DISABLE_USART3) && defined(USART1)
#undef USART3
#endif
#if defined(USART_DISABLE_UART4) && defined(UART4)
#undef UART4
#endif
#if defined(USART_DISABLE_UART5) && defined(UART5)
#undef UART5
#endif
#if defined(USART_DISABLE_USART6) && defined(USART6)
#undef SART6
#endif
#if defined(USART_DISABLE_UART7) && defined(UART7)
#undef UART7
#endif
#if defined(USART_DISABLE_UART8) && defined(UART8)
#undef UART8
#endif
/* Set alternate function mappings */
#if defined(STM32F4xx) || defined(STM32F7xx)
#if defined(USART1)
#define GPIO_AF_USART1 GPIO_AF7_USART1
#define IRQ_USART1 USART1_IRQn
#endif
#if defined(USART2)
#define GPIO_AF_USART2 GPIO_AF7_USART2
#define IRQ_USART2 USART2_IRQn
#endif
#if defined(USART3)
#define GPIO_AF_USART3 GPIO_AF7_USART3
#define IRQ_USART3 USART3_IRQn
#endif
#if defined(UART4)
#define GPIO_AF_UART4 GPIO_AF8_UART4
#define IRQ_UART4 UART4_IRQn
#endif
#if defined(UART5)
#define IRQ_UART5 UART5_IRQn
#endif
#if defined(USART6)
#define GPIO_AF_USART6 GPIO_AF8_USART6
#define IRQ_USART6 USART6_IRQn
#endif
#if defined(UART7)
#define GPIO_AF_UART7 GPIO_AF8_UART7
#define IRQ_UART7 UART7_IRQn
#endif
#if defined(UART8)
#define GPIO_AF_UART8 GPIO_AF8_UART8
#define IRQ_UART8 UART8_IRQn
#endif
#elif defined(STM32F0xx)
#if defined(GPIO_AF1_USART1)
#define GPIO_AF_USART1 GPIO_AF1_USART1
#elif defined(GPIO_AF0_USART1)
#define GPIO_AF_USART1 GPIO_AF0_USART1
#endif
#if defined(GPIO_AF1_USART2)
#define GPIO_AF_USART2 GPIO_AF1_USART2
#elif defined(GPIO_AF0_USART2)
#define GPIO_AF_USART2 GPIO_AF0_USART2
#endif
#if defined(GPIO_AF4_USART3)
#define GPIO_AF_USART3 GPIO_AF4_USART3
#elif defined(GPIO_AF1_USART3)
#define GPIO_AF_USART3 GPIO_AF1_USART3
#elif defined(GPIO_AF0_USART3)
#define GPIO_AF_USART3 GPIO_AF0_USART3
#endif
#if defined(GPIO_AF4_USART4)
#define GPIO_AF_USART4 GPIO_AF4_USART4
#elif defined(GPIO_AF0_USART4)
#define GPIO_AF_USART4 GPIO_AF0_USART4
#endif
#if defined(GPIO_AF4_USART5)
#define GPIO_AF_USART5 GPIO_AF4_USART5
#elif defined(GPIO_AF2_USART5)
#define GPIO_AF_USART5 GPIO_AF2_USART5
#elif defined(GPIO_AF1_USART5)
#define GPIO_AF_USART5 GPIO_AF1_USART5
#endif
#if defined(GPIO_AF5_USART6)
#define GPIO_AF_USART6 GPIO_AF5_USART6
#elif defined(GPIO_AF2_USART6)
#define GPIO_AF_USART6 GPIO_AF2_USART6
#elif defined(GPIO_AF1_USART6)
#define GPIO_AF_USART6 GPIO_AF1_USART6
#endif
#if defined(GPIO_AF2_USART7)
#define GPIO_AF_USART7 GPIO_AF2_USART7
#elif defined(GPIO_AF1_USART7)
#define GPIO_AF_USART7 GPIO_AF1_USART7
#endif
#if defined(GPIO_AF2_USART8)
#define GPIO_AF_USART8 GPIO_AF2_USART8
#elif defined(GPIO_AF1_USART8)
#define GPIO_AF_USART8 GPIO_AF1_USART8
#elif defined(GPIO_AF0_USART8)
#define GPIO_AF_USART8 GPIO_AF0_USART8
#endif
#if defined(USART1)
#define IRQ_USART1 USART1_IRQn
#endif
#if defined(USART2)
#define IRQ_USART2 USART2_IRQn
#endif
#if defined(USART8)
#define IRQ_USART3 USART3_8_IRQn
#define IRQ_USART4 USART3_8_IRQn
#define IRQ_USART5 USART3_8_IRQn
#define IRQ_USART6 USART3_8_IRQn
#define IRQ_USART7 USART3_8_IRQn
#define IRQ_USART8 USART3_8_IRQn
#elif defined(USART6)
#define IRQ_USART3 USART3_6_IRQn
#define IRQ_USART4 USART3_6_IRQn
#define IRQ_USART5 USART3_6_IRQn
#define IRQ_USART6 USART3_6_IRQn
#elif defined(USART4)
#define IRQ_USART3 USART3_4_IRQn
#define IRQ_USART4 USART3_4_IRQn
#endif
#endif
/* Set variables for buffers */
#ifdef USART1
uint8_t USART1_Buffer[TM_USART1_BUFFER_SIZE];
#endif
#ifdef USART2
uint8_t USART2_Buffer[TM_USART2_BUFFER_SIZE];
#endif
#ifdef USART3
uint8_t USART3_Buffer[TM_USART3_BUFFER_SIZE];
#endif
#ifdef UART4
uint8_t UART4_Buffer[TM_UART4_BUFFER_SIZE];
#endif
#ifdef UART5
uint8_t UART5_Buffer[TM_UART5_BUFFER_SIZE];
#endif
#ifdef USART6
uint8_t USART6_Buffer[TM_USART6_BUFFER_SIZE];
#endif
#ifdef UART7
uint8_t UART7_Buffer[TM_UART7_BUFFER_SIZE];
#endif
#ifdef UART8
uint8_t UART8_Buffer[TM_UART8_BUFFER_SIZE];
#endif
/* STM32F0xx added */
#ifdef USART4
uint8_t USART4_Buffer[TM_USART4_BUFFER_SIZE];
#endif
#ifdef USART5
uint8_t USART5_Buffer[TM_USART5_BUFFER_SIZE];
#endif
#ifdef USART7
uint8_t USART7_Buffer[TM_USART7_BUFFER_SIZE];
#endif
#ifdef USART8
uint8_t USART8_Buffer[TM_USART8_BUFFER_SIZE];
#endif
#ifdef USART1
TM_BUFFER_t TM_USART1 = {TM_USART1_BUFFER_SIZE, 0, 0, USART1_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef USART2
TM_BUFFER_t TM_USART2 = {TM_USART2_BUFFER_SIZE, 0, 0, USART2_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef USART3
TM_BUFFER_t TM_USART3 = {TM_USART3_BUFFER_SIZE, 0, 0, USART3_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef UART4
TM_BUFFER_t TM_UART4 = {TM_UART4_BUFFER_SIZE, 0, 0, UART4_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef UART5
TM_BUFFER_t TM_UART5 = {TM_UART5_BUFFER_SIZE, 0, 0, UART5_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef USART6
TM_BUFFER_t TM_USART6 = {TM_USART6_BUFFER_SIZE, 0, 0, USART6_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef UART7
TM_BUFFER_t TM_UART7 = {TM_UART7_BUFFER_SIZE, 0, 0, UART7_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef UART8
TM_BUFFER_t TM_UART8 = {TM_UART8_BUFFER_SIZE, 0, 0, UART8_Buffer, 0, USART_STRING_DELIMITER};
#endif
/* STM32F0xx added */
#ifdef USART4
TM_BUFFER_t TM_USART4 = {TM_USART8_BUFFER_SIZE, 0, 0, USART4_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef USART5
TM_BUFFER_t TM_USART5 = {TM_USART5_BUFFER_SIZE, 0, 0, USART5_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef USART7
TM_BUFFER_t TM_USART7 = {TM_USART7_BUFFER_SIZE, 0, 0, USART7_Buffer, 0, USART_STRING_DELIMITER};
#endif
#ifdef USART8
TM_BUFFER_t TM_USART8 = {TM_USART8_BUFFER_SIZE, 0, 0, USART8_Buffer, 0, USART_STRING_DELIMITER};
#endif
/* Private functions */
void TM_USART1_InitPins(TM_USART_PinsPack_t pinspack);
void TM_USART2_InitPins(TM_USART_PinsPack_t pinspack);
void TM_USART3_InitPins(TM_USART_PinsPack_t pinspack);
void TM_UART4_InitPins(TM_USART_PinsPack_t pinspack);
void TM_UART5_InitPins(TM_USART_PinsPack_t pinspack);
void TM_USART6_InitPins(TM_USART_PinsPack_t pinspack);
void TM_UART7_InitPins(TM_USART_PinsPack_t pinspack);
void TM_UART8_InitPins(TM_USART_PinsPack_t pinspack);
static void TM_USART_INT_InsertToBuffer(TM_BUFFER_t* u, uint8_t c);
static void TM_USART_INT_ClearAllFlags(USART_TypeDef* USARTx, IRQn_Type irq);
static TM_BUFFER_t* TM_USART_INT_GetUSARTBuffer(USART_TypeDef* USARTx);
static uint8_t TM_USART_INT_GetSubPriority(USART_TypeDef* USARTx);
uint8_t TM_USART_BufferFull(USART_TypeDef* USARTx);
/* Private initializator function */
static void TM_USART_INT_Init(
USART_TypeDef* USARTx,
TM_USART_PinsPack_t pinspack,
uint32_t baudrate,
TM_USART_HardwareFlowControl_t FlowControl,
uint32_t Mode,
uint32_t Parity,
uint32_t StopBits,
uint32_t WordLength
);
void TM_USART_Init(USART_TypeDef* USARTx, TM_USART_PinsPack_t pinspack, uint32_t baudrate) {
#ifdef USART1
if (USARTx == USART1) {
TM_USART_INT_Init(USART1, pinspack, baudrate, TM_USART1_HARDWARE_FLOW_CONTROL, TM_USART1_MODE, TM_USART1_PARITY, TM_USART1_STOP_BITS, TM_USART1_WORD_LENGTH);
}
#endif
#ifdef USART2
if (USARTx == USART2) {
TM_USART_INT_Init(USART2, pinspack, baudrate, TM_USART2_HARDWARE_FLOW_CONTROL, TM_USART2_MODE, TM_USART2_PARITY, TM_USART2_STOP_BITS, TM_USART2_WORD_LENGTH);
}
#endif
#ifdef USART3
if (USARTx == USART3) {
TM_USART_INT_Init(USART3, pinspack, baudrate, TM_USART3_HARDWARE_FLOW_CONTROL, TM_USART3_MODE, TM_USART3_PARITY, TM_USART3_STOP_BITS, TM_USART3_WORD_LENGTH);
}
#endif
#ifdef UART4
if (USARTx == UART4) {
TM_USART_INT_Init(UART4, pinspack, baudrate, TM_UART4_HARDWARE_FLOW_CONTROL, TM_UART4_MODE, TM_UART4_PARITY, TM_UART4_STOP_BITS, TM_UART4_WORD_LENGTH);
}
#endif
#ifdef UART5
if (USARTx == UART5) {
TM_USART_INT_Init(UART5, pinspack, baudrate, TM_UART5_HARDWARE_FLOW_CONTROL, TM_UART5_MODE, TM_UART5_PARITY, TM_UART5_STOP_BITS, TM_UART5_WORD_LENGTH);
}
#endif
#ifdef USART6
if (USARTx == USART6) {
TM_USART_INT_Init(USART6, pinspack, baudrate, TM_USART6_HARDWARE_FLOW_CONTROL, TM_USART6_MODE, TM_USART6_PARITY, TM_USART6_STOP_BITS, TM_USART6_WORD_LENGTH);
}
#endif
#ifdef UART7
if (USARTx == UART7) {
TM_USART_INT_Init(UART7, pinspack, baudrate, TM_UART7_HARDWARE_FLOW_CONTROL, TM_UART7_MODE, TM_UART7_PARITY, TM_UART7_STOP_BITS, TM_UART7_WORD_LENGTH);
}
#endif
#ifdef UART8
if (USARTx == UART8) {
TM_USART_INT_Init(UART8, pinspack, baudrate, TM_UART8_HARDWARE_FLOW_CONTROL, TM_UART8_MODE, TM_UART8_PARITY, TM_UART8_STOP_BITS, TM_UART8_WORD_LENGTH);
}
#endif
/* STM32F0xx related */
#ifdef USART4
if (USARTx == USART4) {
TM_USART_INT_Init(USART4, pinspack, baudrate, TM_USART4_HARDWARE_FLOW_CONTROL, TM_USART4_MODE, TM_USART4_PARITY, TM_USART4_STOP_BITS, TM_USART4_WORD_LENGTH);
}
#endif
#ifdef USART5
if (USARTx == USART5) {
TM_USART_INT_Init(USART5, pinspack, baudrate, TM_USART5_HARDWARE_FLOW_CONTROL, TM_USART5_MODE, TM_USART5_PARITY, TM_USART5_STOP_BITS, TM_USART5_WORD_LENGTH);
}
#endif
#ifdef USART7
if (USARTx == USART7) {
TM_USART_INT_Init(USART7, pinspack, baudrate, TM_USART7_HARDWARE_FLOW_CONTROL, TM_USART7_MODE, TM_USART7_PARITY, TM_USART7_STOP_BITS, TM_USART7_WORD_LENGTH);
}
#endif
#ifdef USART8
if (USARTx == USART8) {
TM_USART_INT_Init(USART8, pinspack, baudrate, TM_USART8_HARDWARE_FLOW_CONTROL, TM_USART8_MODE, TM_USART8_PARITY, TM_USART8_STOP_BITS, TM_USART8_WORD_LENGTH);
}
#endif
}
void TM_USART_InitWithFlowControl(USART_TypeDef* USARTx, TM_USART_PinsPack_t pinspack, uint32_t baudrate, TM_USART_HardwareFlowControl_t FlowControl) {
#ifdef USART1
if (USARTx == USART1) {
TM_USART_INT_Init(USART1, pinspack, baudrate, FlowControl, TM_USART1_MODE, TM_USART1_PARITY, TM_USART1_STOP_BITS, TM_USART1_WORD_LENGTH);
}
#endif
#ifdef USART2
if (USARTx == USART2) {
TM_USART_INT_Init(USART2, pinspack, baudrate, FlowControl, TM_USART2_MODE, TM_USART2_PARITY, TM_USART2_STOP_BITS, TM_USART2_WORD_LENGTH);
}
#endif
#ifdef USART3
if (USARTx == USART3) {
TM_USART_INT_Init(USART3, pinspack, baudrate, FlowControl, TM_USART3_MODE, TM_USART3_PARITY, TM_USART3_STOP_BITS, TM_USART3_WORD_LENGTH);
}
#endif
#ifdef UART4
if (USARTx == UART4) {
TM_USART_INT_Init(UART4, pinspack, baudrate, FlowControl, TM_UART4_MODE, TM_UART4_PARITY, TM_UART4_STOP_BITS, TM_UART4_WORD_LENGTH);
}
#endif
#ifdef UART5
if (USARTx == UART5) {
TM_USART_INT_Init(UART5, pinspack, baudrate, FlowControl, TM_UART5_MODE, TM_UART5_PARITY, TM_UART5_STOP_BITS, TM_UART5_WORD_LENGTH);
}
#endif
#ifdef USART6
if (USARTx == USART6) {
TM_USART_INT_Init(USART6, pinspack, baudrate, FlowControl, TM_USART6_MODE, TM_USART6_PARITY, TM_USART6_STOP_BITS, TM_USART6_WORD_LENGTH);
}
#endif
#ifdef UART7
if (USARTx == UART7) {
TM_USART_INT_Init(UART7, pinspack, baudrate, FlowControl, TM_UART7_MODE, TM_UART7_PARITY, TM_UART7_STOP_BITS, TM_UART7_WORD_LENGTH);
}
#endif
#ifdef UART8
if (USARTx == UART8) {
TM_USART_INT_Init(UART8, pinspack, baudrate, FlowControl, TM_UART8_MODE, TM_UART8_PARITY, TM_UART8_STOP_BITS, TM_UART8_WORD_LENGTH);
}
#endif
/* STM32F0xx related */
#ifdef USART4
if (USARTx == USART4) {
TM_USART_INT_Init(USART4, pinspack, baudrate, FlowControl, TM_USART4_MODE, TM_USART4_PARITY, TM_USART4_STOP_BITS, TM_USART4_WORD_LENGTH);
}
#endif
#ifdef USART5
if (USARTx == USART5) {
TM_USART_INT_Init(USART5, pinspack, baudrate, FlowControl, TM_USART5_MODE, TM_USART5_PARITY, TM_USART5_STOP_BITS, TM_USART5_WORD_LENGTH);
}
#endif
#ifdef USART7
if (USARTx == USART7) {
TM_USART_INT_Init(USART7, pinspack, baudrate, FlowControl, TM_USART7_MODE, TM_USART7_PARITY, TM_USART7_STOP_BITS, TM_USART7_WORD_LENGTH);
}
#endif
#ifdef USART8
if (USARTx == USART8) {
TM_USART_INT_Init(USART8, pinspack, baudrate, FlowControl, TM_USART8_MODE, TM_USART8_PARITY, TM_USART8_STOP_BITS, TM_USART8_WORD_LENGTH);
}
#endif
}
uint8_t TM_USART_Getc(USART_TypeDef* USARTx) {
uint8_t c;
/* Read character from buffer */
if (TM_BUFFER_Read(TM_USART_INT_GetUSARTBuffer(USARTx), &c, 1)) {
return c;
}
/* Read was not successfull */
return 0;
}
uint16_t TM_USART_Gets(USART_TypeDef* USARTx, char* buffer, uint16_t bufsize) {
return TM_BUFFER_ReadString(TM_USART_INT_GetUSARTBuffer(USARTx), buffer, bufsize);
}
void TM_USART_Puts(USART_TypeDef* USARTx, char* str) {
/* Go through entire string */
while (*str) {
/* Wait to be ready, buffer empty */
USART_WAIT(USARTx);
/* Send data */
USART_WRITE_DATA(USARTx, (uint16_t)(*str++));
/* Wait to be ready, buffer empty */
USART_WAIT(USARTx);
}
}
void TM_USART_Send(USART_TypeDef* USARTx, uint8_t* DataArray, uint32_t count) {
/* Go through entire data array */
while (count--) {
/* Wait to be ready, buffer empty */
USART_WAIT(USARTx);
/* Send data */
USART_WRITE_DATA(USARTx, (uint16_t)(*DataArray++));
/* Wait to be ready, buffer empty */
USART_WAIT(USARTx);
}
}
int16_t TM_USART_FindCharacter(USART_TypeDef* USARTx, uint8_t c) {
return TM_BUFFER_FindElement(TM_USART_INT_GetUSARTBuffer(USARTx), c);
}
int16_t TM_USART_FindString(USART_TypeDef* USARTx, char* str) {
return TM_BUFFER_Find(TM_USART_INT_GetUSARTBuffer(USARTx), (uint8_t *)str, strlen(str));
}
uint8_t TM_USART_BufferEmpty(USART_TypeDef* USARTx) {
return TM_BUFFER_GetFull(TM_USART_INT_GetUSARTBuffer(USARTx)) == 0;
}
uint8_t TM_USART_BufferFull(USART_TypeDef* USARTx) {
return TM_BUFFER_GetFree(TM_USART_INT_GetUSARTBuffer(USARTx)) == 0;
}
uint16_t TM_USART_BufferCount(USART_TypeDef* USARTx) {
return TM_BUFFER_GetFull(TM_USART_INT_GetUSARTBuffer(USARTx));
}
void TM_USART_ClearBuffer(USART_TypeDef* USARTx) {
TM_BUFFER_Reset(TM_USART_INT_GetUSARTBuffer(USARTx));
}
void TM_USART_SetCustomStringEndCharacter(USART_TypeDef* USARTx, uint8_t Character) {
TM_BUFFER_SetStringDelimiter(TM_USART_INT_GetUSARTBuffer(USARTx), Character);
}
/************************************/
/* CALLBACKS */
/************************************/
__weak void TM_USART_InitCustomPinsCallback(USART_TypeDef* USARTx, uint16_t AlternateFunction) {
/* NOTE: This function Should not be modified, when the callback is needed,
the TM_USART_InitCustomPinsCallback could be implemented in the user file
*/
}
__weak void TM_USART1_ReceiveHandler(uint8_t c) { }
__weak void TM_USART2_ReceiveHandler(uint8_t c) { }
__weak void TM_USART3_ReceiveHandler(uint8_t c) { }
__weak void TM_UART4_ReceiveHandler(uint8_t c) { }
__weak void TM_UART5_ReceiveHandler(uint8_t c) { }
__weak void TM_USART6_ReceiveHandler(uint8_t c) { }
__weak void TM_UART7_ReceiveHandler(uint8_t c) { }
__weak void TM_UART8_ReceiveHandler(uint8_t c) { }
/* Private functions */
static void TM_USART_INT_InsertToBuffer(TM_BUFFER_t* u, uint8_t c) {
TM_BUFFER_Write(u, &c, 1);
}
static TM_BUFFER_t* TM_USART_INT_GetUSARTBuffer(USART_TypeDef* USARTx) {
TM_BUFFER_t* u;
#ifdef USART1
if (USARTx == USART1) {
u = &TM_USART1;
}
#endif
#ifdef USART2
if (USARTx == USART2) {
u = &TM_USART2;
}
#endif
#ifdef USART3
if (USARTx == USART3) {
u = &TM_USART3;
}
#endif
#ifdef UART4
if (USARTx == UART4) {
u = &TM_UART4;
}
#endif
#ifdef UART5
if (USARTx == UART5) {
u = &TM_UART5;
}
#endif
#ifdef USART6
if (USARTx == USART6) {
u = &TM_USART6;
}
#endif
#ifdef UART7
if (USARTx == UART7) {
u = &TM_UART7;
}
#endif
#ifdef UART8
if (USARTx == UART8) {
u = &TM_UART8;
}
#endif
/* STM32F0xx related */
#ifdef USART4
if (USARTx == USART4) {
u = &TM_USART4;
}
#endif
#ifdef USART5
if (USARTx == USART5) {
u = &TM_USART5;
}
#endif
#ifdef USART7
if (USARTx == USART7) {
u = &TM_USART7;
}
#endif
#ifdef USART8
if (USARTx == USART8) {
u = &TM_USART8;
}
#endif
return u;
}
static uint8_t TM_USART_INT_GetSubPriority(USART_TypeDef* USARTx) {
uint8_t u;
#ifdef USART1
if (USARTx == USART1) {
u = 0;
}
#endif
#ifdef USART2
if (USARTx == USART2) {
u = 1;
}
#endif
#ifdef USART3
if (USARTx == USART3) {
u = 2;
}
#endif
#ifdef UART4
if (USARTx == UART4) {
u = 4;
}
#endif
#ifdef UART5
if (USARTx == UART5) {
u = 5;
}
#endif
#ifdef USART6
if (USARTx == USART6) {
u = 6;
}
#endif
#ifdef UART7
if (USARTx == UART7) {
u = 7;
}
#endif
#ifdef UART8
if (USARTx == UART8) {
u = 8;
}
#endif
/* STM32F0xx related */
#ifdef USART4
if (USARTx == USART4) {
u = 4;
}
#endif
#ifdef USART5
if (USARTx == USART5) {
u = 5;
}
#endif
#ifdef USART7
if (USARTx == USART7) {
u = 7;
}
#endif
#ifdef USART8
if (USARTx == USART8) {
u = 8;
}
#endif
return u;
}
/* PIN initializations */
#ifdef USART1
void TM_USART1_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOA)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOA, GPIO_PIN_9 | GPIO_PIN_10, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART1);
}
#endif
#if defined(GPIOB)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOB, GPIO_PIN_6 | GPIO_PIN_7, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART1);
}
#endif
#if defined(GPIOA) && defined(GPIOB)
if (pinspack == TM_USART_PinsPack_3) {
TM_GPIO_InitAlternate(GPIOA, GPIO_PIN_9, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART1);
TM_GPIO_InitAlternate(GPIOB, GPIO_PIN_7, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART1);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(USART1, GPIO_AF_USART1);
}
}
#endif
#ifdef USART2
void TM_USART2_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOA)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOA, GPIO_PIN_2 | GPIO_PIN_3, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART2);
}
#endif
#if defined(GPIOD)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOD, GPIO_PIN_5 | GPIO_PIN_6, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART2);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(USART2, GPIO_AF_USART2);
}
}
#endif
#ifdef USART3
void TM_USART3_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOB)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOB, GPIO_PIN_10 | GPIO_PIN_11, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART3);
}
#endif
#if defined(GPIOC)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_10 | GPIO_PIN_11, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART3);
}
#endif
#if defined(GPIOD)
if (pinspack == TM_USART_PinsPack_3) {
TM_GPIO_InitAlternate(GPIOD, GPIO_PIN_8 | GPIO_PIN_9, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART3);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(USART3, GPIO_AF_USART3);
}
}
#endif
#ifdef UART4
void TM_UART4_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOA)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOA, GPIO_PIN_0 | GPIO_PIN_1, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_UART4);
}
#endif
#if defined(GPIOC)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_10 | GPIO_PIN_11, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_UART4);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(UART4, GPIO_AF_UART4);
}
}
#endif
#ifdef UART5
void TM_UART5_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOC) && defined(GPIOD)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_12, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_UART5);
TM_GPIO_InitAlternate(GPIOD, GPIO_PIN_2, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_UART5);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(UART5, GPIO_AF_UART5);
}
}
#endif
#ifdef USART6
void TM_USART6_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOC)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_6 | GPIO_PIN_7, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART6);
}
#endif
#if defined(GPIOG)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOG, GPIO_PIN_14 | GPIO_PIN_9, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART6);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(USART6, GPIO_AF_USART6);
}
}
#endif
#ifdef UART7
void TM_UART7_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOE)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOE, GPIO_PIN_8 | GPIO_PIN_7, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_UART7);
}
#endif
#if defined(GPIOF)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOF, GPIO_PIN_7 | GPIO_PIN_6, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_UART7);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(UART7, GPIO_AF_UART7);
}
}
#endif
#ifdef UART8
void TM_UART8_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOE)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOE, GPIO_PIN_1 | GPIO_PIN_0, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_UART8);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(UART8, GPIO_AF_UART8);
}
}
#endif
/* STM32F0xx related */
#ifdef USART4
void TM_USART4_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOA)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOA, GPIO_PIN_0 | GPIO_PIN_1, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART4);
}
#endif
#if defined(GPIOC)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_10 | GPIO_PIN_11, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART4);
}
#endif
#if defined(GPIOE)
if (pinspack == TM_USART_PinsPack_3) {
TM_GPIO_InitAlternate(GPIOE, GPIO_PIN_8 | GPIO_PIN_9, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART4);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(USART4, GPIO_AF_USART4);
}
}
#endif
#ifdef USART5
void TM_USART5_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOB)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOB, GPIO_PIN_3 | GPIO_PIN_4, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART5);
}
#endif
#if defined(GPIOC) && defined(GPIOD)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_12, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART5);
TM_GPIO_InitAlternate(GPIOD, GPIO_PIN_2, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART5);
}
#endif
#if defined(GPIOE)
if (pinspack == TM_USART_PinsPack_3) {
TM_GPIO_InitAlternate(GPIOE, GPIO_PIN_10 | GPIO_PIN_11, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART5);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(USART5, GPIO_AF_USART5);
}
}
#endif
#ifdef USART7
void TM_USART7_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOC)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_0 | GPIO_PIN_1, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART7);
}
#endif
#if defined(GPIOC)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_6 | GPIO_PIN_7, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART7);
}
#endif
#if defined(GPIOF)
if (pinspack == TM_USART_PinsPack_3) {
TM_GPIO_InitAlternate(GPIOF, GPIO_PIN_2 | GPIO_PIN_3, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART7);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(USART7, GPIO_AF_USART7);
}
}
#endif
#ifdef USART8
void TM_USART8_InitPins(TM_USART_PinsPack_t pinspack) {
/* Init pins */
#if defined(GPIOC)
if (pinspack == TM_USART_PinsPack_1) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_2 | GPIO_PIN_3, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART8);
}
#endif
#if defined(GPIOC)
if (pinspack == TM_USART_PinsPack_2) {
TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_8 | GPIO_PIN_9, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART8);
}
#endif
#if defined(GPIOD)
if (pinspack == TM_USART_PinsPack_3) {
TM_GPIO_InitAlternate(GPIOD, GPIO_PIN_13 | GPIO_PIN_14, TM_GPIO_OType_PP, TM_GPIO_PuPd_UP, TM_GPIO_Speed_High, GPIO_AF_USART8);
}
#endif
if (pinspack == TM_USART_PinsPack_Custom) {
/* Init custom pins, callback used */
TM_USART_InitCustomPinsCallback(USART8, GPIO_AF_USART8);
}
}
#endif
/* Interrupt handlers */
#ifdef USART1
void USART1_IRQHandler(void) {
/* Check if interrupt was because data is received */
if (USART1->USART_STATUS_REG & USART_ISR_RXNE) {
#ifdef TM_USART1_USE_CUSTOM_IRQ
/* Call user function */
TM_USART1_ReceiveHandler(USART_READ_DATA(USART1));
#else
/* Put received data into internal buffer */
TM_USART_INT_InsertToBuffer(&TM_USART1, USART_READ_DATA(USART1));
#endif
}
/* Clear all USART flags */
TM_USART_INT_ClearAllFlags(USART1, IRQ_USART1);
}
#endif
#ifdef USART2
void USART2_IRQHandler(void) {
/* Check if interrupt was because data is received */
if (USART2->USART_STATUS_REG & USART_ISR_RXNE) {
#ifdef TM_USART2_USE_CUSTOM_IRQ
/* Call user function */
TM_USART2_ReceiveHandler(USART_READ_DATA(USART2));
#else
/* Put received data into internal buffer */
TM_USART_INT_InsertToBuffer(&TM_USART2, USART_READ_DATA(USART2));
#endif
}
/* Clear all USART flags */
TM_USART_INT_ClearAllFlags(USART2, IRQ_USART2);
}
#endif
#ifdef USART3
void USART3_IRQHandler(void) {
/* Check if interrupt was because data is received */
if (USART3->USART_STATUS_REG & USART_ISR_RXNE) {
#ifdef TM_USART3_USE_CUSTOM_IRQ
/* Call user function */
TM_USART3_ReceiveHandler(USART_READ_DATA(USART3));
#else
/* Put received data into internal buffer */
TM_USART_INT_InsertToBuffer(&TM_USART3, USART_READ_DATA(USART3));
#endif
}
/* Clear all USART flags */
TM_USART_INT_ClearAllFlags(USART3, IRQ_USART3);
}
#endif
#ifdef UART4
void UART4_IRQHandler(void) {
/* Check if interrupt was because data is received */
if (UART4->USART_STATUS_REG & USART_ISR_RXNE) {
#ifdef TM_UART4_USE_CUSTOM_IRQ
/* Call user function */
TM_UART4_ReceiveHandler(USART_READ_DATA(UART4));
#else
/* Put received data into internal buffer */
TM_USART_INT_InsertToBuffer(&TM_UART4, USART_READ_DATA(UART4));
#endif
}
/* Clear all USART flags */
TM_USART_INT_ClearAllFlags(UART4, IRQ_UART4);
}
#endif
#ifdef UART5
void UART5_IRQHandler(void) {
/* Check if interrupt was because data is received */
if (UART5->USART_STATUS_REG & USART_ISR_RXNE) {
#ifdef TM_UART5_USE_CUSTOM_IRQ
/* Call user function */
TM_UART5_ReceiveHandler(USART_READ_DATA(UART5));
#else
/* Put received data into internal buffer */
TM_USART_INT_InsertToBuffer(&TM_UART5, USART_READ_DATA(UART5));
#endif
}
/* Clear all USART flags */
TM_USART_INT_ClearAllFlags(UART5, IRQ_UART5);
}
#endif
#ifdef USART6
void USART6_IRQHandler(void) {
/* Check if interrupt was because data is received */
if (USART6->USART_STATUS_REG & USART_ISR_RXNE) {
#ifdef TM_USART6_USE_CUSTOM_IRQ
/* Call user function */
TM_USART6_ReceiveHandler(USART_READ_DATA(USART6));
#else
/* Put received data into internal buffer */
TM_USART_INT_InsertToBuffer(&TM_USART6, USART_READ_DATA(USART6));
#endif
}
/* Clear all USART flags */
TM_USART_INT_ClearAllFlags(USART6, IRQ_USART6);
}
#endif