[ZigBee] 16、Zigbee協議棧應用(二)——基於OSAL的無線控制LED閃爍分析(下)


 

 

說在前面:上一篇介紹了無線LED閃爍實現的OSAL部分,本篇介紹如何實現無線數據收發及數據處理:

 

上一篇是用SI跟着流程查看源碼,我個人認為以架構的思維去了解代碼能讓人更清晰

 

 

::ZMain.c程序入口文件

這里chipcon_cstartup.s51是匯編的啟動文件,ZMain.c相當於main文件,里面有main函數:

 1 int main( void )
 2 {
 3 osal_int_disable( INTS_ALL );// Turn off interrupts 關中斷  4     HAL_BOARD_INIT();// Initialization for board related stuff such as LEDs
 5     zmain_vdd_check();// Make sure supply voltage is high enough to run   檢查芯片是否上電正常
 6     InitBoard( OB_COLD );// Initialize board I/O  初始化I/O,LED,Timer等
 7     HalDriverInit();// Initialze HAL drivers 初始化硬件抽象層驅動模塊
 8     osal_nv_init( NULL );// Initialize NV System 初始化flash存儲器
 9     znpTestRF();// Initialize and check the ZNP RF Test Mode NV items. 
10     ZMacInit();// Initialize the MAC  初始化MAC層
11     zmain_ext_addr();// Determine the extended address  確定IEEE64位地址
12 
13 #if defined ZCL_KEY_ESTABLISH
14     zmain_cert_init();// Initialize the Certicom certificate information.
15 #endif
16 
17     zgInit();// Initialize basic NV items  初始化非易失變量
18 
19 #ifndef NONWK
20     afInit();// Since the AF isn't a task, call it's initialization routine
21 #endif
22 
23 osal_init_system();// Initialize the operating system 初始化OS(重點介紹1)
24 osal_int_enable( INTS_ALL );// Allow interrupts 使能中斷
25     InitBoard( OB_READY );// Final board initialization      最終板載初始化
26     zmain_dev_info();// Display information about this device     顯示設備信息(這里有LCD屏幕)
27 
28 #ifdef LCD_SUPPORTED/* Display the device info on the LCD 將信息顯示在LCD上*/
29     zmain_lcd_init();      
30 #endif
31 
32 #ifdef WDT_IN_PM1
33     WatchDogEnable( WDTIMX );/* If WDT is used, this is a good place to enable it. */
34 #endif
35 
36 osal_start_znp(); // No Return from here 執行操作系統(重點介紹2)
37 
38     return 0;  // Shouldn't get here.
39 } // main()

main主要是初始化,然后啟動OS,進入大循環,根據任務優先級處理相應任務。

 

::OSAL_SampleApp.c任務數組及任務初始化文件

上篇講到main函數核心有:

 

