[Zigbee] 16、Zigbee协议栈应用(二)——基于OSAL的无线控制LED闪烁...

[复制链接]
 楼主| characteristic 发表于 2020-1-17 14:18 | 显示全部楼层 |阅读模式
[ZigBee] 16、Zigbee协议栈应用(二)——基于OSAL的无线控制LED闪烁分析(下)


说在前面:上一篇介绍了无线LED闪烁实现的OSAL部分,本篇介绍如何实现无线数据收发及数据处理:

上一篇是用SI跟着流程查看源码,我个人认为以架构的思维去了解代码能让人更清晰



 楼主| characteristic 发表于 2020-1-17 14:19 | 显示全部楼层
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. #if defined ZCL_KEY_ESTABLISH
  13.     zmain_cert_init();// Initialize the Certicom certificate information.
  14. #endif

  15.     zgInit();// Initialize basic NV items  初始化非易失变量

  16. #ifndef NONWK
  17.     afInit();// Since the AF isn't a task, call it's initialization routine
  18. #endif

  19.     osal_init_system();// Initialize the operating system     初始化OS(重点介绍1)
  20.     osal_int_enable( INTS_ALL );// Allow interrupts       使能中断
  21.     InitBoard( OB_READY );// Final board initialization      最终板载初始化
  22.     zmain_dev_info();// Display information about this device     显示设备信息(这里有LCD屏幕)

  23. #ifdef LCD_SUPPORTED/* Display the device info on the LCD 将信息显示在LCD上*/
  24.     zmain_lcd_init();      
  25. #endif

  26. #ifdef WDT_IN_PM1
  27.     WatchDogEnable( WDTIMX );/* If WDT is used, this is a good place to enable it. */
  28. #endif

  29.     osal_start_znp(); // No Return from here    执行操作系统(重点介绍2)

  30.     return 0;  // Shouldn't get here.
  31. } // main()
 楼主| characteristic 发表于 2020-1-17 14:19 | 显示全部楼层
main主要是初始化,然后启动OS,进入大循环,根据任务优先级处理相应任务。
 楼主| characteristic 发表于 2020-1-17 14:20 | 显示全部楼层
OSAL_SampleApp.c任务数组及任务初始化文件

上篇讲到main函数核心有:

586725e215216f13c1.png

767795e21521d6feb1.png
 楼主| characteristic 发表于 2020-1-17 14:20 | 显示全部楼层
初始化最核心的是OSAL任务初始化:(这里的tasksArr是所有任务的索引,后文还会介绍)

  1. /*********************************************************************
  2. * GLOBAL VARIABLES
  3. */

  4. // The order in this table must be identical to the task initialization calls below in osalInitTask.
  5. const pTaskEventHandlerFn tasksArr[] =
  6. {
  7.     macEventLoop,
  8.     nwk_event_loop,
  9.     Hal_ProcessEvent,
  10. #if defined( MT_TASK )
  11.     MT_ProcessEvent,
  12. #endif
  13.     APS_event_loop,
  14. #if defined ( ZIGBEE_FRAGMENTATION )
  15.     APSF_ProcessEvent,
  16. #endif
  17.     ZDApp_event_loop,
  18. #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  19.     ZDNwkMgr_event_loop,
  20. #endif
  21.     SampleApp_ProcessEvent
  22. };

  23. const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
  24. uint16 *tasksEvents;

  25. /*********************************************************************
  26. * FUNCTIONS
  27. *********************************************************************/

  28. /*********************************************************************
  29. * @fn      osalInitTasks
  30. *
  31. * [url=home.php?mod=space&uid=247401]@brief[/url]   This function invokes the initialization function for each task.
  32. *
  33. * @param   void
  34. *
  35. * [url=home.php?mod=space&uid=266161]@return[/url]  none
  36. */
  37. void osalInitTasks( void )
  38. {
  39.     uint8 taskID = 0;

  40.     // 分配内存,返回指向缓冲区的指针
  41.     tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
  42.     // 设置所分配的内存空间单元值为0
  43.     osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

  44.     // 任务优先级由高向低依次排列,高优先级对应taskID 的值反而小
  45.     macTaskInit( taskID++ );  //macTaskInit(0) ,用户不需考虑
  46.     nwk_init( taskID++ );     //nwk_init(1),用户不需考虑
  47.     Hal_Init( taskID++ );     //Hal_Init(2) ,用户需考虑
  48. #if defined( MT_TASK )
  49.     MT_TaskInit( taskID++ );
  50. #endif
  51.     APS_Init( taskID++ );      //APS_Init(3) ,用户不需考虑
  52. #if defined ( ZIGBEE_FRAGMENTATION )
  53.     APSF_Init( taskID++ );
  54. #endif
  55.     ZDApp_Init( taskID++ );    //ZDApp_Init(4) ,用户需考虑
  56. #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  57.     ZDNwkMgr_Init( taskID++ );
  58. #endif
  59.     //用户创建的任务
  60.     SampleApp_Init( taskID );  // SampleApp_Init _Init(5) ,用户需考虑
  61. }
 楼主| characteristic 发表于 2020-1-17 14:21 | 显示全部楼层
