printf_fifo.c
4.57 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "os_config.h"
#include "c_def.h"
#include "debug.h"
//#include "oem.h"
//#include "prj.h"
#include "printf_fifo.h"
void printf_fifo_init( PRINTF_FIFO *fifo, U8 *buf, U32 bufsize,
FIFO_CALLBACK pput, FIFO_CALLBACK pget, FIFO_CALLBACK pempty, FIFO_CALLBACK pfull,
FIFO_CALLBACK phalfEmpty, FIFO_CALLBACK phalfFull )
{
fifo->buf = buf;
fifo->maxsize = bufsize;
fifo->head = fifo->tail = fifo->buf;
fifo->size = 0;
fifo->putCallback = pput;
fifo->getCallback = pget;
fifo->emptyCallback = pempty;
fifo->fullCallback = pfull;
fifo->halfEmptyCallbk = phalfEmpty;
fifo->halfFullCallbk = phalfFull;
}
U32 fifo_getDataSize( PRINTF_FIFO *fifo )
{
return fifo->size;
}
U32 fifo_getEmptySpace( PRINTF_FIFO *fifo )
{
return (fifo->maxsize - fifo->size );
}
//static U32 fifoPut( PRINTF_FIFO *fifo, U8 *src, U32 size, BOOL isr )
U32 fifoPut( PRINTF_FIFO *fifo, U8 *src, U32 size, BOOL isr )
{
TX_INTERRUPT_SAVE_AREA;
/*
U32 sz = size;
U8 *tail = fifo->tail;
U8 *fifoend = fifo->buf + fifo->maxsize;
DBG_Assert( size > 0 );
DBG_Assert( printf_fifo_getEmptySpace( fifo ) >= size );
*/
U32 sz;
U8 *tail = fifo->tail;
U8 *fifoend = fifo->buf + fifo->maxsize;
DBG_Assert( size > 0 );
if( size == 0 ) return 0;
sz = fifo->maxsize - fifo->size;
if( size >= sz ) size = sz;
else sz = size;
if( size == 0 ) return 0; // no space
while ( size-- > 0 ) {
*tail ++ = *src ++;
if( tail >= fifoend ) {
tail = fifo->buf;
}
}
if( isr ) {
fifo->tail = tail;
fifo->size += sz;
}
else {
TX_DISABLE;
fifo->tail = tail;
fifo->size += sz;
TX_RESTORE;
}
#if 0//fifo->halfFullCallbk is NULL
if( fifo->size >= fifo->maxsize/2 )
{
if( fifo->halfFullCallbk != NULL )
{
fifo->halfFullCallbk( fifo );
}
}
#endif
if( fifo->putCallback != NULL )
{
fifo->putCallback( fifo );
}
return sz;
}
U32 fifo_put( PRINTF_FIFO *fifo, U8 *src, U32 size )
{
return fifoPut( fifo, src, size, FALSE );
}
U32 fifo_iput( PRINTF_FIFO *fifo, U8 *src, U32 size )
{
//scanf_rxPutCharCallback
return fifoPut( fifo, src, size, TRUE );
}
//static U32 fifoGet( PRINTF_FIFO *fifo, U8 *dst, U32 size, BOOL isr )
U32 fifoGet( PRINTF_FIFO *fifo, U8 *dst, U32 size, BOOL isr )
{
TX_INTERRUPT_SAVE_AREA
/*
U32 sz = size;
U8 *head = fifo->head;
U8 *fifoend = fifo->buf + fifo->maxsize;
DBG_Assert( size > 0 );
DBG_Assert( fifo_getDataSize( fifo ) >= size );
*/
U32 sz;
U8 *head = fifo->head;
U8 *fifoend = fifo->buf + fifo->maxsize;
DBG_Assert( size > 0 );
if( size == 0 ) return 0;
sz = fifo->size;
if( size >= sz ) size = sz;
else sz = size;
if( size == 0 ) return 0; // empty
while ( size-- > 0 ) {
*dst ++ = *head ++;
if( head >= fifoend ) {
head = fifo->buf;
}
}
if( isr )
{
//asm("break 1,1");
fifo->head = head;
fifo->size -= sz;
}
else
{
TX_DISABLE;
fifo->head = head;
fifo->size -= sz;
TX_RESTORE;
}
#if 0
if( fifo->size <= fifo->maxsize/2 )
{
#if 0
if( fifo->halfEmptyCallbk != NULL )
{
//set the "UART_TX_EVENT_FIFO_EMPTY" event.
//fifoHalfEmptyCallback
fifo->halfEmptyCallbk( fifo );
}
#else
#ifdef UART_TX_EVENT_FIFO_EMPTY_ENABLE
//fifoHalfEmptyCallback
DBG_Assert(FALSE);
UART_TX_EVENT_FIFO_EMPTY_SET;
#endif
#endif
}
#endif
if( fifo->getCallback!= NULL )
{
fifo->getCallback( fifo );
}
return sz;
}
U32 fifo_get( PRINTF_FIFO *fifo, U8 *dst, U32 size )
{
return fifoGet( fifo, dst, size, FALSE );
}
U32 fifo_iget( PRINTF_FIFO *fifo, U8 *dst, U32 size )
{
return fifoGet( fifo, dst, size, TRUE );
}
void fifo_clear( PRINTF_FIFO *fifo )
{
TX_INTERRUPT_SAVE_AREA;
TX_DISABLE;
fifo->head = fifo->tail = fifo->buf;
fifo->size = 0;
TX_RESTORE;
}
U32 fifo_peekChar( PRINTF_FIFO *fifo, U8 cha )
{
U8 *head = fifo->head;
int size = fifo->size;
U8 *fifoend = fifo->buf + fifo->maxsize;
U32 index = 0;
while( size -- ) {
if( cha == *head ) {
return ++index;
}
++index;
if( ++head >= fifoend ) head = fifo->buf;
}
return 0;
}
/*
U32 printf_fifo_idiscardData( PRINTF_FIFO *fifo, U32 size )
{
U8 *head = fifo->head;
U8 *fifoend = fifo->buf + fifo->maxsize;
DBG_Assert( size <= fifo->size );
fifo->head += size;
if( fifo->head >= fifoend ) {
fifo->head -= fifo->maxsize;
}
fifo->size -= size;
return size;
}
U32 printf_fifo_discardData( PRINTF_FIFO *fifo, U32 size )
{
TX_INTERRUPT_SAVE_AREA
U8 *head = fifo->head;
U8 *fifoend = fifo->buf + fifo->maxsize;
DBG_Assert( size <= fifo->size );
TX_DISABLE;
fifo->head += size;
if( fifo->head >= fifoend ) {
fifo->head -= fifo->maxsize;
}
fifo->size -= size;
TX_RESTORE;
return size;
}
*/