diff --git a/lib/usart.c b/lib/usart.c index 4c862a8..1897df2 100644 --- a/lib/usart.c +++ b/lib/usart.c @@ -25,16 +25,17 @@ #include // general purpose input output library #include // universal synchronous asynchronous receiver transmitter library #include // interrupt handler +#include // 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 diff --git a/lib/usart.h b/lib/usart.h index 0fd167e..8911e59 100644 --- a/lib/usart.h +++ b/lib/usart.h @@ -25,8 +25,8 @@ /* serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */ #define BAUD 115200 -/* USART RX and TX buffer sizes */ -#define USART_BUFFER 64 +/* RX and TX buffer sizes */ +#define USART_BUFFER 128 /* show the user how much received is available */ extern volatile uint8_t usart_received;