37 #include "mcu_periph/uart_arch.h"
44 #ifndef UART_THREAD_STACK_SIZE
45 #define UART_THREAD_STACK_SIZE 512
58 #define SERIAL_INIT_NULL { NULL, NULL, NULL, NULL, NULL, 0, 0 }
66 uint8_t c = sdGet((SerialDriver *)(
p->reg_addr));
69 chMtxLock(init_struct->
rx_mtx);
72 p->rx_buf[
p->rx_insert_idx] =
c;
74 if (temp !=
p->rx_extract_idx) {
75 p->rx_insert_idx = temp;
77 chMtxUnlock(init_struct->
rx_mtx);
78 chSemSignal(init_struct->
rx_sem);
90 chSemWait(init_struct->
tx_sem);
92 while (
p->tx_insert_idx !=
p->tx_extract_idx) {
93 #if USE_UART_SOFT_FLOW_CONTROL
99 uint8_t data =
p->tx_buf[
p->tx_extract_idx];
100 sdPut((SerialDriver *)
p->reg_addr, data);
101 chMtxLock(init_struct->
tx_mtx);
104 chMtxUnlock(init_struct->
tx_mtx);
105 #if USE_UART_SOFT_FLOW_CONTROL
108 while ((((SerialDriver *)
p->reg_addr)->usart->SR & USART_SR_TC) == 0) ;
112 p->tx_running =
false;
118 #define UART1_BAUD SERIAL_DEFAULT_BITRATE
123 #define USE_UART1_TX TRUE
126 #define USE_UART1_RX TRUE
134 #define UART1_CR2 USART_CR2_STOP1_BITS
141 static SerialConfig usart1_config = {
155 static __attribute__((noreturn))
void thd_uart1_rx(
void *arg)
158 chRegSetThreadName(
"uart1_rx");
172 static __attribute__((noreturn))
void thd_uart1_tx(
void *arg)
175 chRegSetThreadName(
"uart1_tx");
184 void uart1_init(
void)
189 #if USE_UART1_TX && defined UART1_GPIO_PORT_TX
192 #if USE_UART1_RX && defined UART1_GPIO_PORT_RX
196 sdStart(&SD1, &usart1_config);
197 uart1.reg_addr = &SD1;
198 uart1.baudrate = UART1_BAUD;
199 uart1.init_struct = &uart1_init_struct;
200 uart1_init_struct.
conf = &usart1_config;
204 uart1_init_struct.
rx_mtx = &uart1_rx_mtx;
205 uart1_init_struct.
rx_sem = &uart1_rx_sem;
206 chThdCreateStatic(wa_thd_uart1_rx,
sizeof(wa_thd_uart1_rx),
207 NORMALPRIO + 1, thd_uart1_rx, NULL);
210 uart1_init_struct.
tx_mtx = &uart1_tx_mtx;
211 uart1_init_struct.
tx_sem = &uart1_tx_sem;
212 chThdCreateStatic(wa_thd_uart1_tx,
sizeof(wa_thd_uart1_tx),
213 NORMALPRIO + 1, thd_uart1_tx, NULL);
223 #define UART2_BAUD SERIAL_DEFAULT_BITRATE
228 #define USE_UART2_TX TRUE
231 #define USE_UART2_RX TRUE
236 #ifndef UART2_HW_FLOW_CONTROL
237 #define UART2_HW_FLOW_CONTROL FALSE
240 #if UART2_HW_FLOW_CONTROL && defined(UART2_CR3)
241 #warning "UART2_CR3 reset to your value, HW flow control not enabled! You may want to set USART_CR3_CTSE | USART_CR3_RTSE yourself."
249 #define UART2_CR2 USART_CR2_STOP1_BITS
253 #if UART2_HW_FLOW_CONTROL
254 #define UART2_CR3 USART_CR3_CTSE | USART_CR3_RTSE
260 static SerialConfig usart2_config = {
274 static __attribute__((noreturn))
void thd_uart2_rx(
void *arg)
277 chRegSetThreadName(
"uart2_rx");
291 static __attribute__((noreturn))
void thd_uart2_tx(
void *arg)
294 chRegSetThreadName(
"uart2_tx");
303 void uart2_init(
void)
308 #if USE_UART2_TX && defined UART2_GPIO_PORT_TX
311 #if USE_UART2_RX && defined UART2_GPIO_PORT_RX
315 sdStart(&SD2, &usart2_config);
316 uart2.reg_addr = &SD2;
317 uart2.baudrate = UART2_BAUD;
318 uart2.init_struct = &uart2_init_struct;
319 uart2_init_struct.
conf = &usart2_config;
323 uart2_init_struct.
rx_mtx = &uart2_rx_mtx;
324 uart2_init_struct.
rx_sem = &uart2_rx_sem;
325 chThdCreateStatic(wa_thd_uart2_rx,
sizeof(wa_thd_uart2_rx),
326 NORMALPRIO + 1, thd_uart2_rx, NULL);
329 uart2_init_struct.
tx_mtx = &uart2_tx_mtx;
330 uart2_init_struct.
tx_sem = &uart2_tx_sem;
331 chThdCreateStatic(wa_thd_uart2_tx,
sizeof(wa_thd_uart2_tx),
332 NORMALPRIO + 1, thd_uart2_tx, NULL);
341 #define UART3_BAUD SERIAL_DEFAULT_BITRATE
346 #define USE_UART3_TX TRUE
349 #define USE_UART3_RX TRUE
357 #define UART3_CR2 USART_CR2_STOP1_BITS
364 static SerialConfig usart3_config = {
378 static __attribute__((noreturn))
void thd_uart3_rx(
void *arg)
381 chRegSetThreadName(
"uart3_rx");
395 static __attribute__((noreturn))
void thd_uart3_tx(
void *arg)
398 chRegSetThreadName(
"uart3_tx");
407 void uart3_init(
void)
412 #if USE_UART3_TX && defined UART3_GPIO_PORT_TX
415 #if USE_UART3_RX && defined UART3_GPIO_PORT_RX
419 sdStart(&SD3, &usart3_config);
420 uart3.reg_addr = &SD3;
421 uart3.baudrate = UART3_BAUD;
422 uart3.init_struct = &uart3_init_struct;
423 uart3_init_struct.
conf = &usart3_config;
427 uart3_init_struct.
rx_mtx = &uart3_rx_mtx;
428 uart3_init_struct.
rx_sem = &uart3_rx_sem;
429 chThdCreateStatic(wa_thd_uart3_rx,
sizeof(wa_thd_uart3_rx),
430 NORMALPRIO + 1, thd_uart3_rx, NULL);
433 uart3_init_struct.
tx_mtx = &uart3_tx_mtx;
434 uart3_init_struct.
tx_sem = &uart3_tx_sem;
435 chThdCreateStatic(wa_thd_uart3_tx,
sizeof(wa_thd_uart3_tx),
436 NORMALPRIO + 1, thd_uart3_tx, NULL);
445 #define UART4_BAUD SERIAL_DEFAULT_BITRATE
450 #define USE_UART4_TX TRUE
453 #define USE_UART4_RX TRUE
461 #define UART4_CR2 USART_CR2_STOP1_BITS
468 static SerialConfig usart4_config = {
482 static __attribute__((noreturn))
void thd_uart4_rx(
void *arg)
485 chRegSetThreadName(
"uart4_rx");
499 static __attribute__((noreturn))
void thd_uart4_tx(
void *arg)
502 chRegSetThreadName(
"uart4_tx");
511 void uart4_init(
void)
516 #if USE_UART4_TX && defined UART4_GPIO_PORT_TX
519 #if USE_UART4_RX && defined UART4_GPIO_PORT_RX
523 sdStart(&SD4, &usart4_config);
524 uart4.reg_addr = &SD4;
525 uart4.baudrate = UART4_BAUD;
526 uart4.init_struct = &uart4_init_struct;
527 uart4_init_struct.
conf = &usart4_config;
531 uart4_init_struct.
rx_mtx = &uart4_rx_mtx;
532 uart4_init_struct.
rx_sem = &uart4_rx_sem;
533 chThdCreateStatic(wa_thd_uart4_rx,
sizeof(wa_thd_uart4_rx),
534 NORMALPRIO + 1, thd_uart4_rx, NULL);
537 uart4_init_struct.
tx_mtx = &uart4_tx_mtx;
538 uart4_init_struct.
tx_sem = &uart4_tx_sem;
539 chThdCreateStatic(wa_thd_uart4_tx,
sizeof(wa_thd_uart4_tx),
540 NORMALPRIO + 1, thd_uart4_tx, NULL);
549 #define UART5_BAUD SERIAL_DEFAULT_BITRATE
554 #define USE_UART5_TX TRUE
557 #define USE_UART5_RX TRUE
565 #define UART5_CR2 USART_CR2_STOP1_BITS
572 static SerialConfig usart5_config = {
586 static __attribute__((noreturn))
void thd_uart5_rx(
void *arg)
589 chRegSetThreadName(
"uart5_rx");
603 static __attribute__((noreturn))
void thd_uart5_tx(
void *arg)
606 chRegSetThreadName(
"uart5_tx");
615 void uart5_init(
void)
620 #if USE_UART5_TX && defined UART5_GPIO_PORT_TX
623 #if USE_UART5_RX && defined UART5_GPIO_PORT_RX
627 sdStart(&SD5, &usart5_config);
628 uart5.reg_addr = &SD5;
629 uart5.baudrate = UART5_BAUD;
630 uart5.init_struct = &uart5_init_struct;
631 uart5_init_struct.
conf = &usart5_config;
635 uart5_init_struct.
rx_mtx = &uart5_rx_mtx;
636 uart5_init_struct.
rx_sem = &uart5_rx_sem;
637 chThdCreateStatic(wa_thd_uart5_rx,
sizeof(wa_thd_uart5_rx),
638 NORMALPRIO + 1, thd_uart5_rx, NULL);
641 uart5_init_struct.
tx_mtx = &uart5_tx_mtx;
642 uart5_init_struct.
tx_sem = &uart5_tx_sem;
643 chThdCreateStatic(wa_thd_uart5_tx,
sizeof(wa_thd_uart5_tx),
644 NORMALPRIO + 1, thd_uart5_tx, NULL);
653 #define UART6_BAUD SERIAL_DEFAULT_BITRATE
658 #define USE_UART6_TX TRUE
661 #define USE_UART6_RX TRUE
669 #define UART6_CR2 USART_CR2_STOP1_BITS
676 static SerialConfig usart6_config = {
690 static __attribute__((noreturn))
void thd_uart6_rx(
void *arg)
693 chRegSetThreadName(
"uart6_rx");
707 static __attribute__((noreturn))
void thd_uart6_tx(
void *arg)
710 chRegSetThreadName(
"uart6_tx");
719 void uart6_init(
void)
724 #if USE_UART6_TX && defined UART6_GPIO_PORT_TX
727 #if USE_UART6_RX && defined UART6_GPIO_PORT_RX
731 sdStart(&SD6, &usart6_config);
732 uart6.reg_addr = &SD6;
733 uart6.baudrate = UART6_BAUD;
734 uart6.init_struct = &uart6_init_struct;
735 uart6_init_struct.
conf = &usart6_config;
739 uart6_init_struct.
rx_mtx = &uart6_rx_mtx;
740 uart6_init_struct.
rx_sem = &uart6_rx_sem;
741 chThdCreateStatic(wa_thd_uart6_rx,
sizeof(wa_thd_uart6_rx),
742 NORMALPRIO + 1, thd_uart6_rx, NULL);
745 uart6_init_struct.
tx_mtx = &uart6_tx_mtx;
746 uart6_init_struct.
tx_sem = &uart6_tx_sem;
747 chThdCreateStatic(wa_thd_uart6_tx,
sizeof(wa_thd_uart6_tx),
748 NORMALPRIO + 1, thd_uart6_tx, NULL);
751 #if defined UART6_GPIO_CTS && defined UART6_GPIO_PORT_CTS
762 #define UART7_BAUD SERIAL_DEFAULT_BITRATE
767 #define USE_UART7_TX TRUE
770 #define USE_UART7_RX TRUE
778 #define UART7_CR2 USART_CR2_STOP1_BITS
785 static SerialConfig usart7_config = {
799 static __attribute__((noreturn))
void thd_uart7_rx(
void *arg)
802 chRegSetThreadName(
"uart7_rx");
816 static __attribute__((noreturn))
void thd_uart7_tx(
void *arg)
819 chRegSetThreadName(
"uart7_tx");
828 void uart7_init(
void)
833 #if USE_UART7_TX && defined UART7_GPIO_PORT_TX
836 #if USE_UART7_RX && defined UART7_GPIO_PORT_RX
840 sdStart(&SD7, &usart7_config);
841 uart7.reg_addr = &SD7;
842 uart7.baudrate = UART7_BAUD;
843 uart7.init_struct = &uart7_init_struct;
844 uart7_init_struct.
conf = &usart7_config;
848 uart7_init_struct.
rx_mtx = &uart7_rx_mtx;
849 uart7_init_struct.
rx_sem = &uart7_rx_sem;
850 chThdCreateStatic(wa_thd_uart7_rx,
sizeof(wa_thd_uart7_rx),
851 NORMALPRIO + 1, thd_uart7_rx, NULL);
854 uart7_init_struct.
tx_mtx = &uart7_tx_mtx;
855 uart7_init_struct.
tx_sem = &uart7_tx_sem;
856 chThdCreateStatic(wa_thd_uart7_tx,
sizeof(wa_thd_uart7_tx),
857 NORMALPRIO + 1, thd_uart7_tx, NULL);
866 #define UART8_BAUD SERIAL_DEFAULT_BITRATE
871 #define USE_UART8_TX TRUE
874 #define USE_UART8_RX TRUE
882 #define UART8_CR2 USART_CR2_STOP1_BITS
889 static SerialConfig usart8_config = {
903 static __attribute__((noreturn))
void thd_uart8_rx(
void *arg)
906 chRegSetThreadName(
"uart8_rx");
920 static __attribute__((noreturn))
void thd_uart8_tx(
void *arg)
923 chRegSetThreadName(
"uart8_tx");
932 void uart8_init(
void)
937 #if USE_UART8_TX && defined UART8_GPIO_PORT_TX
940 #if USE_UART8_RX && defined UART8_GPIO_PORT_RX
944 sdStart(&SD8, &usart8_config);
945 uart8.reg_addr = &SD8;
946 uart8.baudrate = UART8_BAUD;
947 uart8.init_struct = &uart8_init_struct;
948 uart8_init_struct.
conf = &usart8_config;
952 uart8_init_struct.
rx_mtx = &uart8_rx_mtx;
953 uart8_init_struct.
rx_sem = &uart8_rx_sem;
954 chThdCreateStatic(wa_thd_uart8_rx,
sizeof(wa_thd_uart8_rx),
955 NORMALPRIO + 1, thd_uart8_rx, NULL);
958 uart8_init_struct.
tx_mtx = &uart8_tx_mtx;
959 uart8_init_struct.
tx_sem = &uart8_tx_sem;
960 chThdCreateStatic(wa_thd_uart8_tx,
sizeof(wa_thd_uart8_tx),
961 NORMALPRIO + 1, thd_uart8_tx, NULL);
974 chMtxLock(init_struct->
rx_mtx);
975 uint8_t ret =
p->rx_buf[
p->rx_extract_idx];
977 chMtxUnlock(init_struct->
rx_mtx);
987 SerialConfig *
conf = init_struct->
conf;
992 sdStop((SerialDriver *)(
p->reg_addr));
993 sdStart((SerialDriver *)(
p->reg_addr),
conf);
1000 bool rx_enabled __attribute__((unused)),
bool hw_flow_control __attribute__((unused))) {}
1003 #define __USART_CR1_M USART_CR1_M_0
1004 #elif defined STM32F1 || defined STM32F4 || defined STM32F3
1005 #define __USART_CR1_M USART_CR1_M
1007 #error unsupported board
1017 SerialConfig *
conf = init_struct->
conf;
1021 conf->cr1 |= USART_CR1_PCE;
1022 conf->cr1 &= ~USART_CR1_PS;
1024 conf->cr1 &= ~__USART_CR1_M;
1026 conf->cr1 |= __USART_CR1_M;
1029 conf->cr1 |= USART_CR1_PCE;
1030 conf->cr1 |= USART_CR1_PS;
1032 conf->cr1 &= ~__USART_CR1_M;
1034 conf->cr1 |= __USART_CR1_M;
1037 conf->cr1 &= ~USART_CR1_PCE;
1038 conf->cr1 &= ~__USART_CR1_M;
1041 conf->cr2 &= ~USART_CR2_STOP;
1043 conf-> cr2 |= USART_CR2_STOP2_BITS;
1045 conf-> cr2 |= USART_CR2_STOP1_BITS;
1048 sdStop((SerialDriver *)(
p->reg_addr));
1049 sdStart((SerialDriver *)(
p->reg_addr),
conf);
1059 SerialConfig *
conf = init_struct->
conf;
1061 conf->cr2 |= USART_CR2_RXINV;
1063 conf->cr2 &= ~USART_CR2_RXINV;
1066 conf->cr2 |= USART_CR2_TXINV;
1068 conf->cr2 &= ~USART_CR2_TXINV;
1070 sdStop((SerialDriver *)(
p->reg_addr));
1071 sdStart((SerialDriver *)(
p->reg_addr),
conf);
1080 int space =
p->tx_extract_idx -
p->tx_insert_idx - 1;
1086 chMtxLock(init_struct->
tx_mtx);
1100 chMtxLock(init_struct->
tx_mtx);
1102 if (temp ==
p->tx_extract_idx) {
1103 chMtxUnlock(init_struct->
tx_mtx);
1106 p->tx_buf[
p->tx_insert_idx] = data;
1107 p->tx_insert_idx = temp;
1109 chMtxUnlock(init_struct->
tx_mtx);
1111 chSemSignal(init_struct->
tx_sem);
1114 p->tx_buf[
p->tx_insert_idx] = data;
1128 chMtxLock(init_struct->
tx_mtx);
1129 int16_t space =
p->tx_extract_idx -
p->tx_insert_idx;
1134 chMtxUnlock(init_struct->
tx_mtx);
1140 for (i = 0; i < len; i++) {
1141 p->tx_buf[
p->tx_insert_idx] = data[i];
1146 chMtxUnlock(init_struct->
tx_mtx);
1148 chSemSignal(init_struct->
tx_sem);
1157 chMtxUnlock(init_struct->
tx_mtx);
1160 chSemSignal(init_struct->
tx_sem);