#include "lpc213x.h"
#include "Includes.h"
#define BIT(x) (1<<x)
#define SET_BIT(x, y) (x|=(1<<y))
#define CLR_BIT(x, y) (x&=~(1<<y))
#define GET_BIT(x, y) (x&(1<<y))
volatile struct
{
INT8U rd_buf[32];
INT8U td_buf[2048];
INT16U rd_max;
INT16U td_max;
INT16U rd_cnt;
INT16U td_cnt;
} uart0;
OS_EVENT *uart0_rd_ok;
OS_EVENT *uart0_td_ok;
extern void UART0_Handler(void);
void UART0_Exception(void)
{
unsigned char i, k;
k = U0IIR&0x0F;
switch(k)
{
case 0x02:
for (i=0; i<16; i++)
{
if (uart0.td_cnt < uart0.td_max)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
}
else
{
U0IER = U0IER & (~0x02);
OSSemPost(uart0_td_ok);
goto UART0_Exception_RET;
}
}
break;
case 0x04:
for (i=0; i<8; i++)
{
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0_rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
}
break;
case 0x0C:
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0_rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
break;
default:
break;
}
UART0_Exception_RET:
VICVectAddr = 0x00;
}
void UART0_init(void)
{
INT16U i;
for (i=0; i<32; i++)
{
uart0.rd_buf[i] = 0x00;
}
for (i=0; i<2048; i++)
{
uart0.td_buf[i] = (unsigned char)i;
}
uart0.rd_max = 8;
uart0.td_max = 0;
uart0.rd_cnt = 0;
uart0.td_cnt = 0;
SET_BIT(PINSEL0, 0);
CLR_BIT(PINSEL0, 1);
SET_BIT(PINSEL0, 2);
CLR_BIT(PINSEL0, 3);
U0LCR = 0x83;
U0DLL = 0x06;
U0DLM = 0x00;
U0LCR = 0x03;
U0FCR = 0x87;
U0IER = 0x01;
VICIntSelect = 0x00000000;
VICVectCntl1 = 0x26;
VICVectAddr1 = (unsigned long)UART0_Handler;
VICIntEnable = (1<<6);
}
void UART0_putc(unsigned char c)
{
U0THR = c;
while ((U0LSR&0x40) == 0)
{
}
}
void UART0_puts(char *p)
{
while(*p)
{
UART0_putc(*p);
p++;
}
}
void UART0_send(INT16U len)
{
//unsigned short i;
//for (i=0; i<len; i++)
//{
// UART0_putc(uart0.td_buf[i]);
//}
uart0.td_cnt = 0;
uart0.td_max = len;
if (uart0.td_max > 0)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
U0IER = U0IER | 0x02; /* 允许发送中断 */
}
}
void UART0_task(void *pdata)
{
INT8U err;
pdata = pdata;
uart0_rd_ok = OSSemCreate(0);
uart0_td_ok = OSSemCreate(0);
UART0_init();
do
{
OSSemPend(uart0_rd_ok, 0, &err);
UART0_send(1024);
OSSemPend(uart0_td_ok, 0, &err);
uart0.rd_cnt = 0;
} while(1);
} |