初始化最核心的是OSAL任務初始化:(這里的tasksArr是所有任務的索引,后文還會介紹)

 1 /*********************************************************************
 2  * GLOBAL VARIABLES
 3  */
 4 
 5 // The order in this table must be identical to the task initialization calls below in osalInitTask.
 6 const pTaskEventHandlerFn tasksArr[] =
 7 {
 8     macEventLoop,
 9     nwk_event_loop,
10     Hal_ProcessEvent,
11 #if defined( MT_TASK )
12     MT_ProcessEvent,
13 #endif
14     APS_event_loop,
15 #if defined ( ZIGBEE_FRAGMENTATION )
16     APSF_ProcessEvent,
17 #endif
18     ZDApp_event_loop,
19 #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
20     ZDNwkMgr_event_loop,
21 #endif
22 SampleApp_ProcessEvent 23 };
24 
25 const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
26 uint16 *tasksEvents;
27 
28 /*********************************************************************
29  * FUNCTIONS
30  *********************************************************************/
31 
32 /*********************************************************************
33  * @fn      osalInitTasks
34  *
35  * @brief   This function invokes the initialization function for each task.
36  *
37  * @param   void
38  *
39  * @return  none
40  */
41 void osalInitTasks( void )
42 {
43     uint8 taskID = 0;
44 
45     // 分配內存,返回指向緩沖區的指針
46     tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
47     // 設置所分配的內存空間單元值為0
48     osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
49 
50     // 任務優先級由高向低依次排列,高優先級對應taskID 的值反而小
51     macTaskInit( taskID++ );  //macTaskInit(0) ,用戶不需考慮
52     nwk_init( taskID++ );     //nwk_init(1),用戶不需考慮
53     Hal_Init( taskID++ );     //Hal_Init(2) ,用戶需考慮
54 #if defined( MT_TASK )
55     MT_TaskInit( taskID++ );
56 #endif
57     APS_Init( taskID++ );      //APS_Init(3) ,用戶不需考慮
58 #if defined ( ZIGBEE_FRAGMENTATION )
59     APSF_Init( taskID++ );
60 #endif
61     ZDApp_Init( taskID++ );    //ZDApp_Init(4) ,用戶需考慮
62 #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
63     ZDNwkMgr_Init( taskID++ );
64 #endif
65     //用戶創建的任務
66 SampleApp_Init( taskID ); // SampleApp_Init _Init(5) ,用戶需考慮
67 }

 

::SampApp.c文件APP任務實現文件

承接上面66行,SampleApp_Init( uint8 task_id )負責初始化本工程定制化任務無線LED閃爍相關的初始化工作:

 1 void SampleApp_Init( uint8 task_id )
 2 { 
 3   SampleApp_TaskID = task_id;   //osal分配的任務ID隨着用戶添加任務的增多而改變
 4   SampleApp_NwkState = DEV_INIT;//設備狀態設定為ZDO層中定義的初始化狀態
 5   SampleApp_TransID = 0;        //消息發送ID(多消息時有順序之分)
 6   
 7   // Device hardware initialization can be added here or in main() (Zmain.c).
 8   // If the hardware is application specific - add it here.
 9   // If the hardware is other parts of the device add it in main().
10 
11  #if defined ( BUILD_ALL_DEVICES )
12   // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
13   // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
14   // together - if they are - we will start up a coordinator. Otherwise,
15   // the device will start as a router.
16   if ( readCoordinatorJumper() )
17     zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
18   else
19     zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
20 #endif // BUILD_ALL_DEVICES
21 
22 //該段的意思是,如果設置了HOLD_AUTO_START宏定義,將會在啟動芯片的時候會暫停啟動
23 //流程,只有外部觸發以后才會啟動芯片。其實就是需要一個按鈕觸發它的啟動流程。  
24 #if defined ( HOLD_AUTO_START )
25   // HOLD_AUTO_START is a compile option that will surpress ZDApp
26   //  from starting the device and wait for the application to
27   //  start the device.
28   ZDOInitDevice(0);
29 #endif
30 
31   // Setup for the periodic message's destination address 設置發送數據的方式和目的地址尋址模式
32   // Broadcast to everyone 發送模式:廣播發送
33   SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//廣播
34   SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端點號
35   SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;//指定目的網絡地址為廣播地址
36 
37   // Setup for the flash command's destination address - Group 1 組播發送
38   SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; //組尋址
39   SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端點號
40   SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;//組號0x0001
41 
42   // Fill out the endpoint description. 定義本設備用來通信的APS層端點描述符
43   SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; //指定端點號
44   SampleApp_epDesc.task_id = &SampleApp_TaskID;   //SampleApp 描述符的任務ID
45   SampleApp_epDesc.simpleDesc
46             = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;//SampleApp簡單描述符
47   SampleApp_epDesc.latencyReq = noLatencyReqs;    //延時策略
48 
49   // Register the endpoint description with the AF
50   afRegister( &SampleApp_epDesc );    //向AF層登記描述符
51 
52   // Register for all key events - This app will handle all key events
53   RegisterForKeys( SampleApp_TaskID ); // 登記所有的按鍵事件
54 
55   // By default, all devices start out in Group 1
56   SampleApp_Group.ID = 0x0001;//組號
57   osal_memcpy( SampleApp_Group.name, "Group 1", 7  );//設定組名
58   aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );//把該組登記添加到APS中
59 
60 #if defined ( LCD_SUPPORTED )
61   HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); //如果支持LCD,顯示提示信息
62 #endif
63 }
SampleApp_Init( uint8 task_id )

 

