37 #include "mcu_periph/uart_arch.h"
60 chMtxLock(init_struct->
rx_mtx);
68 chMtxUnlock(init_struct->
rx_mtx);
69 chSemSignal(init_struct->
rx_sem);
79 chSemWait (init_struct->
tx_sem);
83 sdWrite((SerialDriver *)p->
reg_addr, &data,
sizeof(data));
86 chMtxLock(init_struct->
tx_mtx);
89 chMtxUnlock(init_struct->
tx_mtx);
96 #define UART1_BAUD SERIAL_DEFAULT_BITRATE
101 #define USE_UART1_TX TRUE
104 #define USE_UART1_RX TRUE
107 static SerialConfig usart1_config = {
110 USART_CR2_STOP1_BITS,
114 static struct SerialInit uart1_init_struct = { NULL, NULL, NULL, NULL, NULL };
121 static __attribute__((noreturn)) void thd_uart1_rx(
void *arg)
124 chRegSetThreadName(
"uart1_rx");
138 static __attribute__((noreturn)) void thd_uart1_tx(
void *arg)
141 chRegSetThreadName(
"uart1_tx");
150 void uart1_init(
void)
155 #if USE_UART1_TX && defined UART1_GPIO_PORT_TX
158 #if USE_UART1_RX && defined UART1_GPIO_PORT_RX
162 sdStart(&SD1, &usart1_config);
163 uart1.reg_addr = &SD1;
164 uart1.baudrate = UART1_BAUD;
165 uart1.init_struct = &uart1_init_struct;
166 uart1_init_struct.
conf = &usart1_config;
170 uart1_init_struct.
rx_mtx = &uart1_rx_mtx;
171 uart1_init_struct.
rx_sem = &uart1_rx_sem;
172 chThdCreateStatic(wa_thd_uart1_rx,
sizeof(wa_thd_uart1_rx),
173 NORMALPRIO+1, thd_uart1_rx, NULL);
176 uart1_init_struct.
tx_mtx = &uart1_tx_mtx;
177 uart1_init_struct.
tx_sem = &uart1_tx_sem;
178 chThdCreateStatic(wa_thd_uart1_tx,
sizeof(wa_thd_uart1_tx),
179 NORMALPRIO+1, thd_uart1_tx, NULL);
189 #define UART2_BAUD SERIAL_DEFAULT_BITRATE
194 #define USE_UART2_TX TRUE
197 #define USE_UART2_RX TRUE
201 #ifndef UART2_HW_FLOW_CONTROL
202 #define UART2_HW_FLOW_CONTROL FALSE
205 static SerialConfig usart2_config = {
208 USART_CR2_STOP1_BITS,
209 #if UART2_HW_FLOW_CONTROL
210 USART_CR3_CTSE | USART_CR3_RTSE
216 static struct SerialInit uart2_init_struct = { NULL, NULL, NULL, NULL, NULL };
223 static __attribute__((noreturn)) void thd_uart2_rx(
void *arg)
226 chRegSetThreadName(
"uart2_rx");
240 static __attribute__((noreturn)) void thd_uart2_tx(
void *arg)
243 chRegSetThreadName(
"uart2_tx");
252 void uart2_init(
void)
257 #if USE_UART2_TX && defined UART2_GPIO_PORT_TX
260 #if USE_UART2_RX && defined UART2_GPIO_PORT_RX
264 sdStart(&SD2, &usart2_config);
265 uart2.reg_addr = &SD2;
266 uart2.baudrate = UART2_BAUD;
267 uart2.init_struct = &uart2_init_struct;
268 uart2_init_struct.
conf = &usart2_config;
272 uart2_init_struct.
rx_mtx = &uart2_rx_mtx;
273 uart2_init_struct.
rx_sem = &uart2_rx_sem;
274 chThdCreateStatic(wa_thd_uart2_rx,
sizeof(wa_thd_uart2_rx),
275 NORMALPRIO, thd_uart2_rx, NULL);
278 uart2_init_struct.
tx_mtx = &uart2_tx_mtx;
279 uart2_init_struct.
tx_sem = &uart2_tx_sem;
280 chThdCreateStatic(wa_thd_uart2_tx,
sizeof(wa_thd_uart2_tx),
281 NORMALPRIO, thd_uart2_tx, NULL);
290 #define UART3_BAUD SERIAL_DEFAULT_BITRATE
295 #define USE_UART3_TX TRUE
298 #define USE_UART3_RX TRUE
301 static SerialConfig usart3_config = {
304 USART_CR2_STOP1_BITS,
308 static struct SerialInit uart3_init_struct = { NULL, NULL, NULL, NULL, NULL };
315 static __attribute__((noreturn)) void thd_uart3_rx(
void *arg)
318 chRegSetThreadName(
"uart3_rx");
332 static __attribute__((noreturn)) void thd_uart3_tx(
void *arg)
335 chRegSetThreadName(
"uart3_tx");
344 void uart3_init(
void)
349 #if USE_UART3_TX && defined UART3_GPIO_PORT_TX
352 #if USE_UART3_RX && defined UART3_GPIO_PORT_RX
356 sdStart(&SD3, &usart3_config);
357 uart3.reg_addr = &SD3;
358 uart3.baudrate = UART3_BAUD;
359 uart3.init_struct = &uart3_init_struct;
360 uart3_init_struct.
conf = &usart3_config;
364 uart3_init_struct.
rx_mtx = &uart3_rx_mtx;
365 uart3_init_struct.
rx_sem = &uart3_rx_sem;
366 chThdCreateStatic(wa_thd_uart3_rx,
sizeof(wa_thd_uart3_rx),
367 NORMALPRIO, thd_uart3_rx, NULL);
370 uart3_init_struct.
tx_mtx = &uart3_tx_mtx;
371 uart3_init_struct.
tx_sem = &uart3_tx_sem;
372 chThdCreateStatic(wa_thd_uart3_tx,
sizeof(wa_thd_uart3_tx),
373 NORMALPRIO, thd_uart3_tx, NULL);
382 #define UART4_BAUD SERIAL_DEFAULT_BITRATE
387 #define USE_UART4_TX TRUE
390 #define USE_UART4_RX TRUE
393 static SerialConfig usart4_config = {
396 USART_CR2_STOP1_BITS,
400 static struct SerialInit uart4_init_struct = { NULL, NULL, NULL, NULL, NULL };
407 static __attribute__((noreturn)) void thd_uart4_rx(
void *arg)
410 chRegSetThreadName(
"uart4_rx");
424 static __attribute__((noreturn)) void thd_uart4_tx(
void *arg)
427 chRegSetThreadName(
"uart4_tx");
436 void uart4_init(
void)
441 #if USE_UART4_TX && defined UART4_GPIO_PORT_TX
444 #if USE_UART4_RX && defined UART4_GPIO_PORT_RX
448 sdStart(&SD4, &usart4_config);
449 uart4.reg_addr = &SD4;
450 uart4.baudrate = UART4_BAUD;
451 uart4.init_struct = &uart4_init_struct;
452 uart4_init_struct.
conf = &usart4_config;
456 uart4_init_struct.
rx_mtx = &uart4_rx_mtx;
457 uart4_init_struct.
rx_sem = &uart4_rx_sem;
458 chThdCreateStatic(wa_thd_uart4_rx,
sizeof(wa_thd_uart4_rx),
459 NORMALPRIO, thd_uart4_rx, NULL);
462 uart4_init_struct.
tx_mtx = &uart4_tx_mtx;
463 uart4_init_struct.
tx_sem = &uart4_tx_sem;
464 chThdCreateStatic(wa_thd_uart4_tx,
sizeof(wa_thd_uart4_tx),
465 NORMALPRIO, thd_uart4_tx, NULL);
474 #define UART5_BAUD SERIAL_DEFAULT_BITRATE
479 #define USE_UART5_TX TRUE
482 #define USE_UART5_RX TRUE
485 static SerialConfig usart5_config = {
488 USART_CR2_STOP1_BITS,
492 static struct SerialInit uart5_init_struct = { NULL, NULL, NULL, NULL, NULL };
499 static __attribute__((noreturn)) void thd_uart5_rx(
void *arg)
502 chRegSetThreadName(
"uart5_rx");
516 static __attribute__((noreturn)) void thd_uart5_tx(
void *arg)
519 chRegSetThreadName(
"uart5_tx");
528 void uart5_init(
void)
533 #if USE_UART5_TX && defined UART5_GPIO_PORT_TX
536 #if USE_UART5_RX && defined UART5_GPIO_PORT_RX
540 sdStart(&SD5, &usart5_config);
541 uart5.reg_addr = &SD5;
542 uart5.baudrate = UART5_BAUD;
543 uart5.init_struct = &uart5_init_struct;
544 uart5_init_struct.
conf = &usart5_config;
548 uart5_init_struct.
rx_mtx = &uart5_rx_mtx;
549 uart5_init_struct.
rx_sem = &uart5_rx_sem;
550 chThdCreateStatic(wa_thd_uart5_rx,
sizeof(wa_thd_uart5_rx),
551 NORMALPRIO, thd_uart5_rx, NULL);
554 uart5_init_struct.
tx_mtx = &uart5_tx_mtx;
555 uart5_init_struct.
tx_sem = &uart5_tx_sem;
556 chThdCreateStatic(wa_thd_uart5_tx,
sizeof(wa_thd_uart5_tx),
557 NORMALPRIO, thd_uart5_tx, NULL);
566 #define UART6_BAUD SERIAL_DEFAULT_BITRATE
571 #define USE_UART6_TX TRUE
574 #define USE_UART6_RX TRUE
577 static SerialConfig usart6_config = {
580 USART_CR2_STOP1_BITS,
584 static struct SerialInit uart6_init_struct = { NULL, NULL, NULL, NULL, NULL };
591 static __attribute__((noreturn)) void thd_uart6_rx(
void *arg)
594 chRegSetThreadName(
"uart6_rx");
608 static __attribute__((noreturn)) void thd_uart6_tx(
void *arg)
611 chRegSetThreadName(
"uart6_tx");
620 void uart6_init(
void)
625 #if USE_UART6_TX && defined UART6_GPIO_PORT_TX
628 #if USE_UART6_RX && defined UART6_GPIO_PORT_RX
632 sdStart(&SD6, &usart6_config);
633 uart6.reg_addr = &SD6;
634 uart6.baudrate = UART6_BAUD;
635 uart6.init_struct = &uart6_init_struct;
636 uart6_init_struct.
conf = &usart6_config;
640 uart6_init_struct.
rx_mtx = &uart6_rx_mtx;
641 uart6_init_struct.
rx_sem = &uart6_rx_sem;
642 chThdCreateStatic(wa_thd_uart6_rx,
sizeof(wa_thd_uart6_rx),
643 NORMALPRIO, thd_uart6_rx, NULL);
646 uart6_init_struct.
tx_mtx = &uart6_tx_mtx;
647 uart6_init_struct.
tx_sem = &uart6_tx_sem;
648 chThdCreateStatic(wa_thd_uart6_tx,
sizeof(wa_thd_uart6_tx),
649 NORMALPRIO, thd_uart6_tx, NULL);
658 #define UART7_BAUD SERIAL_DEFAULT_BITRATE
663 #define USE_UART7_TX TRUE
666 #define USE_UART7_RX TRUE
669 static SerialConfig usart7_config = {
672 USART_CR2_STOP1_BITS,
676 static struct SerialInit uart7_init_struct = { NULL, NULL, NULL, NULL, NULL };
683 static __attribute__((noreturn)) void thd_uart7_rx(
void *arg)
686 chRegSetThreadName(
"uart7_rx");
700 static __attribute__((noreturn)) void thd_uart7_tx(
void *arg)
703 chRegSetThreadName(
"uart7_tx");
712 void uart7_init(
void)
717 #if USE_UART7_TX && defined UART7_GPIO_PORT_TX
720 #if USE_UART7_RX && defined UART7_GPIO_PORT_RX
724 sdStart(&SD7, &usart7_config);
725 uart7.reg_addr = &SD7;
726 uart7.baudrate = UART7_BAUD;
727 uart7.init_struct = &uart7_init_struct;
728 uart7_init_struct.
conf = &usart7_config;
732 uart7_init_struct.
rx_mtx = &uart7_rx_mtx;
733 uart7_init_struct.
rx_sem = &uart7_rx_sem;
734 chThdCreateStatic(wa_thd_uart7_rx,
sizeof(wa_thd_uart7_rx),
735 NORMALPRIO, thd_uart7_rx, NULL);
738 uart7_init_struct.
tx_mtx = &uart7_tx_mtx;
739 uart7_init_struct.
tx_sem = &uart7_tx_sem;
740 chThdCreateStatic(wa_thd_uart7_tx,
sizeof(wa_thd_uart7_tx),
741 NORMALPRIO, thd_uart7_tx, NULL);
750 #define UART8_BAUD SERIAL_DEFAULT_BITRATE
755 #define USE_UART8_TX TRUE
758 #define USE_UART8_RX TRUE
761 static SerialConfig usart8_config = {
764 USART_CR2_STOP1_BITS,
768 static struct SerialInit uart8_init_struct = { NULL, NULL, NULL, NULL, NULL };
775 static __attribute__((noreturn)) void thd_uart8_rx(
void *arg)
778 chRegSetThreadName(
"uart8_rx");
792 static __attribute__((noreturn)) void thd_uart8_tx(
void *arg)
795 chRegSetThreadName(
"uart8_tx");
804 void uart8_init(
void)
809 #if USE_UART8_TX && defined UART8_GPIO_PORT_TX
812 #if USE_UART8_RX && defined UART8_GPIO_PORT_RX
816 sdStart(&SD8, &usart8_config);
817 uart8.reg_addr = &SD8;
818 uart8.baudrate = UART8_BAUD;
819 uart8.init_struct = &uart8_init_struct;
820 uart8_init_struct.
conf = &usart8_config;
824 uart8_init_struct.
rx_mtx = &uart8_rx_mtx;
825 uart8_init_struct.
rx_sem = &uart8_rx_sem;
826 chThdCreateStatic(wa_thd_uart8_rx,
sizeof(wa_thd_uart8_rx),
827 NORMALPRIO, thd_uart8_rx, NULL);
830 uart8_init_struct.
tx_mtx = &uart8_tx_mtx;
831 uart8_init_struct.
tx_sem = &uart8_tx_sem;
832 chThdCreateStatic(wa_thd_uart8_tx,
sizeof(wa_thd_uart8_tx),
833 NORMALPRIO, thd_uart8_tx, NULL);
846 chMtxLock(init_struct->
rx_mtx);
849 chMtxUnlock(init_struct->
rx_mtx);
859 SerialConfig *
conf = init_struct->
conf;
864 sdStop((SerialDriver*)(p->
reg_addr));
865 sdStart((SerialDriver*)(p->
reg_addr), conf);
872 bool rx_enabled __attribute__((unused)),
bool hw_flow_control __attribute__((unused))) {}
875 #define __USART_CR1_M USART_CR1_M_0
876 #elif defined STM32F1 || defined STM32F4
877 #define __USART_CR1_M USART_CR1_M
879 #error unsupported board
889 SerialConfig *
conf = init_struct->
conf;
893 conf->cr1 |= USART_CR1_PCE;
894 conf->cr1 &= ~USART_CR1_PS;
896 conf->cr1 &= ~__USART_CR1_M;
898 conf->cr1 |= __USART_CR1_M;
901 conf->cr1 |= USART_CR1_PCE;
902 conf->cr1 |= USART_CR1_PS;
904 conf->cr1 &= ~__USART_CR1_M;
906 conf->cr1 |= __USART_CR1_M;
909 conf->cr1 &= ~USART_CR1_PCE;
910 conf->cr1 &= ~__USART_CR1_M;
913 conf->cr2 &= ~USART_CR2_STOP;
915 conf-> cr2 |= USART_CR2_STOP2_BITS;
917 conf-> cr2 |= USART_CR2_STOP1_BITS;
920 sdStop((SerialDriver*)(p->
reg_addr));
921 sdStart((SerialDriver*)(p->
reg_addr), conf);
931 SerialConfig *
conf = init_struct->
conf;
933 conf->cr2 |= USART_CR2_RXINV;
935 conf->cr2 &= ~USART_CR2_RXINV;
938 conf->cr2 |= USART_CR2_TXINV;
940 conf->cr2 &= ~USART_CR2_TXINV;
942 sdStop((SerialDriver*)(p->
reg_addr));
943 sdStart((SerialDriver*)(p->
reg_addr), conf);
958 chMtxLock(init_struct->
tx_mtx);
972 chMtxLock(init_struct->
tx_mtx);
975 chMtxUnlock(init_struct->
tx_mtx);
981 chMtxUnlock(init_struct->
tx_mtx);
983 chSemSignal (init_struct->
tx_sem);
1001 chMtxLock(init_struct->
tx_mtx);
1007 chMtxUnlock(init_struct->
tx_mtx);
1013 for (i = 0; i < len; i++) {
1019 chMtxUnlock(init_struct->
tx_mtx);
1021 chSemSignal (init_struct->
tx_sem);
1030 chMtxUnlock(init_struct->
tx_mtx);
1033 chSemSignal (init_struct->
tx_sem);
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.
uint8_t tx_buf[UART_TX_BUFFER_SIZE]
Transmit buffer.
uint8_t rx_buf[UART_RX_BUFFER_SIZE]
Receive buffer.
void uart_periph_set_baudrate(struct uart_periph *p, uint32_t baud)
Set baudrate.
volatile uint8_t tx_running
uint8_t uart_getch(struct uart_periph *p)
Some architecture independent helper functions for GPIOs.
void * init_struct
User init struct.
#define UART2_GPIO_PORT_TX
UART2 (with optional flow control activated by default)
static void handle_uart_tx(struct uart_periph *p)
TX handler.
void uart_put_buffer(struct uart_periph *p, long fd, const uint8_t *data, uint16_t len)
Uart transmit buffer implementation.
void WEAK uart_periph_invert_data_logic(struct uart_periph *p, bool invert_rx, bool invert_tx)
#define UART_RX_BUFFER_SIZE
bool uart_check_free_space(struct uart_periph *p, long *fd, uint16_t len)
void uart_send_message(struct uart_periph *p, long fd)
void uart_periph_set_mode(struct uart_periph *p, bool tx_enabled, bool rx_enabled, bool hw_flow_control)
Set mode (not necessary, or can be set by SerialConfig)
void * reg_addr
UART Register.
static SEMAPHORE_DECL(spi1_sem, 0)
Configure SPI peripherals.
#define UART3_GPIO_PORT_RX
#define UART5_GPIO_PORT_TX
#define UART7_GPIO_PORT_RX
#define UART6_GPIO_PORT_TX
void uart_periph_init(struct uart_periph *p)
#define UART4_GPIO_PORT_RX
#define UART6_GPIO_PORT_RX
void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
Uart transmit implementation.
#define UART1_GPIO_PORT_RX
#define UART5_GPIO_PORT_RX
void uart_periph_set_bits_stop_parity(struct uart_periph *p, uint8_t bits, uint8_t stop, uint8_t parity)
Set parity and stop bits.
#define UART2_GPIO_PORT_RX
#define UART4_GPIO_PORT_TX
#define UART8_GPIO_PORT_RX
static MUTEX_DECL(sys_time_mtx)
#define UART3_GPIO_PORT_TX
int baudrate
UART Baudrate.
#define UART_TX_BUFFER_SIZE
#define UART8_GPIO_PORT_TX
#define UART1_GPIO_PORT_TX
#define UART7_GPIO_PORT_TX
static void handle_uart_rx(struct uart_periph *p)
RX handler.
static THD_WORKING_AREA(wa_thd_spi1, 1024)