×
嵌入式 > 技术百科 > 详情

ZigBee协议栈(二)--OSAL控制LED灯

发布时间:2020-06-17 发布时间:
|

说在前面:上一篇介绍了无线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闪烁相关的初始化工作:

 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 }



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
Keil5(MDK5)在调试(debug)过程中遇到的问题