8 #include PLATFORM_HEADER
9 #include "hal/micro/micro-common.h"
15 #define _LLIO_STDIN ((int) stdin)
16 #define _LLIO_STDOUT ((int) stdout)
17 #define _LLIO_STDERR ((int) stderr)
18 #define _LLIO_ERROR (-1)
19 #define __write _write
22 void __io_putchar(
char c );
25 __io_putchar((
char) c);
29 #define RECEIVE_QUEUE_SIZE (128)
31 int8u rxQ[RECEIVE_QUEUE_SIZE];
44 assert( (baudrate >= 300) && (baudrate <=921600) );
46 tmp = (2*12000000L + baudrate/2) / baudrate;
47 SC1_UARTFRAC = tmp & 1;
48 SC1_UARTPER = tmp / 2;
53 tempcfg = SC_UART8BIT;
56 if (parity == PARITY_ODD) {
57 tempcfg |= SC_UARTPAR | SC_UARTODD;
58 }
else if( parity == PARITY_EVEN ) {
59 tempcfg |= SC_UARTPAR;
62 if ((stopbits & 0x0F) >= 2) {
63 tempcfg |= SC_UART2STP;
65 SC1_UARTCFG = tempcfg;
67 SC1_MODE = SC1_MODE_UART;
77 SC1_INTMODE = SC_RXVALLEVEL;
79 INT_SC1CFG |= (INT_SCRXVAL |
91 size_t __write(
int handle,
const unsigned char * buffer,
size_t size)
97 if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR) {
104 while ((SC1_UARTSTAT&SC_UARTTXIDLE)!=SC_UARTTXIDLE) {}
109 if(SC1_MODE != SC1_MODE_UART) {
115 while ((SC1_UARTSTAT&SC_UARTTXFREE)!=SC_UARTTXFREE) {}
128 size_t fflush(
int handle)
131 return __write(_LLIO_STDOUT,
NULL, 0);
134 static void halInternalUart1TxIsr(
void)
144 size_t __read(
int handle,
unsigned char * buffer,
size_t size)
150 if (handle != _LLIO_STDIN)
155 for(nChars = 0; (rxUsed>0) && (nChars < size); nChars++) {
157 *buffer++ = rxQ[rxTail];
158 rxTail = (rxTail+1) % RECEIVE_QUEUE_SIZE;
166 static void halInternalUart1RxIsr(
void)
173 while ( SC1_UARTSTAT & SC_UARTRXVAL ) {
174 int8u errors = SC1_UARTSTAT & (SC_UARTFRMERR |
177 int8u incoming = (int8u) SC1_DATA;
179 if ( (errors == 0) && (rxUsed < (RECEIVE_QUEUE_SIZE-1)) ) {
180 rxQ[rxHead] = incoming;
181 rxHead = (rxHead+1) % RECEIVE_QUEUE_SIZE;
201 interrupt = INT_SC1FLAG;
202 interrupt &= INT_SC1CFG;
204 while (interrupt != 0) {
206 INT_SC1FLAG = interrupt;
209 if ( interrupt & (INT_SCRXVAL |
214 halInternalUart1RxIsr();
218 if ( interrupt & (INT_SCTXFREE |
221 halInternalUart1TxIsr();
224 interrupt = INT_SC1FLAG;
225 interrupt &= INT_SC1CFG;
238 if (__read(_LLIO_STDIN,data,1))
244 void __io_putchar(
char c )
246 __write(_LLIO_STDOUT, (
unsigned char *)&c, 1);
252 __read(_LLIO_STDIN, &c, 1);
256 void __io_flush(
void )
258 __write(_LLIO_STDOUT,
NULL, 0);