37 #include "mcu_periph/uart_arch.h"
59 chMtxLock(init_struct->
rx_mtx);
67 chMtxUnlock(init_struct->
rx_mtx);
68 chSemSignal(init_struct->
rx_sem);
78 chSemWait (init_struct->
tx_sem);
82 sdWrite((SerialDriver *)p->
reg_addr, &data,
sizeof(data));
85 chMtxLock(init_struct->
tx_mtx);
88 chMtxUnlock(init_struct->
tx_mtx);
95 #define UART1_BAUD SERIAL_DEFAULT_BITRATE
100 #define USE_UART1_TX TRUE
103 #define USE_UART1_RX TRUE
106 static SerialConfig usart1_config = {
109 USART_CR2_STOP1_BITS,
113 static struct SerialInit uart1_init_struct = { NULL, NULL, NULL, NULL, NULL };
120 static __attribute__((noreturn)) void thd_uart1_rx(
void *arg)
123 chRegSetThreadName(
"uart1_rx");
137 static __attribute__((noreturn)) void thd_uart1_tx(
void *arg)
140 chRegSetThreadName(
"uart1_tx");
149 void uart1_init(
void)
154 #if USE_UART1_TX && defined UART1_GPIO_PORT_TX
157 #if USE_UART1_RX && defined UART1_GPIO_PORT_RX
161 sdStart(&SD1, &usart1_config);
162 uart1.reg_addr = &SD1;
163 uart1.init_struct = &uart1_init_struct;
164 uart1_init_struct.
conf = &usart1_config;
168 uart1_init_struct.
rx_mtx = &uart1_rx_mtx;
169 uart1_init_struct.
rx_sem = &uart1_rx_sem;
170 chThdCreateStatic(wa_thd_uart1_rx,
sizeof(wa_thd_uart1_rx),
171 NORMALPRIO+1, thd_uart1_rx, NULL);
174 uart1_init_struct.
tx_mtx = &uart1_tx_mtx;
175 uart1_init_struct.
tx_sem = &uart1_tx_sem;
176 chThdCreateStatic(wa_thd_uart1_tx,
sizeof(wa_thd_uart1_tx),
177 NORMALPRIO+1, thd_uart1_tx, NULL);
187 #define UART2_BAUD SERIAL_DEFAULT_BITRATE
192 #define USE_UART2_TX TRUE
195 #define USE_UART2_RX TRUE
198 static SerialConfig usart2_config = {
201 USART_CR2_STOP1_BITS,
205 static struct SerialInit uart2_init_struct = { NULL, NULL, NULL, NULL, NULL };
212 static __attribute__((noreturn)) void thd_uart2_rx(
void *arg)
215 chRegSetThreadName(
"uart2_rx");
229 static __attribute__((noreturn)) void thd_uart2_tx(
void *arg)
232 chRegSetThreadName(
"uart2_tx");
241 void uart2_init(
void)
246 #if USE_UART2_TX && defined UART2_GPIO_PORT_TX
249 #if USE_UART2_RX && defined UART2_GPIO_PORT_RX
253 sdStart(&SD2, &usart2_config);
254 uart2.reg_addr = &SD2;
255 uart2.init_struct = &uart2_init_struct;
256 uart2_init_struct.
conf = &usart2_config;
260 uart2_init_struct.
rx_mtx = &uart2_rx_mtx;
261 uart2_init_struct.
rx_sem = &uart2_rx_sem;
262 chThdCreateStatic(wa_thd_uart2_rx,
sizeof(wa_thd_uart2_rx),
263 NORMALPRIO, thd_uart2_rx, NULL);
266 uart2_init_struct.
tx_mtx = &uart2_tx_mtx;
267 uart2_init_struct.
tx_sem = &uart2_tx_sem;
268 chThdCreateStatic(wa_thd_uart2_tx,
sizeof(wa_thd_uart2_tx),
269 NORMALPRIO, thd_uart2_tx, NULL);
278 #define UART3_BAUD SERIAL_DEFAULT_BITRATE
283 #define USE_UART3_TX TRUE
286 #define USE_UART3_RX TRUE
289 static SerialConfig usart3_config = {
292 USART_CR2_STOP1_BITS,
296 static struct SerialInit uart3_init_struct = { NULL, NULL, NULL, NULL, NULL };
303 static __attribute__((noreturn)) void thd_uart3_rx(
void *arg)
306 chRegSetThreadName(
"uart3_rx");
320 static __attribute__((noreturn)) void thd_uart3_tx(
void *arg)
323 chRegSetThreadName(
"uart3_tx");
332 void uart3_init(
void)
337 #if USE_UART3_TX && defined UART3_GPIO_PORT_TX
340 #if USE_UART3_RX && defined UART3_GPIO_PORT_RX
344 sdStart(&SD3, &usart3_config);
345 uart3.reg_addr = &SD3;
346 uart3.init_struct = &uart3_init_struct;
347 uart3_init_struct.
conf = &usart3_config;
351 uart3_init_struct.
rx_mtx = &uart3_rx_mtx;
352 uart3_init_struct.
rx_sem = &uart3_rx_sem;
353 chThdCreateStatic(wa_thd_uart3_rx,
sizeof(wa_thd_uart3_rx),
354 NORMALPRIO, thd_uart3_rx, NULL);
357 uart3_init_struct.
tx_mtx = &uart3_tx_mtx;
358 uart3_init_struct.
tx_sem = &uart3_tx_sem;
359 chThdCreateStatic(wa_thd_uart3_tx,
sizeof(wa_thd_uart3_tx),
360 NORMALPRIO, thd_uart3_tx, NULL);
369 #define UART4_BAUD SERIAL_DEFAULT_BITRATE
374 #define USE_UART4_TX TRUE
377 #define USE_UART4_RX TRUE
380 static SerialConfig usart4_config = {
383 USART_CR2_STOP1_BITS,
387 static struct SerialInit uart4_init_struct = { NULL, NULL, NULL, NULL, NULL };
394 static __attribute__((noreturn)) void thd_uart4_rx(
void *arg)
397 chRegSetThreadName(
"uart4_rx");
411 static __attribute__((noreturn)) void thd_uart4_tx(
void *arg)
414 chRegSetThreadName(
"uart4_tx");
423 void uart4_init(
void)
428 #if USE_UART4_TX && defined UART4_GPIO_PORT_TX
431 #if USE_UART4_RX && defined UART4_GPIO_PORT_RX
435 sdStart(&SD4, &usart4_config);
436 uart4.reg_addr = &SD4;
437 uart4.init_struct = &uart4_init_struct;
438 uart4_init_struct.
conf = &usart4_config;
442 uart4_init_struct.
rx_mtx = &uart4_rx_mtx;
443 uart4_init_struct.
rx_sem = &uart4_rx_sem;
444 chThdCreateStatic(wa_thd_uart4_rx,
sizeof(wa_thd_uart4_rx),
445 NORMALPRIO, thd_uart4_rx, NULL);
448 uart4_init_struct.
tx_mtx = &uart4_tx_mtx;
449 uart4_init_struct.
tx_sem = &uart4_tx_sem;
450 chThdCreateStatic(wa_thd_uart4_tx,
sizeof(wa_thd_uart4_tx),
451 NORMALPRIO, thd_uart4_tx, NULL);
460 #define UART5_BAUD SERIAL_DEFAULT_BITRATE
465 #define USE_UART5_TX TRUE
468 #define USE_UART5_RX TRUE
471 static SerialConfig usart5_config = {
474 USART_CR2_STOP1_BITS,
478 static struct SerialInit uart5_init_struct = { NULL, NULL, NULL, NULL, NULL };
485 static __attribute__((noreturn)) void thd_uart5_rx(
void *arg)
488 chRegSetThreadName(
"uart5_rx");
502 static __attribute__((noreturn)) void thd_uart5_tx(
void *arg)
505 chRegSetThreadName(
"uart5_tx");
514 void uart5_init(
void)
519 #if USE_UART5_TX && defined UART5_GPIO_PORT_TX
522 #if USE_UART5_RX && defined UART5_GPIO_PORT_RX
526 sdStart(&SD5, &usart5_config);
527 uart5.reg_addr = &SD5;
528 uart5.init_struct = &uart5_init_struct;
529 uart5_init_struct.
conf = &usart5_config;
533 uart5_init_struct.
rx_mtx = &uart5_rx_mtx;
534 uart5_init_struct.
rx_sem = &uart5_rx_sem;
535 chThdCreateStatic(wa_thd_uart5_rx,
sizeof(wa_thd_uart5_rx),
536 NORMALPRIO, thd_uart5_rx, NULL);
539 uart5_init_struct.
tx_mtx = &uart5_tx_mtx;
540 uart5_init_struct.
tx_sem = &uart5_tx_sem;
541 chThdCreateStatic(wa_thd_uart5_tx,
sizeof(wa_thd_uart5_tx),
542 NORMALPRIO, thd_uart5_tx, NULL);
551 #define UART6_BAUD SERIAL_DEFAULT_BITRATE
556 #define USE_UART6_TX TRUE
559 #define USE_UART6_RX TRUE
562 static SerialConfig usart6_config = {
565 USART_CR2_STOP1_BITS,
569 static struct SerialInit uart6_init_struct = { NULL, NULL, NULL, NULL, NULL };
576 static __attribute__((noreturn)) void thd_uart6_rx(
void *arg)
579 chRegSetThreadName(
"uart6_rx");
593 static __attribute__((noreturn)) void thd_uart6_tx(
void *arg)
596 chRegSetThreadName(
"uart6_tx");
605 void uart6_init(
void)
610 #if USE_UART6_TX && defined UART6_GPIO_PORT_TX
613 #if USE_UART6_RX && defined UART6_GPIO_PORT_RX
617 sdStart(&SD6, &usart6_config);
618 uart6.reg_addr = &SD6;
619 uart6.init_struct = &uart6_init_struct;
620 uart6_init_struct.
conf = &usart6_config;
624 uart6_init_struct.
rx_mtx = &uart6_rx_mtx;
625 uart6_init_struct.
rx_sem = &uart6_rx_sem;
626 chThdCreateStatic(wa_thd_uart6_rx,
sizeof(wa_thd_uart6_rx),
627 NORMALPRIO, thd_uart6_rx, NULL);
630 uart6_init_struct.
tx_mtx = &uart6_tx_mtx;
631 uart6_init_struct.
tx_sem = &uart6_tx_sem;
632 chThdCreateStatic(wa_thd_uart6_tx,
sizeof(wa_thd_uart6_tx),
633 NORMALPRIO, thd_uart6_tx, NULL);
642 #define UART7_BAUD SERIAL_DEFAULT_BITRATE
647 #define USE_UART7_TX TRUE
650 #define USE_UART7_RX TRUE
653 static SerialConfig usart7_config = {
656 USART_CR2_STOP1_BITS,
660 static struct SerialInit uart7_init_struct = { NULL, NULL, NULL, NULL, NULL };
667 static __attribute__((noreturn)) void thd_uart7_rx(
void *arg)
670 chRegSetThreadName(
"uart7_rx");
684 static __attribute__((noreturn)) void thd_uart7_tx(
void *arg)
687 chRegSetThreadName(
"uart7_tx");
696 void uart7_init(
void)
701 #if USE_UART7_TX && defined UART7_GPIO_PORT_TX
704 #if USE_UART7_RX && defined UART7_GPIO_PORT_RX
708 sdStart(&SD7, &usart7_config);
709 uart7.reg_addr = &SD7;
710 uart7.init_struct = &uart7_init_struct;
711 uart7_init_struct.
conf = &usart7_config;
715 uart7_init_struct.
rx_mtx = &uart7_rx_mtx;
716 uart7_init_struct.
rx_sem = &uart7_rx_sem;
717 chThdCreateStatic(wa_thd_uart7_rx,
sizeof(wa_thd_uart7_rx),
718 NORMALPRIO, thd_uart7_rx, NULL);
721 uart7_init_struct.
tx_mtx = &uart7_tx_mtx;
722 uart7_init_struct.
tx_sem = &uart7_tx_sem;
723 chThdCreateStatic(wa_thd_uart7_tx,
sizeof(wa_thd_uart7_tx),
724 NORMALPRIO, thd_uart7_tx, NULL);
733 #define UART8_BAUD SERIAL_DEFAULT_BITRATE
738 #define USE_UART8_TX TRUE
741 #define USE_UART8_RX TRUE
744 static SerialConfig usart8_config = {
747 USART_CR2_STOP1_BITS,
751 static struct SerialInit uart8_init_struct = { NULL, NULL, NULL, NULL, NULL };
758 static __attribute__((noreturn)) void thd_uart8_rx(
void *arg)
761 chRegSetThreadName(
"uart8_rx");
775 static __attribute__((noreturn)) void thd_uart8_tx(
void *arg)
778 chRegSetThreadName(
"uart8_tx");
787 void uart8_init(
void)
792 #if USE_UART8_TX && defined UART8_GPIO_PORT_TX
795 #if USE_UART8_RX && defined UART8_GPIO_PORT_RX
799 sdStart(&SD8, &usart8_config);
800 uart8.reg_addr = &SD8;
801 uart8.init_struct = &uart8_init_struct;
802 uart8_init_struct.
conf = &usart8_config;
806 uart8_init_struct.
rx_mtx = &uart8_rx_mtx;
807 uart8_init_struct.
rx_sem = &uart8_rx_sem;
808 chThdCreateStatic(wa_thd_uart8_rx,
sizeof(wa_thd_uart8_rx),
809 NORMALPRIO, thd_uart8_rx, NULL);
812 uart8_init_struct.
tx_mtx = &uart8_tx_mtx;
813 uart8_init_struct.
tx_sem = &uart8_tx_sem;
814 chThdCreateStatic(wa_thd_uart8_tx,
sizeof(wa_thd_uart8_tx),
815 NORMALPRIO, thd_uart8_tx, NULL);
828 chMtxLock(init_struct->
rx_mtx);
831 chMtxUnlock(init_struct->
rx_mtx);
841 SerialConfig *
conf = init_struct->
conf;
845 sdStop((SerialDriver*)(p->
reg_addr));
846 sdStart((SerialDriver*)(p->
reg_addr), conf);
853 bool rx_enabled __attribute__((unused)),
bool hw_flow_control __attribute__((unused))) {}
862 SerialConfig *
conf = init_struct->
conf;
866 conf->cr1 |= USART_CR1_PCE;
867 conf->cr1 &= ~USART_CR1_PS;
869 conf->cr1 &= ~USART_CR1_M;
871 conf->cr1 |= USART_CR1_M;
874 conf->cr1 |= USART_CR1_PCE;
875 conf->cr1 |= USART_CR1_PS;
877 conf->cr1 &= ~USART_CR1_M;
879 conf->cr1 |= USART_CR1_M;
882 conf->cr1 &= ~USART_CR1_PCE;
883 conf->cr1 &= ~USART_CR1_M;
886 conf->cr2 &= ~USART_CR2_STOP;
888 conf-> cr2 |= USART_CR2_STOP2_BITS;
890 conf-> cr2 |= USART_CR2_STOP1_BITS;
893 sdStop((SerialDriver*)(p->
reg_addr));
894 sdStart((SerialDriver*)(p->
reg_addr), conf);
908 chMtxLock(init_struct->
tx_mtx);
922 chMtxLock(init_struct->
tx_mtx);
925 chMtxUnlock(init_struct->
tx_mtx);
931 chMtxUnlock(init_struct->
tx_mtx);
933 chSemSignal (init_struct->
tx_sem);
951 chMtxLock(init_struct->
tx_mtx);
957 chMtxUnlock(init_struct->
tx_mtx);
963 for (i = 0; i < len; i++) {
969 chMtxUnlock(init_struct->
tx_mtx);
971 chSemSignal (init_struct->
tx_sem);
980 chMtxUnlock(init_struct->
tx_mtx);
983 chSemSignal (init_struct->
tx_sem);
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.
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.
#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)
#define UART7_GPIO_PORT_RX
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 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
void gpio_setup_pin_af(ioportid_t port, uint16_t pin, uint8_t af)
Setup a gpio for input or output with alternate function.
#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
#define UART_TX_BUFFER_SIZE
#define UART8_GPIO_PORT_TX
#define UART1_GPIO_PORT_TX
#define UART2_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)