SampApp.c文件APP任务实现文件

承接上面66行,SampleApp_Init( uint8 task_id )负责初始化本工程定制化任务无线LED闪烁相关的初始化工作:
SampleApp_Init( uint8 task_id )
  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. #if defined ( BUILD_ALL_DEVICES )
  11.   // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
  12.   // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
  13.   // together - if they are - we will start up a coordinator. Otherwise,
  14.   // the device will start as a router.
  15.   if ( readCoordinatorJumper() )
  16.     zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
  17.   else
  18.     zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
  19. #endif // BUILD_ALL_DEVICES

  20. //该段的意思是,如果设置了HOLD_AUTO_START宏定义,将会在启动芯片的时候会暂停启动
  21. //流程,只有外部触发以后才会启动芯片。其实就是需要一个按钮触发它的启动流程。  
  22. #if defined ( HOLD_AUTO_START )
  23.   // HOLD_AUTO_START is a compile option that will surpress ZDApp
  24.   //  from starting the device and wait for the application to
  25.   //  start the device.
  26.   ZDOInitDevice(0);
  27. #endif

  28.   // Setup for the periodic message's destination address 设置发送数据的方式和目的地址寻址模式
  29.   // Broadcast to everyone 发送模式:广播发送
  30.   SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播
  31.   SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
  32.   SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;//指定目的网络地址为广播地址

  33.   // Setup for the flash command's destination address - Group 1 组播发送
  34.   SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; //组寻址
  35.   SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
  36.   SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;//组号0x0001

  37.   // Fill out the endpoint description. 定义本设备用来通信的APS层端点描述符
  38.   SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
  39.   SampleApp_epDesc.task_id = &SampleApp_TaskID;   //SampleApp 描述符的任务ID
  40.   SampleApp_epDesc.simpleDesc
  41.             = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;//SampleApp简单描述符
  42.   SampleApp_epDesc.latencyReq = noLatencyReqs;    //延时策略

  43.   // Register the endpoint description with the AF
  44.   afRegister( &SampleApp_epDesc );    //向AF层登记描述符

  45.   // Register for all key events - This app will handle all key events
  46.   RegisterForKeys( SampleApp_TaskID ); // 登记所有的按键事件

  47.   // By default, all devices start out in Group 1
  48.   SampleApp_Group.ID = 0x0001;//组号
  49.   osal_memcpy( SampleApp_Group.name, "Group 1", 7  );//设定组名
  50.   aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );//把该组登记添加到APS中

  51. #if defined ( LCD_SUPPORTED )
  52.   HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); //如果支持LCD,显示提示信息
  53. #endif
  54. }
 楼主| characteristic 发表于 2020-1-17 14:21 | 显示全部楼层
上篇讲过OS启动后进入大循环,扫描当前优先级最高的任务执行!

883925e215268f20b9.png
 楼主| characteristic 发表于 2020-1-17 14:21 | 显示全部楼层
