summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2020-10-25 16:52:57 +0100
committerxengineering <mail2xengineering@protonmail.com>2020-10-25 16:52:57 +0100
commit56c268e88a87f708de1824d87fbf7b824d4c1c2a (patch)
tree0a4ab861b2f03c2fde1de8d7a3b61d2603b4d503
parent58d3787ca563b7d6a2cb4e0a1ab7fdde67fd5557 (diff)
downloadstm32f103c8-examples-56c268e88a87f708de1824d87fbf7b824d4c1c2a.tar
stm32f103c8-examples-56c268e88a87f708de1824d87fbf7b824d4c1c2a.tar.zst
stm32f103c8-examples-56c268e88a87f708de1824d87fbf7b824d4c1c2a.zip
Remove any Heap Allocation
-rw-r--r--libraries/fifo.c61
-rw-r--r--libraries/fifo.h61
-rw-r--r--libraries/usart.c12
-rw-r--r--libraries/usart.h5
-rw-r--r--usart_irq/main.c9
5 files changed, 69 insertions, 79 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
diff --git a/usart_irq/main.c b/usart_irq/main.c
index c3e7966..8c79471 100644
--- a/usart_irq/main.c
+++ b/usart_irq/main.c
@@ -11,6 +11,8 @@
#define DELAY 1000000
+#define RX_BUFFER_SIZE 50
+#define TX_BUFFER_SIZE 50
void clock_init(void);
@@ -24,13 +26,16 @@ USART usart3;
int main(void)
{
+ uint8_t rx_buffer[RX_BUFFER_SIZE];
+ uint8_t tx_buffer[TX_BUFFER_SIZE];
+
clock_init();
gpio_init();
- usart_init(&usart3);
+ usart_init(&usart3, rx_buffer, RX_BUFFER_SIZE, tx_buffer, TX_BUFFER_SIZE);
gpio_set(GPIOC, GPIO13);
- char welcome[] = "Hallo Freunde!\r";
+ char welcome[] = "Hello world!\r";
usart_write(&usart3, welcome);
uint8_t buff;