Project

General

Profile

Download (3.05 KB) Statistics
| Branch: | Tag: | Revision:
1 32985a29 laforge
/* 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 f57b548d (no author)
21 f1202753 (no author)
#include "fifo.h"
22 f57b548d (no author)
23 8a863d16 (no author)
#include <errno.h>
24
#include <string.h>
25
26 f57b548d (no author)
#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 8aaa856c (no author)
		fifo->irq |= FIFO_IRQ_LO;
45 f57b548d (no author)
	else
46 8aaa856c (no author)
		fifo->irq &= FIFO_IRQ_LO;
47 f57b548d (no author)
48
	if (fifo->size - avail >= fifo->watermark)
49 8aaa856c (no author)
		fifo->irq |= FIFO_IRQ_HI;
50 f57b548d (no author)
	else
51 8aaa856c (no author)
		fifo->irq &= FIFO_IRQ_HI;
52 f57b548d (no author)
}
53
54
void fifo_check_raise_int(struct fifo *fifo)
55
{
56
	if (fifo->irq & fifo->irq_en)
57 8a863d16 (no author)
		fifo->callback(fifo, fifo->irq, fifo->cb_data);
58 f57b548d (no author)
}
59
60
61 373c172a Harald Welte
uint16_t fifo_data_put(struct fifo *fifo, uint16_t len, uint8_t *data)
62 f57b548d (no author)
{
63
	if (len > fifo_available(fifo)) {
64 8a863d16 (no author)
		len = fifo_available(fifo);
65 f57b548d (no author)
		fifo->irq |= FIFO_IRQ_OFLOW;
66
	}
67
68
	if (len + fifo->producer <= fifo->size) {
69
		/* easy case */
70 caf50003 (no author)
		memcpy(&fifo->data[fifo->producer], data, len);
71 f57b548d (no author)
		fifo->producer += len;
72
	} else {
73
		/* difficult: wrap around */
74 373c172a Harald Welte
		uint16_t chunk_len;
75 f57b548d (no author)
76
		chunk_len = fifo->size - fifo->producer;
77 caf50003 (no author)
		memcpy(&fifo->data[fifo->producer], data, chunk_len);
78 f57b548d (no author)
79 caf50003 (no author)
		memcpy(&fifo->data[0], data + chunk_len, len - chunk_len);
80 f57b548d (no author)
		fifo->producer = len - chunk_len;
81
	}
82
83
	fifo_check_water(fifo);
84
85
	return len;
86
}
87
88
89 373c172a Harald Welte
uint16_t fifo_data_get(struct fifo *fifo, uint16_t len, uint8_t *data)
90 f57b548d (no author)
{
91 373c172a Harald Welte
	uint16_t avail = fifo_available(fifo);
92 f57b548d (no author)
93
	if (avail < len)
94
		len = avail;
95
96
	if (fifo->producer > fifo->consumer) {
97
		/* easy case */
98 caf50003 (no author)
		memcpy(data, &fifo->data[fifo->consumer], len);
99 f57b548d (no author)
	} else {
100
		/* difficult case: wrap */
101 373c172a Harald Welte
		uint16_t chunk_len = fifo->size - fifo->consumer;
102 caf50003 (no author)
		memcpy(data, &fifo->data[fifo->consumer], chunk_len);
103
		memcpy(data+chunk_len, &fifo->data[0], len - chunk_len);
104 f57b548d (no author)
	}
105
106
	fifo_check_water(fifo);
107
108
	return len;
109
}
110
111 373c172a Harald Welte
int fifo_init(struct fifo *fifo, uint16_t size, 
112
	      void (*cb)(struct fifo *fifo, uint8_t event, void *data), void *cb_data)
113 f57b548d (no author)
{
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 8a863d16 (no author)
	fifo->callback = cb;
122 f57b548d (no author)
	fifo->cb_data = cb_data;
123
124
	return 0;
125
}
Add picture from clipboard (Maximum size: 48.8 MB)