Project

General

Profile

Download (3.05 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Implementation of a virtual FIFO for OpenPCD
2
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
3
 *
4
 *  This program is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by 
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program; if not, write to the Free Software
16
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 */
19

    
20

    
21
#include "fifo.h"
22

    
23
#include <errno.h>
24
#include <string.h>
25

    
26
#define FIFO_IRQ_LO	0x01
27
#define FIFO_IRQ_HI	0x02
28
#define FIFO_IRQ_OFLOW	0x04
29

    
30
/* returns number of data bytes present in the fifo */
31
int fifo_available(struct fifo *fifo)
32
{
33
	if (fifo->producer > fifo->consumer)
34
		return fifo->producer - fifo->consumer;
35
	else
36
		return (fifo->size - fifo->consumer) + fifo->producer;
37
}
38

    
39
void fifo_check_water(struct fifo *fifo)
40
{
41
	int avail = fifo_available(fifo);
42

    
43
	if (avail <= fifo->watermark)
44
		fifo->irq |= FIFO_IRQ_LO;
45
	else
46
		fifo->irq &= FIFO_IRQ_LO;
47

    
48
	if (fifo->size - avail >= fifo->watermark)
49
		fifo->irq |= FIFO_IRQ_HI;
50
	else
51
		fifo->irq &= FIFO_IRQ_HI;
52
}
53

    
54
void fifo_check_raise_int(struct fifo *fifo)
55
{
56
	if (fifo->irq & fifo->irq_en)
57
		fifo->callback(fifo, fifo->irq, fifo->cb_data);
58
}
59

    
60

    
61
uint16_t fifo_data_put(struct fifo *fifo, uint16_t len, uint8_t *data)
62
{
63
	if (len > fifo_available(fifo)) {
64
		len = fifo_available(fifo);
65
		fifo->irq |= FIFO_IRQ_OFLOW;
66
	}
67

    
68
	if (len + fifo->producer <= fifo->size) {
69
		/* easy case */
70
		memcpy(&fifo->data[fifo->producer], data, len);
71
		fifo->producer += len;
72
	} else {
73
		/* difficult: wrap around */
74
		uint16_t chunk_len;
75

    
76
		chunk_len = fifo->size - fifo->producer;
77
		memcpy(&fifo->data[fifo->producer], data, chunk_len);
78

    
79
		memcpy(&fifo->data[0], data + chunk_len, len - chunk_len);
80
		fifo->producer = len - chunk_len;
81
	}
82

    
83
	fifo_check_water(fifo);
84

    
85
	return len;
86
}
87

    
88

    
89
uint16_t fifo_data_get(struct fifo *fifo, uint16_t len, uint8_t *data)
90
{
91
	uint16_t avail = fifo_available(fifo);
92

    
93
	if (avail < len)
94
		len = avail;
95

    
96
	if (fifo->producer > fifo->consumer) {
97
		/* easy case */
98
		memcpy(data, &fifo->data[fifo->consumer], len);
99
	} else {
100
		/* difficult case: wrap */
101
		uint16_t chunk_len = fifo->size - fifo->consumer;
102
		memcpy(data, &fifo->data[fifo->consumer], chunk_len);
103
		memcpy(data+chunk_len, &fifo->data[0], len - chunk_len);
104
	}
105

    
106
	fifo_check_water(fifo);
107

    
108
	return len;
109
}
110

    
111
int fifo_init(struct fifo *fifo, uint16_t size, 
112
	      void (*cb)(struct fifo *fifo, uint8_t event, void *data), void *cb_data)
113
{
114
	if (size > sizeof(fifo->data))
115
		return -EINVAL;
116

    
117
	memset(fifo->data, 0, sizeof(fifo->data));
118
	fifo->size = size;
119
	fifo->producer = fifo->consumer = 0;
120
	fifo->watermark = 0;
121
	fifo->callback = cb;
122
	fifo->cb_data = cb_data;
123

    
124
	return 0;
125
}
126

    
(5-5/40)
Add picture from clipboard (Maximum size: 48.8 MB)