1 //==========================================================================
3 // io/serial/common/serial.c
5 // High level serial driver
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 // Copyright (C) 2003 Gary Thomas
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
45 // Contributors: gthomas, grante, jlarmour, jskov
47 // Purpose: Top level serial driver
50 //####DESCRIPTIONEND####
52 //==========================================================================
54 #include <pkgconf/io.h>
55 #include <pkgconf/io_serial.h>
57 #include <cyg/io/io.h>
58 #include <cyg/io/devtab.h>
59 #include <cyg/io/serial.h>
60 #include <cyg/infra/cyg_ass.h> // assertion support
61 #include <cyg/infra/diag.h> // diagnostic output
63 static Cyg_ErrNo serial_write(cyg_io_handle_t handle, const void *buf, cyg_uint32 *len);
64 static Cyg_ErrNo serial_read(cyg_io_handle_t handle, void *buf, cyg_uint32 *len);
65 static cyg_bool serial_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info);
66 static Cyg_ErrNo serial_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *buf, cyg_uint32 *len);
67 static Cyg_ErrNo serial_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *buf, cyg_uint32 *len);
69 DEVIO_TABLE(cyg_io_serial_devio,
77 static void serial_init(serial_channel *chan);
78 static void serial_xmt_char(serial_channel *chan);
79 static void serial_rcv_char(serial_channel *chan, unsigned char c);
80 #ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
81 static void serial_indicate_status(serial_channel *chan,
82 cyg_serial_line_status_t *s);
84 #if CYGINT_IO_SERIAL_BLOCK_TRANSFER
85 static rcv_req_reply_t serial_data_rcv_req(serial_channel *chan, int avail,
87 unsigned char** space);
88 static void serial_data_rcv_done(serial_channel *chan, int chars_rcvd);
89 static xmt_req_reply_t serial_data_xmt_req(serial_channel *chan, int space,
91 unsigned char** chars);
92 static void serial_data_xmt_done(serial_channel *chan, int chars_sent);
93 # ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
94 SERIAL_CALLBACKS(cyg_io_serial_callbacks,
101 serial_data_xmt_done,
102 serial_indicate_status);
105 SERIAL_CALLBACKS(cyg_io_serial_callbacks,
110 serial_data_rcv_done,
112 serial_data_xmt_done);
115 # ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
116 SERIAL_CALLBACKS(cyg_io_serial_callbacks,
120 serial_indicate_status);
122 SERIAL_CALLBACKS(cyg_io_serial_callbacks,
129 // ---------------------------------------------------------------------------
131 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
133 static __inline__ void
134 throttle_tx( serial_channel *chan )
136 chan->flow_desc.flags |= CYG_SERIAL_FLOW_OUT_THROTTLED;
137 // the throttling itself occurs in the serial_xmt_char() callback
140 static __inline__ void
141 restart_tx( serial_channel *chan )
143 serial_funs *funs = chan->funs;
145 chan->flow_desc.flags &= ~CYG_SERIAL_FLOW_OUT_THROTTLED;
147 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
148 // See if there is now enough room to say it is available
151 cbuf_t *cbuf = &chan->out_cbuf;
154 space = cbuf->len - cbuf->nb;
155 if (space >= cbuf->low_water)
156 cyg_selwakeup( &cbuf->selinfo );
159 if ( chan->out_cbuf.nb > 0 )
160 (funs->start_xmit)(chan);
163 static __inline__ void
164 throttle_rx( serial_channel *chan, cyg_bool force )
166 serial_funs *funs = chan->funs;
167 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
168 cyg_uint32 prev_flags = chan->flow_desc.flags;
171 chan->flow_desc.flags |= CYG_SERIAL_FLOW_IN_THROTTLED;
172 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
173 // send an xoff if not already done (throttled)
175 ((chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_RX) &&
176 (0==(prev_flags & CYG_SERIAL_FLOW_IN_THROTTLED))) ) {
177 CYG_ASSERT(force||(chan->flow_desc.xchar=='\0')||(chan->flow_desc.xchar==CYGDAT_IO_SERIAL_FLOW_CONTROL_XOFF_CHAR), "xchar already set (XOFF)");
178 chan->flow_desc.xchar = CYGDAT_IO_SERIAL_FLOW_CONTROL_XOFF_CHAR;
179 // Make sure xmit is running so we can send it
180 (funs->start_xmit)(chan);
183 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
186 cyg_uint32 len = sizeof(i);
188 // set hardware flow control - don't care if it fails
189 if ( force || (chan->config.flags & CYGNUM_SERIAL_FLOW_RTSCTS_RX) ||
190 (chan->config.flags & CYGNUM_SERIAL_FLOW_DSRDTR_RX) )
191 (funs->set_config)(chan,
192 CYG_IO_SET_CONFIG_SERIAL_HW_RX_FLOW_THROTTLE,
198 static __inline__ void
199 restart_rx( serial_channel *chan, cyg_bool force )
201 serial_funs *funs = chan->funs;
202 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
203 cyg_uint32 prev_flags = chan->flow_desc.flags;
206 chan->flow_desc.flags &= ~CYG_SERIAL_FLOW_IN_THROTTLED;
207 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
208 // send an xon, if we haven't already
210 ((chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_RX) &&
211 (prev_flags & CYG_SERIAL_FLOW_IN_THROTTLED)) ) {
212 CYG_ASSERT(force||(chan->flow_desc.xchar=='\0')||(chan->flow_desc.xchar==CYGDAT_IO_SERIAL_FLOW_CONTROL_XON_CHAR), "xchar already set (XON)");
213 chan->flow_desc.xchar = CYGDAT_IO_SERIAL_FLOW_CONTROL_XON_CHAR;
214 (funs->start_xmit)(chan); // Make sure xmit is running so we can send it
217 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
220 cyg_uint32 len = sizeof(i);
222 // set hardware flow control - don't care if it fails
223 if ( force || (chan->config.flags & CYGNUM_SERIAL_FLOW_RTSCTS_RX) ||
224 (chan->config.flags & CYGNUM_SERIAL_FLOW_DSRDTR_RX) )
225 (funs->set_config)(chan,
226 CYG_IO_SET_CONFIG_SERIAL_HW_RX_FLOW_THROTTLE,
234 // ---------------------------------------------------------------------------
237 serial_init(serial_channel *chan)
239 if (chan->init) return;
240 if (chan->out_cbuf.len != 0) {
241 #ifdef CYGDBG_IO_INIT
242 diag_printf("Set output buffer - buf: %p len: %d\n", chan->out_cbuf.data, chan->out_cbuf.len);
244 chan->out_cbuf.waiting = false;
245 chan->out_cbuf.abort = false;
246 #ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
247 chan->out_cbuf.blocking = true;
249 chan->out_cbuf.pending = 0;
250 cyg_drv_mutex_init(&chan->out_cbuf.lock);
251 cyg_drv_cond_init(&chan->out_cbuf.wait, &chan->out_cbuf.lock);
252 chan->out_cbuf.low_water = chan->out_cbuf.len / 4;
253 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
254 cyg_selinit( &chan->out_cbuf.selinfo );
257 if (chan->in_cbuf.len != 0) {
258 cbuf_t *cbuf = &chan->in_cbuf;
260 #ifdef CYGDBG_IO_INIT
261 diag_printf("Set input buffer - buf: %p len: %d\n", cbuf->data, cbuf->len);
263 cbuf->waiting = false;
265 #ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
266 cbuf->blocking = true;
268 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
269 cyg_selinit( &cbuf->selinfo );
271 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
273 (CYGNUM_IO_SERIAL_FLOW_CONTROL_LOW_WATER_PERCENT * cbuf->len) / 100;
275 (CYGNUM_IO_SERIAL_FLOW_CONTROL_HIGH_WATER_PERCENT * cbuf->len) / 100;
276 # ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
277 // But make sure it is at least 35 below buffer size, to allow
278 // for 16 byte fifos, twice, plus some latency before s/w flow
279 // control can kick in. This doesn't apply to h/w flow control
280 // as it is near-instantaneous
281 if ( (cbuf->len - cbuf->high_water) < 35 )
282 cbuf->high_water = cbuf->len - 35;
283 // and just in case...
284 if ( cbuf->high_water <= 0 )
285 cbuf->high_water = 1;
286 if ( cbuf->low_water > cbuf->high_water )
287 cbuf->low_water = cbuf->high_water;
290 cyg_drv_mutex_init(&cbuf->lock);
291 cyg_drv_cond_init(&cbuf->wait, &cbuf->lock);
293 #ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
294 chan->status_callback = NULL;
297 #ifdef CYGDBG_USE_ASSERTS
298 #if CYGINT_IO_SERIAL_BLOCK_TRANSFER
299 chan->in_cbuf.block_mode_xfer_running = false;
300 chan->out_cbuf.block_mode_xfer_running = false;
301 #endif // CYGINT_IO_SERIAL_BLOCK_TRANSFER
302 #endif // CYGDBG_USE_ASSERTS
306 // ---------------------------------------------------------------------------
309 serial_write(cyg_io_handle_t handle, const void *_buf, cyg_uint32 *len)
311 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
312 serial_channel *chan = (serial_channel *)t->priv;
313 serial_funs *funs = chan->funs;
314 cyg_int32 size = *len;
315 cyg_uint8 *buf = (cyg_uint8 *)_buf;
317 cbuf_t *cbuf = &chan->out_cbuf;
318 Cyg_ErrNo res = ENOERR;
320 cyg_drv_mutex_lock(&cbuf->lock);
323 if (cbuf->len == 0) {
324 // Non interrupt driven (i.e. polled) operation
326 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
327 while ( ( 0 != (chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED) ) ||
328 ((funs->putc)(chan, *buf) == false) )
329 ; // Ignore full, keep trying
331 while ((funs->putc)(chan, *buf) == false)
332 ; // Ignore full, keep trying
337 cyg_drv_dsr_lock(); // Avoid race condition testing pointers
339 next = cbuf->put + 1;
340 if (next == cbuf->len) next = 0;
341 if (cbuf->nb == cbuf->len) {
342 cbuf->waiting = true;
343 // Buffer full - wait for space
344 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
345 if ( 0 == (chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED) )
347 (funs->start_xmit)(chan); // Make sure xmit is running
349 // Check flag: 'start_xmit' may have obviated the need
352 #ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
353 // Optionally return if configured for non-blocking mode.
354 if (!cbuf->blocking) {
355 *len -= size; // number of characters actually sent
356 cbuf->waiting = false;
357 res = (*len == 0) ? -EAGAIN : ENOERR;
360 #endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
361 cbuf->pending += size; // Have this much more to send [eventually]
362 if( !cyg_drv_cond_wait(&cbuf->wait) )
364 cbuf->pending -= size;
368 *len -= size; // number of characters actually sent
370 cbuf->waiting = false;
375 cbuf->data[cbuf->put++] = *buf++;
378 size--; // Only count if actually sent!
381 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
382 if ( 0 == (chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED) )
384 (funs->start_xmit)(chan); // Start output as necessary
385 cyg_drv_dsr_unlock();
387 cyg_drv_mutex_unlock(&cbuf->lock);
392 // ---------------------------------------------------------------------------
395 serial_read(cyg_io_handle_t handle, void *_buf, cyg_uint32 *len)
397 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
398 serial_channel *chan = (serial_channel *)t->priv;
399 serial_funs *funs = chan->funs;
400 cyg_uint8 *buf = (cyg_uint8 *)_buf;
402 cbuf_t *cbuf = &chan->in_cbuf;
403 Cyg_ErrNo res = ENOERR;
404 #ifdef XX_CYGDBG_DIAG_BUF
405 extern int enable_diag_uart;
406 int _enable = enable_diag_uart;
408 externC cyg_tick_count_t cyg_current_time(void);
409 #endif // CYGDBG_DIAG_BUF
411 cyg_drv_mutex_lock(&cbuf->lock);
414 if (cbuf->len == 0) {
415 // Non interrupt driven (i.e. polled) operation
416 while (size++ < *len) {
417 cyg_uint8 c = (funs->getc)(chan);
418 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
419 // for software flow control, if the driver returns one of the
420 // characters we act on it and then drop it (the app must not
422 if ( chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_TX ) {
423 if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XOFF_CHAR ) {
425 } else if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XON_CHAR ) {
438 cyg_drv_dsr_lock(); // Avoid races
439 while (size < *len) {
441 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
442 if ( (cbuf->nb <= cbuf->low_water) &&
443 (chan->flow_desc.flags & CYG_SERIAL_FLOW_IN_THROTTLED) )
444 restart_rx( chan, false );
446 *buf++ = cbuf->data[cbuf->get];
447 if (++cbuf->get == cbuf->len) cbuf->get = 0;
451 #ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
452 if (!cbuf->blocking) {
453 *len = size; // characters actually read
454 res = size == 0 ? -EAGAIN : ENOERR;
457 #endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
458 cbuf->waiting = true;
459 #ifdef XX_CYGDBG_DIAG_BUF
460 enable_diag_uart = 0;
461 HAL_CLOCK_READ(&_time);
462 _stime = (int)cyg_current_time();
463 diag_printf("READ wait - get: %d, put: %d, time: %x.%x\n", cbuf->get, cbuf->put, _stime, _time);
464 enable_diag_uart = _enable;
465 #endif // CYGDBG_DIAG_BUF
466 if( !cyg_drv_cond_wait(&cbuf->wait) )
468 #ifdef XX_CYGDBG_DIAG_BUF
469 enable_diag_uart = 0;
470 HAL_CLOCK_READ(&_time);
471 _stime = (int)cyg_current_time();
472 diag_printf("READ continue - get: %d, put: %d, time: %x.%x\n", cbuf->get, cbuf->put, _stime, _time);
473 enable_diag_uart = _enable;
474 #endif // CYGDBG_DIAG_BUF
477 *len = size; // characters actually read
479 cbuf->waiting = false;
485 cyg_drv_dsr_unlock();
487 #ifdef XX_CYGDBG_DIAG_BUF
489 enable_diag_uart = 0;
490 HAL_CLOCK_READ(&_time);
491 _stime = (int)cyg_current_time();
492 diag_printf("READ done - size: %d, len: %d, time: %x.%x\n", size, *len, _stime, _time);
493 enable_diag_uart = _enable;
494 cyg_drv_isr_unlock();
495 #endif // CYGDBG_DIAG_BUF
496 cyg_drv_mutex_unlock(&cbuf->lock);
501 // ---------------------------------------------------------------------------
504 serial_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info)
506 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
508 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
509 serial_channel *chan = (serial_channel *)t->priv;
510 cyg_bool retval = false;
516 cbuf_t *cbuf = &chan->in_cbuf;
518 cyg_drv_mutex_lock(&cbuf->lock);
519 cyg_drv_dsr_lock(); // Avoid races
521 // Check for data in the input buffer. If there is none,
522 // register the select operation, otherwise return true.
525 cyg_selrecord( info, &cbuf->selinfo );
528 cyg_drv_dsr_unlock();
529 cyg_drv_mutex_unlock(&cbuf->lock);
535 // Check for space in the output buffer. If there is none,
536 // register the select operation, otherwise return true.
539 cbuf_t *cbuf = &chan->out_cbuf;
541 cyg_drv_mutex_lock(&cbuf->lock);
542 cyg_drv_dsr_lock(); // Avoid races
544 space = cbuf->len - cbuf->nb;
545 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
546 if ( (space < cbuf->low_water) ||
547 (chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED) )
548 cyg_selrecord( info, &cbuf->selinfo );
550 if (space < cbuf->low_water)
551 cyg_selrecord( info, &cbuf->selinfo );
555 cyg_drv_dsr_unlock();
556 cyg_drv_mutex_unlock(&cbuf->lock);
560 case 0: // exceptions - none supported
566 // With no select support, we simply return true.
572 // ---------------------------------------------------------------------------
575 serial_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *xbuf,
578 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
579 serial_channel *chan = (serial_channel *)t->priv;
580 cyg_serial_info_t *buf = (cyg_serial_info_t *)xbuf;
581 Cyg_ErrNo res = ENOERR;
582 cbuf_t *out_cbuf = &chan->out_cbuf;
583 cbuf_t *in_cbuf = &chan->in_cbuf;
584 serial_funs *funs = chan->funs;
587 case CYG_IO_GET_CONFIG_SERIAL_INFO:
588 if (*len < sizeof(cyg_serial_info_t)) {
592 *len = sizeof(chan->config);
595 case CYG_IO_GET_CONFIG_SERIAL_BUFFER_INFO:
596 // return rx/tx buffer sizes and counts
598 cyg_serial_buf_info_t *p;
599 if (*len < sizeof(cyg_serial_buf_info_t))
602 *len = sizeof(cyg_serial_buf_info_t);
603 p = (cyg_serial_buf_info_t *)xbuf;
605 p->rx_bufsize = in_cbuf->len;
607 p->rx_count = in_cbuf->nb;
611 p->tx_bufsize = out_cbuf->len;
613 p->tx_count = out_cbuf->nb;
619 case CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN:
620 // Wait for any pending output to complete
621 if (out_cbuf->len == 0) break; // Nothing to do if not buffered
622 cyg_drv_mutex_lock(&out_cbuf->lock); // Stop any further output processing
624 while (out_cbuf->pending || (out_cbuf->nb > 0)) {
625 out_cbuf->waiting = true;
626 if(!cyg_drv_cond_wait(&out_cbuf->wait) )
629 cyg_drv_dsr_unlock();
630 cyg_drv_mutex_unlock(&out_cbuf->lock);
633 case CYG_IO_GET_CONFIG_SERIAL_INPUT_FLUSH:
634 // Flush any buffered input
635 if (in_cbuf->len == 0) break; // Nothing to do if not buffered
636 cyg_drv_mutex_lock(&in_cbuf->lock); // Stop any further input processing
638 if (in_cbuf->waiting) {
639 in_cbuf->abort = true;
640 cyg_drv_cond_broadcast(&in_cbuf->wait);
641 in_cbuf->waiting = false;
643 in_cbuf->get = in_cbuf->put = in_cbuf->nb = 0; // Flush buffered input
645 // Pass to the hardware driver in case it wants to flush FIFOs etc.
646 (funs->set_config)(chan,
647 CYG_IO_SET_CONFIG_SERIAL_INPUT_FLUSH,
649 cyg_drv_dsr_unlock();
650 cyg_drv_mutex_unlock(&in_cbuf->lock);
651 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
652 // Restart receiver if it was shutdown
653 if ((chan->flow_desc.flags & CYG_SERIAL_FLOW_IN_THROTTLED) != 0) {
654 restart_rx( chan, false );
659 case CYG_IO_GET_CONFIG_SERIAL_ABORT:
660 // Abort any outstanding I/O, including blocked reads
661 // Caution - assumed to be called from 'timeout' (i.e. DSR) code
662 if (in_cbuf->len != 0) {
663 in_cbuf->abort = true;
664 cyg_drv_cond_broadcast(&in_cbuf->wait);
666 if (out_cbuf->len != 0) {
667 out_cbuf->abort = true;
668 cyg_drv_cond_broadcast(&out_cbuf->wait);
672 case CYG_IO_GET_CONFIG_SERIAL_OUTPUT_FLUSH:
673 // Throw away any pending output
674 if (out_cbuf->len == 0) break; // Nothing to do if not buffered
675 cyg_drv_mutex_lock(&out_cbuf->lock); // Stop any further output processing
677 if (out_cbuf->nb > 0) {
678 out_cbuf->get = out_cbuf->put = out_cbuf->nb = 0; // Empties queue!
679 (funs->stop_xmit)(chan); // Done with transmit
681 // Pass to the hardware driver in case it wants to flush FIFOs etc.
682 (funs->set_config)(chan,
683 CYG_IO_SET_CONFIG_SERIAL_OUTPUT_FLUSH,
685 if (out_cbuf->waiting) {
686 out_cbuf->abort = true;
687 cyg_drv_cond_broadcast(&out_cbuf->wait);
688 out_cbuf->waiting = false;
690 cyg_drv_dsr_unlock();
691 cyg_drv_mutex_unlock(&out_cbuf->lock);
694 #ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
695 case CYG_IO_GET_CONFIG_READ_BLOCKING:
696 if (*len < sizeof(cyg_uint32)) {
699 *(cyg_uint32*)xbuf = (in_cbuf->blocking) ? 1 : 0;
702 case CYG_IO_GET_CONFIG_WRITE_BLOCKING:
703 if (*len < sizeof(cyg_uint32)) {
706 *(cyg_uint32*)xbuf = (out_cbuf->blocking) ? 1 : 0;
708 #endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
717 // ---------------------------------------------------------------------------
720 serial_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *xbuf,
723 Cyg_ErrNo res = ENOERR;
724 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
725 serial_channel *chan = (serial_channel *)t->priv;
726 #ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
727 cbuf_t *out_cbuf = &chan->out_cbuf;
728 cbuf_t *in_cbuf = &chan->in_cbuf;
730 serial_funs *funs = chan->funs;
733 #ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
734 case CYG_IO_SET_CONFIG_READ_BLOCKING:
735 if (*len < sizeof(cyg_uint32) || 0 == in_cbuf->len) {
738 in_cbuf->blocking = (1 == *(cyg_uint32*)xbuf) ? true : false;
740 case CYG_IO_SET_CONFIG_WRITE_BLOCKING:
741 if (*len < sizeof(cyg_uint32) || 0 == out_cbuf->len) {
744 out_cbuf->blocking = (1 == *(cyg_uint32*)xbuf) ? true : false;
746 #endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
748 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
749 case CYG_IO_SET_CONFIG_SERIAL_FLOW_CONTROL_METHOD:
751 cyg_uint32 *f = (cyg_uint32 *)xbuf;
753 if (*len < sizeof(*f))
758 chan->config.flags &= ~(CYGNUM_SERIAL_FLOW_XONXOFF_RX|
759 CYGNUM_SERIAL_FLOW_XONXOFF_TX|
760 CYGNUM_SERIAL_FLOW_RTSCTS_RX|
761 CYGNUM_SERIAL_FLOW_RTSCTS_TX|
762 CYGNUM_SERIAL_FLOW_DSRDTR_RX|
763 CYGNUM_SERIAL_FLOW_DSRDTR_TX);
764 chan->config.flags |= (*f & (
765 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
766 CYGNUM_SERIAL_FLOW_XONXOFF_RX|
767 CYGNUM_SERIAL_FLOW_XONXOFF_TX|
769 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
770 CYGNUM_SERIAL_FLOW_RTSCTS_RX|
771 CYGNUM_SERIAL_FLOW_RTSCTS_TX|
772 CYGNUM_SERIAL_FLOW_DSRDTR_RX|
773 CYGNUM_SERIAL_FLOW_DSRDTR_TX|
776 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
777 // up to hardware driver to clear flags if rejected
778 res = (funs->set_config)(chan,
779 CYG_IO_SET_CONFIG_SERIAL_HW_FLOW_CONFIG,
782 cyg_drv_dsr_unlock();
786 case CYG_IO_SET_CONFIG_SERIAL_FLOW_CONTROL_FORCE:
788 cyg_uint32 *f = (cyg_uint32 *)xbuf;
790 if (*len < sizeof(*f))
795 case CYGNUM_SERIAL_FLOW_THROTTLE_RX:
796 throttle_rx( chan, true );
798 case CYGNUM_SERIAL_FLOW_RESTART_RX:
799 restart_rx( chan, true );
801 case CYGNUM_SERIAL_FLOW_THROTTLE_TX:
804 case CYGNUM_SERIAL_FLOW_RESTART_TX:
811 cyg_drv_dsr_unlock();
814 #endif // CYGPKG_IO_SERIAL_FLOW_CONTROL
816 #ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
817 case CYG_IO_SET_CONFIG_SERIAL_STATUS_CALLBACK:
819 cyg_serial_line_status_callback_fn_t newfn;
820 CYG_ADDRWORD newpriv;
821 cyg_serial_line_status_callback_t *tmp =
822 (cyg_serial_line_status_callback_t *)xbuf;
824 if ( *len < sizeof(*tmp) )
830 // prevent callbacks while we do this
832 // store old callbacks in same structure
833 tmp->fn = chan->status_callback;
834 tmp->priv = chan->status_callback_priv;
835 chan->status_callback = newfn;
836 chan->status_callback_priv = newpriv;
837 cyg_drv_dsr_unlock();
844 // pass down to lower layers
845 return (funs->set_config)(chan, key, xbuf, len);
850 // ---------------------------------------------------------------------------
853 serial_xmt_char(serial_channel *chan)
855 cbuf_t *cbuf = &chan->out_cbuf;
856 serial_funs *funs = chan->funs;
860 #if CYGINT_IO_SERIAL_BLOCK_TRANSFER
861 CYG_ASSERT(false == cbuf->block_mode_xfer_running,
862 "Attempting char xmt while block transfer is running");
864 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
865 // if we are required to send an XON/XOFF char, send it before
867 // FIXME: what if XON gets corrupted in transit to the other end?
868 // Should we resend XON even though the other end may not be wanting
869 // to send us stuff at this point?
870 if ( chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_RX ) {
871 if ( chan->flow_desc.xchar ) {
872 if ( (funs->putc)(chan, chan->flow_desc.xchar) ) {
873 chan->flow_desc.xchar = '\0';
874 } else { // otherwise there's no space and we have to wait
880 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
881 // if we're meant to be throttled, just stop and leave
882 if ( chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED ) {
883 (funs->stop_xmit)(chan); // Stop transmitting for now
887 while (cbuf->nb > 0) {
888 c = cbuf->data[cbuf->get];
889 if ((funs->putc)(chan, c)) {
891 if (cbuf->get == cbuf->len) cbuf->get = 0;
894 // See if there is now enough room to restart writer
895 space = cbuf->len - cbuf->nb;
896 if (space >= cbuf->low_water) {
898 cbuf->waiting = false;
899 cyg_drv_cond_broadcast(&cbuf->wait);
901 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
902 cyg_selwakeup( &cbuf->selinfo );
905 return; // Need to wait for more space
908 (funs->stop_xmit)(chan); // Done with transmit
910 // must signal waiters, and wake up selecters for the case when
911 // this was the last char to be sent and they hadn't been signalled
912 // before (e.g. because of flow control)
914 cbuf->waiting = false;
915 cyg_drv_cond_broadcast(&cbuf->wait);
917 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
918 cyg_selwakeup( &cbuf->selinfo );
922 // ---------------------------------------------------------------------------
925 serial_rcv_char(serial_channel *chan, unsigned char c)
927 cbuf_t *cbuf = &chan->in_cbuf;
929 #if CYGINT_IO_SERIAL_BLOCK_TRANSFER
930 CYG_ASSERT(false == cbuf->block_mode_xfer_running,
931 "Attempting char rcv while block transfer is running");
933 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
934 // for software flow control, if the driver returns one of the characters
935 // we act on it and then drop it (the app must not see it)
936 if ( chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_TX ) {
937 if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XOFF_CHAR ) {
939 return; // it wasn't a "real" character
940 } else if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XON_CHAR ) {
942 return; // it wasn't a "real" character
946 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
947 // If we've hit the high water mark, tell the other side to stop
948 if ( cbuf->nb >= cbuf->high_water ) {
949 throttle_rx( chan, false );
952 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
953 // Wake up any pending selectors if we are about to
954 // put some data into a previously empty buffer.
956 cyg_selwakeup( &cbuf->selinfo );
959 // If the flow control is not enabled/sufficient and the buffer is
960 // already full, just throw new characters away.
962 if ( cbuf->nb < cbuf->len ) {
963 cbuf->data[cbuf->put++] = c;
964 if (cbuf->put == cbuf->len) cbuf->put = 0;
966 } // note trailing else
967 #ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
969 // Overrun. Report the error.
970 cyg_serial_line_status_t stat;
971 stat.which = CYGNUM_SERIAL_STATUS_OVERRUNERR;
972 serial_indicate_status(chan, &stat);
977 #ifdef XX_CYGDBG_DIAG_BUF
978 extern int enable_diag_uart;
979 int _enable = enable_diag_uart;
981 externC cyg_tick_count_t cyg_current_time(void);
982 enable_diag_uart = 0;
983 HAL_CLOCK_READ(&_time);
984 _stime = (int)cyg_current_time();
985 diag_printf("Signal reader - time: %x.%x\n", _stime, _time);
986 enable_diag_uart = _enable;
987 #endif // CYGDBG_DIAG_BUF
988 cbuf->waiting = false;
989 cyg_drv_cond_broadcast(&cbuf->wait);
993 //----------------------------------------------------------------------------
994 // Flow control indication callback
996 #ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
998 serial_indicate_status(serial_channel *chan, cyg_serial_line_status_t *s )
1000 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
1001 if ( CYGNUM_SERIAL_STATUS_FLOW == s->which ) {
1005 throttle_tx( chan );
1008 if ( chan->status_callback )
1009 (*chan->status_callback)(s, chan->status_callback_priv);
1011 #endif // ifdef CYGOPT_IO_SERIAL_SUPPORT_LINE_STATUS
1013 //----------------------------------------------------------------------------
1014 // Block transfer functions. Not all drivers require these. Those that
1015 // do must follow the required semantics:
1017 // Attempt to transfer as much via the block transfer function as
1018 // possible, _but_ if that fails, do the remaining bytes via the
1019 // single-char function. That ensures that all policy decisions can be
1020 // made in this driver, and not in the device driver.
1022 // Note: if the driver uses DMA for transmission, an initial failing
1023 // call to the xmt_req function must cause the start_xmit function to
1024 // fall-back to regular CPU-interrupt based single-character
1027 #if CYGINT_IO_SERIAL_BLOCK_TRANSFER
1029 static rcv_req_reply_t
1030 serial_data_rcv_req(serial_channel *chan, int avail,
1031 int* space_avail, unsigned char** space)
1033 cbuf_t *cbuf = &chan->in_cbuf;
1036 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
1037 // When there is software flow-control, force the serial device
1038 // driver to use the single-char xmt/rcv functions, since these
1039 // have to make policy decision based on the data. Rcv function
1040 // may also have to transmit data to throttle the xmitter.
1041 if (chan->config.flags & (CYGNUM_SERIAL_FLOW_XONXOFF_TX|CYGNUM_SERIAL_FLOW_XONXOFF_RX))
1042 return CYG_RCV_DISABLED;
1045 CYG_ASSERT(false == cbuf->block_mode_xfer_running,
1046 "Attempting new block transfer while another is running");
1049 if (gap == cbuf->len)
1050 return CYG_RCV_FULL;
1052 #ifdef CYGDBG_USE_ASSERTS
1053 cbuf->block_mode_xfer_running = true;
1057 // Buffer is empty. Reset put/get indexes to get max transfer in
1063 // Free space (G = get, P = put, x = data, . = empty)
1064 // positive: xxxxP.....Gxxx
1065 // negative: ..GxxxxxP..... [offer last chunk only]
1067 // First try for a gap between put and get locations
1068 gap = cbuf->get - cbuf->put;
1070 // If failed, the gap is between put and the end of buffer
1071 gap = cbuf->len - cbuf->put;
1075 if (avail < gap) gap = avail; // bound by what's available from hw
1078 *space = &cbuf->data[cbuf->put];
1080 CYG_ASSERT((gap+cbuf->nb) <= cbuf->len, "Buffer will overflow");
1081 CYG_ASSERT(cbuf->put < cbuf->len, "Invalid put ptr");
1082 CYG_ASSERT(cbuf->get < cbuf->len, "Invalid get ptr");
1088 serial_data_rcv_done(serial_channel *chan, int chars_rcvd)
1090 cbuf_t *cbuf = &chan->in_cbuf;
1092 cbuf->put += chars_rcvd;
1093 cbuf->nb += chars_rcvd;
1095 if (cbuf->put == cbuf->len) cbuf->put = 0;
1097 CYG_ASSERT(cbuf->nb <= cbuf->len, "Buffer overflow");
1098 CYG_ASSERT(cbuf->put < cbuf->len, "Invalid put ptr");
1099 CYG_ASSERT(cbuf->get < cbuf->len, "Invalid get ptr");
1101 if (cbuf->waiting) {
1102 cbuf->waiting = false;
1103 cyg_drv_cond_broadcast(&cbuf->wait);
1105 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
1106 // If we've hit the high water mark, tell the other side to stop
1107 if ( cbuf->nb >= cbuf->high_water ) {
1108 throttle_rx( chan, false );
1111 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
1112 // Wake up any pending selectors if we have
1113 // put some data into a previously empty buffer.
1114 if (chars_rcvd == cbuf->nb)
1115 cyg_selwakeup( &cbuf->selinfo );
1118 #ifdef CYGDBG_USE_ASSERTS
1119 cbuf->block_mode_xfer_running = false;
1123 static xmt_req_reply_t
1124 serial_data_xmt_req(serial_channel *chan, int space,
1125 int* chars_avail, unsigned char** chars)
1127 cbuf_t *cbuf = &chan->out_cbuf;
1130 #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
1131 // When there is software flow-control, force the serial device
1132 // driver to use the single-char xmt/rcv functions, since these
1133 // have to make policy decision based on the data. Rcv function
1134 // may also have to transmit data to throttle the xmitter.
1135 if (chan->config.flags & (CYGNUM_SERIAL_FLOW_XONXOFF_TX|CYGNUM_SERIAL_FLOW_XONXOFF_RX))
1136 return CYG_XMT_DISABLED;
1139 CYG_ASSERT(false == cbuf->block_mode_xfer_running,
1140 "Attempting new block transfer while another is running");
1142 #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
1143 // if we're meant to be throttled, just stop and leave
1144 if ( chan->flow_desc.flags & CYG_SERIAL_FLOW_OUT_THROTTLED ) {
1145 (chan->funs->stop_xmit)(chan); // Stop transmitting for now
1146 return CYG_XMT_EMPTY;
1150 // Available data (G = get, P = put, x = data, . = empty)
1152 // negative: xxxxP.....Gxxx [offer last chunk only]
1153 // positive: ..GxxxxxP.....
1155 return CYG_XMT_EMPTY;
1157 #ifdef CYGDBG_USE_ASSERTS
1158 cbuf->block_mode_xfer_running = true;
1161 if (cbuf->get >= cbuf->put) {
1162 avail = cbuf->len - cbuf->get;
1164 avail = cbuf->put - cbuf->get;
1167 if (avail > space) avail = space; // bound by space in hardware
1169 *chars_avail = avail;
1170 *chars = &cbuf->data[cbuf->get];
1172 CYG_ASSERT(avail <= cbuf->len, "Avail overflow");
1173 CYG_ASSERT(cbuf->nb <= cbuf->len, "Buffer overflow");
1174 CYG_ASSERT(cbuf->put < cbuf->len, "Invalid put ptr");
1175 CYG_ASSERT(cbuf->get < cbuf->len, "Invalid get ptr");
1181 serial_data_xmt_done(serial_channel *chan, int chars_sent)
1183 cbuf_t *cbuf = &chan->out_cbuf;
1184 serial_funs *funs = chan->funs;
1187 cbuf->get += chars_sent;
1188 cbuf->nb -= chars_sent;
1190 if (cbuf->get == cbuf->len) cbuf->get = 0;
1192 CYG_ASSERT(cbuf->nb <= cbuf->len, "Buffer overflow");
1193 CYG_ASSERT(cbuf->nb >= 0, "Buffer underflow");
1194 CYG_ASSERT(cbuf->put < cbuf->len, "Invalid put ptr");
1195 CYG_ASSERT(cbuf->get < cbuf->len, "Invalid get ptr");
1197 if (0 == cbuf->nb) {
1198 (funs->stop_xmit)(chan); // Done with transmit
1199 cbuf->get = cbuf->put = 0; // reset ptrs if empty
1202 // See if there is now enough room to restart writer
1203 space = cbuf->len - cbuf->nb;
1204 if (space >= cbuf->low_water) {
1205 if (cbuf->waiting) {
1206 cbuf->waiting = false;
1207 cyg_drv_cond_broadcast(&cbuf->wait);
1209 #ifdef CYGPKG_IO_SERIAL_SELECT_SUPPORT
1210 cyg_selwakeup( &cbuf->selinfo );
1214 #ifdef CYGDBG_USE_ASSERTS
1215 cbuf->block_mode_xfer_running = false;
1219 #endif // CYGINT_IO_SERIAL_BLOCK_TRANSFER
1221 // ---------------------------------------------------------------------------