zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    WCF 限流 Throttling
    VS2010安装了Hide Main Menu 插件
    ASP.NET MVC基于标注特性的Model验证:将ValidationAttribute应用到参数上
    UML面向对象分析设计
    我的WCF开发框架简化版及基于NET.TCP传输方式的实现
    一周最新示例代码回顾 (6/4–6/10)
    Windows Azure 上的托管服务CDN (下) Hosted Service
    计算机考研的调查和改进建议
    并发编程中的重重量级模型和轻量级模型
    JIRA_5_和GreenHopper5.9破解及汉化
  • 原文地址:https://www.cnblogs.com/zjutlitao/p/5727859.html
Copyright © 2011-2022 走看看