-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfifo.h
76 lines (58 loc) · 1.42 KB
/
fifo.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef _FIFO_H_
#define _FIFO_H_
#include <avr/io.h>
#include <avr/interrupt.h>
//#define _syster
typedef struct
{
uint8_t volatile count; // # Zeichen im Puffer
uint8_t size; // Puffer-Größe
uint16_t *pread; // Lesezeiger
uint16_t *pwrite; // Schreibzeiger
uint8_t read2end, write2end; // # Zeichen bis zum Überlauf Lese-/Schreibzeiger
} fifo_t;
extern void fifo_init (fifo_t*, uint16_t* buf, const uint8_t size);
extern uint16_t fifo_put (fifo_t*, const uint16_t data);
extern uint16_t fifo_get_wait (fifo_t*);
extern uint16_t fifo_get_nowait (fifo_t*);
static inline uint16_t
_inline_fifo_put (fifo_t *f, const uint16_t data)
{
if (f->count >= f->size)
return 0;
uint16_t * pwrite = f->pwrite;
*(pwrite++) = data;
uint8_t write2end = f->write2end;
if (--write2end == 0)
{
write2end = f->size;
pwrite -= write2end;
}
f->write2end = write2end;
f->pwrite = pwrite;
uint8_t sreg = SREG;
cli();
f->count++;
SREG = sreg;
return 1;
}
static inline uint16_t
_inline_fifo_get (fifo_t *f)
{
uint16_t *pread = f->pread;
uint16_t data = *(pread++);
uint16_t read2end = f->read2end;
if (--read2end == 0)
{
read2end = f->size;
pread -= read2end;
}
f->pread = pread;
f->read2end = read2end;
uint8_t sreg = SREG;
cli();
f->count--;
SREG = sreg;
return data;
}
#endif /* FIFO_H */