28 #include <libopencm3/stm32/rcc.h>
29 #include <libopencm3/stm32/gpio.h>
30 #include <libopencm3/stm32/usart.h>
31 #include <libopencm3/cm3/nvic.h>
32 #include <libopencmsis/core_cm3.h>
40 #define USART_RCC RCC_USART1
41 #define USART_IRQ NVIC_USART1_IRQ
42 #define USART_ISR usart1_isr
43 #define USART_PORT GPIOA
44 #define USART_PORT_RCC RCC_GPIOA
45 #define USART_PIN_TX GPIO_USART1_TX
46 #define USART_PIN_RX GPIO_USART1_RX
49 #define USART_BAUDRATE 115200
53 static volatile uint8_t
rx_i = 0;
56 static volatile uint8_t
tx_i = 0;
71 usart_set_databits(
USART, 8);
72 usart_set_stopbits(
USART, USART_STOPBITS_1);
73 usart_set_mode(
USART, USART_MODE_TX_RX);
74 usart_set_parity(
USART, USART_PARITY_NONE);
75 usart_set_flow_control(
USART, USART_FLOWCONTROL_NONE);
78 usart_enable_rx_interrupt(
USART);
92 usart_send_blocking(
USART, c);
100 usart_wait_send_ready(
USART);
109 usart_disable_rx_interrupt(
USART);
113 usart_enable_rx_interrupt(
USART);
120 usart_enable_tx_interrupt(
USART);
123 usart_disable_tx_interrupt(
USART);
126 usart_enable_tx_interrupt(
USART);
132 if (usart_get_interrupt_source(
USART, USART_SR_TXE)) {
134 usart_disable_tx_interrupt(
USART);
141 if (usart_get_interrupt_source(
USART, USART_SR_RXNE)) {
static uint8_t rx_buffer[USART_BUFFER]
ring buffer for received data
#define USART
USART peripheral.
static uint8_t tx_buffer[USART_BUFFER]
ring buffer for data to transmit
#define USART_PIN_TX
USART transmit pin (PA9)
#define USART_IRQ
USART peripheral interrupt signal.
volatile uint8_t usart_received
how many bytes available in the received buffer since last read
#define USART_ISR
USART interrupt service routine.
#define USART_BUFFER
transmit and receive buffer sizes
#define USART_PORT_RCC
USART port peripheral clock.
static volatile uint8_t rx_i
current position of read received data
#define USART_PORT
USART port.
static volatile uint8_t tx_used
how much data needs to be transmitted
#define USART_PIN_RX
USART receive pin (PA10)
void usart_putchar_nonblocking(char c)
send character over USART (non-blocking)
static volatile uint8_t tx_i
current position if transmitted data
library for USART communication (API)
static volatile uint8_t rx_used
how much data has been received and not red
#define USART_RCC
USART peripheral clock.
void usart_flush(void)
ensure all data has been transmitted (blocking)
#define USART_BAUDRATE
serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) ...
char usart_getchar(void)
get character received over USART (blocking)
void usart_putchar_blocking(char c)
send character over USART (blocking)
void usart_setup(void)
setup USART peripheral