由于从未接触过操作系统的源码,所以对zigbee的协议栈看起来很头疼,现在还不知道实现原理是什么,通过这段时间的学习,只是对程序的大致框架有了一些认识。
现在记录一实现无线串口通信的过程。
首先是MT_UART.c这个文件,TI默认的串口传输协议帧结构比较复杂,以0xfe开头,又包括数据长度,数据,校验神马的,所以按照webee的教材就比着写了一个简单的函数。
void MT_UartProcessZToolData ( uint8 port, uint8 event )
{
uint8 flag = 0,i,j=0;
uint8 buf[128];
(void)event;
while(Hal_UART_RxBufLen(port))
{
HalUARTRead(port,&buf[j],1);
j++;
flag = 1;
}
if(flag == 1)
{
pMsg = (mtOSALSerialData_t*)osal_msg_allocate(sizeof(mtOSALSerialData_t)+j+1); //申请内存
pMsg->hdr.event=CMD_SERIAL_MSG; //个人觉得这个是中断号
pMsg->msg=(uint8*)(pMsg+1);
pMsg->msg[0]=j;
for(i=0;i<j;i++)
pMsg->msg[i+1]=buf[i];
osal_msg_send(App_TaskID,(byte*)pMsg); //申请任务
osal_msg_deallocate((uint8*)pMsg); //释放内存
}
}
然后在Sampapp.c文件中,
先是串口的初始化
void SampleApp_Init( uint8 task_id )
{
SampleApp_TaskID = task_id;
SampleApp_NwkState = DEV_INIT;
SampleApp_TransID = 0;
/*******串口初始化函数**********/
MT_UartInit();
}
在SampleApp_ProcessEvent函数,消息处理函数,做出相应的操作
if ( events & SYS_EVENT_MSG )
{
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
case CMD_SERIAL_MSG: //上面串口的中断号
SampleApp_Serial_CMD((mtOSALSerialData_t *)MSGpkt); //相应的操作函数
// Received whenever the device changes state in the network
case ZDO_STATE_CHANGE:
SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
if ( (SampleApp_NwkState == DEV_ZB_COORD)
|| (SampleApp_NwkState == DEV_ROUTER)
|| (SampleApp_NwkState == DEV_END_DEVICE) )
{
// Start sending the periodic message in a regular interval.
osal_start_timerEx( SampleApp_TaskID,
SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
}
else
{
// Device is no longer in the network
}
break;
default:
break;
}
操作函数
void SampleApp_Serial_CMD(mtOSALSerialData_t *MSGpkt)
{
uint8 i, len,*str=NULL;
str = MSGpkt->msg;
len = *str;
for(i=1; i<=len; i++)
HalUARTWrite(0,str+i,1);
//HalUARTWrite(0,"\n",1);
if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
SAMPLEAPP_AF_UART,
len+1,
(uint8*)str,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
}
else
{
// Error occurred in request to send.
}
剩余部分是接收端函数,类似于无线点灯,不在赘述。
当产生接受中断后,把获取的数据包通过串口读写函数发送即可。
}