Subversion Repositories NaviCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ingob 1
#include "fifo.h"
2
 
3
u8 fifo_init (fifo_t *f, u8 *buffer, const u8 size)
4
{
5
        if(f == NULL) return(0);
6
        f->buffer = buffer;
7
        f->count = 0;
8
        f->pread = buffer;
9
        f->pwrite = buffer;
10
        f->size = size;
11
        return(1);
12
}
13
 
14
u8 fifo_put (fifo_t *f, const u8 data)
15
{
16
        if (f->count >= f->size) return(0);     // return 0 in case of FIFO overflow.
17
 
18
        u8 * pwrite = f->pwrite;
19
 
20
        *(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 
22
        f->pwrite = pwrite;
23
        f->count++;
24
        return 1;
25
}
26
 
27
u8 fifo_get (fifo_t *f, u8 *pdata)
28
{
29
        if((f == NULL) || (pdata == NULL)) return(0);
30
        if(!f->count) return(0);
31
 
32
        u8 *pread = f->pread;
33
        *pdata = *(pread++);
34
        if(pread >= f->buffer + f->size) pread = f->buffer; // start at the begining after reaching the end 
35
        f->pread = pread;
36
        f->count--;
37
        return(1);
38
}
39
 
40
u8 fifo_get_wait (fifo_t *f, u8 *pdata)
41
{
42
        while (!f->count);
43
 
44
        return fifo_get(f, pdata);
45
}