summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
Diffstat (limited to 'libraries')
-rw-r--r--libraries/fifo.c61
-rw-r--r--libraries/fifo.h61
-rw-r--r--libraries/usart.c12
-rw-r--r--libraries/usart.h5
4 files changed, 62 insertions, 77 deletions
diff --git a/libraries/fifo.c b/libraries/fifo.c
index faa9fc4..8c5ad8b 100644
--- a/libraries/fifo.c
+++ b/libraries/fifo.c
@@ -3,64 +3,59 @@
#include "fifo.h"
-void fifo_init(FIFO *fifo, uint16_t bufferlength)
+void fifo_init(FIFO *fifo, uint8_t *buffer, uint16_t bufferlength)
{
- fifo->buffer_ptr = calloc(bufferlength, sizeof(uint8_t));
- fifo->read_index = 0;
- fifo->write_index = 0;
- fifo->bufferlength = bufferlength;
-}
-
-void fifo_deinit(FIFO *fifo)
-{
- free(fifo->buffer_ptr);
+ fifo->buffer_ptr = buffer;
+ fifo->bufferlength = bufferlength;
+ fifo->read_index = 0;
+ fifo->write_index = 0;
}
uint16_t fifo_get_buffersize(FIFO *fifo)
{
- return fifo->bufferlength;
+ return fifo->bufferlength;
}
uint16_t fifo_bytes_available(FIFO *fifo)
{
- if (fifo->write_index > fifo->read_index){
- return (fifo->write_index - fifo->read_index);
- }
- else if (fifo->write_index < fifo->read_index){
- return (fifo_get_buffersize(fifo) - (fifo->read_index - fifo->write_index));
- }
- else { // read_index == write_index
- return 0;
- }
+ if (fifo->write_index > fifo->read_index){
+ return (fifo->write_index - fifo->read_index);
+ }
+ else if (fifo->write_index < fifo->read_index){
+ return (fifo_get_buffersize(fifo) - (fifo->read_index - fifo->write_index));
+ }
+ else { // read_index == write_index
+ return 0;
+ }
}
//uint16_t fifo_lines_available(FIFO *fifo){}
void fifo_move_read_index(FIFO *fifo)
{
- fifo->read_index++;
- fifo->read_index %= fifo_get_buffersize(fifo);
+ fifo->read_index++;
+ fifo->read_index %= fifo_get_buffersize(fifo);
}
void fifo_move_write_index(FIFO *fifo)
{
- fifo->write_index++;
- fifo->write_index %= fifo_get_buffersize(fifo);
+ fifo->write_index++;
+ fifo->write_index %= fifo_get_buffersize(fifo);
}
void fifo_get_byte(FIFO *fifo, uint8_t *data_sink)
{
- if(fifo_bytes_available(fifo)){
- *data_sink = fifo->buffer_ptr[fifo->read_index];
- fifo_move_read_index(fifo);
- }
+ if(fifo_bytes_available(fifo)){
+ *data_sink = fifo->buffer_ptr[fifo->read_index];
+ fifo_move_read_index(fifo);
+ }
}
void fifo_put_byte(FIFO *fifo, uint8_t *data_source)
{
- fifo->buffer_ptr[fifo->write_index] = *data_source;
- fifo_move_write_index(fifo);
- if(fifo->read_index == fifo->write_index){ // buffer overflow case
- fifo_move_read_index(fifo);
- }
+ fifo->buffer_ptr[fifo->write_index] = *data_source;
+ fifo_move_write_index(fifo);
+ if(fifo->read_index == fifo->write_index){ // buffer overflow case
+ fifo_move_read_index(fifo);
+ }
}
diff --git a/libraries/fifo.h b/libraries/fifo.h
index c56d410..700d2ec 100644
--- a/libraries/fifo.h
+++ b/libraries/fifo.h
@@ -1,30 +1,30 @@
-/*
- FIFO Ring Buffer uint8_t array ring_buffer (example length is 8)
- with data (x) and read and write index:
-
- --- --- --- --- --- --- --- ---
- | | | x | x | x | | | |
- --- --- --- --- --- --- --- ---
- ^ ^
- | |
- read_index |
- write_index
-
- --- --- --- --- --- --- --- ---
- | x | x | | | | x | x | x |
- --- --- --- --- --- --- --- ---
- ^ ^
- | |
- write_index |
- read_index
-
- - input data goes to position of write_index
- - output data comes from read_index position
- - after input/output operation the according index has to be modified
- - according to the idea of ring buffers the write_index could be
- smaller than the read_index
+/*
+ FIFO Ring Buffer uint8_t array ring_buffer (example length is 8)
+ with data (x) and read and write index:
+
+ --- --- --- --- --- --- --- ---
+ | | | x | x | x | | | |
+ --- --- --- --- --- --- --- ---
+ ^ ^
+ | |
+ read_index |
+ write_index
+
+ --- --- --- --- --- --- --- ---
+ | x | x | | | | x | x | x |
+ --- --- --- --- --- --- --- ---
+ ^ ^
+ | |
+ write_index |
+ read_index
+
+ - input data goes to position of write_index
+ - output data comes from read_index position
+ - after input/output operation the according index has to be modified
+ - according to the idea of ring buffers the write_index could be
+ smaller than the read_index
*/
@@ -37,15 +37,14 @@
typedef struct FIFO{
- uint8_t *buffer_ptr;
- uint16_t read_index;
- uint16_t write_index;
- uint16_t bufferlength;
+ uint8_t *buffer_ptr;
+ uint16_t bufferlength;
+ uint16_t read_index;
+ uint16_t write_index;
} FIFO;
-void fifo_init(FIFO *fifo, uint16_t bufferlength);
-void fifo_deinit(FIFO *fifo);
+void fifo_init(FIFO *fifo, uint8_t *buffer, uint16_t bufferlength);
uint16_t fifo_get_buffersize(FIFO *fifo);
diff --git a/libraries/usart.c b/libraries/usart.c
index 61de99c..67dd30a 100644
--- a/libraries/usart.c
+++ b/libraries/usart.c
@@ -8,7 +8,7 @@ extern USART usart3;
// init functions
-void usart_init(USART *usart)
+void usart_init(USART *usart, uint8_t *rx_buffer, uint16_t rx_buffersize, uint8_t *tx_buffer, uint16_t tx_buffersize)
{
// clock config
rcc_periph_clock_enable(RCC_GPIOB); // for USART3
@@ -32,14 +32,8 @@ void usart_init(USART *usart)
usart_enable(USART3);
// fifo init
- fifo_init(&usart->rx_fifo, USART_BUFFER_SIZE);
- fifo_init(&usart->tx_fifo, USART_BUFFER_SIZE);
-}
-
-void usart_deinit(USART *usart)
-{
- fifo_deinit(&usart->rx_fifo);
- fifo_deinit(&usart->tx_fifo);
+ fifo_init(&usart->rx_fifo, rx_buffer, rx_buffersize);
+ fifo_init(&usart->tx_fifo, tx_buffer, tx_buffersize);
}
diff --git a/libraries/usart.h b/libraries/usart.h
index cac971b..8e0d98c 100644
--- a/libraries/usart.h
+++ b/libraries/usart.h
@@ -13,8 +13,6 @@
#include "fifo.h"
-#define USART_BUFFER_SIZE 30
-
typedef struct USART{
@@ -26,8 +24,7 @@ typedef struct USART{
// init functions
-void usart_init(USART *usart);
-void usart_deinit(USART *usart);
+void usart_init(USART *usart, uint8_t *rx_buffer, uint16_t rx_buffersize, uint8_t *tx_buffer, uint16_t tx_buffersize);
// write functions