Advertisement
Guest User

Ankur Patel

a guest
Mar 13th, 2010
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 KB | None | 0 0
  1. static Cyg_ErrNo serial_read(cyg_io_handle_t handle, void *_buf, cyg_uint32 *len)
  2. {
  3.     cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
  4.     serial_channel *chan = (serial_channel *)t->priv;
  5.     serial_funs *funs = chan->funs;
  6.     cyg_uint8 *buf = (cyg_uint8 *)_buf;
  7.     cyg_int32 size = 0;
  8.     cbuf_t *cbuf = &chan->in_cbuf;
  9.     Cyg_ErrNo res = ENOERR;
  10. //      *len = 1;
  11. #ifdef XX_CYGDBG_DIAG_BUF
  12.             extern int enable_diag_uart;
  13.             int _enable = enable_diag_uart;
  14.             int _time, _stime;
  15.             externC cyg_tick_count_t cyg_current_time(void);
  16. #endif // CYGDBG_DIAG_BUF
  17.  
  18.     cyg_drv_mutex_lock(&cbuf->lock);
  19.     cbuf->abort = false;
  20.  
  21. printf("under serial_read\r\n");
  22.     if (cbuf->len == 0) {
  23.         // Non interrupt driven (i.e. polled) operation
  24.     printf("Before While size %d len %d\r\n",size,*len);
  25.         while (size++ < *len) {
  26.        printf("size %d len %d\r\n",size,*len);
  27.             cyg_uint8 c = (funs->getc)(chan);
  28.         printf("polling base %d %c\r\n",size,c);
  29. #ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_SOFTWARE
  30.             // for software flow control, if the driver returns one of the
  31.             // characters we act on it and then drop it (the app must not
  32.             // see it)
  33.             if ( chan->config.flags & CYGNUM_SERIAL_FLOW_XONXOFF_TX ) {
  34.                 if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XOFF_CHAR ) {
  35.                     throttle_tx( chan );
  36.                 } else if ( c == CYGDAT_IO_SERIAL_FLOW_CONTROL_XON_CHAR ) {
  37.                     restart_tx( chan );
  38.                 }
  39.                 else
  40.                     *buf++ = c;
  41.             }
  42.             else
  43.                 *buf++ = c;
  44. #else
  45.             *buf++ = c;
  46. #endif    
  47.         }
  48.     } else {
  49.     printf("interrupt driver \r\n");
  50.         cyg_drv_dsr_lock();  // Avoid races
  51.         while (size < *len) {
  52.             if (cbuf->nb > 0) {
  53. #ifdef CYGPKG_IO_SERIAL_FLOW_CONTROL
  54.                 if ( (cbuf->nb <= cbuf->low_water) &&
  55.                      (chan->flow_desc.flags & CYG_SERIAL_FLOW_IN_THROTTLED) )
  56.                     restart_rx( chan, false );
  57. #endif
  58.                 *buf++ = cbuf->data[cbuf->get];
  59.                 if (++cbuf->get == cbuf->len) cbuf->get = 0;
  60.                 cbuf->nb--;
  61.                 size++;
  62.             } else {
  63. #ifdef CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
  64.                 if (!cbuf->blocking) {
  65.                     *len = size;        // characters actually read
  66.                     res = size == 0 ? -EAGAIN : ENOERR;
  67.                     break;
  68.                 }
  69. #endif // CYGOPT_IO_SERIAL_SUPPORT_NONBLOCKING
  70.                 cbuf->waiting = true;
  71. #ifdef XX_CYGDBG_DIAG_BUF
  72.                 enable_diag_uart = 0;
  73.                 HAL_CLOCK_READ(&_time);
  74.                 _stime = (int)cyg_current_time();
  75.                 diag_printf("READ wait - get: %d, put: %d, time: %x.%x\n", cbuf->get, cbuf->put, _stime, _time);
  76.                 enable_diag_uart = _enable;
  77. #endif // CYGDBG_DIAG_BUF
  78.                 if( !cyg_drv_cond_wait(&cbuf->wait) )
  79.                     cbuf->abort = true;
  80. #ifdef XX_CYGDBG_DIAG_BUF
  81.                 enable_diag_uart = 0;
  82.                 HAL_CLOCK_READ(&_time);
  83.                 _stime = (int)cyg_current_time();
  84.                 diag_printf("READ continue - get: %d, put: %d, time: %x.%x\n", cbuf->get, cbuf->put, _stime, _time);
  85.                 enable_diag_uart = _enable;
  86. #endif // CYGDBG_DIAG_BUF
  87.                 if (cbuf->abort) {
  88.                     // Give up!
  89.                     *len = size;        // characters actually read
  90.                     cbuf->abort = false;
  91.                     cbuf->waiting = false;
  92.                     res = -EINTR;
  93.                     break;
  94.                 }
  95.             }
  96.         }
  97.         cyg_drv_dsr_unlock();
  98.     }
  99. #ifdef XX_CYGDBG_DIAG_BUF
  100.     cyg_drv_isr_lock();
  101.     enable_diag_uart = 0;
  102.     HAL_CLOCK_READ(&_time);
  103.     _stime = (int)cyg_current_time();
  104.     diag_printf("READ done - size: %d, len: %d, time: %x.%x\n", size, *len, _stime, _time);
  105.     enable_diag_uart = _enable;
  106.     cyg_drv_isr_unlock();
  107. #endif // CYGDBG_DIAG_BUF
  108.     cyg_drv_mutex_unlock(&cbuf->lock);
  109.     return res;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement