天天看點

基于GenericAPP無線收發實驗

GenericAPP無線收發基礎實驗中,協調器代碼與終端基本一樣

1、協調器主要函數

基于GenericAPP無線收發實驗

2、終端主要函數

基于GenericAPP無線收發實驗

3、GenericApp_SendTheMessage函數中代碼的不同之處

協調器

基于GenericAPP無線收發實驗

終端

基于GenericAPP無線收發實驗

4、GenericApp_MessageMSGCB函數中代碼的不同之處

協調器

基于GenericAPP無線收發實驗

終端

基于GenericAPP無線收發實驗

5、GenericApp_ProcessEvent函數中代碼的不同之處

協調器

基于GenericAPP無線收發實驗

終端

基于GenericAPP無線收發實驗

6、調試程式出現的問題

基于GenericAPP無線收發實驗

解決未定義buf

基于GenericAPP無線收發實驗

實驗現象:上電後協調器組網,終端聯網後發“D1”,協調器收到資料“D1”後led1閃爍。協調器也發“D1”給終端,終端收到後led1也閃爍。

終端程式(Enddevice.c)

/*********************************************************************
 * INCLUDES
 */
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "ZGlobals.h"

#include "Common.h"
#include "DebugTrace.h"

#if !defined( WIN32 )
  #include "OnBoard.h"
#endif

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"

/* RTOS */
#if defined( IAR_ARMCM3_LM )
#include "RTOS_App.h"
#endif  
/*********************************************************************
 * GLOBAL VARIABLES
 */
uint8 AppTitle[] = "Enddevice"; //應用程式名稱

// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
  GENERICAPP_CLUSTERID
};

const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
  GENERICAPP_ENDPOINT,              //  int Endpoint;
  GENERICAPP_PROFID,                //  uint16 AppProfId[2];
  GENERICAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  GENERICAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  GENERICAPP_FLAGS,                 //  int   AppFlags:4;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList,  //  byte *pAppInClusterList;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList   //  byte *pAppInClusterList;
};

endPointDesc_t GenericApp_epDesc;

/*********************************************************************
 * LOCAL VARIABLES
 */
byte GenericApp_TaskID;   // Task ID for internal task/event processing
                          // This variable will be received when
                          // GenericApp_Init() is called.
devStates_t GenericApp_NwkState;


byte GenericApp_TransID;  // This is the unique message ID (counter)

afAddrType_t GenericApp_DstAddr;

/*********************************************************************
 * LOCAL FUNCTIONS
 */
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );


void GenericApp_Init( uint8 task_id )
{
  GenericApp_TaskID = task_id;    //osal配置設定的任務ID随着使用者添加任務的增多而改變
  GenericApp_NwkState = DEV_INIT; 
  GenericApp_TransID = 0;         //消息發送ID(多消息時有順序之分)

  // Fill out the endpoint description.(定義本裝置用來通信的APS層端點描述符)
  GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;//應用程式的端口号
  GenericApp_epDesc.task_id = &GenericApp_TaskID; //描述符的任務ID   
  GenericApp_epDesc.simpleDesc                    //簡單描述符
            = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
  
  GenericApp_epDesc.latencyReq = noLatencyReqs;   //延時政策
  afRegister( &GenericApp_epDesc );   //向AF層登記描述符
}

uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  
  devStates_t GenericApp_NwkState;//裝置狀态
  GenericApp_NwkState = DEV_INIT;

  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      {
        case AF_INCOMING_MSG_CMD:
          GenericApp_MessageMSGCB( MSGpkt );
          break;
        case ZDO_STATE_CHANGE:
          GenericApp_NwkState =(devStates_t)(MSGpkt->hdr.status);
          if(GenericApp_NwkState == DEV_END_DEVICE)
          {
            GenericApp_SendTheMessage();
          }
          break;  

        default:
          break;
      }
      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );

      // Next
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
    }
    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }
  return 0;
}

static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  unsigned char buf[3]; 
  switch ( pkt->clusterId )
  {
    case GENERICAPP_CLUSTERID:
          osal_memset(buf, 0, 3);	      //數組初始化
          osal_memcpy(buf, pkt->cmd.Data, 2); //拷貝資料到數組中
          if(buf[0]=='D' && buf[1]=='1')      //比較資料
          {
            HalLedBlink(HAL_LED_1, 0, 50, 500);	//led1閃爍
          }
          else
          {
            HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
          }
   break;
  }
}


static void GenericApp_SendTheMessage( void )
{
  byte SendData[3]="D1";

  afAddrType_t devDstAddr;	//設定發送模式、端點号、位址
  devDstAddr.addrMode=(afAddrMode_t)Addr16Bit;
  devDstAddr.endPoint=GENERICAPP_ENDPOINT;
  devDstAddr.addr.shortAddr=0x0000;

  //無線發送資料
  AF_DataRequest(&devDstAddr,
  	&GenericApp_epDesc,
  	GENERICAPP_CLUSTERID,
  	2,
  	SendData,
  	&GenericApp_TransID,
  	AF_DISCV_ROUTE,
  	AF_DEFAULT_RADIUS);
}
/*********************************************************************
 */
           

協調器程式(Coordinator.c)

/*********************************************************************
 * INCLUDES
 */
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"

#include "Common.h"
#include "DebugTrace.h"

#if !defined( WIN32 )
  #include "OnBoard.h"
#endif

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"

/* RTOS */
#if defined( IAR_ARMCM3_LM )
#include "RTOS_App.h"
#endif  


uint8 AppTitle[] = "Coordinator"; //應用程式名稱

// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
  GENERICAPP_CLUSTERID
};

const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
  GENERICAPP_ENDPOINT,              //  int Endpoint;
  GENERICAPP_PROFID,                //  uint16 AppProfId[2];
  GENERICAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  GENERICAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  GENERICAPP_FLAGS,                 //  int   AppFlags:4;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList,  //  byte *pAppInClusterList;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList   //  byte *pAppInClusterList;
};

endPointDesc_t GenericApp_epDesc;
byte GenericApp_TaskID;   // Task ID for internal task/event processing
devStates_t GenericApp_NwkState;
byte GenericApp_TransID;  // This is the unique message ID (counter)
afAddrType_t GenericApp_DstAddr;

static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );

void GenericApp_Init( uint8 task_id )
{
  GenericApp_TaskID = task_id;    //osal配置設定的任務ID随着使用者添加任務的增多而改變
  GenericApp_NwkState = DEV_INIT; 
  GenericApp_TransID = 0;         //消息發送ID(多消息時有順序之分)

  // Fill out the endpoint description.(定義本裝置用來通信的APS層端點描述符)
  GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;//應用程式的端口号
  GenericApp_epDesc.task_id = &GenericApp_TaskID; //描述符的任務ID   
  GenericApp_epDesc.simpleDesc                    //簡單描述符
            = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
  
  GenericApp_epDesc.latencyReq = noLatencyReqs;   //延時政策
  afRegister( &GenericApp_epDesc );   //向AF層登記描述符
}

uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;

  if ( events & SYS_EVENT_MSG )	//判斷是否是系統消息
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      {
        case AF_INCOMING_MSG_CMD:
          GenericApp_MessageMSGCB( MSGpkt );
          break;
        default:
          break;
      }      
      osal_msg_deallocate( (uint8 *)MSGpkt );// Release the memory
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );// Next
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }
  return 0;
}

static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  unsigned char buf[3];
  switch ( pkt->clusterId )
  {
    case GENERICAPP_CLUSTERID:
          osal_memset(buf, 0, 3);	//數組初始化
          osal_memcpy(buf, pkt->cmd.Data, 2);//拷貝資料到數組中
          if(buf[0]=='D' && buf[1]=='1')	//比較資料
          {
            HalLedBlink(HAL_LED_1, 0, 50, 500);	//led1閃爍
            GenericApp_SendTheMessage();	//發送資料
          }
          else
          {
            HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
          }
    break;
  }
}

static void GenericApp_SendTheMessage( void )
{
  byte SendData[3]={'D', '1'};

  afAddrType_t devDstAddr;	//設定發送模式、端點号、位址
  devDstAddr.addrMode=(afAddrMode_t)Addr16Bit;
  devDstAddr.endPoint=GENERICAPP_ENDPOINT;
  devDstAddr.addr.shortAddr=0xFFFF;

  //無線發送資料
  AF_DataRequest(&devDstAddr,
  	&GenericApp_epDesc,
  	GENERICAPP_CLUSTERID,
  	2,
  	SendData,
  	&GenericApp_TransID,
  	AF_DISCV_ROUTE,
  	AF_DEFAULT_RADIUS);
}



           

繼續閱讀