其中若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.     if ( events & SYS_EVENT_MSG ) //接收系统消息再进行判断
  7.     {
  8.         //接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记
  9.         MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
  10.         while ( MSGpkt )
  11.         {
  12.             switch ( MSGpkt->hdr.event )
  13.             {
  14.             // Received when a key is pressed
  15.             case KEY_CHANGE://按键事件
  16.                 SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
  17.                 break;

  18.             // Received when a messages is received (OTA) for this endpoint
  19.             case AF_INCOMING_MSG_CMD://接收数据事件,调用函数AF_DataRequest()接收数据
  20.                 SampleApp_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理(1、数据发送函数)
  21.                 break;

  22.             // Received whenever the device changes state in the network
  23.             case ZDO_STATE_CHANGE:
  24.                 //只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。
  25.                 //同时完成对协调器,路由器,终端的设置
  26.                 SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
  27.                 //if ( (SampleApp_NwkState == DEV_ZB_COORD)//实验中协调器只接收数据所以取消发送事件
  28.                 if ( (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) )
  29.                 {
  30.                     // Start sending the periodic message in a regular interval.
  31.                     //这个定时器只是为发送周期信息开启的,设备启动初始化后从这里开始
  32.                     //触发第一个周期信息的发送,然后周而复始下去
  33.                     osal_start_timerEx( SampleApp_TaskID,
  34.                                         SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  35.                                         SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
  36.                 }
  37.                 else
  38.                 {
  39.                     // Device is no longer in the network
  40.                 }
  41.                 break;

  42.             default:
  43.                 break;
  44.             }

  45.             // Release the memory 事件处理完了,释放消息占用的内存
  46.             osal_msg_deallocate( (uint8 *)MSGpkt );

  47.             // Next - if one is available 指针指向下一个放在缓冲区的待处理的事件,
  48.             //返回while ( MSGpkt )重新处理事件,直到缓冲区没有等待处理事件为止
  49.             MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
  50.         }

  51.         // return unprocessed events 返回未处理的事件
  52.         return (events ^ SYS_EVENT_MSG);
  53.     }

  54.     // Send a message out - This event is generated by a timer
  55.     //  (setup in SampleApp_Init()).
  56.     if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  57.     {
  58.         // Send the periodic message 处理周期性事件,
  59.         //利用SampleApp_SendPeriodicMessage()处理完当前的周期性事件,然后启动定时器
  60.         //开启下一个周期性事情,这样一种循环下去,也即是上面说的周期性事件了,
  61.         //可以做为传感器定时采集、上传任务
  62.         SampleApp_SendPeriodicMessage();

  63.         // Setup to send message again in normal period (+ a little jitter)
  64.         osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  65.                             (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );

  66.         // return unprocessed events 返回未处理的事件
  67.         return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  68.     }

  69.     // Discard unknown events
  70.     return 0;
  71. }
 楼主| characteristic 发表于 2020-1-17 14:22 | 显示全部楼层
接收函数:

  1. //接收数据,参数为接收到的数据
  2. void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
  3. {
  4.     uint16 flashTime;
  5.     byte buf[3];

  6.     switch ( pkt->clusterId ) //判断簇ID
  7.     {
  8.     case SAMPLEAPP_PERIODIC_CLUSTERID: //收到广播数据
  9.         osal_memset(buf, 0 , 3);
  10.         osal_memcpy(buf, pkt->cmd.Data, 2); //复制数据到缓冲区中

  11.         if(buf[0] == 'D' && buf[1] == '1')  //判断收到的数据是否为"D1"
  12.         {
  13.             HalLedBlink(HAL_LED_1, 0, 50, 500);//如果是则Led1间隔500ms闪烁
  14. #if defined(ZDO_COORDINATOR) //协调器收到"D1"后,返回"D1"给终端,让终端Led1也闪烁
  15.             SampleApp_SendPeriodicMessage();
  16. #endif
  17.         }
  18.         else
  19.         {
  20.             HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
  21.         }
  22.         break;

  23.     case SAMPLEAPP_FLASH_CLUSTERID: //收到组播数据
  24.         flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );
  25.         HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );
  26.         break;
  27.     }
  28. }
 楼主| characteristic 发表于 2020-1-17 14:22 | 显示全部楼层
