測試/示例代碼
/*
*********************************************************************************************************
* 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){ // 因?yàn)榘l(fā)現(xiàn)幀頭后才掛載事件,所以下一次回掉正好是說明字符數(shù)的那個(gè)字符,否則還得根據(jù)bytesCnt來判斷當(dāng)前位置 RxMac_SetOnFeeded(sender,NULL); if(*pCurChar > '0' && *pCurChar <= '9' ){ // bytesCnt是當(dāng)前收到了多少個(gè)字符,所以接收區(qū)大小為當(dāng)前字符數(shù)加上還要接收的 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]; // 協(xié)議示例1: // 幀頭 :HEADER 或者 START // 強(qiáng)幀尾 :END // 強(qiáng)特殊串: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);
} // 協(xié)議示例2: // 幀頭 : START // 幀頭后的第1個(gè)字符表示后面還要接收多少個(gè)字符 1-9,'4'表示4個(gè),如果不是數(shù)字或?yàn)?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(); // 選擇想要實(shí)驗(yàn)的協(xié)議來初始化 protocol1_init(); //protocol2_init(); while (DEF_TRUE)
{ // 獲取下一個(gè)字符 c = SCI_GetCharB(SCI0,0,&err); // 回顯 SCI_PutCharB(SCI0,c,0); // 喂給接收機(jī) RxMac_FeedData(&_rxmac,c);
}
}