33 #include <libopencm3/stm32/gpio.h>
34 #include <libopencm3/stm32/rcc.h>
35 #include <libopencm3/stm32/usart.h>
36 #include <libopencm3/cm3/nvic.h>
47 usart_set_baudrate((
uint32_t)
p->reg_addr, baud);
50 USART_CR1((
uint32_t)
p->reg_addr) &= ~USART_CR1_IDLEIE;
53 USART_CR2((
uint32_t)
p->reg_addr) &= ~USART_CR2_LBDIE;
56 USART_CR1((
uint32_t)
p->reg_addr) |= USART_CR1_RXNEIE;
67 usart_set_parity((
uint32_t)
p->reg_addr, USART_PARITY_EVEN);
69 usart_set_databits((
uint32_t)
p->reg_addr, 8);
71 usart_set_databits((
uint32_t)
p->reg_addr, 9);
74 usart_set_parity((
uint32_t)
p->reg_addr, USART_PARITY_ODD);
76 usart_set_databits((
uint32_t)
p->reg_addr, 8);
78 usart_set_databits((
uint32_t)
p->reg_addr, 9);
81 usart_set_parity((
uint32_t)
p->reg_addr, USART_PARITY_NONE);
82 usart_set_databits((
uint32_t)
p->reg_addr, 8);
86 usart_set_stopbits((
uint32_t)
p->reg_addr, USART_STOPBITS_2);
88 usart_set_stopbits((
uint32_t)
p->reg_addr, USART_STOPBITS_1);
96 mode |= USART_MODE_TX;
99 mode |= USART_MODE_RX;
105 if (hw_flow_control) {
106 usart_set_flow_control((
uint32_t)
p->reg_addr, USART_FLOWCONTROL_RTS_CTS);
108 usart_set_flow_control((
uint32_t)
p->reg_addr, USART_FLOWCONTROL_NONE);
117 if (temp ==
p->tx_extract_idx) {
121 USART_CR1((
uint32_t)
p->reg_addr) &= ~USART_CR1_TXEIE;
125 p->tx_buf[
p->tx_insert_idx] = data;
126 p->tx_insert_idx = temp;
128 p->tx_running =
true;
132 USART_CR1((
uint32_t)
p->reg_addr) |= USART_CR1_TXEIE;
139 if (((USART_CR1((
uint32_t)
p->reg_addr) & USART_CR1_TXEIE) != 0) &&
140 ((USART_SR((
uint32_t)
p->reg_addr) & USART_SR_TXE) != 0)) {
142 if (
p->tx_insert_idx !=
p->tx_extract_idx) {
143 usart_send((
uint32_t)
p->reg_addr,
p->tx_buf[
p->tx_extract_idx]);
147 p->tx_running =
false;
148 USART_CR1((
uint32_t)
p->reg_addr) &= ~USART_CR1_TXEIE;
152 if (((USART_CR1((
uint32_t)
p->reg_addr) & USART_CR1_RXNEIE) != 0) &&
153 ((USART_SR((
uint32_t)
p->reg_addr) & USART_SR_RXNE) != 0) &&
154 ((USART_SR((
uint32_t)
p->reg_addr) & USART_SR_ORE) == 0) &&
155 ((USART_SR((
uint32_t)
p->reg_addr) & USART_SR_NE) == 0) &&
156 ((USART_SR((
uint32_t)
p->reg_addr) & USART_SR_FE) == 0)) {
158 p->rx_buf[
p->rx_insert_idx] = usart_recv((
uint32_t)
p->reg_addr);
160 if (temp !=
p->rx_extract_idx) {
161 p->rx_insert_idx = temp;
165 if (((USART_CR1((
uint32_t)
p->reg_addr) & USART_CR1_RXNEIE) != 0) &&
166 ((USART_SR((
uint32_t)
p->reg_addr) & USART_SR_ORE) != 0)) {
170 if (((USART_CR1((
uint32_t)
p->reg_addr) & USART_CR1_RXNEIE) != 0) &&
171 ((USART_SR((
uint32_t)
p->reg_addr) & USART_SR_NE) != 0)) {
175 if (((USART_CR1((
uint32_t)
p->reg_addr) & USART_CR1_RXNEIE) != 0) &&
176 ((USART_SR((
uint32_t)
p->reg_addr) & USART_SR_FE) != 0)) {
190 nvic_enable_irq(IRQn);
198 #define USE_UART1_TX TRUE
201 #define USE_UART1_RX TRUE
204 #ifndef UART1_HW_FLOW_CONTROL
205 #define UART1_HW_FLOW_CONTROL FALSE
209 #define UART1_BITS UBITS_8
213 #define UART1_STOP USTOP_1
217 #define UART1_PARITY UPARITY_NO
220 void uart1_init(
void)
224 uart1.reg_addr = (
void *)USART1;
227 rcc_periph_clock_enable(RCC_USART1);
239 #if UART1_HW_FLOW_CONTROL
240 #warning "USING UART1 FLOW CONTROL. Make sure to pull down CTS if you are not connecting any flow-control-capable hardware."
254 void usart1_isr(
void) {
usart_isr(&uart1); }
263 #define USE_UART2_TX TRUE
266 #define USE_UART2_RX TRUE
269 #ifndef UART2_HW_FLOW_CONTROL
270 #define UART2_HW_FLOW_CONTROL FALSE
274 #define UART2_BITS UBITS_8
278 #define UART2_STOP USTOP_1
282 #define UART2_PARITY UPARITY_NO
285 void uart2_init(
void)
289 uart2.reg_addr = (
void *)USART2;
292 rcc_periph_clock_enable(RCC_USART2);
304 #if UART2_HW_FLOW_CONTROL && defined(STM32F4)
305 #warning "USING UART2 FLOW CONTROL. Make sure to pull down CTS if you are not connecting any flow-control-capable hardware."
319 void usart2_isr(
void) {
usart_isr(&uart2); }
328 #define USE_UART3_TX TRUE
331 #define USE_UART3_RX TRUE
334 #ifndef UART3_HW_FLOW_CONTROL
335 #define UART3_HW_FLOW_CONTROL FALSE
339 #define UART3_BITS UBITS_8
343 #define UART3_STOP USTOP_1
347 #define UART3_PARITY UPARITY_NO
350 void uart3_init(
void)
354 uart3.reg_addr = (
void *)USART3;
357 rcc_periph_clock_enable(RCC_USART3);
369 #if UART3_HW_FLOW_CONTROL && defined(STM32F4)
370 #warning "USING UART3 FLOW CONTROL. Make sure to pull down CTS if you are not connecting any flow-control-capable hardware."
384 void usart3_isr(
void) {
usart_isr(&uart3); }
393 #define USE_UART4_TX TRUE
396 #define USE_UART4_RX TRUE
400 #define UART4_BITS UBITS_8
404 #define UART4_STOP USTOP_1
408 #define UART4_PARITY UPARITY_NO
411 void uart4_init(
void)
415 uart4.reg_addr = (
void *)UART4;
418 rcc_periph_clock_enable(RCC_UART4);
436 void uart4_isr(
void) {
usart_isr(&uart4); }
445 #define USE_UART5_TX TRUE
448 #define USE_UART5_RX TRUE
452 #define UART5_BITS UBITS_8
456 #define UART5_STOP USTOP_1
460 #define UART5_PARITY UPARITY_NO
463 void uart5_init(
void)
467 uart5.reg_addr = (
void *)UART5;
470 rcc_periph_clock_enable(RCC_UART5);
488 void uart5_isr(
void) {
usart_isr(&uart5); }
493 #if USE_UART6 && defined STM32F4
497 #define USE_UART6_TX TRUE
500 #define USE_UART6_RX TRUE
503 #ifndef UART6_HW_FLOW_CONTROL
504 #define UART6_HW_FLOW_CONTROL FALSE
508 #define UART6_BITS UBITS_8
512 #define UART6_STOP USTOP_1
516 #define UART6_PARITY UPARITY_NO
519 void uart6_init(
void)
523 uart6.reg_addr = (
void *)USART6;
526 rcc_periph_clock_enable(RCC_USART6);
539 #if UART6_HW_FLOW_CONTROL
540 #warning "USING UART6 FLOW CONTROL. Make sure to pull down CTS if you are not connecting any flow-control-capable hardware."
552 void usart6_isr(
void) {
usart_isr(&uart6); }
561 #define USE_UART7_TX TRUE
564 #define USE_UART7_RX TRUE
568 #define UART7_BITS UBITS_8
572 #define UART7_STOP USTOP_1
576 #define UART7_PARITY UPARITY_NO
579 void uart7_init(
void)
583 uart7.reg_addr = (
void *)UART7;
586 rcc_periph_clock_enable(RCC_UART7);
604 void uart7_isr(
void) {
usart_isr(&uart7); }
613 #define USE_UART8_TX TRUE
616 #define USE_UART8_RX TRUE
620 #define UART8_BITS UBITS_8
624 #define UART8_STOP USTOP_1
628 #define UART8_PARITY UPARITY_NO
631 void uart8_init(
void)
635 uart8.reg_addr = (
void *)UART8;
638 rcc_periph_clock_enable(RCC_UART8);
656 void uart8_isr(
void) {
usart_isr(&uart8); }
#define UART4_GPIO_PORT_RX
#define UART1_GPIO_PORT_TX
#define UART6_GPIO_PORT_RX
#define UART2_GPIO_PORT_RX
#define UART1_GPIO_PORT_RX
#define UART6_GPIO_PORT_TX
#define UART4_GPIO_PORT_TX
#define UART3_GPIO_PORT_TX
#define UART3_GPIO_PORT_RX
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.
#define UART8_GPIO_PORT_RX
#define USE_UART7_RX
SBUS / Spektrum port.
#define UART2_HW_FLOW_CONTROL
#define UART7_GPIO_PORT_RX
#define UART8_GPIO_PORT_TX
#define UART2_GPIO_PORT_TX
UART2 (with optional flow control activated by default)
#define UART1_GPIO_AF
UART defines.
#define UART6_GPIO_PORT_CTS
#define UART5_GPIO_PORT_TX
#define UART5_GPIO_PORT_RX
Some architecture independent helper functions for GPIOs.
#define USE_UART6_RX
SBUS / Spektrum port.
void uart_put_byte(struct uart_periph *periph, long fd, uint8_t data)
void uart_periph_set_bits_stop_parity(struct uart_periph *periph, uint8_t bits, uint8_t stop, uint8_t parity)
void uart_periph_set_baudrate(struct uart_periph *periph, uint32_t baud)
#define UART_TX_BUFFER_SIZE
#define UART_RX_BUFFER_SIZE
#define UART7_GPIO_PORT_TX
UART7 (Modem with optional flow control disabled by default)
#define USE_UART8_RX
SBUS / Spektrum port.
#define UART3_GPIO_PORT_CTS
#define UART2_GPIO_PORT_RTS
#define UART3_GPIO_PORT_RTS
#define UART2_GPIO_PORT_CTS
static uint8_t mode
mode holds the current sonar mode mode = 0 used at high altitude, uses 16 wave patterns mode = 1 used...
static void usart_enable_irq(uint8_t IRQn)
void uart_periph_set_mode(struct uart_periph *p, bool tx_enabled, bool rx_enabled, bool hw_flow_control)
static void usart_isr(struct uart_periph *p)
void uart_periph_init(struct uart_periph *p)
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.