发送函数:

  1. //分析发送周期信息
  2. void SampleApp_SendPeriodicMessage( void )
  3. {
  4.     byte SendData[3] = "D1";

  5.     // 调用AF_DataRequest将数据无线广播出去
  6.     if( AF_DataRequest( &SampleApp_Periodic_DstAddr,//发送目的地址+端点地址和传送模式
  7.                         &SampleApp_epDesc,//源(答复或确认)终端的描述(比如操作系统中任务ID等)源EP
  8.                         SAMPLEAPP_PERIODIC_CLUSTERID, //被Profile指定的有效的集群号
  9.                         2,       // 发送数据长度
  10.                         SendData,// 发送数据缓冲区
  11.                         &SampleApp_TransID,     // 任务ID号
  12.                         AF_DISCV_ROUTE,      // 有效位掩码的发送选项
  13.                         AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )  //传送跳数,通常设置为AF_DEFAULT_RADIUS
  14.     {
  15.     }
  16.     else
  17.     {
  18.         HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
  19.         // Error occurred in request to send.
  20.     }
  21. }
 楼主| characteristic 发表于 2020-1-17 14:23 | 显示全部楼层
Zigbee系列**:

[ZigBee] 1、 ZigBee简介   https://bbs.21ic.com/icview-2894000-1-1.html

[ZigBee] 2、 ZigBee开发环境搭建   https://bbs.21ic.com/icview-2894002-1-1.html

[ZigBee] 3、ZigBee基础实验——GPIO输出控制实验-控制Led亮灭   https://bbs.21ic.com/icview-2894004-1-1.html

[ZigBee] 4、ZigBee基础实验——中断   https://bbs.21ic.com/icview-2894006-1-1.html

[ZigBee] 5、ZigBee基础实验——图文与代码详解定时器1(16位定时器)  https://bbs.21ic.com/icview-2894008-1-1.html

[ZigBee] 6、ZigBee基础实验——定时器3和定时器4(8 位定时器)  https://bbs.21ic.com/icview-2894896-1-1.html

[ZigBee] 7、ZigBee之UART剖析(ONLY串口发送)   https://bbs.21ic.com/icview-2894898-1-1.html

[ZigBee] 8、ZigBee之UART剖析·二(串口收发)   https://bbs.21ic.com/icview-2894904-1-1.html

[ZigBee] 9、ZigBee之AD剖析——AD采集CC2530温度串口显示   https://bbs.21ic.com/icview-2894906-1-1.html

[ZigBee] 10、ZigBee之睡眠定时器   https://bbs.21ic.com/icview-2894910-1-1.html

[ZigBee] 11、ZigBee之睡眠定时器二   https://bbs.21ic.com/icview-2894914-1-1.html

[ZigBee] 12、ZigBee之看门狗定时器——饿了就咬人的GOOD DOG    https://bbs.21ic.com/icview-2894916-1-1.html

[ZigBee] 13、ZigBee基础阶段性回顾与加深理解——用定时器1产生PWM来控制LED亮度(七色灯)    https://bbs.21ic.com/icview-2895880-1-1.html

[ZigBee] 14、Zigbee无线通信前奏——BasicRF 简单无线点对点传输协议    https://bbs.21ic.com/icview-2895884-1-1.html

[ZigBee] 15、Zigbee协议栈应用(一)——Zigbee协议栈介绍及简单例子(长文,OSAL及Zigbee入门知识)   https://bbs.21ic.com/icview-2895886-1-1.html
qcliu 发表于 2020-2-1 11:27 | 显示全部楼层
非常感谢楼主分享
tfqi 发表于 2020-2-1 11:31 | 显示全部楼层
非常感谢楼主分享
wiba 发表于 2020-2-1 11:38 | 显示全部楼层
非常感谢楼主分享
zljiu 发表于 2020-2-1 11:40 | 显示全部楼层
非常感谢楼主分享
coshi 发表于 2020-2-1 11:45 | 显示全部楼层
非常感谢楼主分享
aoyi 发表于 2020-2-1 11:49 | 显示全部楼层
非常感谢楼主分享
drer 发表于 2020-2-1 12:03 | 显示全部楼层
非常感谢楼主分享
gwsan 发表于 2020-2-1 12:23 | 显示全部楼层
非常感谢楼主分享
kxsi 发表于 2020-2-1 12:28 | 显示全部楼层
非常感谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

18

主题

367

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部