From a793508bc38481f820146f62dcdacfbeef1df6ee Mon Sep 17 00:00:00 2001 From: Elliot Williams Date: Sun, 25 Oct 2015 23:47:28 +0100 Subject: [PATCH] Demo of (global) buffer routine This one prints out a lot. It's nice to visualize how it's all working. --- circular_buffer.c | 38 -------------- circular_buffer.h | 40 --------------- main.c | 125 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 94 insertions(+), 109 deletions(-) delete mode 100644 circular_buffer.c delete mode 100644 circular_buffer.h diff --git a/circular_buffer.c b/circular_buffer.c deleted file mode 100644 index ff382eb..0000000 --- a/circular_buffer.c +++ /dev/null @@ -1,38 +0,0 @@ -#include "circular_buffer.h" - -enum BufferStatus bufferWrite(volatile struct Buffer *buffer, uint8_t byte){ - /* Modulo arithmetic should work in any case, - * but if your compiler doesn't optimize for powers of two, you might want to */ - uint8_t next_index = (((buffer->newest_index)+1) % BUFFER_SIZE); - - if (next_index == buffer->oldest_index){ - return BUFFER_FULL; - } - - buffer->data[buffer->newest_index] = byte; - buffer->newest_index = next_index; - return BUFFER_OK; -} - -enum BufferStatus bufferRead(volatile struct Buffer *buffer, uint8_t *byte){ - - if (buffer->newest_index == buffer->oldest_index){ - return BUFFER_EMPTY; - } - - *byte = buffer->data[buffer->oldest_index]; - buffer->oldest_index = ((buffer->oldest_index+1) % BUFFER_SIZE); - return BUFFER_OK; -} - -enum BufferStatus bufferPeek(volatile struct Buffer *buffer, uint8_t *byte){ - - - uint8_t last_index = ((BUFFER_SIZE + (buffer->newest_index) - 1) % BUFFER_SIZE); - if (buffer->newest_index == buffer->oldest_index){ - return BUFFER_EMPTY; - } - *byte = buffer->data[last_index]; - return BUFFER_OK; -} - diff --git a/circular_buffer.h b/circular_buffer.h deleted file mode 100644 index f64b2bf..0000000 --- a/circular_buffer.h +++ /dev/null @@ -1,40 +0,0 @@ -#include -#define BUFFER_SIZE 16 - - /* Use: - * - * enum BufferStatus status; - * struct Buffer buffer {{},0,0}; - * status = bufferWrite(&buffer, 'A'); - * if (status == BUFFER_FULL){ - * handle_it(); - * } - * - * uint8_t buffer_byte; - * status = bufferRead(&buffer, &buffer_byte); - * if (status == BUFFER_OK){ - * do_something(buffer_byte); - * } - * - * */ - -enum BufferStatus {BUFFER_OK, BUFFER_EMPTY, BUFFER_FULL}; - -struct Buffer { - uint8_t data[BUFFER_SIZE]; - uint8_t newest_index; - uint8_t oldest_index; -}; -// A weakness of this implementation is that all buffers are the same length, -// being defined by BUFFER_SIZE above. Meh. - -// Read and write to circular buffer -// Takes Buffer struct as first argument, -// and either data or pointer to some memory as second. -// Returns the result codes enumerated in BufferStatus - -enum BufferStatus bufferWrite(volatile struct Buffer *buffer, uint8_t byte); -enum BufferStatus bufferRead(volatile struct Buffer *buffer, uint8_t *byte); -enum BufferStatus bufferPeek(volatile struct Buffer *buffer, uint8_t *byte); - - diff --git a/main.c b/main.c index cfdc1d6..0bb4035 100644 --- a/main.c +++ b/main.c @@ -1,56 +1,119 @@ #include -#include #include #include "USART.h" -#include "circular_buffer.h" -volatile struct Buffer rx_buffer = {{}, 0, 0}; -volatile struct Buffer tx_buffer = {{}, 0, 0}; +#define BUFFER_SIZE 16 -static inline void initUSART_interrupts(void){ - UCSR0B |= (1<