//操作系统任务初始化
void osalInitTasks( void )
{
uint8 taskID = ; // 分配内存,返回指向缓冲区的指针
tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
// 设置所分配的内存空间单元值为0
osal_memset( tasksEvents, , (sizeof( uint16 ) * tasksCnt)); // 任务优先级由高向低依次排列,高优先级对应taskID 的值反而小
macTaskInit( taskID++ ); //macTaskInit(0) ,用户不需考虑
nwk_init( taskID++ ); //nwk_init(1),用户不需考虑
Hal_Init( taskID++ ); //Hal_Init(2) ,用户需考虑
#if defined( MT_TASK )
MT_TaskInit( taskID++ );
#endif
APS_Init( taskID++ ); //APS_Init(3) ,用户不需考虑
#if defined ( ZIGBEE_FRAGMENTATION )
APSF_Init( taskID++ );
#endif
ZDApp_Init( taskID++ ); //ZDApp_Init(4) ,用户需考虑
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
ZDNwkMgr_Init( taskID++ );
#endif
//用户创建的任务
SampleApp_Init( taskID ); // SampleApp_Init _Init(5) ,用户需考虑
} //用户创建的任务
void SampleApp_Init( uint8 task_id )
{
SampleApp_TaskID = task_id; //osal分配的任务ID随着用户添加任务的增多而改变
SampleApp_NwkState = DEV_INIT;//设备状态设定为ZDO层中定义的初始化状态
SampleApp_TransID = ; //消息发送ID(多消息时有顺序之分) // Device hardware initialization can be added here or in main() (Zmain.c).
// If the hardware is application specific - add it here.
// If the hardware is other parts of the device add it in main(). #if defined ( BUILD_ALL_DEVICES )
// The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
// We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
// together - if they are - we will start up a coordinator. Otherwise,
// the device will start as a router.
if ( readCoordinatorJumper() )
zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
else
zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
#endif // BUILD_ALL_DEVICES //该段的意思是,如果设置了HOLD_AUTO_START宏定义,将会在启动芯片的时候会暂停启动
//流程,只有外部触发以后才会启动芯片。其实就是需要一个按钮触发它的启动流程。
#if defined ( HOLD_AUTO_START )
// HOLD_AUTO_START is a compile option that will surpress ZDApp
// from starting the device and wait for the application to
// start the device.
ZDOInitDevice();
#endif // Setup for the periodic message's destination address 设置发送数据的方式和目的地址寻址模式
// Broadcast to everyone 发送模式:广播发送
SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播
SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;//指定目的网络地址为广播地址 // Setup for the flash command's destination address - Group 1 组播发送
SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; //组寻址
SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;//组号0x0001 // Fill out the endpoint description. 定义本设备用来通信的APS层端点描述符
SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号
SampleApp_epDesc.task_id = &SampleApp_TaskID; //SampleApp 描述符的任务ID
SampleApp_epDesc.simpleDesc
= (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;//SampleApp简单描述符
SampleApp_epDesc.latencyReq = noLatencyReqs; //延时策略 // Register the endpoint description with the AF
afRegister( &SampleApp_epDesc ); //向AF层登记描述符 // Register for all key events - This app will handle all key events
RegisterForKeys( SampleApp_TaskID ); // 登记所有的按键事件 // By default, all devices start out in Group 1
SampleApp_Group.ID = 0x0001;//组号
osal_memcpy( SampleApp_Group.name, "Group 1", );//设定组名
aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );//把该组登记添加到APS中 #if defined ( LCD_SUPPORTED )
HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); //如果支持LCD,显示提示信息
#endif
} enum
{
AddrNotPresent = , //间接传送
AddrGroup = , //组寻址
Addr16Bit = , //单点传送
Addr64Bit = ,
AddrBroadcast = //广播传送
}; //用户应用任务的事件处理函数 开始操作系统是不断查询是否有事件产生
uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
{
afIncomingMSGPacket_t *MSGpkt;
(void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) //接收系统消息再进行判断
{
//接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
// Received when a key is pressed
case KEY_CHANGE://按键事件
SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
break; // Received when a messages is received (OTA) for this endpoint
case AF_INCOMING_MSG_CMD://接收数据事件,调用函数AF_DataRequest()接收数据
SampleApp_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理
break; // Received whenever the device changes state in the network
case ZDO_STATE_CHANGE:
//只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。
//同时完成对协调器,路由器,终端的设置
SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
//if ( (SampleApp_NwkState == DEV_ZB_COORD)//实验中协调器只接收数据所以取消发送事件
if ( (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) )
{
// Start sending the periodic message in a regular interval.
//这个定时器只是为发送周期信息开启的,设备启动初始化后从这里开始
//触发第一个周期信息的发送,然后周而复始下去
osal_start_timerEx( SampleApp_TaskID,
SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
}
else
{
// Device is no longer in the network
}
break; default:
break;
} // Release the memory 事件处理完了,释放消息占用的内存
osal_msg_deallocate( (uint8 *)MSGpkt ); // Next - if one is available 指针指向下一个放在缓冲区的待处理的事件,
//返回while ( MSGpkt )重新处理事件,直到缓冲区没有等待处理事件为止
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
} // return unprocessed events 返回未处理的事件
return (events ^ SYS_EVENT_MSG);
} // Send a message out - This event is generated by a timer
// (setup in SampleApp_Init()).
if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
{
// Send the periodic message 处理周期性事件,
//利用SampleApp_SendPeriodicMessage()处理完当前的周期性事件,然后启动定时器
//开启下一个周期性事情,这样一种循环下去,也即是上面说的周期性事件了,
//可以做为传感器定时采集、上传任务
SampleApp_SendPeriodicMessage(); // Setup to send message again in normal period (+ a little jitter)
osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
(SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); // return unprocessed events 返回未处理的事件
return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
} // Discard unknown events
return ;
} //接收数据,参数为接收到的数据
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
uint16 flashTime;
byte buf[]; switch ( pkt->clusterId ) //判断簇ID
{
case SAMPLEAPP_PERIODIC_CLUSTERID: //收到广播数据
osal_memset(buf, , );
osal_memcpy(buf, pkt->cmd.Data, ); //复制数据到缓冲区中 if(buf[]=='D' && buf[]=='') //判断收到的数据是否为"D1"
{
HalLedBlink(HAL_LED_1, , , );//如果是则Led1间隔500ms闪烁
#if defined(ZDO_COORDINATOR) //协调器收到"D1"后,返回"D1"给终端,让终端Led1也闪烁
SampleApp_SendPeriodicMessage();
#endif
}
else
{
HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
}
break; case SAMPLEAPP_FLASH_CLUSTERID: //收到组播数据
flashTime = BUILD_UINT16(pkt->cmd.Data[], pkt->cmd.Data[] );
HalLedBlink( HAL_LED_4, , , (flashTime / ) );
break;
}
} //分析发送周期信息
void SampleApp_SendPeriodicMessage( void )
{
byte SendData[]="D1"; // 调用AF_DataRequest将数据无线广播出去
if( AF_DataRequest( &SampleApp_Periodic_DstAddr,//发送目的地址+端点地址和传送模式
&SampleApp_epDesc,//源(答复或确认)终端的描述(比如操作系统中任务ID等)源EP
SAMPLEAPP_PERIODIC_CLUSTERID, //被Profile指定的有效的集群号
, // 发送数据长度
SendData,// 发送数据缓冲区
&SampleApp_TransID, // 任务ID号
AF_DISCV_ROUTE, // 有效位掩码的发送选项
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) //传送跳数,通常设置为AF_DEFAULT_RADIUS
{
}
else
{
HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
// Error occurred in request to send.
}
}

操作系统初始化任务分配内存(tasksEvents)以及任务ID号taskID------》用户创建的任务void SampleApp_Init( uint8 task_id )---->开始操作系统,提取需要处理的事件events=tasksEvents[idx])-------->用户应用任务的事件处理函数SampleApp_ProcessEvent(uint8 task_id, uint16 events )------->SampleApp_MessageMSGCB( MSGpkt )以及SampleApp_SendPeriodicMessage();

 

