测试/示例代码
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
* Framework
*
* By : Lin Shijun
* Note: This is a framework for uCos-ii project with only S12CPU, none float, banked memory model.
* You can use this framework with same modification as the start point of your project.
* I've removed the os_probe module,since I thought it useless in most case.
* This framework is adapted from the official release.
*********************************************************************************************************
*/ #include "includes.h" #include "SCI_def.h" #include "RxMac.h" /*
*********************************************************************************************************
* STACK SPACE DECLARATION
*********************************************************************************************************
*/ static OS_STK AppTaskStartStk[APP_TASK_START_STK_SIZE]; /*
*********************************************************************************************************
* TASK FUNCTION DECLARATION
*********************************************************************************************************
*/ static void AppTaskStart(void *p_arg); /*
*********************************************************************************************************
* CALLBACK FUNCITON
*********************************************************************************************************
*/ void onGetData(pRX_MAC sender,pRB_BYTE pCurChar,INT16U bytesCnt){ // 因为发现帧头后才挂载事件,所以下一次回掉正好是说明字符数的那个字符,否则还得根据bytesCnt来判断当前位置 RxMac_SetOnFeeded(sender,NULL); if(*pCurChar > '0' && *pCurChar <= '9' ){ // bytesCnt是当前收到了多少个字符,所以接收区大小为当前字符数加上还要接收的 RxMac_SetRxSize(sender,*pCurChar - '0' + bytesCnt);
}
} void onFlushed(pRX_MAC sender,pRB_BYTE pBuf,INT16U len,RX_STATE state,pRX_FLAG pHorU,pRX_FLAG pEnder){
SCI_PutCharsB(SCI0,"\nFlushed:",9,0); if(state.headerFound)
SCI_PutCharsB(SCI0,"headerFound,",12,0); if(state.enderFound)
SCI_PutCharsB(SCI0,"enderFound,",11,0); if(state.isFull)
SCI_PutCharsB(SCI0,"full,",5,0); if(state.uniqueFound)
SCI_PutCharsB(SCI0,"unique,",7,0);
SCI_PutCharsB(SCI0,"\nDatas:",7,0);
SCI_PutCharsB(SCI0,pBuf,len,0);
SCI_PutCharsB(SCI0,"\n",1,0);
RxMac_ResetRxSize(sender);
} void onGetHeader(pRX_MAC sender,pRX_FLAG pFlag){
SCI_PutCharsB(SCI0,"\nFoundHeader:",13,0);
SCI_PutCharsB(SCI0,pFlag->pBuf,pFlag->len,0);
SCI_PutCharsB(SCI0,"\n",1,0);
} void onGetHeader2(pRX_MAC sender,pRX_FLAG pFlag){
SCI_PutCharsB(SCI0,"\nFoundHeader:",13,0);
SCI_PutCharsB(SCI0,pFlag->pBuf,pFlag->len,0);
SCI_PutCharsB(SCI0,"\n",1,0);
RxMac_SetOnFeeded(sender,onGetData);
} /*
*********************************************************************************************************
* FLAGS
*********************************************************************************************************
*/ RX_MAC _rxmac; #define BUF_SIZE 20 INT8U buffer[BUF_SIZE];
RX_FLAG flags[4]; // 协议示例1: // 帧头 :HEADER 或者 START // 强帧尾 :END // 强特殊串:12345 // static void protocol1_init(){
RX_FLAG_INIT(&flags[0],"HEADER",6,FLAG_OPTION_HEADER);
RX_FLAG_INIT(&flags[1],"START",5,FLAG_OPTION_HEADER);
RX_FLAG_INIT(&flags[2],"END",3,FLAG_OPTION_STRONG_ENDER);
RX_FLAG_INIT(&flags[3],"12345",5,FLAG_OPTION_STRONG_UNIQUE);
RxMac_Init(&_rxmac,flags,4,6,buffer,BUF_SIZE,NULL,onGetHeader,onFlushed);
} // 协议示例2: // 帧头 : START // 帧头后的第1个字符表示后面还要接收多少个字符 1-9,'4'表示4个,如果不是数字或为'0',则等待帧尾 // 帧尾 : END // 特殊串: NOW // static void protocol2_init(){
RX_FLAG_INIT(&flags[0],"START",5,FLAG_OPTION_HEADER);
RX_FLAG_INIT(&flags[1],"END",3,FLAG_OPTION_ENDER);
RX_FLAG_INIT(&flags[2],"NOW",3,FLAG_OPTION_UNIQUE);
RxMac_Init(&_rxmac,flags,3,5,buffer,BUF_SIZE,NULL,onGetHeader2,onFlushed);
} /*
*********************************************************************************************************
* MAIN FUNCTION
*********************************************************************************************************
*/ void main(void) {
INT8U err;
BSP_IntDisAll(); /* Disable ALL interrupts to the interrupt controller */ OSInit(); /* Initialize uC/OS-II */ err = OSTaskCreate(AppTaskStart, NULL,
(OS_STK *)&AppTaskStartStk[APP_TASK_START_STK_SIZE - 1],
APP_TASK_START_PRIO);
OSStart();
} static void AppTaskStart (void *p_arg)
{
INT8U c,err;
(void)p_arg; /* Prevent compiler warning */ BSP_Init();
SCI_Init(SCI0);
SCI_EnableTrans(SCI0);
SCI_EnableRecv(SCI0);
SCI_EnableRxInt(SCI0);
SCI_BufferInit(); // 选择想要实验的协议来初始化 protocol1_init(); //protocol2_init(); while (DEF_TRUE)
{ // 获取下一个字符 c = SCI_GetCharB(SCI0,0,&err); // 回显 SCI_PutCharB(SCI0,c,0); // 喂给接收机 RxMac_FeedData(&_rxmac,c);
}
}