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 sdWrite((SerialDriver *)p->reg_addr, &p->tx_buf[0], p->tx_insert_idx);
117 }
118 p->tx_extract_idx = p->tx_insert_idx;
119 chMtxUnlock(init_struct->tx_mtx);
120#endif
121 }
122 p->tx_running = false;
123}
124
125#if USE_UART1
126
127#ifndef UART1_BAUD
128#define UART1_BAUD SERIAL_DEFAULT_BITRATE
129#endif
130
131/* by default enable UART Tx and Rx */
132#ifndef USE_UART1_TX
133#define USE_UART1_TX TRUE
134#endif
135#ifndef USE_UART1_RX
136#define USE_UART1_RX TRUE
137#endif
138
139#ifndef UART1_CR1
140#define UART1_CR1 0
141#endif
142
143#ifndef UART1_CR2
144#define UART1_CR2 USART_CR2_STOP1_BITS
145#endif
146
147#ifndef UART1_CR3
148#define UART1_CR3 0
149#endif
150
152 UART1_BAUD, /* BITRATE */
153 UART1_CR1, /* USART CR1 */
154 UART1_CR2, /* USART CR2 */
155 UART1_CR3 /* USART CR3 */
156};
157
159
160// Threads RX and TX
161#if USE_UART1_RX
164
165static __attribute__((noreturn)) void thd_uart1_rx(void *arg)
166{
167 (void) arg;
168 chRegSetThreadName("uart1_rx");
169
170 while (TRUE) {
172 }
173}
174
176#endif
177
178#if USE_UART1_TX
181
182static __attribute__((noreturn)) void thd_uart1_tx(void *arg)
183{
184 (void) arg;
185 chRegSetThreadName("uart1_tx");
186
187 while (TRUE) {
189 }
190}
192#endif
193
194void uart1_init(void)
195{
197
198 // Only set pin if enabled and not statically defined in board file
199#if USE_UART1_TX && defined UART1_GPIO_PORT_TX
201#endif
202#if USE_UART1_RX && defined UART1_GPIO_PORT_RX
204#endif
205
207 uart1.reg_addr = &SD1;
208 uart1.baudrate = UART1_BAUD;
209 uart1.init_struct = &uart1_init_struct;
211
212 // Create threads
213#if USE_UART1_RX
218#endif
219#if USE_UART1_TX
224#endif
225}
226
227#endif
228
229
230#if USE_UART2
231
232#ifndef UART2_BAUD
233#define UART2_BAUD SERIAL_DEFAULT_BITRATE
234#endif
235
236/* by default enable UART Tx and Rx */
237#ifndef USE_UART2_TX
238#define USE_UART2_TX TRUE
239#endif
240#ifndef USE_UART2_RX
241#define USE_UART2_RX TRUE
242#endif
243
244
245/* by default disable HW flow control */
246#ifndef UART2_HW_FLOW_CONTROL
247#define UART2_HW_FLOW_CONTROL FALSE
248#endif
249
250#if UART2_HW_FLOW_CONTROL && defined(UART2_CR3)
251#warning "UART2_CR3 reset to your value, HW flow control not enabled! You may want to set USART_CR3_CTSE | USART_CR3_RTSE yourself."
252#endif
253
254#ifndef UART2_CR1
255#define UART2_CR1 0
256#endif
257
258#ifndef UART2_CR2
259#define UART2_CR2 USART_CR2_STOP1_BITS
260#endif
261
262#ifndef UART2_CR3
263#if UART2_HW_FLOW_CONTROL
264#define UART2_CR3 USART_CR3_CTSE | USART_CR3_RTSE
265#else
266#define UART2_CR3 0
267#endif
268#endif
269
271 UART2_BAUD, /* BITRATE */
272 UART2_CR1, /* USART CR1 */
273 UART2_CR2, /* USART CR2 */
274 UART2_CR3 /* USART CR3 */
275};
276
278
279// Threads RX and TX
280#if USE_UART2_RX
283
284static __attribute__((noreturn)) void thd_uart2_rx(void *arg)
285{
286 (void) arg;
287 chRegSetThreadName("uart2_rx");
288
289 while (TRUE) {
291 }
292}
293
295#endif
296
297#if USE_UART2_TX
300
301static __attribute__((noreturn)) void thd_uart2_tx(void *arg)
302{
303 (void) arg;
304 chRegSetThreadName("uart2_tx");
305
306 while (TRUE) {
308 }
309}
311#endif
312
313void uart2_init(void)
314{
316
317 // Only set pin if enabled and not statically defined in board file
318#if USE_UART2_TX && defined UART2_GPIO_PORT_TX
320#endif
321#if USE_UART2_RX && defined UART2_GPIO_PORT_RX
323#endif
324
326 uart2.reg_addr = &SD2;
327 uart2.baudrate = UART2_BAUD;
328 uart2.init_struct = &uart2_init_struct;
330
331 // Create threads
332#if USE_UART2_RX
337#endif
338#if USE_UART2_TX
343#endif
344}
345
346#endif
347
348#if USE_UART3
349
350#ifndef UART3_BAUD
351#define UART3_BAUD SERIAL_DEFAULT_BITRATE
352#endif
353
354/* by default enable UART Tx and Rx */
355#ifndef USE_UART3_TX
356#define USE_UART3_TX TRUE
357#endif
358#ifndef USE_UART3_RX
359#define USE_UART3_RX TRUE
360#endif
361
362#ifndef UART3_CR1
363#define UART3_CR1 0
364#endif
365
366#ifndef UART3_CR2
367#define UART3_CR2 USART_CR2_STOP1_BITS
368#endif
369
370#ifndef UART3_CR3
371#define UART3_CR3 0
372#endif
373
375 UART3_BAUD, /* BITRATE */
376 UART3_CR1, /* USART CR1 */
377 UART3_CR2, /* USART CR2 */
378 UART3_CR3 /* USART CR3 */
379};
380
382
383// Threads RX and TX
384#if USE_UART3_RX
387
388static __attribute__((noreturn)) void thd_uart3_rx(void *arg)
389{
390 (void) arg;
391 chRegSetThreadName("uart3_rx");
392
393 while (TRUE) {
395 }
396}
397
399#endif
400
401#if USE_UART3_TX
404
405static __attribute__((noreturn)) void thd_uart3_tx(void *arg)
406{
407 (void) arg;
408 chRegSetThreadName("uart3_tx");
409
410 while (TRUE) {
412 }
413}
415#endif
416
417void uart3_init(void)
418{
420
421 // Only set pin if enabled and not statically defined in board file
422#if USE_UART3_TX && defined UART3_GPIO_PORT_TX
424#endif
425#if USE_UART3_RX && defined UART3_GPIO_PORT_RX
427#endif
428
430 uart3.reg_addr = &SD3;
431 uart3.baudrate = UART3_BAUD;
432 uart3.init_struct = &uart3_init_struct;
434
435 // Create threads
436#if USE_UART3_RX
441#endif
442#if USE_UART3_TX
447#endif
448}
449
450#endif
451
452#if USE_UART4
453
454#ifndef UART4_BAUD
455#define UART4_BAUD SERIAL_DEFAULT_BITRATE
456#endif
457
458/* by default enable UART Tx and Rx */
459#ifndef USE_UART4_TX
460#define USE_UART4_TX TRUE
461#endif
462#ifndef USE_UART4_RX
463#define USE_UART4_RX TRUE
464#endif
465
466#ifndef UART4_CR1
467#define UART4_CR1 0
468#endif
469
470#ifndef UART4_CR2
471#define UART4_CR2 USART_CR2_STOP1_BITS
472#endif
473
474#ifndef UART4_CR3
475#define UART4_CR3 0
476#endif
477
479 UART4_BAUD, /* BITRATE */
480 UART4_CR1, /* USART CR1 */
481 UART4_CR2, /* USART CR2 */
482 UART4_CR3 /* USART CR3 */
483};
484
486
487// Threads RX and TX
488#if USE_UART4_RX
491
492static __attribute__((noreturn)) void thd_uart4_rx(void *arg)
493{
494 (void) arg;
495 chRegSetThreadName("uart4_rx");
496
497 while (TRUE) {
499 }
500}
501
503#endif
504
505#if USE_UART4_TX
508
509static __attribute__((noreturn)) void thd_uart4_tx(void *arg)
510{
511 (void) arg;
512 chRegSetThreadName("uart4_tx");
513
514 while (TRUE) {
516 }
517}
519#endif
520
521void uart4_init(void)
522{
524
525 // Only set pin if enabled and not statically defined in board file
526#if USE_UART4_TX && defined UART4_GPIO_PORT_TX
528#endif
529#if USE_UART4_RX && defined UART4_GPIO_PORT_RX
531#endif
532
534 uart4.reg_addr = &SD4;
535 uart4.baudrate = UART4_BAUD;
536 uart4.init_struct = &uart4_init_struct;
538
539 // Create threads
540#if USE_UART4_RX
545#endif
546#if USE_UART4_TX
551#endif
552}
553
554#endif
555
556#if USE_UART5
557
558#ifndef UART5_BAUD
559#define UART5_BAUD SERIAL_DEFAULT_BITRATE
560#endif
561
562/* by default enable UART Tx and Rx */
563#ifndef USE_UART5_TX
564#define USE_UART5_TX TRUE
565#endif
566#ifndef USE_UART5_RX
567#define USE_UART5_RX TRUE
568#endif
569
570#ifndef UART5_CR1
571#define UART5_CR1 0
572#endif
573
574#ifndef UART5_CR2
575#define UART5_CR2 USART_CR2_STOP1_BITS
576#endif
577
578#ifndef UART5_CR3
579#define UART5_CR3 0
580#endif
581
583 UART5_BAUD, /* BITRATE */
584 UART5_CR1, /* USART CR1 */
585 UART5_CR2, /* USART CR2 */
586 UART5_CR3 /* USART CR3 */
587};
588
590
591// Threads RX and TX
592#if USE_UART5_RX
595
596static __attribute__((noreturn)) void thd_uart5_rx(void *arg)
597{
598 (void) arg;
599 chRegSetThreadName("uart5_rx");
600
601 while (TRUE) {
603 }
604}
605
607#endif
608
609#if USE_UART5_TX
612
613static __attribute__((noreturn)) void thd_uart5_tx(void *arg)
614{
615 (void) arg;
616 chRegSetThreadName("uart5_tx");
617
618 while (TRUE) {
620 }
621}
623#endif
624
625void uart5_init(void)
626{
628
629 // Only set pin if enabled and not statically defined in board file
630#if USE_UART5_TX && defined UART5_GPIO_PORT_TX
632#endif
633#if USE_UART5_RX && defined UART5_GPIO_PORT_RX
635#endif
636
638 uart5.reg_addr = &SD5;
639 uart5.baudrate = UART5_BAUD;
640 uart5.init_struct = &uart5_init_struct;
642
643 // Create threads
644#if USE_UART5_RX
649#endif
650#if USE_UART5_TX
655#endif
656}
657
658#endif
659
660#if USE_UART6
661
662#ifndef UART6_BAUD
663#define UART6_BAUD SERIAL_DEFAULT_BITRATE
664#endif
665
666/* by default enable UART Tx and Rx */
667#ifndef USE_UART6_TX
668#define USE_UART6_TX TRUE
669#endif
670#ifndef USE_UART6_RX
671#define USE_UART6_RX TRUE
672#endif
673
674#ifndef UART6_CR1
675#define UART6_CR1 0
676#endif
677
678#ifndef UART6_CR2
679#define UART6_CR2 USART_CR2_STOP1_BITS
680#endif
681
682#ifndef UART6_CR3
683#define UART6_CR3 0
684#endif
685
687 UART6_BAUD, /* BITRATE */
688 UART6_CR1, /* USART CR1 */
689 UART6_CR2, /* USART CR2 */
690 UART6_CR3 /* USART CR3 */
691};
692
694
695// Threads RX and TX
696#if USE_UART6_RX
699
700static __attribute__((noreturn)) void thd_uart6_rx(void *arg)
701{
702 (void) arg;
703 chRegSetThreadName("uart6_rx");
704
705 while (TRUE) {
707 }
708}
709
711#endif
712
713#if USE_UART6_TX
716
717static __attribute__((noreturn)) void thd_uart6_tx(void *arg)
718{
719 (void) arg;
720 chRegSetThreadName("uart6_tx");
721
722 while (TRUE) {
724 }
725}
727#endif
728
729void uart6_init(void)
730{
732
733 // Only set pin if enabled and not statically defined in board file
734#if USE_UART6_TX && defined UART6_GPIO_PORT_TX
736#endif
737#if USE_UART6_RX && defined UART6_GPIO_PORT_RX
739#endif
740
742 uart6.reg_addr = &SD6;
743 uart6.baudrate = UART6_BAUD;
744 uart6.init_struct = &uart6_init_struct;
746
747 // Create threads
748#if USE_UART6_RX
753#endif
754#if USE_UART6_TX
759#endif
760
761#if defined UART6_GPIO_CTS && defined UART6_GPIO_PORT_CTS
764#endif
765}
766
767#endif
768
769#if USE_UART7
770
771#ifndef UART7_BAUD
772#define UART7_BAUD SERIAL_DEFAULT_BITRATE
773#endif
774
775/* by default enable UART Tx and Rx */
776#ifndef USE_UART7_TX
777#define USE_UART7_TX TRUE
778#endif
779#ifndef USE_UART7_RX
780#define USE_UART7_RX TRUE
781#endif
782
783#ifndef UART7_CR1
784#define UART7_CR1 0
785#endif
786
787#ifndef UART7_CR2
788#define UART7_CR2 USART_CR2_STOP1_BITS
789#endif
790
791#ifndef UART7_CR3
792#define UART7_CR3 0
793#endif
794
796 UART7_BAUD, /* BITRATE */
797 UART7_CR1, /* USART CR1 */
798 UART7_CR2, /* USART CR2 */
799 UART7_CR3 /* USART CR3 */
800};
801
803
804// Threads RX and TX
805#if USE_UART7_RX
808
809static __attribute__((noreturn)) void thd_uart7_rx(void *arg)
810{
811 (void) arg;
812 chRegSetThreadName("uart7_rx");
813
814 while (TRUE) {
816 }
817}
818
820#endif
821
822#if USE_UART7_TX
825
826static __attribute__((noreturn)) void thd_uart7_tx(void *arg)
827{
828 (void) arg;
829 chRegSetThreadName("uart7_tx");
830
831 while (TRUE) {
833 }
834}
836#endif
837
838void uart7_init(void)
839{
841
842 // Only set pin if enabled and not statically defined in board file
843#if USE_UART7_TX && defined UART7_GPIO_PORT_TX
845#endif
846#if USE_UART7_RX && defined UART7_GPIO_PORT_RX
848#endif
849
851 uart7.reg_addr = &SD7;
852 uart7.baudrate = UART7_BAUD;
853 uart7.init_struct = &uart7_init_struct;
855
856 // Create threads
857#if USE_UART7_RX
862#endif
863#if USE_UART7_TX
868#endif
869}
870
871#endif
872
873#if USE_UART8
874
875#ifndef UART8_BAUD
876#define UART8_BAUD SERIAL_DEFAULT_BITRATE
877#endif
878
879/* by default enable UART Tx and Rx */
880#ifndef USE_UART8_TX
881#define USE_UART8_TX TRUE
882#endif
883#ifndef USE_UART8_RX
884#define USE_UART8_RX TRUE
885#endif
886
887#ifndef UART8_CR1
888#define UART8_CR1 0
889#endif
890
891#ifndef UART8_CR2
892#define UART8_CR2 USART_CR2_STOP1_BITS
893#endif
894
895#ifndef UART8_CR3
896#define UART8_CR3 0
897#endif
898
900 UART8_BAUD, /* BITRATE */
901 UART8_CR1, /* USART CR1 */
902 UART8_CR2, /* USART CR2 */
903 UART8_CR3 /* USART CR3 */
904};
905
907
908// Threads RX and TX
909#if USE_UART8_RX
912
913static __attribute__((noreturn)) void thd_uart8_rx(void *arg)
914{
915 (void) arg;
916 chRegSetThreadName("uart8_rx");
917
918 while (TRUE) {
920 }
921}
922
924#endif
925
926#if USE_UART8_TX
929
930static __attribute__((noreturn)) void thd_uart8_tx(void *arg)
931{
932 (void) arg;
933 chRegSetThreadName("uart8_tx");
934
935 while (TRUE) {
937 }
938}
940#endif
941
942void uart8_init(void)
943{
945
946 // Only set pin if enabled and not statically defined in board file
947#if USE_UART8_TX && defined UART8_GPIO_PORT_TX
949#endif
950#if USE_UART8_RX && defined UART8_GPIO_PORT_RX
952#endif
953
955 uart8.reg_addr = &SD8;
956 uart8.baudrate = UART8_BAUD;
957 uart8.init_struct = &uart8_init_struct;
959
960 // Create threads
961#if USE_UART8_RX
966#endif
967#if USE_UART8_TX
972#endif
973}
974
975#endif
976
977
979{
980 //to keep compatibility with loop oriented paparazzi architecture, read is not blocking
981 //struct SerialInit *init_struct = (struct SerialInit*)(p->init_struct);
982 //chSemWait (init_struct->rx_sem);
983 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
984 chMtxLock(init_struct->rx_mtx);
985 uint8_t ret = p->rx_buf[p->rx_extract_idx];
986 p->rx_extract_idx = (p->rx_extract_idx + 1) % UART_RX_BUFFER_SIZE;
987 chMtxUnlock(init_struct->rx_mtx);
988 return ret;
989}
990
995{
996 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
997 SerialConfig *conf = init_struct->conf;
998 // set new baudrate
999 conf->speed = baud;
1000 p->baudrate = baud;
1001 // restart periph
1002 sdStop((SerialDriver *)(p->reg_addr));
1003 sdStart((SerialDriver *)(p->reg_addr), conf);
1004}
1005
1009void uart_periph_set_mode(struct uart_periph *p __attribute__((unused)), bool tx_enabled __attribute__((unused)),
1010 bool rx_enabled __attribute__((unused)), bool hw_flow_control __attribute__((unused))) {}
1011#if defined(STM32H7XX)
1012#define __USART_CR1_M USART_CR1_M0
1013#elif defined(STM32F7XX)
1014#define __USART_CR1_M USART_CR1_M_0
1015#elif defined(STM32F1XX) || defined (STM32F3XX) || defined(STM32F4XX)
1016#define __USART_CR1_M USART_CR1_M
1017#else
1018#error Unsupported board
1019#endif
1020
1026{
1027 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1028 SerialConfig *conf = init_struct->conf;
1029
1030 /* Configure USART parity and data bits */
1031 if (parity == UPARITY_EVEN) {
1032 conf->cr1 |= USART_CR1_PCE; // set parity control bit
1033 conf->cr1 &= ~USART_CR1_PS; // clear parity selection bit
1034 if (bits == UBITS_7) {
1035 conf->cr1 &= ~__USART_CR1_M; // clear word length bit
1036 } else { // 8 data bits by default
1037 conf->cr1 |= __USART_CR1_M; // set word length bit
1038 }
1039 } else if (parity == UPARITY_ODD) {
1040 conf->cr1 |= USART_CR1_PCE; // set parity control bit
1041 conf->cr1 |= USART_CR1_PS; // set parity selection bit
1042 if (bits == UBITS_7) {
1043 conf->cr1 &= ~__USART_CR1_M; // clear word length bit
1044 } else { // 8 data bits by default
1045 conf->cr1 |= __USART_CR1_M; // set word length bit
1046 }
1047 } else { // 8 data bist, NO_PARITY by default
1048 conf->cr1 &= ~USART_CR1_PCE; // clear parity control bit
1049 conf->cr1 &= ~__USART_CR1_M; // clear word length bit
1050 }
1051 /* Configure USART stop bits */
1052 conf->cr2 &= ~USART_CR2_STOP; // clear stop bits
1053 if (stop == USTOP_2) {
1054 conf-> cr2 |= USART_CR2_STOP2_BITS; // set bits for 2 stops
1055 } else { // 1 stop bit by default
1056 conf-> cr2 |= USART_CR2_STOP1_BITS; // set bits for 1 stop
1057 }
1058
1059 sdStop((SerialDriver *)(p->reg_addr));
1060 sdStart((SerialDriver *)(p->reg_addr), conf);
1061}
1062
1063#if defined(STM32F7XX) || defined(STM32H7XX)
1068{
1069 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1070 SerialConfig *conf = init_struct->conf;
1071 if (invert_rx) {
1072 conf->cr2 |= USART_CR2_RXINV; // set rxinv bit
1073 } else {
1074 conf->cr2 &= ~USART_CR2_RXINV; // clear rxinv bit
1075 }
1076 if (invert_tx) {
1077 conf->cr2 |= USART_CR2_TXINV; // set txinv bit
1078 } else {
1079 conf->cr2 &= ~USART_CR2_TXINV; // clear txinv bit
1080 }
1081 sdStop((SerialDriver *)(p->reg_addr));
1082 sdStart((SerialDriver *)(p->reg_addr), conf);
1083}
1084#endif
1085
1086// Check free space and set a positive value for fd if valid
1087// and lock driver with mutex
1088int uart_check_free_space(struct uart_periph *p, long *fd, uint16_t len)
1089{
1090 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1091 int space = p->tx_extract_idx - p->tx_insert_idx - 1;
1092 if (space < 0) {
1094 }
1095 if (space >= len) {
1096 *fd = 1;
1097 chMtxLock(init_struct->tx_mtx);
1098 return space;
1099 }
1100 return 0;
1101}
1102
1106void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
1107{
1108 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1109 if (fd == 0) {
1110 // if fd is zero, assume the driver is not already locked
1111 chMtxLock(init_struct->tx_mtx);
1112 uint16_t temp = (p->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;
1113 if (temp == p->tx_extract_idx) {
1114 chMtxUnlock(init_struct->tx_mtx);
1115 return; // no room
1116 }
1117 p->tx_buf[p->tx_insert_idx] = data;
1118 p->tx_insert_idx = temp;
1119
1120 chMtxUnlock(init_struct->tx_mtx);
1121 // send signal to start transmission
1122 chSemSignal(init_struct->tx_sem);
1123 } else {
1124 // assume driver is locked and available space have been checked
1125 p->tx_buf[p->tx_insert_idx] = data;
1126 p->tx_insert_idx = (p->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;
1127 }
1128}
1129
1133void uart_put_buffer(struct uart_periph *p, long fd, const uint8_t *data, uint16_t len)
1134{
1135 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1136 if (fd == 0) {
1137 // if fd is zero, assume the driver is not already locked
1138 // and available space should be checked
1139 chMtxLock(init_struct->tx_mtx);
1140 int16_t space = p->tx_extract_idx - p->tx_insert_idx;
1141 if (space <= 0) {
1143 }
1144 if ((uint16_t)(space - 1) < len) {
1145 chMtxUnlock(init_struct->tx_mtx);
1146 return; // no room
1147 }
1148 }
1149 // insert data into buffer
1150 int i;
1151 for (i = 0; i < len; i++) {
1152 p->tx_buf[p->tx_insert_idx] = data[i];
1153 p->tx_insert_idx = (p->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;
1154 }
1155 // unlock if needed
1156 if (fd == 0) {
1157 chMtxUnlock(init_struct->tx_mtx);
1158 // send signal to start transmission
1159 chSemSignal(init_struct->tx_sem);
1160 }
1161}
1162
1163void uart_send_message(struct uart_periph *p, long fd)
1164{
1165 struct SerialInit *init_struct = (struct SerialInit *)(p->init_struct);
1166 // unlock driver in case it is not done (fd > 0)
1167 if (fd != 0) {
1168 chMtxUnlock(init_struct->tx_mtx);
1169 }
1170 // send signal to start transmission
1171 chSemSignal(init_struct->tx_sem);
1172}
1173
1174#endif // HAL_USE_SERIAL
1175
#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)