Rev 41 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 41 | Rev 153 | ||
---|---|---|---|
1 | #include "fifo.h" |
1 | #include "fifo.h" |
2 | 2 | ||
3 | u8 fifo_init (fifo_t *f, u8 *buffer, const u8 size) |
3 | u8 fifo_init (fifo_t *f, u8 *buffer, const u16 size) |
4 | { |
4 | { |
5 | if(f == NULL) return(0); |
5 | if(f == NULL) return(0); |
6 | f->buffer = buffer; |
6 | f->buffer = buffer; |
7 | f->count = 0; |
- | |
8 | f->pread = buffer; |
- | |
9 | f->pwrite = buffer; |
7 | f->size = size; |
10 | f->size = size; |
8 | fifo_purge(f); |
11 | return(1); |
9 | return(1); |
12 | } |
10 | } |
13 | 11 | ||
14 | u8 fifo_put (fifo_t *f, const u8 data) |
12 | u8 fifo_put (fifo_t *f, const u8 data) |
15 | { |
13 | { |
16 | if (f->count >= f->size) return(0); // return 0 in case of FIFO overflow. |
14 | if (f->count >= f->size) return(0); // return 0 in case of FIFO overflow. |
17 | 15 | ||
18 | u8 * pwrite = f->pwrite; |
16 | u8 * pwrite = f->pwrite; |
19 | 17 | ||
20 | *(pwrite++) = data; // copy data byte to buffer |
18 | *(pwrite++) = data; // copy data byte to buffer |
21 | if(pwrite >= f->buffer + f->size) pwrite = f->buffer; // start at the begining after reaching the end |
19 | if(pwrite >= f->buffer + f->size) pwrite = f->buffer; // start at the begining after reaching the end |
22 | f->pwrite = pwrite; |
20 | f->pwrite = pwrite; |
23 | f->count++; |
21 | f->count++; |
24 | return 1; |
22 | return 1; |
25 | } |
23 | } |
26 | 24 | ||
27 | u8 fifo_get (fifo_t *f, u8 *pdata) |
25 | u8 fifo_get (fifo_t *f, u8 *pdata) |
28 | { |
26 | { |
29 | if((f == NULL) || (pdata == NULL)) return(0); |
27 | if((f == NULL) || (pdata == NULL)) return(0); |
30 | if(!f->count) return(0); |
28 | if(!f->count) return(0); |
31 | 29 | ||
32 | u8 *pread = f->pread; |
30 | u8 *pread = f->pread; |
33 | *pdata = *(pread++); |
31 | *pdata = *(pread++); |
34 | if(pread >= f->buffer + f->size) pread = f->buffer; // start at the begining after reaching the end |
32 | if(pread >= f->buffer + f->size) pread = f->buffer; // start at the begining after reaching the end |
35 | f->pread = pread; |
33 | f->pread = pread; |
36 | f->count--; |
34 | f->count--; |
37 | return(1); |
35 | return(1); |
38 | } |
36 | } |
39 | 37 | ||
40 | u8 fifo_get_wait (fifo_t *f, u8 *pdata) |
38 | u8 fifo_get_wait (fifo_t *f, u8 *pdata) |
41 | { |
39 | { |
42 | while (!f->count); |
40 | while (!f->count); |
43 | 41 | ||
44 | return fifo_get(f, pdata); |
42 | return fifo_get(f, pdata); |
45 | } |
43 | } |
- | 44 | ||
- | 45 | void fifo_purge(fifo_t* f) |
|
- | 46 | { |
|
- | 47 | if((f == NULL)) return; |
|
- | 48 | f->count = 0; |
|
- | 49 | f->pread = f->buffer; |
|
- | 50 | f->pwrite = f->buffer; |
|
- | 51 | return; |
|
- | 52 | } |
|
46 | 53 |