Zigbee之TICC2530开发笔记(1)

来源:互联网 发布:淘宝商务男装品牌 编辑:程序博客网 时间:2024/06/05 00:45

TI CC2530协议栈开发:

几个重要的函数:

1.uint8 osal_set_event( uint8 task_id, uint16 event_flag )      //发送任务事件。

UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events ){......... if(GenericApp_NwkState == DEV_ZB_COORD){osal_set_event(GenericApp_TaskID,GenericApp_SEND_MSG_EVT);}if ( events & GenericApp_SEND_MSG_EVT ){RedLED=1;//点亮LED
return (events ^ GenericApp_SEND_MSG_EVT);}}
如果模块变为协调器 C,则发送任务事件 GenericApp_SEND_MSG_EVT  下面为处理该任务事件函数,点亮一个灯。



2.uint8 osal_start_timerEx( uint8 taskID, uint16 event_id, uint16 timeout_value )  //定时发送任务事件。

UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events ){......... if(GenericApp_NwkState == DEV_ZB_COORD){osal_start_timerEx(GenericApp_TaskID,GenericApp_SEND_MSG_EVT,3000);}if ( events & GenericApp_SEND_MSG_EVT ){RedLED=1;//点亮LEDreturn (events ^ GenericApp_SEND_MSG_EVT);}}

和上一个函数功能基本一样,只是这个是3S后发出任务事件,即:模块变为协调器 C,3S后LED点亮

3.uint8 osal_msg_send( uint8 destination_task, uint8 *msg_ptr )//发送消息

UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events ){if ( events & SYS_EVENT_MSG )  {    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );//从消息队列里取出来引发系统事件的消息    while ( MSGpkt )    {      switch ( MSGpkt->hdr.event )      {        case ZDO_CB_MSG:          GenericApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );          break;                  case KEY_CHANGE:          GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );          break;}}  }   if(GenericApp_NwkState == DEV_ZB_COORD)          {//如果本模块成为了协调器,会进入到这来来                               keyChange_t *msgPtr;                msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) );//定义个按钮改变的消息                if ( msgPtr )                {                  msgPtr->hdr.event = KEY_CHANGE;//给这个消息填写相关的值,类型是按钮状态改变KEY_CHANGE                   msgPtr->keys=3;//消息里面的内容是3                                  osal_msg_send( GenericApp_TaskID, (uint8 *)msgPtr );//把发送给应用层GenericApp_TaskID的消息投到消息队列,并且osal_set_event( GenericApp_TaskID, SYS_EVENT_MSG );                }}}void GenericApp_HandleKeys( byte shift, byte keys ){  void GenericApp_HandleKeys( byte shift, byte keys ){  LS164_BYTE(keys);}}
如果模块变为协调器 C,则发送keyChange_t 类型的消息,判断:case KEY_CHANGE:
          GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );

条件成立,LED点亮;

4.AF_DataRequest()//发送函数

    SDApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;    SDApp_DstAddr.addr.shortAddr = 0x0000;//接收模块的网络地址    // Take the first endpoint, Can be changed to search through endpoints    SDApp_DstAddr.endPoint =SDApp_ENDPOINT ;//接收模块的端点房间号         SDApp_epDesc.endPoint = SDApp_ENDPOINT;    SDApp_epDesc.task_id = &SDApp_TaskID;    SDApp_epDesc.simpleDesc            = (SimpleDescriptionFormat_t *)&SDApp_SimpleDesc;    SDApp_epDesc.latencyReq = noLatencyReqs;    // Register the endpoint description with the AF    afRegister( &SDApp_epDesc );               AF_DataRequest( &SDApp_DstAddr, &SDApp_epDesc,//SDApp_epDesc 结构体 端点描述符有源端点的信息,也是10    SDApp_CLUSTERID,//目标端点镞,房间里的接收人数据宏是1,2个字节,所以在射频0x0001   (byte)osal_strlen( theMessageData ) + 1,//发送字符串的长度   (byte *)&theMessageData,//字符串内容数组的首地址   &SDApp_TransID,//全局变量,每发送一次,+1   AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );