OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [serial/] [v2_0/] [src/] [common/] [serial.c] - Blame information for rev 308

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      io/serial/common/serial.c
4
//
5
//      High level serial driver
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):    gthomas
44
// Contributors: gthomas, grante, jlarmour, jskov
45
// Date:         1999-02-04
46
// Purpose:      Top level serial driver
47
// Description: 
48
//
49
//####DESCRIPTIONEND####
50
//
51
//==========================================================================
52
 
53
#include <pkgconf/io.h>
54
#include <pkgconf/io_serial.h>
55
 
56
#include <cyg/io/io.h>
57
#include <cyg/io/devtab.h>
58
#include <cyg/io/serial.h>
59
#include <cyg/infra/cyg_ass.h>      // assertion support
60
#include <cyg/infra/diag.h>         // diagnostic output
61
 
62
static Cyg_ErrNo serial_write(cyg_io_handle_t handle, const void *buf, cyg_uint32 *len);
63
static Cyg_ErrNo serial_read(cyg_io_handle_t handle, void *buf, cyg_uint32 *len);
64
static Cyg_ErrNo serial_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info);
65
static Cyg_ErrNo serial_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *buf, cyg_uint32 *len);
66
static Cyg_ErrNo serial_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *buf, cyg_uint32 *len);
67
 
68
DEVIO_TABLE(cyg_io_serial_devio,
69
            serial_write,
70
            serial_read,
71
            serial_select,
72
            serial_get_config,
73
            serial_set_config
74
    );
75
 
76
static void serial_init(serial_channel *chan);
77
static void serial_xmt_char(serial_channel *chan);
78
static void serial_rcv_char(serial_channel *chan, unsigned char c);
79
#ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
80
static void serial_indicate_status(serial_channel *chan,
81
                                   cyg_serial_line_status_t *s);
82
#endif
83
#if CYGINT_IO_SERIAL_BLOCK_TRANSFER
84
static rcv_req_reply_t serial_data_rcv_req(serial_channel *chan, int avail,
85
                                           int* space_avail,
86
                                           unsigned char** space);
87
static void serial_data_rcv_done(serial_channel *chan, int chars_rcvd);
88
static xmt_req_reply_t serial_data_xmt_req(serial_channel *chan, int space,
89
                                           int* chars_avail,
90
                                           unsigned char** chars);
91
static void serial_data_xmt_done(serial_channel *chan, int chars_sent);
92
# ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
93
SERIAL_CALLBACKS(cyg_io_serial_callbacks,
94
                 serial_init,
95
                 serial_xmt_char,
96
                 serial_rcv_char,
97
                 serial_data_rcv_req,
98
                 serial_data_rcv_done,
99
                 serial_data_xmt_req,
100
                 serial_data_xmt_done,
101
                 serial_indicate_status);
102
 
103
# else
104
SERIAL_CALLBACKS(cyg_io_serial_callbacks,
105
                 serial_init,
106
                 serial_xmt_char,
107
                 serial_rcv_char,
108
                 serial_data_rcv_req,
109
                 serial_data_rcv_done,
110
                 serial_data_xmt_req,
111
                 serial_data_xmt_done);
112
# endif
113
#else
114
# ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
115
SERIAL_CALLBACKS(cyg_io_serial_callbacks,
116
                 serial_init,
117
                 serial_xmt_char,
118
                 serial_rcv_char,
119
                 serial_indicate_status);
120
# else
121
SERIAL_CALLBACKS(cyg_io_serial_callbacks,
122
                 serial_init,
123
                 serial_xmt_char,
124
                 serial_rcv_char);
125
# endif
126
#endif
127
 
128
// ---------------------------------------------------------------------------
129
 
130
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
131
 
132
static __inline__ void
133
throttle_tx( serial_channel *chan )
134
{
135
    chan->flow_desc.flags |= CYG_SERIAL_FLOW_OUT_THROTTLED;
136
    // the throttling itself occurs in the serial_xmt_char() callback
137
}
138
 
139
static __inline__ void
140
restart_tx( serial_channel *chan )
141
{
142
    serial_funs *funs = chan->funs;
143
 
144
    chan->flow_desc.flags &= ~CYG_SERIAL_FLOW_OUT_THROTTLED;
145
 
146
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
147
    // See if there is now enough room to say it is available
148
    // for writing
149
    {
150
        cbuf_t *cbuf = &chan->out_cbuf;
151
        int space;
152
 
153
        space = cbuf->len - cbuf->nb;
154
        if (space >= cbuf->low_water)
155
            cyg_selwakeup( &cbuf->selinfo );
156
    }
157
#endif
158
    if ( chan->out_cbuf.nb > 0 )
159
        (funs->start_xmit)(chan);
160
}
161
 
162
static __inline__ void
163
throttle_rx( serial_channel *chan, cyg_bool force )
164
{
165
    serial_funs *funs = chan->funs;
166
 
167
    chan->flow_desc.flags |= CYG_SERIAL_FLOW_IN_THROTTLED;
168
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
169
    // send an xoff
170
    if ( force || chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_RX ) {
171
        chan->flow_desc.xchar = CYGDAT_IO_SERIAL_FLOW_CONTROL_XOFF_CHAR;
172
        // Make sure xmit is running so we can send it
173
        (funs->start_xmit)(chan);
174
    }
175
#endif
176
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
177
    {
178
        cyg_uint32 i=1;
179
        cyg_uint32 len = sizeof(i);
180
 
181
        // set hardware flow control - don't care if it fails
182
        if ( force || (chan->config.flags & CYGNUM_SERIAL_FLOW_RTSCTS_RX) ||
183
             (chan->config.flags & CYGNUM_SERIAL_FLOW_DSRDTR_RX) )
184
            (funs->set_config)(chan,
185
                               CYG_IO_SET_CONFIG_SERIAL_HW_RX_FLOW_THROTTLE,
186
                               &i, &len);
187
    }
188
#endif
189
}
190
 
191
static __inline__ void
192
restart_rx( serial_channel *chan, cyg_bool force )
193
{
194
    serial_funs *funs = chan->funs;
195
 
196
    chan->flow_desc.flags &= ~CYG_SERIAL_FLOW_IN_THROTTLED;
197
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
198
    // send an xon
199
    if ( force || chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_RX ) {
200
        chan->flow_desc.xchar = CYGDAT_IO_SERIAL_FLOW_CONTROL_XON_CHAR;
201
        (funs->start_xmit)(chan);  // Make sure xmit is running so we can send it
202
    }
203
#endif
204
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
205
    {
206
        cyg_uint32 i=0;
207
        cyg_uint32 len = sizeof(i);
208
 
209
        // set hardware flow control - don't care if it fails
210
        if ( force || (chan->config.flags & CYGNUM_SERIAL_FLOW_RTSCTS_RX) ||
211
             (chan->config.flags & CYGNUM_SERIAL_FLOW_DSRDTR_RX) )
212
            (funs->set_config)(chan,
213
                               CYG_IO_SET_CONFIG_SERIAL_HW_RX_FLOW_THROTTLE,
214
                               &i, &len);
215
    }
216
#endif
217
}
218
 
219
#endif
220
 
221
// ---------------------------------------------------------------------------
222
 
223
static void
224
serial_init(serial_channel *chan)
225
{
226
    if (chan->init) return;
227
    if (chan->out_cbuf.len != 0) {
228
#ifdef CYGDBG_IO_INIT
229
        diag_printf("Set output buffer - buf: %x len: %d\n", chan->out_cbuf.data, chan->out_cbuf.len);
230
#endif
231
        chan->out_cbuf.waiting = false;
232
        chan->out_cbuf.abort = false;
233
#ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
234
        chan->out_cbuf.blocking = true;
235
#endif
236
        chan->out_cbuf.pending = 0;
237
        cyg_drv_mutex_init(&chan->out_cbuf.lock);
238
        cyg_drv_cond_init(&chan->out_cbuf.wait, &chan->out_cbuf.lock);
239
        chan->out_cbuf.low_water = chan->out_cbuf.len / 4;
240
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
241
        cyg_selinit( &chan->out_cbuf.selinfo );
242
#endif        
243
    }
244
    if (chan->in_cbuf.len != 0) {
245
        cbuf_t *cbuf = &chan->in_cbuf;
246
 
247
#ifdef CYGDBG_IO_INIT
248
        diag_printf("Set input buffer - buf: %x len: %d\n", cbuf->data, cbuf->len);
249
#endif
250
        cbuf->waiting = false;
251
        cbuf->abort = false;
252
#ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
253
        cbuf->blocking = true;
254
#endif
255
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
256
        cyg_selinit( &cbuf->selinfo );
257
#endif
258
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
259
        cbuf->low_water =
260
            (CYGNUM_IO_SERIAL_FLOW_CONTROL_LOW_WATER_PERCENT * cbuf->len) / 100;
261
        cbuf->high_water =
262
            (CYGNUM_IO_SERIAL_FLOW_CONTROL_HIGH_WATER_PERCENT * cbuf->len) / 100;
263
# ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
264
        // But make sure it is at least 35 below buffer size, to allow
265
        // for 16 byte fifos, twice, plus some latency before s/w flow
266
        // control can kick in. This doesn't apply to h/w flow control
267
        // as it is near-instaneous
268
        if ( (cbuf->len - cbuf->high_water) < 35 )
269
            cbuf->high_water = cbuf->len - 35;
270
        // and just in case...
271
        if ( cbuf->high_water <= 0 )
272
            cbuf->high_water = 1;
273
        if ( cbuf->low_water > cbuf->high_water )
274
            cbuf->low_water = cbuf->high_water;
275
# endif
276
#endif
277
        cyg_drv_mutex_init(&cbuf->lock);
278
        cyg_drv_cond_init(&cbuf->wait, &cbuf->lock);
279
    }
280
#ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
281
    chan->status_callback = NULL;
282
#endif
283
 
284
#ifdef CYGDBG_USE_ASSERTS
285
#if CYGINT_IO_SERIAL_BLOCK_TRANSFER
286
    chan->in_cbuf.block_mode_xfer_running = false;
287
    chan->out_cbuf.block_mode_xfer_running = false;
288
#endif // CYGINT_IO_SERIAL_BLOCK_TRANSFER
289
#endif // CYGDBG_USE_ASSERTS
290
    chan->init = true;
291
}
292
 
293
// ---------------------------------------------------------------------------
294
// FIXME:@@@ Throughout this file there are uses of cyg_drv_cond_signal and
295
// cyg_drv_cond_broadcast. Does it matter which? -Jifl
296
 
297
static Cyg_ErrNo
298
serial_write(cyg_io_handle_t handle, const void *_buf, cyg_uint32 *len)
299
{
300
    cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
301
    serial_channel *chan = (serial_channel *)t->priv;
302
    serial_funs *funs = chan->funs;
303
    cyg_int32 size = *len;
304
    cyg_uint8 *buf = (cyg_uint8 *)_buf;
305
    int next;
306
    cbuf_t *cbuf = &chan->out_cbuf;
307
    Cyg_ErrNo res = ENOERR;
308
 
309
    cyg_drv_mutex_lock(&cbuf->lock);
310
    cbuf->abort = false;
311
 
312
    if (cbuf->len == 0) {
313
        // Non interrupt driven (i.e. polled) operation
314
        while (size-- > 0) {
315
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
316
            while ( ( 0 == (chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED) ) &&
317
                    ((funs->putc)(chan, *buf) == false) )
318
                ;  // Ignore full, keep trying
319
#else
320
            while ((funs->putc)(chan, *buf) == false)
321
                ;  // Ignore full, keep trying
322
#endif
323
            buf++;
324
        }
325
    } else {
326
        cyg_drv_dsr_lock();  // Avoid race condition testing pointers
327
        while (size > 0) {
328
            next = cbuf->put + 1;
329
            if (next == cbuf->len) next = 0;
330
            if (cbuf->nb == cbuf->len) {
331
                cbuf->waiting = true;
332
                // Buffer full - wait for space
333
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
334
                if ( 0 == (chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED) )
335
#endif
336
                    (funs->start_xmit)(chan);  // Make sure xmit is running
337
 
338
                // Check flag: 'start_xmit' may have obviated the need
339
                // to wait :-)
340
                if (cbuf->waiting) {
341
#ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
342
                    // Optionally return if configured for non-blocking mode.
343
                    if (!cbuf->blocking) {
344
                        *len -= size;   // number of characters actually sent
345
                        cbuf->waiting = false;
346
                        res = -EAGAIN;
347
                        break;
348
                    }
349
#endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
350
                    cbuf->pending += size;  // Have this much more to send [eventually]
351
                    if( !cyg_drv_cond_wait(&cbuf->wait) )
352
                        cbuf->abort = true;
353
                    cbuf->pending -= size;
354
                }
355
                if (cbuf->abort) {
356
                    // Give up!
357
                    *len -= size;   // number of characters actually sent
358
                    cbuf->abort = false;
359
                    cbuf->waiting = false;
360
                    res = -EINTR;
361
                    break;
362
                }
363
            } else {
364
                cbuf->data[cbuf->put++] = *buf++;
365
                cbuf->put = next;
366
                cbuf->nb++;
367
                size--;  // Only count if actually sent!
368
            }
369
        }
370
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
371
        if ( 0 == (chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED) )
372
#endif
373
            (funs->start_xmit)(chan);  // Start output as necessary
374
        cyg_drv_dsr_unlock();
375
    }
376
    cyg_drv_mutex_unlock(&cbuf->lock);
377
    return res;
378
}
379
 
380
 
381
// ---------------------------------------------------------------------------
382
 
383
static Cyg_ErrNo
384
serial_read(cyg_io_handle_t handle, void *_buf, cyg_uint32 *len)
385
{
386
    cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
387
    serial_channel *chan = (serial_channel *)t->priv;
388
    serial_funs *funs = chan->funs;
389
    cyg_uint8 *buf = (cyg_uint8 *)_buf;
390
    cyg_int32 size = 0;
391
    cbuf_t *cbuf = &chan->in_cbuf;
392
    Cyg_ErrNo res = ENOERR;
393
#ifdef XX_CYGDBG_DIAG_BUF
394
            extern int enable_diag_uart;
395
            int _enable = enable_diag_uart;
396
            int _time, _stime;
397
            externC cyg_tick_count_t cyg_current_time(void);
398
#endif // CYGDBG_DIAG_BUF
399
 
400
    cyg_drv_mutex_lock(&cbuf->lock);
401
    cbuf->abort = false;
402
 
403
    if (cbuf->len == 0) {
404
        // Non interrupt driven (i.e. polled) operation
405
        while (size++ < *len) {
406
            cyg_uint8 c = (funs->getc)(chan);
407
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
408
            // for software flow control, if the driver returns one of the
409
            // characters we act on it and then drop it (the app must not
410
            // see it)
411
            if ( chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_TX ) {
412
                if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XOFF_CHAR ) {
413
                    throttle_tx( chan );
414
                } else if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XON_CHAR ) {
415
                    restart_tx( chan );
416
                }
417
                else
418
                    *buf++ = c;
419
            }
420
            else
421
                *buf++ = c;
422
#else
423
            *buf++ = c;
424
#endif    
425
        }
426
    } else {
427
        cyg_drv_dsr_lock();  // Avoid races
428
        while (size < *len) {
429
            if (cbuf->nb > 0) {
430
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
431
                if ( (cbuf->nb <= cbuf->low_water) &&
432
                     (chan->flow_desc.flags & CYG_SERIAL_FLOW_IN_THROTTLED) )
433
                    restart_rx( chan, false );
434
#endif
435
                *buf++ = cbuf->data[cbuf->get];
436
                if (++cbuf->get == cbuf->len) cbuf->get = 0;
437
                cbuf->nb--;
438
                size++;
439
            } else {
440
#ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
441
                if (!cbuf->blocking) {
442
                    *len = size;        // characters actually read
443
                    res = -EAGAIN;
444
                    break;
445
                }
446
#endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
447
                cbuf->waiting = true;
448
#ifdef XX_CYGDBG_DIAG_BUF
449
                enable_diag_uart = 0;
450
                HAL_CLOCK_READ(&_time);
451
                _stime = (int)cyg_current_time();
452
                diag_printf("READ wait - get: %d, put: %d, time: %x.%x\n", cbuf->get, cbuf->put, _stime, _time);
453
                enable_diag_uart = _enable;
454
#endif // CYGDBG_DIAG_BUF
455
                if( !cyg_drv_cond_wait(&cbuf->wait) )
456
                    cbuf->abort = true;
457
#ifdef XX_CYGDBG_DIAG_BUF
458
                enable_diag_uart = 0;
459
                HAL_CLOCK_READ(&_time);
460
                _stime = (int)cyg_current_time();
461
                diag_printf("READ continue - get: %d, put: %d, time: %x.%x\n", cbuf->get, cbuf->put, _stime, _time);
462
                enable_diag_uart = _enable;
463
#endif // CYGDBG_DIAG_BUF
464
                if (cbuf->abort) {
465
                    // Give up!
466
                    *len = size;        // characters actually read
467
                    cbuf->abort = false;
468
                    cbuf->waiting = false;
469
                    res = -EINTR;
470
                    break;
471
                }
472
            }
473
        }
474
        cyg_drv_dsr_unlock();
475
    }
476
#ifdef XX_CYGDBG_DIAG_BUF
477
    cyg_drv_isr_lock();
478
    enable_diag_uart = 0;
479
    HAL_CLOCK_READ(&_time);
480
    _stime = (int)cyg_current_time();
481
    diag_printf("READ done - size: %d, len: %d, time: %x.%x\n", size, *len, _stime, _time);
482
    enable_diag_uart = _enable;
483
    cyg_drv_isr_unlock();
484
#endif // CYGDBG_DIAG_BUF
485
    cyg_drv_mutex_unlock(&cbuf->lock);
486
    return res;
487
}
488
 
489
 
490
// ---------------------------------------------------------------------------
491
 
492
static cyg_bool
493
serial_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info)
494
{
495
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
496
 
497
    cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
498
    serial_channel *chan = (serial_channel *)t->priv;
499
 
500
 
501
    switch( which )
502
    {
503
    case CYG_FREAD:
504
        {
505
            cbuf_t *cbuf = &chan->in_cbuf;
506
 
507
            // Check for data in the input buffer. If there is none,
508
            // register the select operation, otherwise return true.
509
 
510
            if( cbuf->nb == 0 )
511
                cyg_selrecord( info, &cbuf->selinfo );
512
            else return true;
513
        }
514
        break;
515
 
516
    case CYG_FWRITE:
517
        {
518
            // Check for space in the output buffer. If there is none,
519
            // register the select operation, otherwise return true.
520
 
521
            cbuf_t *cbuf = &chan->out_cbuf;
522
            int space = cbuf->len - cbuf->nb;
523
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
524
            if ( (space < cbuf->low_water) ||
525
                 (chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED) )
526
                cyg_selrecord( info, &cbuf->selinfo );
527
#else
528
            if (space < cbuf->low_water)
529
                cyg_selrecord( info, &cbuf->selinfo );
530
#endif
531
            else return true;
532
        }
533
        break;
534
 
535
    case 0: // exceptions - none supported
536
        break;
537
    }
538
    return false;
539
#else
540
 
541
    // With no select support, we simply return true.
542
    return true;
543
#endif    
544
}
545
 
546
 
547
// ---------------------------------------------------------------------------
548
 
549
static Cyg_ErrNo
550
serial_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *xbuf,
551
                  cyg_uint32 *len)
552
{
553
    cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
554
    serial_channel *chan = (serial_channel *)t->priv;
555
    cyg_serial_info_t *buf = (cyg_serial_info_t *)xbuf;
556
    Cyg_ErrNo res = ENOERR;
557
    cbuf_t *out_cbuf = &chan->out_cbuf;
558
    cbuf_t *in_cbuf = &chan->in_cbuf;
559
    serial_funs *funs = chan->funs;
560
 
561
    switch (key) {
562
    case CYG_IO_GET_CONFIG_SERIAL_INFO:
563
        if (*len < sizeof(cyg_serial_info_t)) {
564
            return -EINVAL;
565
        }
566
        *buf = chan->config;
567
        *len = sizeof(chan->config);
568
        break;
569
 
570
    case CYG_IO_GET_CONFIG_SERIAL_BUFFER_INFO:
571
        // return rx/tx buffer sizes and counts
572
        {
573
            cyg_serial_buf_info_t *p;
574
            if (*len < sizeof(cyg_serial_buf_info_t))
575
                return -EINVAL;
576
 
577
            *len = sizeof(cyg_serial_buf_info_t);
578
            p = (cyg_serial_buf_info_t *)xbuf;
579
 
580
            p->rx_bufsize = in_cbuf->len;
581
            if (p->rx_bufsize)
582
                p->rx_count = in_cbuf->nb;
583
            else
584
                p->rx_count = 0;
585
 
586
            p->tx_bufsize = out_cbuf->len;
587
            if (p->tx_bufsize)
588
                p->tx_count = out_cbuf->nb;
589
            else
590
                p->tx_count = 0;
591
        }
592
      break;
593
 
594
    case CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN:
595
// Wait for any pending output to complete
596
        if (out_cbuf->len == 0) break;  // Nothing to do if not buffered
597
        cyg_drv_mutex_lock(&out_cbuf->lock);  // Stop any further output processing
598
        cyg_drv_dsr_lock();
599
        while (out_cbuf->pending || (out_cbuf->nb > 0)) {
600
            out_cbuf->waiting = true;
601
            if(!cyg_drv_cond_wait(&out_cbuf->wait) )
602
                res = -EINTR;
603
        }
604
        cyg_drv_dsr_unlock();
605
        cyg_drv_mutex_unlock(&out_cbuf->lock);
606
        break;
607
 
608
    case CYG_IO_GET_CONFIG_SERIAL_INPUT_FLUSH:
609
        // Flush any buffered input
610
        if (in_cbuf->len == 0) break;  // Nothing to do if not buffered
611
        cyg_drv_mutex_lock(&in_cbuf->lock);  // Stop any further input processing
612
        cyg_drv_dsr_lock();
613
        if (in_cbuf->waiting) {
614
            in_cbuf->abort = true;
615
            cyg_drv_cond_signal(&in_cbuf->wait);
616
            in_cbuf->waiting = false;
617
        }
618
        in_cbuf->get = in_cbuf->put = in_cbuf->nb = 0;  // Flush buffered input
619
        cyg_drv_dsr_unlock();
620
        cyg_drv_mutex_unlock(&in_cbuf->lock);
621
        break;
622
 
623
    case CYG_IO_GET_CONFIG_SERIAL_ABORT:
624
        // Abort any outstanding I/O, including blocked reads
625
        // Caution - assumed to be called from 'timeout' (i.e. DSR) code
626
        if (in_cbuf->len != 0) {
627
            in_cbuf->abort = true;
628
            cyg_drv_cond_signal(&in_cbuf->wait);
629
        }
630
        if (out_cbuf->len != 0) {
631
            out_cbuf->abort = true;
632
            cyg_drv_cond_signal(&out_cbuf->wait);
633
        }
634
        break;
635
 
636
    case CYG_IO_GET_CONFIG_SERIAL_OUTPUT_FLUSH:
637
// Throw away any pending output
638
        if (out_cbuf->len == 0) break;  // Nothing to do if not buffered
639
        cyg_drv_mutex_lock(&out_cbuf->lock);  // Stop any further output processing
640
        cyg_drv_dsr_lock();
641
        if (out_cbuf->nb > 0) {
642
            out_cbuf->get = out_cbuf->put = out_cbuf->nb = 0;  // Empties queue!
643
            (funs->stop_xmit)(chan);  // Done with transmit
644
        }
645
        if (out_cbuf->waiting) {
646
            out_cbuf->abort = true;
647
            cyg_drv_cond_signal(&out_cbuf->wait);
648
            out_cbuf->waiting = false;
649
        }
650
        cyg_drv_dsr_unlock();
651
        cyg_drv_mutex_unlock(&out_cbuf->lock);
652
        break;
653
 
654
#ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
655
    case CYG_IO_GET_CONFIG_READ_BLOCKING:
656
        if (*len < sizeof(cyg_uint32)) {
657
            return -EINVAL;
658
        }
659
        *(cyg_uint32*)xbuf = (in_cbuf->blocking) ? 1 : 0;
660
        break;
661
 
662
    case CYG_IO_GET_CONFIG_WRITE_BLOCKING:
663
        if (*len < sizeof(cyg_uint32)) {
664
            return -EINVAL;
665
        }
666
        *(cyg_uint32*)xbuf = (out_cbuf->blocking) ? 1 : 0;
667
        break;
668
#endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
669
 
670
    default:
671
        res = -EINVAL;
672
    }
673
    return res;
674
}
675
 