cc2530启动流程---广播发送数据的更多相关文章

  1. 一文搞懂 Netty 发送数据全流程 | 你想知道的细节全在这里

    欢迎关注公众号:bin的技术小屋,如果大家在看文章的时候发现图片加载不了,可以到公众号查看原文 本系列Netty源码解析文章基于 4.1.56.Final版本 在<Netty如何高效接收网络数据 ...

  2. Linux网络之设备接口层:发送数据包流程dev_queue_xmit

    转自:http://blog.csdn.net/wdscq1234/article/details/51926808 写在前面 本文主要是分析kernel-3.8的源代码,主要集中在Network的n ...

  3. uip UDP server广播模式(client能够随意port,而且主动向client发送数据)

    眼下移植uip,发现UDP server模式下,必须指定本地port以及clientport,否则仅仅能讲clientport设置为0,才干接收随意port的数据,可是无法发送数据,由于此时clien ...

  4. sim800 gprs发送数据的AT流程

    switch(send_flag) { case 1: uart_send(&huart4,"AT\r\n",4); //AT break; case 2: uart_se ...

  5. Android客户端向服务器端发送数据的流程(1)

    原理: android客户端通过使用org.apache.http.impl.client.DefaultHttpClient类来发送数据; 方法介绍: HttpClient是android中提供的一 ...

  6. Android FM模块学习之一 FM启动流程

    最近在学习FM模块,FM是一个值得学习的模块,可以从上层看到底层. 上层就是FM的按扭操作和界面显示,从而调用到FM底层驱动来实现广播收听的功能. FM启动流程:如下图: 先进入FMRadio.jav ...

  7. 从网卡发送数据再谈TCP/IP协议—网络传输速度计算-网卡构造

    在<在深谈TCP/IP三步握手&四步挥手原理及衍生问题—长文解剖IP>里面提到 单个TCP包每次打包1448字节的数据进行发送(以太网Ethernet最大的数据帧是1518字节,以 ...

  8. “无处不在” 的系统核心服务 —— ActivityManagerService 启动流程解析

    本文基于 Android 9.0 , 代码仓库地址 : android_9.0.0_r45 系列文章目录: Java 世界的盘古和女娲 -- Zygote Zygote 家的大儿子 -- System ...

  9. Android 启动流程分析

    原文:https://www.jianshu.com/p/a5532ecc8377 作者曾经在高通的Android性能组工作,主要工作是优化Android Application的启动时间. APP基 ...

随机推荐

  1. VMware+Windbg双机调试

    虚拟机使用XP系统:

  2. 缺少对象 WScript 问题解决方法

    方法一: 先把脚本保存起来(保证你的脚本能正确运行),例如命名为test.vbs 然后在QTP写以下脚本就可以正确运行了! Dim oShell Set oShell =CreateObject (& ...

  3. hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  4. 第3章 Java语言基础----声明常量

    在程序运行过程中一直不会改变的量称为常量(constant),通常也被称为“final变量”: 声明常量的语句:final 数据类型 常量名[=值] 备注:1)常量名通常使用大写字母,但并不是必须的; ...

  5. 桥接 NAT HOST-ONLY

    无论是vmware,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3.Host-Only 桥接 桥接网络是指本地物理网卡和虚拟网卡通 ...

  6. 剑指offer 二进制1中的个数

    算法-求二进制数中1的个数 问题描述 任意给定一个32位无符号整数n,求n的二进制表示中1的个数,比如n = 5(0101)时,返回2,n = 15(1111)时,返回4 这也是一道比较经典的题目了, ...

  7. Thinkphp中使用Redis

    先确保安装了redis扩展 添加Redis配置 'REDIS_HOST'=>'192.168.0.2', 'REDIS_PORT'=>6379, 其他配置根据自己ThinkPHP版本,找到 ...

  8. Unable to chmod /system/build.prop.: Read-only file system

    Unable to chmod /system/build.prop.: Read-only file system 只读文件系统 所以需要更改 使用下面的命令 mount -o remount,rw ...

  9. html 背景透明文字不透明

    .alpha{ width: 100px; height: 100px; color: #fff; background: rgba(0, 0, 0, .3); filter: progid:DXIm ...

  10. Sockets

    Sockets time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...