00001 00007 /* Embedded XINU, Copyright (C) 2007. All rights reserved. */ 00008 00009 #include <kernel.h> 00010 #include <uart.h> 00011 #include <device.h> 00012 00020 devcall uartRead(device *pdev, unsigned char *buf, int len) 00021 { 00022 irqmask ps; 00023 int count = 0; 00024 char c; 00025 struct uart *puart; 00026 00027 /* Setup and error check pointers to structures */ 00028 ASSERT(NULL != pdev); 00029 puart = (struct uart *)pdev->controlblk; 00030 ASSERT(puart != NULL); 00031 00032 ps = disable(); 00033 00034 /* If in non-blocking mode, ensure there is */ 00035 /* enough input for the entire read request */ 00036 if(puart->iflags & UART_IFLAG_NOBLOCK) 00037 { 00038 if (scount(puart->isema) < len) 00039 { restore(ps); return SYSERR; } 00040 } 00041 00042 /* Put each character into the buffer from the input buffer */ 00043 while (count < len) 00044 { 00045 /* If in non-blocking mode, ensure there is another byte of input */ 00046 if(puart->iflags & UART_IFLAG_NOBLOCK) 00047 { 00048 if (scount(puart->isema) < 1) 00049 { break; } 00050 } 00051 00052 /* Wait for input and read character from the */ 00053 /* input buffer; Preserve the circular buffer */ 00054 wait(puart->isema); 00055 c = puart->in[puart->istart]; 00056 *buf++ = c; 00057 puart->icount--; 00058 puart->istart = (puart->istart + 1) % UART_IBLEN; 00059 count++; 00060 00061 /* If echo is enabled, echo the character */ 00062 if (puart->iflags & UART_IFLAG_ECHO) 00063 { uartPutChar(puart->dev,c); } 00064 } 00065 00066 restore(ps); 00067 return count; 00068 }