1. 程式人生 > >Zigbee之TICC2530開發筆記(1)

Zigbee之TICC2530開發筆記(1)

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;//點亮LED
return (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 );