summaryrefslogtreecommitdiff
path: root/libraries/fifo.h
blob: c56d410ef51d2acfa8fb6794f9b2b654e5eec1a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


/* 
    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
*/


#ifndef FIFO_H
#define FIFO_H


#include <stdint.h>
#include <stdlib.h>


typedef struct FIFO{
    uint8_t *buffer_ptr;
    uint16_t read_index;
    uint16_t write_index;
    uint16_t bufferlength;
} FIFO;


void fifo_init(FIFO *fifo, uint16_t bufferlength);
void fifo_deinit(FIFO *fifo);

uint16_t fifo_get_buffersize(FIFO *fifo);

uint16_t fifo_bytes_available(FIFO *fifo);
uint16_t fifo_lines_available(FIFO *fifo);

void fifo_move_read_index(FIFO *fifo);
void fifo_move_write_index(FIFO *fifo);

void fifo_get_byte(FIFO *fifo, uint8_t *data_sink);
void fifo_put_byte(FIFO *fifo, uint8_t *data_source);


#endif /* FIFO_H */