Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
uart_arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 AggieAir, A Remote Sensing Unmanned Aerial System for Scientific Applications
3 * Utah State University, http://aggieair.usu.edu/
4 *
5 * Michal Podhradsky (michal.podhradsky@aggiemail.usu.edu)
6 * Calvin Coopmans (c.r.coopmans@ieee.org)
7 *
8 * Alexandre Bustico <alexandre.bustico@enac.fr>
9 * Gautier Hattenberger <gautier.hattenberger@enac.fr>
10 *
11 * This file is part of paparazzi.
12 *
13 * paparazzi is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
18 * paparazzi is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with paparazzi; see the file COPYING. If not, write to
25 * the Free Software Foundation, 59 Temple Place - Suite 330,
26 * Boston, MA 02111-1307, USA.
27 */
37#include "mcu_periph/uart_arch.h"
38#include <ch.h>
39#include <hal.h>
40#include "mcu_periph/gpio.h"
41#include BOARD_CONFIG
42
43#if HAL_USE_SERIAL
44
45// Default stack size
46#ifndef UART_THREAD_STACK_SIZE
47#define UART_THREAD_STACK_SIZE 512
48#endif
49
50struct SerialInit {
51 SerialConfig *conf;
58};
59
60#define SERIAL_INIT_NULL { NULL, NULL, NULL, NULL, NULL, 0, 0 }
61
65UNUSED static void handle_uart_rx(struct uart_periph *p)
66{
67 // wait for next incoming byte
68 uint8_t c = sdGet((SerialDriver *)(p->reg_addr));
69
70 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
71 chMtxLock(init_struct->rx_mtx);
72 uint16_t temp = (p->rx_insert_idx + 1) % UART_RX_BUFFER_SIZE;;
73 // insert new byte
74 p->rx_buf[p->rx_insert_idx] = c;
75 // check for more room in queue
76 if (temp != p->rx_extract_idx) {
77 p->rx_insert_idx = temp; // update insert index
78 }
79 chMtxUnlock(init_struct->rx_mtx);
80 chSemSignal(init_struct->rx_sem);
81}
82
86UNUSED static void handle_uart_tx(struct uart_periph *p)
87{
88 // check if more data to send
89 // send by block with sdWrite if possible: not compatible with soft flow control
90 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
91 chSemWait(init_struct->tx_sem);
92 p->tx_running = true;
93 while (p->tx_insert_idx != p->tx_extract_idx) {
94#if USE_UART_SOFT_FLOW_CONTROL
95 if (init_struct->cts_port != 0) {
96 // wait for CTS line to be set to send next byte
97 while (gpio_get(init_struct->cts_port, init_struct->cts_pin) == 1) ;
98 }
99 uint8_t data = p->tx_buf[p->tx_extract_idx];
100 sdPut((SerialDriver *)p->reg_addr, data);
101 chMtxLock(init_struct->tx_mtx);
102 p->tx_extract_idx++;
103 p->tx_extract_idx %= UART_TX_BUFFER_SIZE;
104 chMtxUnlock(init_struct->tx_mtx);
105 if (init_struct->cts_port != 0) {
106 // wait for physical transfer to be completed
107 while ((((SerialDriver *)p->reg_addr)->usart->SR & USART_SR_TC) == 0) ;
108 }
109#else // normal operation without soft flow control
110 chMtxLock(init_struct->tx_mtx);
111 if (p->tx_insert_idx > p->tx_extract_idx) {
112 sdWrite((SerialDriver *)p->reg_addr, &p->tx_buf[p->tx_extract_idx], p->tx_insert_idx - p->tx_extract_idx);
113 } else {
114 // wrapping circular buffer
115 sdWrite((SerialDriver *)p->reg_addr, &p->tx_buf[p->tx_extract_idx], UART_TX_BUFFER_SIZE - p->tx_extract_idx);
116 if(p->tx_insert_idx > 0) {
117 sdWrite((SerialDriver *)p->reg_addr, &p->tx_buf[0], p->tx_insert_idx);
118 }
119 }
120 p->tx_extract_idx = p->tx_insert_idx;
121 chMtxUnlock(init_struct->tx_mtx);
122#endif
123 }
124 p->tx_running = false;
125}
126
127#if USE_UART1
128
129#ifndef UART1_BAUD
130#define UART1_BAUD SERIAL_DEFAULT_BITRATE
131#endif
132
133/* by default enable UART Tx and Rx */
134#ifndef USE_UART1_TX
135#define USE_UART1_TX TRUE
136#endif
137#ifndef USE_UART1_RX
138#define USE_UART1_RX TRUE
139#endif
140
141#ifndef UART1_CR1
142#define UART1_CR1 0
143#endif
144
145#ifndef UART1_CR2
146#define UART1_CR2 USART_CR2_STOP1_BITS
147#endif
148
149#ifndef UART1_CR3
150#define UART1_CR3 0
151#endif
152
154 UART1_BAUD, /* BITRATE */
155 UART1_CR1, /* USART CR1 */
156 UART1_CR2, /* USART CR2 */
157 UART1_CR3 /* USART CR3 */
158};
159
161
162// Threads RX and TX
163#if USE_UART1_RX
166
167static __attribute__((noreturn)) void thd_uart1_rx(void *arg)
168{
169 (void) arg;
170 chRegSetThreadName("uart1_rx");
171
172 while (TRUE) {
174 }
175}
176
178#endif
179
180#if USE_UART1_TX
183
184static __attribute__((noreturn)) void thd_uart1_tx(void *arg)
185{
186 (void) arg;
187 chRegSetThreadName("uart1_tx");
188
189 while (TRUE) {
191 }
192}
194#endif
195
196void uart1_init(void)
197{
199
200 // Only set pin if enabled and not statically defined in board file
201#if USE_UART1_TX && defined UART1_GPIO_PORT_TX
203#endif
204#if USE_UART1_RX && defined UART1_GPIO_PORT_RX
206#endif
207
209 uart1.reg_addr = &SD1;
210 uart1.baudrate = UART1_BAUD;
211 uart1.init_struct = &uart1_init_struct;
213
214 // Create threads
215#if USE_UART1_RX
220#endif
221#if USE_UART1_TX
226#endif
227}
228
229#endif
230
231
232#if USE_UART2
233
234#ifndef UART2_BAUD
235#define UART2_BAUD SERIAL_DEFAULT_BITRATE
236#endif
237
238/* by default enable UART Tx and Rx */
239#ifndef USE_UART2_TX
240#define USE_UART2_TX TRUE
241#endif
242#ifndef USE_UART2_RX
243#define USE_UART2_RX TRUE
244#endif
245
246
247/* by default disable HW flow control */
248#ifndef UART2_HW_FLOW_CONTROL
249#define UART2_HW_FLOW_CONTROL FALSE
250#endif
251
252#if UART2_HW_FLOW_CONTROL && defined(UART2_CR3)
253#warning "UART2_CR3 reset to your value, HW flow control not enabled! You may want to set USART_CR3_CTSE | USART_CR3_RTSE yourself."
254#endif
255
256#ifndef UART2_CR1
257#define UART2_CR1 0
258#endif
259
260#ifndef UART2_CR2
261#define UART2_CR2 USART_CR2_STOP1_BITS
262#endif
263
264#ifndef UART2_CR3
265#if UART2_HW_FLOW_CONTROL
266#define UART2_CR3 USART_CR3_CTSE | USART_CR3_RTSE
267#else
268#define UART2_CR3 0
269#endif
270#endif
271
273 UART2_BAUD, /* BITRATE */
274 UART2_CR1, /* USART CR1 */
275 UART2_CR2, /* USART CR2 */
276 UART2_CR3 /* USART CR3 */
277};
278
280
281// Threads RX and TX
282#if USE_UART2_RX
285
286static __attribute__((noreturn)) void thd_uart2_rx(void *arg)
287{
288 (void) arg;
289 chRegSetThreadName("uart2_rx");
290
291 while (TRUE) {
293 }
294}
295
297#endif
298
299#if USE_UART2_TX
302
303static __attribute__((noreturn)) void thd_uart2_tx(void *arg)
304{
305 (void) arg;
306 chRegSetThreadName("uart2_tx");
307
308 while (TRUE) {
310 }
311}
313#endif
314
315void uart2_init(void)
316{
318
319 // Only set pin if enabled and not statically defined in board file
320#if USE_UART2_TX && defined UART2_GPIO_PORT_TX
322#endif
323#if USE_UART2_RX && defined UART2_GPIO_PORT_RX
325#endif
326
328 uart2.reg_addr = &SD2;
329 uart2.baudrate = UART2_BAUD;
330 uart2.init_struct = &uart2_init_struct;
332
333 // Create threads
334#if USE_UART2_RX
339#endif
340#if USE_UART2_TX
345#endif
346}
347
348#endif
349
350#if USE_UART3
351
352#ifndef UART3_BAUD
353#define UART3_BAUD SERIAL_DEFAULT_BITRATE
354#endif
355
356/* by default enable UART Tx and Rx */
357#ifndef USE_UART3_TX
358#define USE_UART3_TX TRUE
359#endif
360#ifndef USE_UART3_RX
361#define USE_UART3_RX TRUE
362#endif
363
364#ifndef UART3_CR1
365#define UART3_CR1 0
366#endif
367
368#ifndef UART3_CR2
369#define UART3_CR2 USART_CR2_STOP1_BITS
370#endif
371
372#ifndef UART3_CR3
373#define UART3_CR3 0
374#endif
375
377 UART3_BAUD, /* BITRATE */
378 UART3_CR1, /* USART CR1 */
379 UART3_CR2, /* USART CR2 */
380 UART3_CR3 /* USART CR3 */
381};
382
384
385// Threads RX and TX
386#if USE_UART3_RX
389
390static __attribute__((noreturn)) void thd_uart3_rx(void *arg)
391{
392 (void) arg;
393 chRegSetThreadName("uart3_rx");
394
395 while (TRUE) {
397 }
398}
399
401#endif
402
403#if USE_UART3_TX
406
407static __attribute__((noreturn)) void thd_uart3_tx(void *arg)
408{
409 (void) arg;
410 chRegSetThreadName("uart3_tx");
411
412 while (TRUE) {
414 }
415}
417#endif
418
419void uart3_init(void)
420{
422
423 // Only set pin if enabled and not statically defined in board file
424#if USE_UART3_TX && defined UART3_GPIO_PORT_TX
426#endif
427#if USE_UART3_RX && defined UART3_GPIO_PORT_RX
429#endif
430
432 uart3.reg_addr = &SD3;
433 uart3.baudrate = UART3_BAUD;
434 uart3.init_struct = &uart3_init_struct;
436
437 // Create threads
438#if USE_UART3_RX
443#endif
444#if USE_UART3_TX
449#endif
450}
451
452#endif
453
454#if USE_UART4
455
456#ifndef UART4_BAUD
457#define UART4_BAUD SERIAL_DEFAULT_BITRATE
458#endif
459
460/* by default enable UART Tx and Rx */
461#ifndef USE_UART4_TX
462#define USE_UART4_TX TRUE
463#endif
464#ifndef USE_UART4_RX
465#define USE_UART4_RX TRUE
466#endif
467
468#ifndef UART4_CR1
469#define UART4_CR1 0
470#endif
471
472#ifndef UART4_CR2
473#define UART4_CR2 USART_CR2_STOP1_BITS
474#endif
475
476#ifndef UART4_CR3
477#define UART4_CR3 0
478#endif
479
481 UART4_BAUD, /* BITRATE */
482 UART4_CR1, /* USART CR1 */
483 UART4_CR2, /* USART CR2 */
484 UART4_CR3 /* USART CR3 */
485};
486
488
489// Threads RX and TX
490#if USE_UART4_RX
493
494static __attribute__((noreturn)) void thd_uart4_rx(void *arg)
495{
496 (void) arg;
497 chRegSetThreadName("uart4_rx");
498
499 while (TRUE) {
501 }
502}
503
505#endif
506
507#if USE_UART4_TX
510
511static __attribute__((noreturn)) void thd_uart4_tx(void *arg)
512{
513 (void) arg;
514 chRegSetThreadName("uart4_tx");
515
516 while (TRUE) {
518 }
519}
521#endif
522
523void uart4_init(void)
524{
526
527 // Only set pin if enabled and not statically defined in board file
528#if USE_UART4_TX && defined UART4_GPIO_PORT_TX
530#endif
531#if USE_UART4_RX && defined UART4_GPIO_PORT_RX
533#endif
534
536 uart4.reg_addr = &SD4;
537 uart4.baudrate = UART4_BAUD;
538 uart4.init_struct = &uart4_init_struct;
540
541 // Create threads
542#if USE_UART4_RX
547#endif
548#if USE_UART4_TX
553#endif
554}
555
556#endif
557
558#if USE_UART5
559
560#ifndef UART5_BAUD
561#define UART5_BAUD SERIAL_DEFAULT_BITRATE
562#endif
563
564/* by default enable UART Tx and Rx */
565#ifndef USE_UART5_TX
566#define USE_UART5_TX TRUE
567#endif
568#ifndef USE_UART5_RX
569#define USE_UART5_RX TRUE
570#endif
571
572#ifndef UART5_CR1
573#define UART5_CR1 0
574#endif
575
576#ifndef UART5_CR2
577#define UART5_CR2 USART_CR2_STOP1_BITS
578#endif
579
580#ifndef UART5_CR3
581#define UART5_CR3 0
582#endif
583
585 UART5_BAUD, /* BITRATE */
586 UART5_CR1, /* USART CR1 */
587 UART5_CR2, /* USART CR2 */
588 UART5_CR3 /* USART CR3 */
589};
590
592
593// Threads RX and TX
594#if USE_UART5_RX
597
598static __attribute__((noreturn)) void thd_uart5_rx(void *arg)
599{
600 (void) arg;
601 chRegSetThreadName("uart5_rx");
602
603 while (TRUE) {
605 }
606}
607
609#endif
610
611#if USE_UART5_TX
614
615static __attribute__((noreturn)) void thd_uart5_tx(void *arg)
616{
617 (void) arg;
618 chRegSetThreadName("uart5_tx");
619
620 while (TRUE) {
622 }
623}
625#endif
626
627void uart5_init(void)
628{
630
631 // Only set pin if enabled and not statically defined in board file
632#if USE_UART5_TX && defined UART5_GPIO_PORT_TX
634#endif
635#if USE_UART5_RX && defined UART5_GPIO_PORT_RX
637#endif
638
640 uart5.reg_addr = &SD5;
641 uart5.baudrate = UART5_BAUD;
642 uart5.init_struct = &uart5_init_struct;
644
645 // Create threads
646#if USE_UART5_RX
651#endif
652#if USE_UART5_TX
657#endif
658}
659
660#endif
661
662#if USE_UART6
663
664#ifndef UART6_BAUD
665#define UART6_BAUD SERIAL_DEFAULT_BITRATE
666#endif
667
668/* by default enable UART Tx and Rx */
669#ifndef USE_UART6_TX
670#define USE_UART6_TX TRUE
671#endif
672#ifndef USE_UART6_RX
673#define USE_UART6_RX TRUE
674#endif
675
676#ifndef UART6_CR1
677#define UART6_CR1 0
678#endif
679
680#ifndef UART6_CR2
681#define UART6_CR2 USART_CR2_STOP1_BITS
682#endif
683
684#ifndef UART6_CR3
685#define UART6_CR3 0
686#endif
687
689 UART6_BAUD, /* BITRATE */
690 UART6_CR1, /* USART CR1 */
691 UART6_CR2, /* USART CR2 */
692 UART6_CR3 /* USART CR3 */
693};
694
696
697// Threads RX and TX
698#if USE_UART6_RX
701
702static __attribute__((noreturn)) void thd_uart6_rx(void *arg)
703{
704 (void) arg;
705 chRegSetThreadName("uart6_rx");
706
707 while (TRUE) {
709 }
710}
711
713#endif
714
715#if USE_UART6_TX
718
719static __attribute__((noreturn)) void thd_uart6_tx(void *arg)
720{
721 (void) arg;
722 chRegSetThreadName("uart6_tx");
723
724 while (TRUE) {
726 }
727}
729#endif
730
731void uart6_init(void)
732{
734
735 // Only set pin if enabled and not statically defined in board file
736#if USE_UART6_TX && defined UART6_GPIO_PORT_TX
738#endif
739#if USE_UART6_RX && defined UART6_GPIO_PORT_RX
741#endif
742
744 uart6.reg_addr = &SD6;
745 uart6.baudrate = UART6_BAUD;
746 uart6.init_struct = &uart6_init_struct;
748
749 // Create threads
750#if USE_UART6_RX
755#endif
756#if USE_UART6_TX
761#endif
762
763#if defined UART6_GPIO_CTS && defined UART6_GPIO_PORT_CTS
766#endif
767}
768
769#endif
770
771#if USE_UART7
772
773#ifndef UART7_BAUD
774#define UART7_BAUD SERIAL_DEFAULT_BITRATE
775#endif
776
777/* by default enable UART Tx and Rx */
778#ifndef USE_UART7_TX
779#define USE_UART7_TX TRUE
780#endif
781#ifndef USE_UART7_RX
782#define USE_UART7_RX TRUE
783#endif
784
785#ifndef UART7_CR1
786#define UART7_CR1 0
787#endif
788
789#ifndef UART7_CR2
790#define UART7_CR2 USART_CR2_STOP1_BITS
791#endif
792
793#ifndef UART7_CR3
794#define UART7_CR3 0
795#endif
796
798 UART7_BAUD, /* BITRATE */
799 UART7_CR1, /* USART CR1 */
800 UART7_CR2, /* USART CR2 */
801 UART7_CR3 /* USART CR3 */
802};
803
805
806// Threads RX and TX
807#if USE_UART7_RX
810
811static __attribute__((noreturn)) void thd_uart7_rx(void *arg)
812{
813 (void) arg;
814 chRegSetThreadName("uart7_rx");
815
816 while (TRUE) {
818 }
819}
820
822#endif
823
824#if USE_UART7_TX
827
828static __attribute__((noreturn)) void thd_uart7_tx(void *arg)
829{
830 (void) arg;
831 chRegSetThreadName("uart7_tx");
832
833 while (TRUE) {
835 }
836}
838#endif
839
840void uart7_init(void)
841{
843
844 // Only set pin if enabled and not statically defined in board file
845#if USE_UART7_TX && defined UART7_GPIO_PORT_TX
847#endif
848#if USE_UART7_RX && defined UART7_GPIO_PORT_RX
850#endif
851
853 uart7.reg_addr = &SD7;
854 uart7.baudrate = UART7_BAUD;
855 uart7.init_struct = &uart7_init_struct;
857
858 // Create threads
859#if USE_UART7_RX
864#endif
865#if USE_UART7_TX
870#endif
871}
872
873#endif
874
875#if USE_UART8
876
877#ifndef UART8_BAUD
878#define UART8_BAUD SERIAL_DEFAULT_BITRATE
879#endif
880
881/* by default enable UART Tx and Rx */
882#ifndef USE_UART8_TX
883#define USE_UART8_TX TRUE
884#endif
885#ifndef USE_UART8_RX
886#define USE_UART8_RX TRUE
887#endif
888
889#ifndef UART8_CR1
890#define UART8_CR1 0
891#endif
892
893#ifndef UART8_CR2
894#define UART8_CR2 USART_CR2_STOP1_BITS
895#endif
896
897#ifndef UART8_CR3
898#define UART8_CR3 0
899#endif
900
902 UART8_BAUD, /* BITRATE */
903 UART8_CR1, /* USART CR1 */
904 UART8_CR2, /* USART CR2 */
905 UART8_CR3 /* USART CR3 */
906};
907
909
910// Threads RX and TX
911#if USE_UART8_RX
914
915static __attribute__((noreturn)) void thd_uart8_rx(void *arg)
916{
917 (void) arg;
918 chRegSetThreadName("uart8_rx");
919
920 while (TRUE) {
922 }
923}
924
926#endif
927
928#if USE_UART8_TX
931
932static __attribute__((noreturn)) void thd_uart8_tx(void *arg)
933{
934 (void) arg;
935 chRegSetThreadName("uart8_tx");
936
937 while (TRUE) {
939 }
940}
942#endif
943
944void uart8_init(void)
945{
947
948 // Only set pin if enabled and not statically defined in board file
949#if USE_UART8_TX && defined UART8_GPIO_PORT_TX
951#endif
952#if USE_UART8_RX && defined UART8_GPIO_PORT_RX
954#endif
955
957 uart8.reg_addr = &SD8;
958 uart8.baudrate = UART8_BAUD;
959 uart8.init_struct = &uart8_init_struct;
961
962 // Create threads
963#if USE_UART8_RX
968#endif
969#if USE_UART8_TX
974#endif
975}
976
977#endif
978
979
981{
982 //to keep compatibility with loop oriented paparazzi architecture, read is not blocking
983 //struct SerialInit *init_struct = (struct SerialInit*)(p->init_struct);
984 //chSemWait (init_struct->rx_sem);
985 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
986 chMtxLock(init_struct->rx_mtx);
987 uint8_t ret = p->rx_buf[p->rx_extract_idx];
988 p->rx_extract_idx = (p->rx_extract_idx + 1) % UART_RX_BUFFER_SIZE;
989 chMtxUnlock(init_struct->rx_mtx);
990 return ret;
991}
992
997{
998 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
999 SerialConfig *conf = init_struct->conf;
1000 // set new baudrate
1001 conf->speed = baud;
1002 p->baudrate = baud;
1003 // restart periph
1004 sdStop((SerialDriver *)(p->reg_addr));
1005 sdStart((SerialDriver *)(p->reg_addr), conf);
1006}
1007
1011void uart_periph_set_mode(struct uart_periph *p __attribute__((unused)), bool tx_enabled __attribute__((unused)),
1012 bool rx_enabled __attribute__((unused)), bool hw_flow_control __attribute__((unused))) {}
1013#if defined(STM32H7XX)
1014#define __USART_CR1_M USART_CR1_M0
1015#elif defined(STM32F7XX)
1016#define __USART_CR1_M USART_CR1_M_0
1017#elif defined(STM32F1XX) || defined (STM32F3XX) || defined(STM32F4XX)
1018#define __USART_CR1_M USART_CR1_M
1019#else
1020#error Unsupported board
1021#endif
1022
1028{
1029 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1030 SerialConfig *conf = init_struct->conf;
1031
1032 /* Configure USART parity and data bits */
1033 if (parity == UPARITY_EVEN) {
1034 conf->cr1 |= USART_CR1_PCE; // set parity control bit
1035 conf->cr1 &= ~USART_CR1_PS; // clear parity selection bit
1036 if (bits == UBITS_7) {
1037 conf->cr1 &= ~__USART_CR1_M; // clear word length bit
1038 } else { // 8 data bits by default
1039 conf->cr1 |= __USART_CR1_M; // set word length bit
1040 }
1041 } else if (parity == UPARITY_ODD) {
1042 conf->cr1 |= USART_CR1_PCE; // set parity control bit
1043 conf->cr1 |= USART_CR1_PS; // set parity selection bit
1044 if (bits == UBITS_7) {
1045 conf->cr1 &= ~__USART_CR1_M; // clear word length bit
1046 } else { // 8 data bits by default
1047 conf->cr1 |= __USART_CR1_M; // set word length bit
1048 }
1049 } else { // 8 data bist, NO_PARITY by default
1050 conf->cr1 &= ~USART_CR1_PCE; // clear parity control bit
1051 conf->cr1 &= ~__USART_CR1_M; // clear word length bit
1052 }
1053 /* Configure USART stop bits */
1054 conf->cr2 &= ~USART_CR2_STOP; // clear stop bits
1055 if (stop == USTOP_2) {
1056 conf-> cr2 |= USART_CR2_STOP2_BITS; // set bits for 2 stops
1057 } else { // 1 stop bit by default
1058 conf-> cr2 |= USART_CR2_STOP1_BITS; // set bits for 1 stop
1059 }
1060
1061 sdStop((SerialDriver *)(p->reg_addr));
1062 sdStart((SerialDriver *)(p->reg_addr), conf);
1063}
1064
1065#if defined(STM32F7XX) || defined(STM32H7XX)
1070{
1071 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1072 SerialConfig *conf = init_struct->conf;
1073 if (invert_rx) {
1074 conf->cr2 |= USART_CR2_RXINV; // set rxinv bit
1075 } else {
1076 conf->cr2 &= ~USART_CR2_RXINV; // clear rxinv bit
1077 }
1078 if (invert_tx) {
1079 conf->cr2 |= USART_CR2_TXINV; // set txinv bit
1080 } else {
1081 conf->cr2 &= ~USART_CR2_TXINV; // clear txinv bit
1082 }
1083 sdStop((SerialDriver *)(p->reg_addr));
1084 sdStart((SerialDriver *)(p->reg_addr), conf);
1085}
1086#endif
1087
1088// Check free space and set a positive value for fd if valid
1089// and lock driver with mutex
1090int uart_check_free_space(struct uart_periph *p, long *fd, uint16_t len)
1091{
1092 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1093 int space = p->tx_extract_idx - p->tx_insert_idx - 1;
1094 if (space < 0) {
1096 }
1097 if (space >= len) {
1098 *fd = 1;
1099 chMtxLock(init_struct->tx_mtx);
1100 return space;
1101 }
1102 return 0;
1103}
1104
1108void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
1109{
1110 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1111 if (fd == 0) {
1112 // if fd is zero, assume the driver is not already locked
1113 chMtxLock(init_struct->tx_mtx);
1114 uint16_t temp = (p->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;
1115 if (temp == p->tx_extract_idx) {
1116 chMtxUnlock(init_struct->tx_mtx);
1117 return; // no room
1118 }
1119 p->tx_buf[p->tx_insert_idx] = data;
1120 p->tx_insert_idx = temp;
1121
1122 chMtxUnlock(init_struct->tx_mtx);
1123 // send signal to start transmission
1124 chSemSignal(init_struct->tx_sem);
1125 } else {
1126 // assume driver is locked and available space have been checked
1127 p->tx_buf[p->tx_insert_idx] = data;
1128 p->tx_insert_idx = (p->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;
1129 }
1130}
1131
1135void uart_put_buffer(struct uart_periph *p, long fd, const uint8_t *data, uint16_t len)
1136{
1137 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1138 if (fd == 0) {
1139 // if fd is zero, assume the driver is not already locked
1140 // and available space should be checked
1141 chMtxLock(init_struct->tx_mtx);
1142 int16_t space = p->tx_extract_idx - p->tx_insert_idx;
1143 if (space <= 0) {
1145 }
1146 if ((uint16_t)(space - 1) < len) {
1147 chMtxUnlock(init_struct->tx_mtx);
1148 return; // no room
1149 }
1150 }
1151 // insert data into buffer
1152 int i;
1153 for (i = 0; i < len; i++) {
1154 p->tx_buf[p->tx_insert_idx] = data[i];
1155 p->tx_insert_idx = (p->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;
1156 }
1157 // unlock if needed
1158 if (fd == 0) {
1159 chMtxUnlock(init_struct->tx_mtx);
1160 // send signal to start transmission
1161 chSemSignal(init_struct->tx_sem);
1162 }
1163}
1164
1165void uart_send_message(struct uart_periph *p, long fd)
1166{
1167 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1168 // unlock driver in case it is not done (fd > 0)
1169 if (fd != 0) {
1170 chMtxUnlock(init_struct->tx_mtx);
1171 }
1172 // send signal to start transmission
1173 chSemSignal(init_struct->tx_sem);
1174}
1175
1176#endif // HAL_USE_SERIAL
1177
#define UART6_GPIO_RX
Definition apogee_1.0.h:131
#define UART1_GPIO_TX
Definition apogee_1.0.h:116
#define UART4_GPIO_PORT_RX
Definition apogee_1.0.h:124
#define UART1_GPIO_PORT_TX
Definition apogee_1.0.h:115
#define UART4_GPIO_RX
Definition apogee_1.0.h:125
#define UART6_GPIO_PORT_RX
Definition apogee_1.0.h:130
#define UART2_GPIO_PORT_RX
Definition apogee_1.0.h:119
#define UART6_GPIO_TX
Definition apogee_1.0.h:133
#define UART1_GPIO_PORT_RX
Definition apogee_1.0.h:113
#define UART6_GPIO_PORT_TX
Definition apogee_1.0.h:132
#define UART4_GPIO_TX
Definition apogee_1.0.h:127
#define UART1_GPIO_RX
Definition apogee_1.0.h:114
#define UART2_GPIO_RX
Definition apogee_1.0.h:120
#define UART4_GPIO_PORT_TX
Definition apogee_1.0.h:126
#define UNUSED(x)
#define UART3_GPIO_TX
Definition cc3d.h:52
#define UART3_GPIO_PORT_TX
Definition cc3d.h:51
#define UART3_GPIO_RX
Definition cc3d.h:50
#define UART3_GPIO_PORT_RX
Definition cc3d.h:49
void gpio_setup_pin_af(ioportid_t port, uint16_t pin, uint8_t af, bool is_output)
Setup a gpio for input or output with alternate function.
Definition gpio_arch.c:65
static uint8_t gpio_get(ioportid_t port, uint16_t pin)
Get level of a gpio.
Definition gpio_arch.h:94
static THD_WORKING_AREA(wa_thd_spi1, SPI_THREAD_STACK_SIZE)
static MUTEX_DECL(sys_time_mtx)
#define UART8_GPIO_PORT_RX
Definition chimera.h:391
#define UART7_GPIO_RX
Definition chimera.h:413
#define UART2_GPIO_TX
Definition chimera.h:364
#define UART7_GPIO_PORT_RX
Definition chimera.h:412
#define UART8_GPIO_PORT_TX
Definition chimera.h:389
#define UART8_GPIO_TX
Definition chimera.h:390
#define UART2_GPIO_PORT_TX
UART2 (with optional flow control activated by default)
Definition chimera.h:363
#define UART8_GPIO_RX
Definition chimera.h:392
#define UART3_GPIO_AF
#define UART2_GPIO_AF
#define UART7_GPIO_AF
#define UART6_GPIO_AF
#define UART1_GPIO_AF
UART defines.
#define UART5_GPIO_AF
#define UART8_GPIO_AF
#define UART4_GPIO_AF
#define UART6_GPIO_PORT_CTS
Definition crazyflie.h:184
#define UART6_GPIO_CTS
Definition crazyflie.h:185
#define UART5_GPIO_TX
#define UART5_GPIO_PORT_TX
#define UART5_GPIO_RX
#define UART5_GPIO_PORT_RX
Some architecture independent helper functions for GPIOs.
static float p[2][2]
void uart_put_byte(struct uart_periph *periph, long fd, uint8_t data)
Definition uart_arch.c:306
void uart_periph_set_bits_stop_parity(struct uart_periph *periph, uint8_t bits, uint8_t stop, uint8_t parity)
Definition uart_arch.c:296
uint8_t uart_getch(struct uart_periph *p)
Definition uart_arch.c:348
void uart_periph_set_baudrate(struct uart_periph *periph, uint32_t baud)
Definition uart_arch.c:280
#define UART_TX_BUFFER_SIZE
Definition uart_arch.h:34
#define UART_RX_BUFFER_SIZE
Definition uart_arch.h:31
uint16_t foo
Definition main_demo5.c:58
#define UART7_GPIO_PORT_TX
UART7 (Modem with optional flow control disabled by default)
#define UART7_GPIO_TX
int fd
Definition serial.c:26
#define UBITS_7
Definition serial_port.c:49
#define UPARITY_ODD
Definition serial_port.c:56
#define USTOP_2
Definition serial_port.c:53
#define UPARITY_EVEN
Definition serial_port.c:57
#define TRUE
Definition std.h:4
#define FALSE
Definition std.h:5
void uart_periph_set_mode(struct uart_periph *p, bool tx_enabled, bool rx_enabled, bool hw_flow_control)
Definition uart_arch.c:92
void WEAK uart_send_message(struct uart_periph *p, long fd)
Definition uart.c:170
void uart_periph_init(struct uart_periph *p)
Definition uart.c:126
void WEAK uart_put_buffer(struct uart_periph *p, long fd, const uint8_t *data, uint16_t len)
Definition uart.c:161
void WEAK uart_periph_invert_data_logic(struct uart_periph *p, bool invert_rx, bool invert_tx)
Definition uart.c:194
int WEAK uart_check_free_space(struct uart_periph *p, long *fd, uint16_t len)
Definition uart.c:151
UART peripheral.
Definition uart.h:72
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
static SEMAPHORE_DECL(we_ukf_sem, 0)