676
 
677
// ---------------------------------------------------------------------------
678
 
679
static Cyg_ErrNo
680
serial_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *xbuf,
681
                  cyg_uint32 *len)
682
{
683
    Cyg_ErrNo res = ENOERR;
684
    cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
685
    serial_channel *chan = (serial_channel *)t->priv;
686
#ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
687
    cbuf_t *out_cbuf = &chan->out_cbuf;
688
    cbuf_t *in_cbuf = &chan->in_cbuf;
689
#endif
690
    serial_funs *funs = chan->funs;
691
 
692
    switch (key) {
693
#ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
694
    case CYG_IO_SET_CONFIG_READ_BLOCKING:
695
        if (*len < sizeof(cyg_uint32) || 0 == in_cbuf->len) {
696
            return -EINVAL;
697
        }
698
        in_cbuf->blocking = (1 == *(cyg_uint32*)xbuf) ? true : false;
699
        break;
700
    case CYG_IO_SET_CONFIG_WRITE_BLOCKING:
701
        if (*len < sizeof(cyg_uint32) || 0 == out_cbuf->len) {
702
            return -EINVAL;
703
        }
704
        out_cbuf->blocking = (1 == *(cyg_uint32*)xbuf) ? true : false;
705
        break;
706
#endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
707
 
708
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
709
    case CYG_IO_SET_CONFIG_SERIAL_FLOW_CONTROL_METHOD:
710
        {
711
            cyg_uint32 *f = (cyg_uint32 *)xbuf;
712
 
713
            if (*len < sizeof(*f))
714
                return -EINVAL;
715
 
716
            cyg_drv_dsr_lock();
717
 
718
            chan->config.flags &= ~(CYGNUM_SERIAL_FLOW_XONXOFF_RX|
719
                                    CYGNUM_SERIAL_FLOW_XONXOFF_TX|
720
                                    CYGNUM_SERIAL_FLOW_RTSCTS_RX|
721
                                    CYGNUM_SERIAL_FLOW_RTSCTS_TX|
722
                                    CYGNUM_SERIAL_FLOW_DSRDTR_RX|
723
                                    CYGNUM_SERIAL_FLOW_DSRDTR_TX);
724
            chan->config.flags |= (*f & (
725
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
726
                CYGNUM_SERIAL_FLOW_XONXOFF_RX|
727
                CYGNUM_SERIAL_FLOW_XONXOFF_TX|
728
#endif
729
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
730
                CYGNUM_SERIAL_FLOW_RTSCTS_RX|
731
                CYGNUM_SERIAL_FLOW_RTSCTS_TX|
732
                CYGNUM_SERIAL_FLOW_DSRDTR_RX|
733
                CYGNUM_SERIAL_FLOW_DSRDTR_TX|
734
#endif
735
                0));
736
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
737
            // up to hardware driver to clear flags if rejected
738
            res = (funs->set_config)(chan,
739
                                     CYG_IO_SET_CONFIG_SERIAL_HW_FLOW_CONFIG,
740
                                     NULL, NULL);
741
#endif
742
            cyg_drv_dsr_unlock();
743
        }
744
        break;
745
 
746
    case CYG_IO_SET_CONFIG_SERIAL_FLOW_CONTROL_FORCE:
747
        {
748
            cyg_uint32 *f = (cyg_uint32 *)xbuf;
749
 
750
            if (*len < sizeof(*f))
751
                return -EINVAL;
752
 
753
            cyg_drv_dsr_lock();
754
            switch (*f) {
755
            case CYGNUM_SERIAL_FLOW_THROTTLE_RX:
756
                throttle_rx( chan, true );
757
                break;
758
            case CYGNUM_SERIAL_FLOW_RESTART_RX:
759
                restart_rx( chan, true );
760
                break;
761
            case CYGNUM_SERIAL_FLOW_THROTTLE_TX:
762
                throttle_tx( chan );
763
                break;
764
            case CYGNUM_SERIAL_FLOW_RESTART_TX:
765
                restart_tx( chan );
766
                break;
767
            default:
768
                res = -EINVAL;
769
                break;
770
            }
771
            cyg_drv_dsr_unlock();
772
        }
773
        break;
774
#endif // CYGPKG_IO_SERIAL_FLOW_CONTROL
775
 
776
#ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
777
    case CYG_IO_SET_CONFIG_SERIAL_STATUS_CALLBACK:
778
        {
779
            cyg_serial_line_status_callback_fn_t newfn;
780
            CYG_ADDRWORD newpriv;
781
            cyg_serial_line_status_callback_t *tmp =
782
                (cyg_serial_line_status_callback_t *)xbuf;
783
 
784
            if ( *len < sizeof(*tmp) )
785
                return -EINVAL;
786
 
787
            newfn = tmp->fn;
788
            newpriv = tmp->priv;
789
 
790
            // prevent callbacks while we do this
791
            cyg_drv_dsr_lock();
792
            // store old callbacks in same structure
793
            tmp->fn = chan->status_callback;
794
            tmp->priv = chan->status_callback_priv;
795
            chan->status_callback = newfn;
796
            chan->status_callback_priv = newpriv;
797
            cyg_drv_dsr_unlock();
798
            *len = sizeof(*tmp);
799
        }
800
        break;
801
#endif
802
 
803
    default:
804
        // pass down to lower layers
805
        return (funs->set_config)(chan, key, xbuf, len);
806
    }
807
    return res;
808
}
809
 
