www.pudn.com > TMS320F2812_SCI.rar > JGB_KZB_V1.0.c
// main function
void main(void)
{
unsigned int index,temp;
unsigned char array[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
SystemInit();
ParameterInit();
delay(1000); // delay 1s for system stable
while(1)
{
if(SCIA_State.INQUIREEN == 1)
{ // if inquire CPLD message is enable
scia_inquire_mesg[1] = scia_inquire_addr;
scia_inquire_mesg[2] = 0x02;
scia_inquire_mesg[3] = scia_inquire_mesg[0]+scia_inquire_mesg[1]+scia_inquire_mesg[2];
SCIA_TX(scia_inquire_mesg,4);
SCIA_State.INQUIREEN = 0; // disable the inquire,waiting for answer
}
if(SCIA_State.MESGSTATE == 1)
{ // if a full CPLD message have been received
// get the complete message
index = (SCIARxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
array[0] = SCIARxBuffer[index]; // get the start flag of the frame
SCIARxBuffer[index] = 0; // only clear the 0xA5 flag is enough
index = (index+1)%MAX_SCIA_BUFFER_SIZE;
array[1] = SCIARxBuffer[index]; // get the length field of the frame
SCIARxBuf_Front = index;
temp = index;
for(index = 0;index < array[1];index++)
{
temp = (temp+1)%MAX_SCIA_BUFFER_SIZE;
array[index+2] = SCIARxBuffer[temp];
}
SCIARxBuf_Front = (SCIARxBuf_Front+index)%MAX_SCIA_BUFFER_SIZE;
// accumulation and sum checking
for(temp = 0,index = 0;index <= array[1];index++)
temp += array[index];
temp &= 0x00FF;
// here index = 15 or 3
if((array[index] == temp)&&(array[2] == scia_inquire_addr))
{
SCIA_State.INQUIREEN = 1;
SCIA_State.RECVMESGFLAG = 1;
scia_inquire_addr++;
if(scia_inquire_addr > 9) scia_inquire_addr = 1;
if(scia_cmesg_counter != 0) scia_cmesg_counter++;
}
SCIA_State.MESGSTATE = 0; // clear the flag
}
if(SCIA_State.RECVMESGFLAG == 1)
{ // if the CPLD message is right
if(array[1] == 14)
{ // copy the data from the array[16]
scia_position_mesg[scia_pmesg_counter].Addr = array[2];
scia_position_mesg[scia_pmesg_counter].PMesg[0] = array[3];
scia_position_mesg[scia_pmesg_counter].PMesg[1] = array[4];
scia_position_mesg[scia_pmesg_counter].PMesg[2] = array[5];
scia_position_mesg[scia_pmesg_counter].PMesg[3] = array[6];
scia_position_mesg[scia_pmesg_counter].PMesg[4] = array[7];
scia_position_mesg[scia_pmesg_counter].PMesg[5] = array[8];
scia_position_mesg[scia_pmesg_counter].PMesg[6] = array[9];
scia_position_mesg[scia_pmesg_counter].PMesg[7] = array[10];
scia_position_mesg[scia_pmesg_counter].PMesg[8] = array[11];
scia_position_mesg[scia_pmesg_counter].PMesg[9] = array[12];
scia_position_mesg[scia_pmesg_counter].PMesg[10] = array[13];
scia_position_mesg[scia_pmesg_counter].PMesg[11] = array[14];
scia_pmesg_counter ++;
if(scia_cmesg_counter == 0) scia_cmesg_counter = 1;
}
if(scia_cmesg_counter == 9)
{ // if a round search is completed
unsigned char *str,*strptr;
// allocate a dynamic memory space
str = (unsigned char *)malloc(scia_pmesg_counter*13+3);
*str = 0xA5; // the start flag of frame
*(str+1) = scia_pmesg_counter*13 + 1;// the length field of the frame
// copy the position message to send by SCI-B
strptr = (unsigned char *)scia_position_mesg;
for(index = 0;index < (scia_pmesg_counter*13);index++)
{
*(str+index+2) =*(strptr+index);
*(strptr+index) = 0;
}
// calculate the accumulation sum
temp = scia_pmesg_counter*13+2;
*(str+temp) = 0;
for(index = 0;index < temp;index++)
*(str+temp) += *(str+index);
*(str+temp) &= 0x00FF;
// send the position message by SCI-by
SCIB_TX(str,temp+1);
// free the memory space allocated above
free(str);
scia_cmesg_counter = 0;
scia_pmesg_counter = 0;
}
SCIA_State.RECVMESGFLAG = 0;
}
}
}
// Parameter Initialization
void ParameterInit(void)
{
unsigned int index;
// SCI parameter initialization
SCIATxBuf_Front = 0;
SCIATxBuf_Rear = 1;
SCIARxBuf_Front = 0;
SCIARxBuf_Rear = 1;
SCIBTxBuf_Front = 0;
SCIBTxBuf_Rear = 1;
SCIBRxBuf_Front = 0;
SCIBRxBuf_Rear = 1;
for(index=0;index=Len)
{ // if there is enough free space
for(index=0;index=Len)
{ // if there is enough free space
for(index=0;index 0) // if the RXFIFO receive data
{
counter = 1;
index = (SCIARxBuf_Rear+1)%MAX_SCIA_BUFFER_SIZE;
while(index != SCIARxBuf_Front)
{ // get the free space
counter++;
index = (index+1)%MAX_SCIA_BUFFER_SIZE;
}
if(counter >= temp)
{ // if there is enough space
for(index = 0;index < temp;index++)
{
SCIARxBuffer[SCIARxBuf_Rear] = SciaRegs.SCIRXBUF.all;
SCIARxBuf_Rear = (SCIARxBuf_Rear+1)%MAX_SCIA_BUFFER_SIZE;
}
}
else
{ // if there is not enough space
// add the processing error code
}
counter = 0;
}
// SCI-B tranmit section
temp = ScibRegs.SCIFFTX.bit.TXFFST; // Get SCI-A FIFOTX state
if(temp <= 6) // TXFFST<=6,start the operation
{
index = (SCIBTxBuf_Front+1)%MAX_SCIB_BUFFER_SIZE;
while(index != SCIBTxBuf_Rear)
{ // get the number of data in buffer
counter++;
index = (index+1)%MAX_SCIB_BUFFER_SIZE;
}
// choose the minimum value as the result
counter = ((16-temp) <= counter) ? (16-temp) : counter;
for(index = 1;index <= counter;index++)
{
temp = (SCIBTxBuf_Front+index)%MAX_SCIB_BUFFER_SIZE;
ScibRegs.SCITXBUF = SCIBTxBuffer[temp];
SCIBTxBuffer[temp] = 0;
}
SCIBTxBuf_Front = (SCIBTxBuf_Front+counter)%MAX_SCIB_BUFFER_SIZE;
counter = 0;
}
// SCI-A command parsing section
if(CpuTimer0.InterruptCount>=3)
{ // parsing operation happens every 6ms
CpuTimer0.InterruptCount = 0;
index = (SCIARxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
while(index != SCIARxBuf_Rear)
{ // search the SCI-A RX buffer until the end
if(SCIARxBuffer[index] == 0xA5)
{ // if found the flag of frame start : 0xA5
temp = (index +1)%MAX_SCIA_BUFFER_SIZE; // the length field of the frame
if(SCIARxBuffer[temp] == 0x00)
// if the length field is 0,the frame is not completely received
break;
else
{ // get the following data number in buffer
// here the index is unusual now
index = (temp+1)%MAX_SCIA_BUFFER_SIZE;
while(index != SCIARxBuf_Rear)
{ // get the number of data in Rxbuffer
counter++;
index = (index+1)%MAX_SCIA_BUFFER_SIZE;
}
if(counter>=SCIARxBuffer[temp])
// if a full message have been received
SCIA_State.MESGSTATE = 1;
else
// if not ,break out the cycle
break;
counter = 0;
}
}
else
{ // clear the unuseful data
SCIARxBuffer[index] = 0x00;
SCIARxBuf_Front = (SCIARxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
index = (SCIARxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
}
}
}
// 响应中断并允许系统接收更多的中断
PieCtrl.PIEACK.all = PIEACK_GROUP1;
}