




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
實驗?zāi)康?/p>
串口透傳之無線實驗設(shè)備硬件:PC 一臺或者兩EB2530(底板、板、USB線 兩仿真 一軟件:XP/win7/8/8.1系統(tǒng),IAR8.10集成開發(fā)環(huán)境 實驗相關(guān)電路UARTSPIUSART設(shè)置在單獨的I/O引腳。更詳細(xì)的說明請參考參見16.1節(jié)。實驗相關(guān)寄存器與通訊模型打開..\CC2530\第5章zigbee協(xié)議棧應(yīng)用與組網(wǎng)\12.串口透傳之無線工程constcId_tSerialApp_ClusterList[SERIALAPP_MAX_CLUSTERS]={#define SerialApp_Init函數(shù)voidSerialApp_Init(uint8task_id{halUARTCfg_t //串口配置結(jié)構(gòu)體SerialApp_TaskID=task_id; SerialApp_RxSeq=0xC3; SampleApp_NwkState=DEV_INIT;afRegister((endPointDesc_t*)&SerialApp_epDesc); 端口描RegisterForKeystask_id 按 =TRUE; //2x30don'tcare-seeuart =SERIAL_APP_BAUD;uartConfig.flowControl=FALSE;uartConfig.flowControlThresholdSERIAL_APP_THRESHuartConfig.rx.maxBufSize=SERIAL_APP_RX_SZ;//2x30don'tcare-seeuartdriver.uartConfig.tx.maxBufSize=SERIAL_APP_TX_SZ;//2x30don'tcare-seeuartdriver. =SERIAL_APP_IDLE;//2x30don'tcare-seeuartdriver. =TRUE; //2x30don'tcare-seeuartdriver. =SerialApp_CallBack;//接收回調(diào)函數(shù)HalUARTOpen(SERIAL_APP_PORT,&uartConfig);//打開串口#ifdefined(LCD_SUPPORTEDHalLcdWriteString("SerialApp",HAL_LCD_LINE_2);ZDO_RegisterForZDOMsg(SerialApp_TaskID,End_Device_Bind_rsp);//綁ZDO_RegisterForZDOMsg(SerialApp_TaskIDMatch_Desc_rsp //相關(guān)}SerialApp_ProcessEvent函數(shù)UINT16SerialApp_ProcessEvent(uint8task_id,UINT16events{(void)task_id;//Intentionallyunreferencedif(events&SYS_EVENT_MSG{ingMSGPacket_t *)osal_msg_receive(SerialApp_TaskID)){switch(MSGpkt->hdr.event{ SerialApp_ProcessMSGCmd(MSGpkt);caseSampleApp_NwkState=(devStates_t)(MSGpkt->hdr.status);if((SampleApp_NwkState==DEV_ZB_COORD)||(SampleApp_NwkState==||(SampleApp_NwkState==DEV_END_DEVICE){}{
//Startsendingtheperiodicmessageinaregularinterval.HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);if(SampleApp_NwkState!=DEV_ZB_COORD)//Deviceisnolongerinthe}}osal_msg_deallocate((uint8*)MSGpkt}return(events^SYS_EVENT_MSG}if(events&SERIALAPP_SEND_EVT{ returnevents^SERIALAPP_SEND_EVT}if(events&SERIALAPP_RESP_EVT{ //串口響return(events^SERIALAPP_RESP_EVT}return(0);//Discardunknown}SerialApp_Send函數(shù)staticvoid{#ifSERIAL_APP_LOOPBACK//回路測試執(zhí)if(SerialApp_TxLen<{ SERIAL_APP_TX_MAX-}if{if(HalUARTWrite(SERIAL_APP_PORT,SerialApp_TxBuf+1,{SerialApp_TxLen=}{ }}//SerialApp_TxLen0時代表有數(shù)據(jù)要發(fā)送或者正在//SerialApp_TxLen為0時代表沒有數(shù)據(jù)發(fā)送或者已經(jīng)發(fā)送完了。發(fā)送端接收到接收端的確認(rèn)信息后,確定本次數(shù)據(jù)已經(jīng)被接收到會將SerialApp_TxLen置0為接收下次數(shù)據(jù)作準(zhǔn)備if(!SerialApp_TxLen {//Pre-pendsequencenumbertotheTxmessage.SerialApp_TxBuf[0]=++SerialApp_TxSeq;}ifSerialApp_TxLen)//如果接收到內(nèi){//將串口接收到的數(shù)據(jù)無線發(fā)送出去 (endPointDesc_t*)&SerialApp_epDesc,SerialApp_TxLen+1,SerialApp_TxBuf,&SerialApp_MsgID,0,AF_DEFAULT_RADIUS)){}}
//發(fā)送失敗則重發(fā)}void ingMSGPacket_t*pkt{uint8stat;uint8seqnb;uint8switch(pkt->clusterId{Amessagewithaserialdatablocktobetransmittedontheserialport.caseSERIALAPP_CLUSTERID1://收到發(fā)送過來的數(shù)據(jù)通過串口輸出到電腦顯示//Storetheaddressforsendingandretrying.osal_memcpy(&SerialApp_RxAddr,&(pkt->srcAddr),sizeof(afAddrType_t));seqnb=pkt-//Keepmessageifnotarepeatif((seqnb>SerialApp_RxSeq)|| //Normal((seqnb<0x80)&&(SerialApp_RxSeq>0x80)))//Wrap-around{Transmitthedataontheserialport.過串口發(fā)送數(shù)PC pkt-(pkt->cmd.DataLength-1)){//Savefornext ingmessageSerialApp_RxSeq=seqnb;stat=}{stat=}}{stat=}//SelectapproproiateOTAflow-controldelay=(stat==OTA_SER_BUSY)?SERIALAPP_NAK_DELAY://Build&sendOTAresponsemessage.SerialApp_RspBuf[0]=stat;SerialApp_RspBuf[1]=seqnb;SerialApp_RspBuf[2]=LO_UINT16(delay);SerialApp_RspBuf[3]=HI_UINT16(delay//收到數(shù)據(jù)后,發(fā)送一個響應(yīng)osal_set_event(SerialApp_TaskID,SERIALAPP_RESP_EVT);osal_stop_timerEx(SerialApp_TaskID,SERIALAPP_RESP_EVT);響應(yīng)消caseif((pkt->cmd.Data[1]==SerialApp_TxSeq)((pkt->cmd.Data[0]==OTA_SUCCESS)||(pkt->cmd.Data[0]=={SerialApp_TxLen= }{//Re-starttimeoutaccordingtodelaysentfromotherdevice.delay=BUILD_UINT16(pkt->cmd.Data[2],pkt->cmd.Data[3]);osal_start_timerEx(SerialApp_TaskID,SERIALAPP_SEND_EVT,delay);}caseSerialApp_ConnectReqProcess((uint8*)pkt-caseSerialApp_DeviceConnectRsp((uint8*)pkt-}}分別編譯到協(xié)調(diào)器、終端,兩個節(jié)點用USB與電腦相連,打開 32軟件設(shè)串口為:1152008N1后,即可隨便發(fā)消息了。實驗結(jié)果SerialApp_TxLen=0;SerialApp_RxSeq=在Z-Stack中我們可以直接調(diào)用API接口來開啟設(shè)備的串口功能,此API接口函數(shù)為“uint8示要開啟的串,CC2530有兩個串口,串口0和串口1,這個看個人硬件所用的串口來選擇開啟哪個typedef{bool booluint8idleTimeout;halUARTBufControl_trx;halUARTBufControl_ttx;boolintEnable;uint32rxChRvdTime;再調(diào)用“HalUARTOpen()”函數(shù)打開同一個串口時,則會自動清除串口的RX、TX緩存和串口配置“uartRecord”再重新配置。此處我們設(shè)置為TRUE。HA_R3800此處我們設(shè)置為FALSE,關(guān)閉硬件流控功能。置為64,一般設(shè)置為“maxRxBufSize”的一半。如果設(shè)備串口收到數(shù)據(jù)之后在“idleTimout”時間內(nèi)RX沒有收到新的數(shù)據(jù)了,將會觸發(fā)相應(yīng)的此處我們設(shè)置為6ms,當(dāng)串口收到數(shù)據(jù)后,6ms內(nèi)沒有再收到新的數(shù)據(jù),則觸發(fā)rx包含“halUARTBufControl_t”數(shù)據(jù)結(jié)構(gòu),用于操作RX緩沖區(qū)。當(dāng)接收的字節(jié)數(shù)到達(dá)“rx.maxBufSize”時,會產(chǎn)生“HAL_UART_RX_FULL”。此處我們設(shè)置為128個字節(jié)Hanle_Urven串口發(fā)送數(shù)據(jù)就只需要調(diào)用“uint16HalUARTWrite(uint8port,uint8*buf,uint16len)”接協(xié)議棧中“HAL_UART_DMA_TX_MAX”的默認(rèn)值為256。接收數(shù)據(jù)的串口事件只有三個:HAL_UART_RX_FULL、HAL_UART_RX_ABOUT_FULL和HAL_UART_RX_FULL:當(dāng)RXrx.maxBufSizeHAL_UART_RX_ABOUT_FULL:當(dāng)RX 緩存數(shù)據(jù)的字節(jié)數(shù)到達(dá)“maxRxBufSize– 協(xié)議棧學(xué)習(xí)之串口透明傳輸實驗(SerialApp)流程分第一個功能:協(xié)調(diào)器的組網(wǎng),終端設(shè)備和路由設(shè)備發(fā)現(xiàn)網(wǎng)絡(luò)以及//第一步:Z-Stackmain()函數(shù)開始執(zhí)行,main()2件事:一是系統(tǒng)初始化,另intmain(void{//Initializetheoperating //第二步,操作系統(tǒng)初始 //初始化完系統(tǒng)任務(wù)后,正式開始執(zhí)行操作系}//第二步,進(jìn) osal_init_system()函數(shù),執(zhí)行操作系統(tǒng)初始 //初始化操作系統(tǒng),其中最重要的是,初始化操作系統(tǒng)的任務(wù){(diào)//InitializetheMemoryAllocationSystem//Initializethemessagequeueosal_qHead=NULL;//Initializethetimers//InitializethePowerManagementSystem//Initializethesystem //第三步,執(zhí)行操作系統(tǒng)任務(wù)初始化函//Setupefficientsearchforthefirstblockofheap.return(SUCCESS}voidosalInitTasks(void) {uint8taskID=tasksEvents=(uint16*)osal_mem_alloc(sizeof(uint16)* osal_memset(tasksEvents,0,(sizeof(uint16)* //taskID kInit(taskID++);//不需要用戶考慮 //不需要用戶考Hal_Init(taskID++); #ifdefined(MT_TASK)MT_TaskInit(taskID++); //不需要#ifdefined(ZIGBEE_FRAGMENTATION)APSF_Init(taskID++);ZDApp_Init(taskID++); #ifdefined(ZIGBEE_FREQ_AGILITY)||defined(ZIGBEE_PANID_)ZDNwkMgr_Init(taskID++);SerialApp_Init(taskID);//應(yīng)用層SerialApp層初始化,需要用戶考慮 //當(dāng)有按鍵按下的時候,產(chǎn)生一個系}//第四步ZDApp_init()函數(shù),執(zhí)行ZDApp//ThefirstvoidZDApp_Init(uint8task_id {//SavethetaskIDZDAppTaskID=task_id;//InitializetheZDOglobaldeviceshortaddressstorageZDAppNwkAddr.addrMode=Addr16Bit;ZDAppNwkAddr.addr.shortAddr=INVALID_NODE_ADDR;(void)NLME_GetExtAddr();//LoadthesaveExtAddrpointer.//Checkformanual"HoldAutoStart"http://InitializeZDOitemsandsetupthedevice-typeofdevicetocreate.//Registertheendpointdescriptionwiththe//Thistaskdoesn'thaveaSimpledescription,butwestill//toregistertheafRegister((endPointDesc_t*)&ZDApp_epDesc#ifdefined(ZDO_USERDESC_RESPONSE)#endif////Starttheif(devState!=DEV_HOLD) //devStateDEV_INITZDA層時,就{ZnitDevice(0 }{//BlinkLEDtoindicateHOLD_STARTHalLedBlink(HAL_LED_4,0,50,500);}}/*ZDApp_Init()//Thethirdstep,執(zhí)行ZnitDevice()函數(shù),執(zhí)行設(shè)備初始uint8ZnitDevice(uint16startDelay)//Thethirdstep,ZDO層初始化設(shè)備{//TriggerthenetworkZDApp_NetworkInit(extendedDelay //網(wǎng)絡(luò)初始化,跳到相應(yīng)的函數(shù)里頭,執(zhí)行The}voidZDApp_NetworkInit(uint16delay)//Thestep,網(wǎng)絡(luò)初始{if(delay{//Waitawhilebeforestartingtheosal_start_timerEx(ZDAppTaskID,ZDO_NETWORK_INIT,delay ////ZDAppThefifthstepZDApp_event_loop()}{osal_set_event(ZDAppTaskID,ZDO_NETWORK_INIT}}//Thefifthstep,轉(zhuǎn)到ZDApp_event_loop(){if(events&ZDO_NETWORK_INIT //Thefivthstep,網(wǎng)絡(luò)初始化處{//InitializeappsandstartthenetworkdevState=DEV_INIT;//Thesixthstep,去啟動設(shè)備,接著執(zhí)行Thesixthstep,轉(zhuǎn)到ZDO_StartDevice()ZDO_StartDevice((uint8)ZDO_Config_Node_Descriptor.LogicalType,devStartMode,//Returnunprocessedreturn(events^}}//Thesixthstep,執(zhí)行ZDO_StartDevice()函數(shù)voidZDO_StartDevice(bytelogicalType,devStartModes_tstartMode,bytebeaconOrder,bytesuperframeOrder)//Thesixthstep{if(ZG_BUILD_COORDINATOR_TYPE&&logicalType==NODETYPE_COORDINATOR) {if(startMode==MODE_HARD{devState=//NLME_NetworkFormationRequest()建立網(wǎng)絡(luò)后,將給予ZDO層反饋信息。Theseventhstep,ZDAppZDO_NetworkFormationConfirmCB()數(shù)}
superframeOrder,false);ifZG_BUILD_JOINING_TYPE&&logicalTypeNODETYPE_ROUTER||logicalTypeNODETYPE_DEVICE))//當(dāng)為終端設(shè)備或路由時{if((startMode==MODE_JOIN)||(startMode==MODE_REJOIN){devState=//zgDefaultChannelList與協(xié)調(diào)器形成網(wǎng)絡(luò)的通道號匹配。網(wǎng)絡(luò)發(fā)現(xiàn) 繼而轉(zhuǎn)到ZDO_NetworkDiscoveryConfirmCB()函}}}//Theseventhstep,分兩種情況,1.協(xié)調(diào)器 voidZDO_NetworkFormationConfirmCBZStatus_tStatusTheseventhstepZDO層網(wǎng)絡(luò){osal_set_event(ZDAppTaskID,ZDO_NETWORK_START);//發(fā)送網(wǎng)絡(luò)啟動到ZDApp層,接ZDApp_event_loop()函數(shù)}{ifevents&ZDO_NETWORK_START//網(wǎng)絡(luò)啟動{ //網(wǎng)絡(luò)啟動,接著跳轉(zhuǎn)到Theninthstep,執(zhí)ZDApp_NetworkStartEvt()}} //處理網(wǎng)絡(luò)啟動{osal_pwrmgr_device(PWRMGR_ALWAYS_ON); osal_set_event(ZDAppTaskID,ZDO_STATE_CHANGE_EVT);//設(shè)置網(wǎng)絡(luò)狀態(tài)改變,發(fā)送到ZDApp層,轉(zhuǎn)到Thetenthstep,去 ZDApp_event_loop()函數(shù),找到相對應(yīng)的網(wǎng)絡(luò)改變}2)//Theseventhstep(終端設(shè)備)ZDO{//把網(wǎng)絡(luò)發(fā)現(xiàn)這個反饋消息,發(fā)送到ZDA層,轉(zhuǎn)到ZDApp_ProcessOSAg(),執(zhí)行ZDApp_SendMsg(ZDAppTaskID, F,sizeof(ZDO_NetworkDiscoveryCfm_t),(uint8*)&msg);}{ //(終端設(shè)備),網(wǎng)絡(luò)發(fā)現(xiàn)響應(yīng)//ZDO層發(fā)現(xiàn)網(wǎng)絡(luò)反饋信息。然后由網(wǎng)絡(luò)層發(fā)起加入//ZDO層加入網(wǎng)絡(luò)反饋,NLME_JoinRequest()函數(shù)。if(NLME_JoinRequest(((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->extendedPANID,BUILD_UINT16(((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->panIdLSB,((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->panIdMSB),((ZDO_NetworkDiscoveryCfm_t*)msgPtr)->logicalChannel,ZDO_Config_Node_Descriptor.CapabilityFlags)!=ZSuccess){}}
ZDApp_NetworkInit(+((uint16)(osal_rand()&EXTENDED_JOINING_RANDOM_MASK)))voidZDO_JoinConfirmCBuint16PanIdZStatus_tStatus)//Theninthstep(終端設(shè)備),終端設(shè){//將ZDO_NWK_JOIN_IND發(fā)送到ZDA層,執(zhí)行ZDApp_ProcessOSA ZDApp_SendMsg(ZDAppTaskID,ZDO_NWK_JOIN_IND,sizeof(osal_event_hdr_t),(byte*)NULL);}{case if(ZG_BUILD_JOINING_TYPE&&ZG_DEVICE_JOINING_TYPE{}}voidZDApp_ProcessNetworkJoin(void)//處理網(wǎng)絡(luò)加入{if(nwkStatus==ZSuccess{//ZDO_STATE_CHANGE_EVTZDAZDApp_event_loop()函數(shù)。osal_set_event(ZDAppTaskID,ZDO_STATE_CHANGE_EVT);}}4)路由{{//NOTE:firsttwoparametersarenotused,seeNLMEDE.hfor{}
, //路由啟}}{nwkStatus=osal_set_event(ZDAppTaskID,ZDO_ROUTER_START}{if(events&ZDO_ROUTER_START{if(nwkStatus==ZSuccess{if(devState==DEV_END_DEVICEdevState //設(shè)備狀態(tài)變成osal_pwrmgr_device(PWRMGR_ALWAYS_ON}{//remainasend}osal_set_event(ZDAppTaskID,ZDO_STATE_CHANGE_EVT //ZDO狀態(tài)改變//Returnunprocessedreturn(events^}}{if(events&ZDO_STATE_CHANGE_EVT)//Theeighthstep,網(wǎng)絡(luò)改變,這個就是在設(shè)//并在網(wǎng)絡(luò)中的確定后產(chǎn)生的一{ZDO_UpdateNwkStatusdevState);//Theeleventhstep,}}//Theninthstep,執(zhí)行ZDO_UpdateNwkStatus()函數(shù),完成網(wǎng)絡(luò)voidZDO_UpdateNwkStatus(devStates_tstate)//Theninthstep,{zdoSendStateChangeMsg(state*(pItem->epDesc->task_id));//zdo層,這是Thetenthstep,轉(zhuǎn)到ndStateChangeMsg()函ZDAppNwkAddr.addr.shortAddrNLME_GetShortAddr();//NLME_GetShortAddr()函數(shù),獲得16位短地址。(void)NLME_GetExtAddr();//LoadthesaveExtAddrpointer.//64IEEE}//Thetenthstep,執(zhí)行zdoSendStateChangeMsg()函{if(NULL=={if(NULL==(pMsg=(osal_event_hdr_t{//UponfailuretonotifyanyEndPointofthestatechange,re-settheZDOevent//tryagainlaterwhenmoreHeapmaybeosal_set_event(ZDAppTaskIDZDO_STATE_CHANGE_EVTZDO狀態(tài)沒有任何改 處理函數(shù)}{pMsg->event ZDO_STATE_CHANGE這個消息保存到pMsgpMsg->status=state;(void)osal_msg_send(taskId,(uint8*)pMsg); }}}//Theeleventhstep,去執(zhí)行 mand(mtOSALSerialData_t*msg){caseZDO_STATE_CHANGE:
MT_ZdoStateChangeCB((osal_event_hdr_t*)msg);發(fā)生,一旦檢測到有發(fā)生,就轉(zhuǎn)//到相應(yīng)的處理函數(shù),進(jìn)行處理。voidosal_start_systemvoid)//{#ifdefinedZBIT&&definedUBITfor(;;)//ForeverLoop {uint8idx=0;Hal_ProcessPoll();//ThisrecesMT_SerialPoll()andosal_check_timer().do{if(tasksEvents[idx])//Taskishighestprioritythatis{ //得到待處理的最高優(yōu)先級任務(wù)索引號}}while(++idx if(idx {uint16events;events=tasksEvents[idx]; //提取需要處理的任務(wù)中的tasksEvents[idx]=0;//CleartheEventsforthistask. //清除本次任務(wù)的 eventstasksArr[idxidxevents通過指針調(diào)用任務(wù)處理函數(shù),緊接著跳到相應(yīng)的函數(shù)tasksEvents[idx|events;//Addbackunprocessedeventstothecurrenttask.//保存未處 //退出臨界else//Completepassthroughalltaskeventswithno{osal_pwrmgr_powerconserve();//Puttheprocessor/systeminto}}}第二個功能:設(shè)備間的綁定應(yīng),所以,當(dāng)我們按下右鍵時,我們會在SerialApp_ProcessEvent()函數(shù)里找到對應(yīng)的鍵盤相應(yīng)*/UINT16SerialApp_ProcessEvent(uint8task_id,UINT16events)//當(dāng)有傳遞到應(yīng)用層的時候,執(zhí)行此處{if(events&SYS_EVENT_MSG)//有傳遞過來,故通過這個條件語{case SerialApp_HandleKeyskeyChange_t*)MSGpkt)->statekeyChange_t*)MSGpkt)->keys接著跳到相應(yīng)的按}}目。在這里是以SerialApp例子為例。voidSerialApp_HandleKeys(uint8shift,uint8keys{if(keys&HAL_KEY_SW_2 //Joystick{HalLedSet(HAL_LED_4,HAL_LED_MODE_OFF//InitiateanEndDeviceBindRequestforthemandatorydstAddr.addrMode=dstAddr.addr.shortAddr Coordinator地ZDP_EndDeviceBindReq(&dstAddr,NLME_GetShortAddr(), (cId_t*)SerialApp_ClusterList,FALSE);}if(keys&HAL_KEY_SW_4{HalLedSet(HAL_LED_4,HAL_LED_MODE_OFF//InitiateaMatchDescriptionRequest(ServicedstAddr.addrMode=AddrBroadcast;//廣播地址dstAddr.addr.shortAddrNWK_BROADCAST_SHORTADDR;ZDP_MatchDescReq(&dstAddr,NWK_BROADCAST_SHORTADDR, (cId_t*)SerialApp_ClusterList,FALSE);}}}調(diào)用ZDP_EndDeviceBindReq()函數(shù)發(fā)送綁定請求。ZDP_EndDeviceBindReq(&dstAddr, //目的地址設(shè)為0x0000;SERIALAPP_MAX_CLUSTERS,//輸入簇的數(shù)目(cId_t*)SerialApp_ClusterList,//輸入簇列表SERIALAPP_MAX_CLUSTERS輸出簇數(shù)目(cId_t*)SerialApp_ClusterList,//輸出簇列表FALSE該函數(shù)實際調(diào)用無線發(fā)送函數(shù)將綁定請求發(fā)送給協(xié)調(diào)器節(jié)點:默認(rèn)clusterID為End_Device_Bind_req,最后通過fillAndSend(&ZDP_TransID,dstAddr,End_Device_Bind_req,len內(nèi)容;;;AF_DataRequest(&afAddr,&ZDApp_epDesc,clusterID,transSeq,ZDP_TxOptions,AF_DEFAULT_RADIUS協(xié)調(diào)器收到終端設(shè)備綁定請求這個信息會傳送到ZDO層,在ZDO層的處理函數(shù)中,調(diào)用ZDApp_ProcessOSA g((osal_event_hdr_t*)msg_ptr);UINT16ZDApp_event_loop(bytetask_id,UINT16events){uint8if(events&SYS_EVENT_MSG{while((msg_ptr=osal_msg_receive(ZDAppTaskID)){ g((osal_event_hdr_t*)msg_ptr//Releasethememoryosal_msg_deallocate(msg_ptr);}//Returnunprocessedreturn(events^}void g(osal_event_hdr_t*msgPtr{//DataConfirmationmessagebytesentEP; //Thisshouldalwaysbe0bytesentStatus; switch(msgPtr->event){ ingZDO } ingData(( ingMSGPacket_t*pData){uint8x=0;ingMsg_t//解析clusterID這個消inMsg.srcAddr.addrMode=Addr16Bit;inMsg.wasBroadcast=pData->wasBroadcast;inMsg.clusterID=pData->clusterId; //這個clusterID,在這里指的是,終端設(shè)備發(fā)送過來的End_Device_Bind_req這個消息inMsg.SecurityUse=pData-inMsg.asduLen=pData->cmd.DataLength-1;inMsg.asdu=pData->cmd.Data+1;inMsg.TransSeq=pData->cmd.Data[0];handled=ZDO_SendMsgCBs(&inMsg);#ifdefined(MT_ZDO_FUNC)MT_ZdoRsp(&inMsg);while(zdpMsgProcs[x].clusterID!=0xFFFF{){}//Handleunhandledmessagesif(!handled)ZDApp_InMsgCB(&inMsg);}因為ZDO信息處理表zdpMsgProcs[]沒有對應(yīng)的End_Device_Bind_req簇,因此沒有調(diào)用ZDO信息處理表中的處理函數(shù)voidZDApp_RegisterCBs(void{#ifdefined(ZDO_IEEEADDR_REQUEST)||defined(REFLECTOR)ZDO_RegisterForZDOMsg(ZDAppTaskID,IEEE_addr_rsp);#ifdefined(ZDO_NWKADDR_REQUEST)||defined(REFLECTOR)ZDO_RegisterForZDOMsg(ZDAppTaskID,NWK_addr_rsp);#ifdefined(ZDO_COORDINATOR)ZDO_RegisterForZDOMsg(ZDAppTaskID,Bind_rsp);#ifdefined(REFLECTORZDO_RegisterForZDOMsg(ZDAppTaskID,Bind_req);}因此,協(xié)調(diào)器節(jié)點的ZDApp接收到外界輸入的數(shù)據(jù)后,由于了ZDO反饋消息,即ZDO_CB_MSG,ZDApp層任UINT16ZDApp_event_loop(bytetask_id,UINT16events{uint8if(events&SYS_EVENT_MSG{while((msg_ptr=osal_msg_receive(ZDAppTaskID)){ g((osal_event_hdr_t*)msg_ptr//Releasethememoryosal_msg_deallocate(msg_ptr);}//Returnunprocessedreturn(events^} void g(osal_event_hdr_t*msgPtr{//DataConfirmationmessagebytesentEP; //Thisshouldalwaysbe0bytesentStatus; switch(msgPtr->event){ ingZDO ingMSGPacket_t*)msgPtrcaseZDApp_ProcessMsgCBs(( }調(diào)用ZDApp_ProcessMsgCBs()函數(shù)。在這個函數(shù)中根ClusterID(這End_Device_Bind_req)選擇相對應(yīng)的匹配void ingMsg_t*inMsg{case{ZDO_ParseEndDeviceBindReqinMsg&bindReq);//解析綁定請求信(); ingtheclusterlists-ifallocated.if(bindReq.numInClusters)osal_mem_(bindReq.inClusters);if(bindReq.numOutClusters)}}}voidZDO_MatchEndDeviceBind(ZDEndDeviceBind_t*bindReq{zAddrType_tdstAddr;uint8status;Isthisthefirstrequest?接收到的是第一個綁定請if(matchED==NULL{Creatematchinfostructure創(chuàng)建匹配信息結(jié)構(gòu)matchEDZDMatchEndDeviceBind_t*)osal_mem_alloc(sizeofZDMatchEndDeviceBind_t分配空間if(matchED){Clearthestructure先進(jìn)行清除操osal_memset((uint8*)matchED,0,sizeof(ZDMatchEndDeviceBind_t)//Copythefirstrequest'sinformation第一個請求信if(!ZDO_CopyMatchInfo(&(matchED->ed1),bindReq))//不成功{sendRsp=TRUE;}}{sendRsp=TRUE;}if(!sendRsp)//分配空間成功,數(shù)據(jù)結(jié)構(gòu)成{Setintothecorrectstate設(shè)置正確的設(shè)備狀matchED->state=//Setupthetimeout ZDO_EndDeviceBindMatchTimeoutCB}}//{=;//Copythe2ndrequest'sinformation拷貝2個請求信息結(jié)ifZDO_CopyMatchInfo&(matchED->ed2bindReq拷貝不成{status=sendRsp=}//Makeasourcematchfor//對ed1ID與ed2ID進(jìn)行比較,如果有符合的則會返回,相匹配的簇的數(shù)matchED->ed2.numInClustersmatchED->ed2.inClusters,ZDOBuildBufif(matchED->ed1numMatched) //如果有返回ed1相匹配的簇{Savethematchlist申請空間保存相匹配的簇列matchED->ed1Matched=osal_mem_allocshort)(matchED->ed1numMatched*sizeofuint16if(matchED->ed1Matched) {}{//Allocationerror,stopsendRsp=}}Makeasourcematchfored2以ed2為//對ed2的終端匹配請求和ed1的簇列表相比較,返回相相匹配的簇的數(shù)ifmatchED->ed2numMatched {Savethematchlist保存匹配的簇列matchED->ed2Matched=osal_mem_alloc((short)(matchED->ed2numMatched*sizeof(uint16)));if(matchED->ed2Matched){osal_memcpy(matchED->ed2Matched,ZDOBuildBuf,(matchED->ed2numMatched*sizeof(uint16))}{//Allocationerror,stopsendRsp=TRUE;}}if((sendRsp==FALSE)&&(matchED->ed1numMatched||matchED->ed2numMatched){Dothefirstunbind/bindstate發(fā)送響應(yīng)信息給兩}{status=ZDP_NO_MATCH;sendRsp=TRUE;}}ifsendRsp如果沒有相匹配的或匹配不成{sendresponsetothisrequester發(fā)送匹配請求響dstAddr.addrMode ZDP_EndDeviceBindRsp(bindReq->TransSeq,&dstAddr,status,bindReq->SecurityUse);if(matchED->state==ZDMATCH_SENDING_BINDS){//sendresponsetofirstrequesterdstAddr.addrMode=Addr16Bit;dstAddr.addr.shortAddr=matchED->ed1.srcAddr;}//Processended-releasememoryused}}()息End_Device_Bind_rsp給兩個請求終端。因為在ZDMatchSendState()函數(shù)中也是調(diào)用了ZDP_EndDeviceBindRsp()函數(shù),uint8ZDMatchSendState(uint8reason,uint8status,uint8TransSeq{{//Sendtheresponsemessagestorequestingsendresponsetofirstrequester發(fā)送響應(yīng)信息給第一個請求終端sendresponsetosecondrequester發(fā)送響應(yīng)信息給第二請求終if(matchED->state==ZDMATCH_SENDING_BINDS{}//Processended-releasememoryused}return(TRUE}由于終端節(jié)點在SerialApp.c中層過End_Device_Bind_rsp消息,因此當(dāng)接收到協(xié)調(diào)器節(jié)點發(fā)來的綁定響應(yīng)消息將交由SerialApp任務(wù)處理函數(shù)處理:UINT16SerialApp_ProcessEvent(uint8task_id,UINT16events{if(events&SYS_EVENT_MSG{while((MSGpkt= SerialApp_TaskID)){switch(MSGpkt->hdr.event{caseSerialApp_ProcessZDOMsgs(( ingMsg_t*)MSGpkt);}staticvoid ingMsg_t*inMsg{switch(inMsg->clusterID{caseif(ZDO_ParseBindRsp(inMsg)==ZSuccess{//Light}{//FlashLEDtoshow}}第三個功能:實現(xiàn)兩個節(jié)點間的串口通信sourcectrl)Hal_ProcessPollHalUARTPollHalUARTPollDMAdmaCfg.uartCB(HAL_UART_DMA-1,evt);dmaCfg.uartCB這個函數(shù)進(jìn)行了調(diào)用,ctrl/搜索這個dmaCfg.uartCB,發(fā)現(xiàn)SerialApp_Init函數(shù)有兩句話: HalUARTOpen(SERIAL_APP_PORT,&uartConfig);此處將dmaCfg.uartCB這個函數(shù)成為SerialApp_CallBack,也就是說SerialApp_CallBack函數(shù)每次循環(huán)中被調(diào)用一次,對串口的內(nèi)容進(jìn)行查詢,如果DMA中接收到了數(shù)據(jù),則調(diào)用HalUARTReadDMAbufferAF_DataRequest函數(shù)發(fā)送出去,注意:出去的信息的CLUSTERID(ID)號為SERIALAPP_CLUSTERID1??偨Y(jié)一下這個過程:串口數(shù)據(jù)==>DMA接收==>主循環(huán)中通過SerialApp_CallBack查詢==>DMA獲取并發(fā)送到空具體流voidSerialApp_Init(uint8task_id{ =TRUE; //2x30don'tcare-seeuartdriver. =SERIAL_APP_BAUD; =uartConfig.flowControlThreshold=SERIAL_APP_THRESH;//2x30don'tcare-seeuartdriver. SERIAL_APP_RX_SZ;//2x30don'tcare-seeuartdriver. =SERIAL_APP_TX_SZ;//2x30don'tcare-seeuartdriver. =SERIAL_APP_IDLE; //2x30don'tcare-seeuartdriver. =TRUE; //2x30don'tcare-seeuartdriver. SerialApp_CallBack; //SerialApp_CallBack函數(shù),對串HalUARTOpen(SERIAL_APP_PORT,}{//DMAif((event&(HAL_UART_RX_FULL|HAL_UART_RX_ABOUT_FULL|HAL_UART_RX_TIMEOUT))&if(SerialApp_TxLen<SERIAL_APP_TX_MAX)){ //調(diào)用串口發(fā)送函數(shù),將從串口接受到的數(shù)據(jù),發(fā)送出去}}staticvoid{#if //初始化時 ,所以不執(zhí)if這個預(yù)編譯,轉(zhuǎn)到else去執(zhí)if(SerialApp_TxLen<{}if{if(HalUARTWrite(SERIAL_APP_PORT,SerialApp_TxBuf+1,{}{osal_set_event(SerialApp_TaskID,}}(SerialApp_TxLen=HalUARTRead(SERIAL_APP_PORT,SerialApp_TxBuf+1,{//Pre-pendse
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 11856.1-2025烈性酒質(zhì)量要求第1部分:威士忌
- GB 19081-2025飼料加工系統(tǒng)粉塵防爆安全規(guī)范
- 勞動合同范本 派遣
- 養(yǎng)殖場清糞車購銷合同范本
- 區(qū)域銷售協(xié)議合同范本醫(yī)藥
- 包裝印刷公司采購合同范本
- 買宅地合同范例
- 上海住房合同范本
- 個人與團(tuán)隊提成合同范本
- 線上按摩技師合同范本
- 部編版小學(xué)(2024版)小學(xué)道德與法治一年級下冊《有個新目標(biāo)》-第一課時教學(xué)課件
- 稅法(第5版) 課件 第13章 印花稅
- 2024-2025學(xué)年廣州市高二語文上學(xué)期期末考試卷附答案解析
- 咖啡店合同咖啡店合作經(jīng)營協(xié)議
- 2025年山東鋁業(yè)職業(yè)學(xué)院高職單招職業(yè)技能測試近5年??及鎱⒖碱}庫含答案解析
- 全套電子課件:技能成就夢想
- 2024年教育公共基礎(chǔ)知識筆記
- 2025年江蘇農(nóng)林職業(yè)技術(shù)學(xué)院高職單招職業(yè)技能測試近5年常考版參考題庫含答案解析
- 異構(gòu)數(shù)據(jù)融合技術(shù)-深度研究
- 北京市朝陽區(qū)2024-2025學(xué)年七年級上學(xué)期期末考試數(shù)學(xué)試卷(含答案)
- 2024年湖南汽車工程職業(yè)學(xué)院單招職業(yè)技能測試題庫標(biāo)準(zhǔn)卷
評論
0/150
提交評論