810
// ---------------------------------------------------------------------------
811
 
812
static void
813
serial_xmt_char(serial_channel *chan)
814
{
815
    cbuf_t *cbuf = &chan->out_cbuf;
816
    serial_funs *funs = chan->funs;
817
    unsigned char c;
818
    int space;
819
 
820
#if CYGINT_IO_SERIAL_BLOCK_TRANSFER
821
    CYG_ASSERT(false == cbuf->block_mode_xfer_running,
822
               "Attempting char xmt while block transfer is running");
823
#endif
824
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
825
    // if we are required to send an XON/XOFF char, send it before
826
    // anything else
827
    // FIXME: what if XON gets corrupted in transit to the other end?
828
    // Should we resend XON even though the other end may not be wanting
829
    // to send us stuff at this point?
830
    if ( chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_RX ) {
831
        if ( chan->flow_desc.xchar ) {
832
            if ( (funs->putc)(chan, chan->flow_desc.xchar) ) {
833
                chan->flow_desc.xchar = '\0';
834
            } else {  // otherwise there's no space and we have to wait
835
                return;
836
            }
837
        }
838
    }
839
#endif
840
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
841
    // if we're meant to be throttled, just stop and leave
842
    if ( chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED ) {
843
        (funs->stop_xmit)(chan);  // Stop transmitting for now
844
        return;
845
    }
846
#endif
847
    while (cbuf->nb > 0) {
848
        c = cbuf->data[cbuf->get];
849
        if ((funs->putc)(chan, c)) {
850
            cbuf->get++;
851
            if (cbuf->get == cbuf->len) cbuf->get = 0;
852
            cbuf->nb--;
853
        } else {
854
            // See if there is now enough room to restart writer
855
            space = cbuf->len - cbuf->nb;
856
            if (space >= cbuf->low_water) {
857
                if (cbuf->waiting) {
858
                    cbuf->waiting = false;
859
                    cyg_drv_cond_broadcast(&cbuf->wait);
860
                }
861
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
862
                cyg_selwakeup( &cbuf->selinfo );
863
#endif                    
864
            }
865
            return;  // Need to wait for more space
866
        }
867
    }
868
    (funs->stop_xmit)(chan);  // Done with transmit
869
 
870
    // must signal waiters, and wake up selecters for the case when
871
    // this was the last char to be sent and they hadn't been signalled
872
    // before (e.g. because of flow control)
873
    if (cbuf->waiting) {
874
        cbuf->waiting = false;
875
        cyg_drv_cond_signal(&cbuf->wait);
876
    }
877
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
878
    cyg_selwakeup( &cbuf->selinfo );
879
#endif                    
880
}
881
 
882
// ---------------------------------------------------------------------------
883
 
884
static void
885
serial_rcv_char(serial_channel *chan, unsigned char c)
886
{
887
    cbuf_t *cbuf = &chan->in_cbuf;
888
 
889
#if CYGINT_IO_SERIAL_BLOCK_TRANSFER
890
    CYG_ASSERT(false == cbuf->block_mode_xfer_running,
891
               "Attempting char rcv while block transfer is running");
892
#endif
893
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
894
    // for software flow control, if the driver returns one of the characters
895
    // we act on it and then drop it (the app must not see it)
896
    if ( chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_TX ) {
897
        if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XOFF_CHAR ) {
898
            throttle_tx( chan );
899
            return; // it wasn't a "real" character
900
        } else if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XON_CHAR ) {
901
            restart_tx( chan );
902
            return; // it wasn't a "real" character
903
        }
904
    }
905
#endif    
906
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
907
    // If we've hit the high water mark, tell the other side to stop
908
    if ( cbuf->nb >= cbuf->high_water ) {
909
        throttle_rx( chan, false );
910
    }
911
#endif
912
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
913
    // Wake up any pending selectors if we are about to
914
    // put some data into a previously empty buffer.
915
    if( cbuf->nb == 0 )
916
        cyg_selwakeup( &cbuf->selinfo );
917
#endif
918
 
919
    // If the flow control is not enabled/sufficient and the buffer is
920
    // already full, just throw new characters away.
921
 
922
    if ( cbuf->nb < cbuf->len ) {
923
        cbuf->data[cbuf->put++] = c;
924
        if (cbuf->put == cbuf->len) cbuf->put = 0;
925
        cbuf->nb++;
926
    } // note trailing else
927
#ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
928
    else {
929
        // Overrun. Report the error.
930
        cyg_serial_line_status_t stat;
931
        stat.which = CYGNUM_SERIAL_STATUS_OVERRUNERR;
932
        serial_indicate_status(chan, &stat);
933
    }
934
#endif
935
 
936
    if (cbuf->waiting) {
937
#ifdef XX_CYGDBG_DIAG_BUF
938
            extern int enable_diag_uart;
939
            int _enable = enable_diag_uart;
940
            int _time, _stime;
941
            externC cyg_tick_count_t cyg_current_time(void);
942
            enable_diag_uart = 0;
943
            HAL_CLOCK_READ(&_time);
944
            _stime = (int)cyg_current_time();
945
            diag_printf("Signal reader - time: %x.%x\n", _stime, _time);
946
            enable_diag_uart = _enable;
947
#endif // CYGDBG_DIAG_BUF
948
        cbuf->waiting = false;
949
        cyg_drv_cond_signal(&cbuf->wait);
950
    }
951
}
952
 
953
//----------------------------------------------------------------------------
954
// Flow control indication callback
955
 
956
#ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
957
static void
958
serial_indicate_status(serial_channel *chan, cyg_serial_line_status_t *s )
959
{
960
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
961
    if ( CYGNUM_SERIAL_STATUS_FLOW == s->which ) {
962
        if ( s->value )
963
            restart_tx( chan );
964
        else
965
            throttle_tx( chan );
966
    }
967
#endif
968
    if ( chan->status_callback )
969
        (*chan->status_callback)(s, chan->status_callback_priv);
970
}
971
#endif // ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
972
 
973
//----------------------------------------------------------------------------
974
// Block transfer functions. Not all drivers require these. Those that
975
// do must follow the required semantics:
976
//
977
// Attempt to transfer as much via the block transfer function as
978
// possible, _but_ if that fails, do the remaining bytes via the
979
// single-char function. That ensures that all policy decisions can be
980
// made in this driver, and not in the device driver.
981
//
982
// Note: if the driver uses DMA for transmission, an initial failing
983
// call to the xmt_req function must cause the start_xmit function to
984
// fall-back to regular CPU-interrupt based single-character
985
// transmission.
986
 
987
#if CYGINT_IO_SERIAL_BLOCK_TRANSFER
988
 
989
static rcv_req_reply_t
990
serial_data_rcv_req(serial_channel *chan, int avail,
991
                    int* space_avail, unsigned char** space)
992
{
993
    cbuf_t *cbuf = &chan->in_cbuf;
994
    int gap;
995
 
996
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
997
    // When there is software flow-control, force the serial device
998
    // driver to use the single-char xmt/rcv functions, since these
999
    // have to make policy decision based on the data. Rcv function
1000
    // may also have to transmit data to throttle the xmitter.
1001
    if (chan->config.flags & (CYGNUM_SERIAL_FLOW_XONXOFF_TX|CYGNUM_SERIAL_FLOW_XONXOFF_RX))
1002
        return CYG_RCV_DISABLED;
1003
#endif
1004
 
1005
    CYG_ASSERT(false == cbuf->block_mode_xfer_running,
1006
               "Attempting new block transfer while another is running");
1007
    // Check for space
1008
    gap = cbuf->nb;
1009
    if (gap == cbuf->len)
1010
        return CYG_RCV_FULL;
1011
 
1012
#ifdef CYGDBG_USE_ASSERTS
1013
    cbuf->block_mode_xfer_running = true;
1014
#endif
1015
 
1016
    if (0 == gap) {
1017
        // Buffer is empty. Reset put/get indexes to get max transfer in
1018
        // one chunk.
1019
        cbuf->get = 0;
1020
        cbuf->put = 0;
1021
        gap = cbuf->len;
1022
    } else {
1023
        // Free space (G = get, P = put, x = data, . = empty)
1024
        //  positive: xxxxP.....Gxxx
1025
        //  negative: ..GxxxxxP.....        [offer last chunk only]
1026
 
1027
        // First try for a gap between put and get locations
1028
        gap = cbuf->get - cbuf->put;
1029
        if (gap < 0) {
1030
            // If failed, the gap is between put and the end of buffer
1031
            gap = cbuf->len - cbuf->put;
1032
        }
1033
    }
1034
 
1035
    if (avail < gap) gap = avail;   // bound by what's available from hw
1036
 
1037
    *space_avail = gap;
1038
    *space = &cbuf->data[cbuf->put];
1039
 
1040
    CYG_ASSERT((gap+cbuf->nb) <= cbuf->len, "Buffer will overflow");
1041
    CYG_ASSERT(cbuf->put < cbuf->len, "Invalid put ptr");
1042
    CYG_ASSERT(cbuf->get < cbuf->len, "Invalid get ptr");
1043
 
1044
    return CYG_RCV_OK;
1045
}
1046
 
