summaryrefslogtreecommitdiff
path: root/libraries/fifo.c
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2020-10-25 12:49:35 +0100
committerxengineering <mail2xengineering@protonmail.com>2020-10-25 12:49:35 +0100
commit9435a85605d7efc75642491c85b0942bd558fcec (patch)
tree56e0c8f5530cac4e5e09aa174edaa170a0120726 /libraries/fifo.c
parentdb0a19edd8c4cd62c6784d8bb1d91c61a6447a2a (diff)
downloadstm32f103c8-examples-9435a85605d7efc75642491c85b0942bd558fcec.tar
stm32f103c8-examples-9435a85605d7efc75642491c85b0942bd558fcec.tar.zst
stm32f103c8-examples-9435a85605d7efc75642491c85b0942bd558fcec.zip
Add usart_irq Code
Diffstat (limited to 'libraries/fifo.c')
-rw-r--r--libraries/fifo.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/libraries/fifo.c b/libraries/fifo.c
new file mode 100644
index 0000000..faa9fc4
--- /dev/null
+++ b/libraries/fifo.c
@@ -0,0 +1,66 @@
+
+
+#include "fifo.h"
+
+
+void fifo_init(FIFO *fifo, 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);
+}
+
+uint16_t fifo_get_buffersize(FIFO *fifo)
+{
+ 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;
+ }
+}
+
+//uint16_t fifo_lines_available(FIFO *fifo){}
+
+void fifo_move_read_index(FIFO *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);
+}
+
+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);
+ }
+}
+
+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);
+ }
+}