要使用ZStack就不得不先了解ZStack的OSAL工作原理
http://blog.csdn.net/qinpeng_zbdx/article/details/20990797
http://wenku.baidu.com/link?url=OILW1kTqP0nnNnLmuiCa9v...
了解事情的本質后,操作就很簡單了
根據這篇博客可以很清楚的了解如何在ZStack下使用CC2530開發板進行串口通信
http://home.eeworld.com.cn/my/space-uid-530276-blogid-227205.html
在基於ZigBee協議的應用開發中,用戶只需要實現應用程序框架即可。
在APP層,整個程序要實現的功能都在這三個文件里
YourName.c,YourName.h 和OSAL_YourName.c 文件
在YourName.c文件里,定義了這兩個函數:
YourName_Init( task_id )函數 -- 初始化
YourName_ProcessEvent( task_id, events )函數 -- 判斷由參數傳遞的事件類型,然后執行相應的事件處理函數
OSAL_YourName.c 中有兩個重要的東西
數組taskArr:存放着所有任務的事件處理函數的地址
函數osalInitTasks:所有的初始化工作都在這里完成,自動分配給每個任務一個ID
const pTaskEventHandlerFn tasksArr[] = { macEventLoop, nwk_event_loop, Hal_ProcessEvent, #if defined( MT_TASK ) MT_ProcessEvent, #endif APS_event_loop, #if defined ( ZIGBEE_FRAGMENTATION ) APSF_ProcessEvent, #endif ZDApp_event_loop, #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT ) ZDNwkMgr_event_loop, #endif SerialApp_ProcessEvent };void osalInitTasks( void ) { uint8 taskID = 0; tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt); osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt)); macTaskInit( taskID++ ); nwk_init( taskID++ ); Hal_Init( taskID++ ); #if defined( MT_TASK ) MT_TaskInit( taskID++ ); #endif APS_Init( taskID++ ); #if defined ( ZIGBEE_FRAGMENTATION ) APSF_Init( taskID++ ); #endif ZDApp_Init( taskID++ ); #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT ) ZDNwkMgr_Init( taskID++ ); #endif SerialApp_Init( taskID ); }
所有的任務狀態都被初始化為0,代表現在沒有需要相應的事件。
void SerialApp_Init( uint8 task_id ) { halUARTCfg_t uartConfig; SerialApp_TaskID = task_id; SerialApp_RxSeq = 0xC3; afRegister( (endPointDesc_t *)&SerialApp_epDesc ); RegisterForKeys( task_id ); uartConfig.configured = TRUE; uartConfig.baudRate = SERIAL_APP_BAUD; uartConfig.flowControl = TRUE; uartConfig.flowControlThreshold = SERIAL_APP_THRESH; uartConfig.rx.maxBufSize = SERIAL_APP_RX_SZ; uartConfig.tx.maxBufSize = SERIAL_APP_TX_SZ; uartConfig.idleTimeout = SERIAL_APP_IDLE; uartConfig.intEnable = TRUE; uartConfig.callBackFunc = SerialApp_CallBack; HalUARTOpen (SERIAL_APP_PORT, &uartConfig); #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "SerialApp", HAL_LCD_LINE_2 ); #endif ZDO_RegisterForZDOMsg( SerialApp_TaskID, End_Device_Bind_rsp ); ZDO_RegisterForZDOMsg( SerialApp_TaskID, Match_Desc_rsp ); }static void SerialApp_CallBack(uint8 port, uint8 event) { (void)port; unsigned char Uartbuf[10]; unsigned char Outbuf[10]; Outbuf[0] = 'H'; Outbuf[1] = 'E'; Outbuf[2] = 'L'; Outbuf[3] = 'L'; Outbuf[4] = 'O'; Outbuf[5] = 'W'; Outbuf[6] = 'O'; Outbuf[7] = 'R'; Outbuf[8] = 'L'; Outbuf[9] = 'D'; char len; len=HalUARTRead(0,Uartbuf,10); if(len)//whatever it is { HalUARTWrite(0,Outbuf,10); len=0; } }
關於串口的配置在SerialApp_Init函數里uartConfig.callBackFunc 上邊的那些,比特率之類的。
用ZStack安裝路徑里的SerialApp,改一改就能實現