1047
static void
1048
serial_data_rcv_done(serial_channel *chan, int chars_rcvd)
1049
{
1050
    cbuf_t *cbuf = &chan->in_cbuf;
1051
 
1052
    cbuf->put += chars_rcvd;
1053
    cbuf->nb += chars_rcvd;
1054
 
1055
    if (cbuf->put == cbuf->len) cbuf->put = 0;
1056
 
1057
    CYG_ASSERT(cbuf->nb <= cbuf->len, "Buffer overflow");
1058
    CYG_ASSERT(cbuf->put < cbuf->len, "Invalid put ptr");
1059
    CYG_ASSERT(cbuf->get < cbuf->len, "Invalid get ptr");
1060
 
1061
    if (cbuf->waiting) {
1062
        cbuf->waiting = false;
1063
        cyg_drv_cond_signal(&cbuf->wait);
1064
    }
1065
#ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
1066
    // If we've hit the high water mark, tell the other side to stop
1067
    if ( cbuf->nb >= cbuf->high_water ) {
1068
        throttle_rx( chan, false );
1069
    }
1070
#endif
1071
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
1072
    // Wake up any pending selectors if we have
1073
    // put some data into a previously empty buffer.
1074
    if (chars_rcvd == cbuf->nb)
1075
        cyg_selwakeup( &cbuf->selinfo );
1076
#endif
1077
 
1078
#ifdef CYGDBG_USE_ASSERTS
1079
    cbuf->block_mode_xfer_running = false;
1080
#endif
1081
}
1082
 
1083
static xmt_req_reply_t
1084
serial_data_xmt_req(serial_channel *chan, int space,
1085
                    int* chars_avail, unsigned char** chars)
1086
{
1087
    cbuf_t *cbuf = &chan->out_cbuf;
1088
    int avail;
1089
 
1090
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
1091
    // When there is software flow-control, force the serial device
1092
    // driver to use the single-char xmt/rcv functions, since these
1093
    // have to make policy decision based on the data. Rcv function
1094
    // may also have to transmit data to throttle the xmitter.
1095
    if (chan->config.flags & (CYGNUM_SERIAL_FLOW_XONXOFF_TX|CYGNUM_SERIAL_FLOW_XONXOFF_RX))
1096
        return CYG_XMT_DISABLED;
1097
#endif
1098
 
1099
    CYG_ASSERT(false == cbuf->block_mode_xfer_running,
1100
               "Attempting new block transfer while another is running");
1101
 
1102
    // Available data (G = get, P = put, x = data, . = empty)
1103
    //  0:        no data
1104
    //  negative: xxxxP.....Gxxx        [offer last chunk only]
1105
    //  positive: ..GxxxxxP.....
1106
    if (0 == cbuf->nb)
1107
        return CYG_XMT_EMPTY;
1108
 
1109
#ifdef CYGDBG_USE_ASSERTS
1110
    cbuf->block_mode_xfer_running = true;
1111
#endif
1112
 
1113
    if (cbuf->get >= cbuf->put) {
1114
        avail = cbuf->len - cbuf->get;
1115
    } else {
1116
        avail = cbuf->put - cbuf->get;
1117
    }
1118
 
1119
    if (avail > space) avail = space;   // bound by space in hardware
1120
 
1121
    *chars_avail = avail;
1122
    *chars = &cbuf->data[cbuf->get];
1123
 
1124
    CYG_ASSERT(avail <= cbuf->len, "Avail overflow");
1125
    CYG_ASSERT(cbuf->nb <= cbuf->len, "Buffer overflow");
1126
    CYG_ASSERT(cbuf->put < cbuf->len, "Invalid put ptr");
1127
    CYG_ASSERT(cbuf->get < cbuf->len, "Invalid get ptr");
1128
 
1129
    return CYG_XMT_OK;
1130
}
1131
 
1132
static void
1133
serial_data_xmt_done(serial_channel *chan, int chars_sent)
1134
{
1135
    cbuf_t *cbuf = &chan->out_cbuf;
1136
    serial_funs *funs = chan->funs;
1137
    int space;
1138
 
1139
    cbuf->get += chars_sent;
1140
    cbuf->nb -= chars_sent;
1141
 
1142
    if (cbuf->get == cbuf->len) cbuf->get = 0;
1143
 
1144
    CYG_ASSERT(cbuf->nb <= cbuf->len, "Buffer overflow");
1145
    CYG_ASSERT(cbuf->nb >= 0, "Buffer underflow");
1146
    CYG_ASSERT(cbuf->put < cbuf->len, "Invalid put ptr");
1147
    CYG_ASSERT(cbuf->get < cbuf->len, "Invalid get ptr");
1148
 
1149
    if (0 == cbuf->nb) {
1150
        (funs->stop_xmit)(chan);  // Done with transmit
1151
        cbuf->get = cbuf->put = 0; // reset ptrs if empty
1152
    }
1153
 
1154
    // See if there is now enough room to restart writer
1155
    space = cbuf->len - cbuf->nb;
1156
    if (space >= cbuf->low_water) {
1157
        if (cbuf->waiting) {
1158
            cbuf->waiting = false;
1159
            cyg_drv_cond_broadcast(&cbuf->wait);
1160
        }
1161
#ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
1162
        cyg_selwakeup( &cbuf->selinfo );
1163
#endif                    
1164
    }
1165
 
1166
#ifdef CYGDBG_USE_ASSERTS
1167
    cbuf->block_mode_xfer_running = false;
1168
#endif
1169
}
1170
 
1171
#endif // CYGINT_IO_SERIAL_BLOCK_TRANSFER
1172
 
1173
// ---------------------------------------------------------------------------
1174
 
1175
// EOF serial.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.