#include "uart.h"
UartObj::UartObj(void)
{
Init();
}
void UartObj::Init(void)
{
unsigned int i;
for (i = 0; i < sizeof(ReceiveBuffer); i ++) {
ReceiveBuffer = 0;
SendBuffer = 0;
}
ReceiveWritePtr = 0;
ReceiveReadPtr = 0;
ReceivePosition = 0;
ReceiveCount = 0;
SendWritePtr = 0;
SendReadPtr = 0;
SendBusy = false;
State = 0;
EALLOW;
GpioMuxRegs.GPFMUX.bit.SCITXDA_GPIOF4 = 1;
GpioMuxRegs.GPFMUX.bit.SCIRXDA_GPIOF5 = 1;
EDIS;
/* loopback 8 bit data */
SciaRegs.SCICCR.all = 0x07; // 1 bit stop, disable parity, idle mode, 8 bits data
SciaRegs.SCICTL1.all = 0x03; // enable TX
SciaRegs.SCICTL2.all = 0x03; //
SciaRegs.SCIHBAUD = 0;
SciaRegs.SCILBAUD = 0xf3;//SCI_PRD & 0xff;//0xF3;
SciaRegs.SCICTL1.all = 0x23;
// PieCtrlRegs.PIECRTL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER9.bit.INTx1 = 1;
PieCtrlRegs.PIEIER9.bit.INTx2 = 1;
/*设置中断服务程序入口地址*/
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TXAINT = &ISRTxUart;
PieVectTable.RXAINT = &ISRRxUart;
EDIS; // This is needed to disable write to EALLOW protected registers
/*开中断*/
IER |= M_INT9;
}
void UartObj::putchar(const char dat)
{
char ch;
ch = dat;
SendBuffer[SendWritePtr ++] = ch;//写入发送缓冲区(并非FIFO!!!)
SendWritePtr &= 0xff;
if ((!SendBusy) && (dat == 0x0a))
{
SendBusy = true;
SciaRegs.SCITXBUF = SendBuffer[SendReadPtr ++];
SendReadPtr &= 0xff;
// asm(" TRAP #6");//73
// SciaRegs.SCICTL2.bit.TXINTENA = 1;//自动触发
}
}
void UartObj::putstr(const char *str)
{
while (*str)
{
putchar(*str);
str++;
}
}
void UartObj::puts(const char *str)
{
while (*str)
{
putchar(*str);
str++;
}
putchar(0x0d);
putchar(0x0a);//回车换行
}
void UartObj::puthex(unsigned long data)
{
signed char i;
char str[9];
unsigned char val;
for (i = 7; i >= 0; i --)
{
val = data & 0x0f;
data >>= 4;
if (val <= 9)
{
val += '0';
}
else
{
val += 'A' - 10;
}
str = val;
}
str[8] = 0;
putstr (str);
}
void UartObj::puthex(unsigned int data)
{
signed char i;
char str[5];
unsigned char val;
for (i = 3; i >= 0; i --)
{
val = data & 0x0f;
data >>= 4;
if (val <= 9)
{
val += '0';
}
else
{
val += 'A' - 10;
}
str = val;
}
str[4] = 0;
putstr (str);
}
void UartObj::putint(unsigned int data)
{
signed char i;
char str[6];
unsigned char val;
for (i = 4; i >= 0; i --)
{
val = data % 10;
data /= 10;
if ((i < 4) && (val == 0) && (data == 0))
{
val = ' ';
}
else
{
val += '0';
}
str = val;
}
str[5] = 0;
putstr (str);
}
void UartObj::putint(unsigned long data)
{
signed char i;
char str[10];
unsigned char val;
for (i = 8; i >= 0; i --)
{
val = data % 10;
data /= 10;
if ((i < 8) && (val == 0) && (data == 0))
{
val = ' ';
}
else
{
val += '0';
}
str = val;
}
str[9] = 0;
putstr (str);
}
void UartObj::putint(unsigned int data, signed char size)
{
signed char i;
char str[7];
unsigned char val;
size = 5 - size;
for (i = 5; i >= 0; i --)
{
if (i == size)
{
if (size == 5)
{
val = 0;
}
else
{
val = '.';
}
}
else
{
val = data % 10;
data /= 10;
if ((i < size - 1) && (val == 0) && (data == 0))
{
val = ' ';
}
else
{
val += '0';
}
}
str = val;
}
str[6] = '\0';
putstr (str);
}
void UartObj::Decode(void)
{
unsigned char ch;
unsigned int *Ptr;
while (ReceiveWritePtr != ReceiveReadPtr)
{
ch = ReceiveBuffer[ReceiveReadPtr++];
ReceiveReadPtr &= 0xff;
switch (State)
{
case 0://'$'搜索
if (ch == '$')
{
State = 1;
ReceiveCount = 0;
}
ReceivePosition = ReceiveReadPtr;
break;
case 1://命令
if (ch == ':')
{
if (ReceiveCount == 5 &&
ReceiveBuffer[ReceivePosition] == 'W' &&
ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'r' &&
ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'i' &&
ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 't' &&
ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 'e')
{
State = 2;//Write命令地址
}
else if (ReceiveCount == 4 &&
ReceiveBuffer[ReceivePosition] == 'R' &&
ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'e' &&
ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'a' &&
ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 'd')
{
State = 3;//Read命令地址
}
else if (ReceiveCount == 5 &&
ReceiveBuffer[ReceivePosition] == 'E' &&
ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'r' &&
ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'a' &&
ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 's' &&
ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 'e')
{
State = 4;//Erase命令地址
}
else
{
State = 0;
}
ReceiveCount = 0;
ReceivePosition = ReceiveReadPtr;
Address = 0;
Data = 0;
}
else if (ch == ';')
{
if (ReceiveCount == 5 &&
ReceiveBuffer[ReceivePosition] == 'R' &&
ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'e' &&
ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 's' &&
ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 'e' &&
ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 't')
{
State = 0;
for(;;);
}
}
else//命令字符个数
{
ReceiveCount ++;
}
break;
case 2://Write命令地址
case 3://Read命令地址
case 4://Erase命令地址
if ((State == 2) && (ch == ','))
{
State = 5;//Write命令数据
}
else if ((State == 3) && (ch == ';'))
{
Ptr = (unsigned int*)Address;
Data = *Ptr;
if ((Address >= 0x80000) && (Address <= 0x8ffff))
{
putstr("FLASH数据读出:[");
}
else
{
putstr("RAM数据读出:[");
}
puthex(Address);
putstr("]=");
puthex(Data);
puts("");
State = 0;
}
else if ((State == 4) && (ch == ';'))
{
if ((Address >= 0x80000) && (Address <= 0x8ffff))
{
Flash.SectorErase((Address & 0xffff) >> 11);
putstr("FLASE扇区擦除:[");
puthex(Address & 0xfffff800);
putstr("-");
puthex(Address | 0x7ff);
putstr("]");
puts("");
}
State = 0;
}
else if ((ch >= '0') && (ch <= '9'))
{
Address <<= 4;
Address |= ch - '0';
}
else if ((ch >= 'A') && (ch <= 'F'))
{
Address <<= 4;
Address |= ch - 'A' + 10;
}
else if ((ch >= 'a') && (ch <= 'f'))
{
Address <<= 4;
Address |= ch - 'a' + 10;
}
break;
case 5://Write命令数据
if (ch == ';')//命令结束字符
{
if ((Address >= 0x80000) && (Address <= 0x8ffff))
{
Flash.Write(FLASH[Address & 0xffff], Data);
putstr("FLASH数据写入:[");
}
else
{
Ptr = (unsigned int*)Address;
*Ptr = Data;
putstr("RAM数据写入:[");
}
puthex(Address);
putstr("]=");
puthex(Data);
puts("");
State = 0;
}
else if ((ch >= '0') && (ch <= '9'))
{
Data <<= 4;
Data |= ch - '0';
}
else if ((ch >= 'A') && (ch <= 'F'))
{
Data <<= 4;
Data |= ch - 'A' + 10;
}
else if ((ch >= 'a') && (ch <= 'f'))
{
Data <<= 4;
Data |= ch - 'a' + 10;
}
break;
default:
State = 0;
break;
}
}
}
文章评论(0条评论)
登录后参与讨论