上篇講過OS啟動后進入大循環,掃描當前優先級最高的任務執行!

其中若osal_run_task執行了本工程定制化任務的消息,通過調用tasksArr[idx](上面 OSAL_SampleApp.c中講的任務數組就相當於調用了SampleApp_ProcessEvent函數,將消息傳送給任務處理函數:

 1 //用戶應用任務的事件處理函數
 2 uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
 3 {
 4     afIncomingMSGPacket_t *MSGpkt;
 5     (void)task_id;  // Intentionally unreferenced parameter
 6 
 7     if ( events & SYS_EVENT_MSG ) //接收系統消息再進行判斷
 8     {
 9         //接收屬於本應用任務SampleApp的消息,以SampleApp_TaskID標記
10         MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
11         while ( MSGpkt )
12         {
13             switch ( MSGpkt->hdr.event )
14             {
15             // Received when a key is pressed
16 case KEY_CHANGE://按鍵事件
17                 SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
18                 break;
19 
20             // Received when a messages is received (OTA) for this endpoint
21 case AF_INCOMING_MSG_CMD://接收數據事件,調用函數AF_DataRequest()接收數據
22 SampleApp_MessageMSGCB( MSGpkt );//調用回調函數對收到的數據進行處理(1、數據發送函數)
23                 break;
24 
25             // Received whenever the device changes state in the network
26 case ZDO_STATE_CHANGE: 27                 //只要網絡狀態發生改變,就通過ZDO_STATE_CHANGE事件通知所有的任務。
28                 //同時完成對協調器,路由器,終端的設置
29                 SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
30                 //if ( (SampleApp_NwkState == DEV_ZB_COORD)//實驗中協調器只接收數據所以取消發送事件
31                 if ( (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) )
32                 {
33                     // Start sending the periodic message in a regular interval.
34                     //這個定時器只是為發送周期信息開啟的,設備啟動初始化后從這里開始
35                     //觸發第一個周期信息的發送,然后周而復始下去
36                     osal_start_timerEx( SampleApp_TaskID,
37                                         SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
38                                         SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
39                 }
40                 else
41                 {
42                     // Device is no longer in the network
43                 }
44                 break;
45 
46             default:
47                 break;
48             }
49 
50             // Release the memory 事件處理完了,釋放消息占用的內存
51             osal_msg_deallocate( (uint8 *)MSGpkt );
52 
53             // Next - if one is available 指針指向下一個放在緩沖區的待處理的事件,
54             //返回while ( MSGpkt )重新處理事件,直到緩沖區沒有等待處理事件為止
55             MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
56         }
57 
58         // return unprocessed events 返回未處理的事件
59         return (events ^ SYS_EVENT_MSG);
60     }
61 
62     // Send a message out - This event is generated by a timer
63     //  (setup in SampleApp_Init()).
64     if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
65     {
66 // Send the periodic message 處理周期性事件, 67 //利用SampleApp_SendPeriodicMessage()處理完當前的周期性事件,然后啟動定時器 68 //開啟下一個周期性事情,這樣一種循環下去,也即是上面說的周期性事件了, 69 //可以做為傳感器定時采集、上傳任務 70  SampleApp_SendPeriodicMessage(); 71 
72         // Setup to send message again in normal period (+ a little jitter)
73         osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
74                             (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );
75 
76         // return unprocessed events 返回未處理的事件
77         return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
78     }
79 
80     // Discard unknown events
81     return 0;
82 }

 

接收函數:

 1 //接收數據,參數為接收到的數據
 2 void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
 3 {
 4     uint16 flashTime;
 5     byte buf[3];
 6 
 7     switch ( pkt->clusterId ) //判斷簇ID
 8     {
 9     case SAMPLEAPP_PERIODIC_CLUSTERID: //收到廣播數據
10         osal_memset(buf, 0 , 3);
11         osal_memcpy(buf, pkt->cmd.Data, 2); //復制數據到緩沖區中
12 
13         if(buf[0] == 'D' && buf[1] == '1')  //判斷收到的數據是否為"D1"
14         {
15             HalLedBlink(HAL_LED_1, 0, 50, 500);//如果是則Led1間隔500ms閃爍
16 #if defined(ZDO_COORDINATOR) //協調器收到"D1"后,返回"D1"給終端,讓終端Led1也閃爍
17             SampleApp_SendPeriodicMessage();
18 #endif
19         }
20         else
21         {
22             HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
23         }
24         break;
25 
26     case SAMPLEAPP_FLASH_CLUSTERID: //收到組播數據
27         flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );
28         HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );
29         break;
30     }
31 }

 

