|
|
|
@ -25,16 +25,17 @@ |
|
|
|
|
#include <libopencm3/stm32/gpio.h> // general purpose input output library |
|
|
|
|
#include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library |
|
|
|
|
#include <libopencm3/cm3/nvic.h> // interrupt handler |
|
|
|
|
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities |
|
|
|
|
|
|
|
|
|
#include "usart.h" // USART header and definitions |
|
|
|
|
|
|
|
|
|
/* input and output ring buffer, indexes, and available memory */ |
|
|
|
|
uint8_t rx_buffer[USART_BUFFER] = {0}; |
|
|
|
|
volatile uint8_t rx_i = 0; |
|
|
|
|
volatile uint8_t rx_used = 0; |
|
|
|
|
uint8_t tx_buffer[USART_BUFFER] = {0}; |
|
|
|
|
volatile uint8_t tx_i = 0; |
|
|
|
|
volatile uint8_t tx_used = 0; |
|
|
|
|
static uint8_t rx_buffer[USART_BUFFER] = {0}; |
|
|
|
|
static volatile uint8_t rx_i = 0; |
|
|
|
|
static volatile uint8_t rx_used = 0; |
|
|
|
|
static uint8_t tx_buffer[USART_BUFFER] = {0}; |
|
|
|
|
static volatile uint8_t tx_i = 0; |
|
|
|
|
static volatile uint8_t tx_used = 0; |
|
|
|
|
/* show the user how much data received over USART is ready */ |
|
|
|
|
volatile uint8_t usart_received = 0; // same as rx_used, but since the user can write this variable we don't rely on it
|
|
|
|
|
|
|
|
|
@ -79,7 +80,7 @@ void usart_putchar_blocking(char c) |
|
|
|
|
void usart_flush(void) |
|
|
|
|
{ |
|
|
|
|
while (tx_used) { // idle until buffer is empty
|
|
|
|
|
__asm__("wfi"); // sleep until interrupt
|
|
|
|
|
__WFI(); // sleep until interrupt
|
|
|
|
|
} |
|
|
|
|
usart_wait_send_ready(USART); // wait until transmit register is empty (transmission might not be complete)
|
|
|
|
|
} |
|
|
|
@ -88,7 +89,7 @@ void usart_flush(void) |
|
|
|
|
char usart_getchar(void) |
|
|
|
|
{ |
|
|
|
|
while (!rx_used) { // idle until data is available
|
|
|
|
|
__asm__("wfi"); // sleep until interrupt;
|
|
|
|
|
__WFI(); // sleep until interrupt;
|
|
|
|
|
} |
|
|
|
|
char to_return = rx_buffer[rx_i]; // get the next available character
|
|
|
|
|
rx_i = (rx_i+1)%sizeof(rx_buffer); // update used buffer
|
|
|
|
@ -97,11 +98,11 @@ char usart_getchar(void) |
|
|
|
|
return to_return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* put character on USART stream (non-blocking using a buffer) */ |
|
|
|
|
/* put character on USART (non-blocking until buffer is full) */ |
|
|
|
|
void usart_putchar_nonblocking(char c) |
|
|
|
|
{ |
|
|
|
|
while (tx_used>=sizeof(tx_buffer)) { // idle until buffer has some space
|
|
|
|
|
__asm__("wfi"); |
|
|
|
|
__WFI(); |
|
|
|
|
} |
|
|
|
|
tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // put character in buffer
|
|
|
|
|
tx_used++; // update used buffer
|
|
|
|
|