Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
886 | killagreg | 1 | #ifndef _FIFO_H_ |
2 | #define _FIFO_H_ |
||
3 | |||
4 | #include <avr/io.h> |
||
5 | #include <avr/interrupt.h> |
||
6 | |||
7 | // the fifo object |
||
8 | typedef struct |
||
9 | { |
||
10 | uint8_t volatile count; // # number of characters in FIFO |
||
11 | uint8_t size; // buffer size |
||
12 | uint8_t *pread; // read pointer |
||
13 | uint8_t *pwrite; // write pointer |
||
14 | uint8_t read2end, write2end; // number of characters for buffer overflow for read/write pointers |
||
15 | } fifo_t; |
||
16 | |||
17 | /* |
||
18 | The initialization of the FIFO sets the read/write pointers etc.. |
||
19 | The FIFO uses the buffer 'buf' which byte length must 'size'. |
||
20 | */ |
||
21 | extern void fifo_init (fifo_t*, uint8_t* buf, const uint8_t size); |
||
22 | |||
23 | /* |
||
24 | Puts a byte into the FIFO. Returns 1 on success ans 0 in case of FIFO overflow. |
||
25 | */ |
||
26 | extern uint8_t fifo_put (fifo_t*, const uint8_t data); |
||
27 | |||
28 | /* |
||
29 | Get the next byte out of the FIFO. If the FIFO is empty the function blocks |
||
30 | until the next byte is put into the FIFO. |
||
31 | */ |
||
32 | extern uint8_t fifo_get_wait (fifo_t*); |
||
33 | |||
34 | /* |
||
35 | Get the next byte from the FIFO as int. Returns -1 if the FIFO is empty. |
||
36 | */ |
||
37 | extern int16_t fifo_get_nowait (fifo_t*); |
||
38 | |||
39 | |||
40 | /* |
||
41 | The same like fifo_put |
||
42 | */ |
||
43 | static inline uint8_t _inline_fifo_put (fifo_t *f, const uint8_t data) |
||
44 | { |
||
45 | if (f->count >= f->size) |
||
46 | return 0; |
||
47 | |||
48 | uint8_t * pwrite = f->pwrite; |
||
49 | |||
50 | *(pwrite++) = data; |
||
51 | |||
52 | uint8_t write2end = f->write2end; |
||
53 | |||
54 | if (--write2end == 0) |
||
55 | { |
||
56 | write2end = f->size; |
||
57 | pwrite -= write2end; |
||
58 | } |
||
59 | |||
60 | f->write2end = write2end; |
||
61 | f->pwrite = pwrite; |
||
62 | |||
63 | uint8_t sreg = SREG; |
||
64 | cli(); |
||
65 | f->count++; |
||
66 | SREG = sreg; |
||
67 | |||
68 | return 1; |
||
69 | } |
||
70 | |||
71 | /* |
||
72 | Get the next byte from FIFO. Before this functionis called |
||
73 | it must be checked that there is a byte in the FIFO to get. |
||
74 | */ |
||
75 | static inline uint8_t _inline_fifo_get (fifo_t *f) |
||
76 | { |
||
77 | uint8_t *pread = f->pread; |
||
78 | uint8_t data = *(pread++); |
||
79 | uint8_t read2end = f->read2end; |
||
80 | |||
81 | if (--read2end == 0) |
||
82 | { |
||
83 | read2end = f->size; |
||
84 | pread -= read2end; |
||
85 | } |
||
86 | |||
87 | f->pread = pread; |
||
88 | f->read2end = read2end; |
||
89 | |||
90 | uint8_t sreg = SREG; |
||
91 | cli(); |
||
92 | f->count--; |
||
93 | SREG = sreg; |
||
94 | |||
95 | return data; |
||
96 | } |
||
97 | |||
98 | #endif /* _FIFO_H_ */ |