發送函數:

 1 //分析發送周期信息
 2 void SampleApp_SendPeriodicMessage( void )
 3 {
 4     byte SendData[3] = "D1";
 5 
 6     // 調用AF_DataRequest將數據無線廣播出去
 7     if( AF_DataRequest( &SampleApp_Periodic_DstAddr,//發送目的地址+端點地址和傳送模式 8 &SampleApp_epDesc,//源(答復或確認)終端的描述(比如操作系統中任務ID等)源EP 9 SAMPLEAPP_PERIODIC_CLUSTERID, //被Profile指定的有效的集群號 10 2, // 發送數據長度 11 SendData,// 發送數據緩沖區 12 &SampleApp_TransID, // 任務ID號 13 AF_DISCV_ROUTE, // 有效位掩碼的發送選項 14 AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) //傳送跳數,通常設置為AF_DEFAULT_RADIUS
15     {
16     }
17     else
18     {
19         HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
20         // Error occurred in request to send.
21     }
22 }

 

 

 

Zigbee系列文章:

[ZigBee] 1、 ZigBee簡介

[ZigBee] 2、 ZigBee開發環境搭建

[ZigBee] 3、ZigBee基礎實驗——GPIO輸出控制實驗-控制Led亮滅

[ZigBee] 4、ZigBee基礎實驗——中斷

[ZigBee] 5、ZigBee基礎實驗——圖文與代碼詳解定時器1(16位定時器)(長文)

[ZigBee] 6、ZigBee基礎實驗——定時器3和定時器4(8 位定時器)

[ZigBee] 7、ZigBee之UART剖析(ONLY串口發送)

[ZigBee] 8、ZigBee之UART剖析·二(串口收發)

[ZigBee] 9、ZigBee之AD剖析——AD采集CC2530溫度串口顯示

[ZigBee] 10、ZigBee之睡眠定時器

[ZigBee] 11、ZigBee之睡眠定時器二

[ZigBee] 12、ZigBee之看門狗定時器——餓了就咬人的GOOD DOG

[ZigBee] 13、ZigBee基礎階段性回顧與加深理解——用定時器1產生PWM來控制LED亮度(七色燈)

[ZigBee] 14、Zigbee無線通信前奏——BasicRF 簡單無線點對點傳輸協議

[ZigBee] 15、Zigbee協議棧應用(一)——Zigbee協議棧介紹及簡單例子(長文,OSAL及Zigbee入門知識)

 

 

PS:如果您覺得還不錯,點個贊,讓更多人受益~

@beautifulzzzz 2016-08-01 continue~  
e-mail:beautifulzzzz@qq.com 
sina:http://weibo.com/beautifulzzzz?is_all=1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM