⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapp.c

📁 FREESCALE的基于802.15.4无线通讯原代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
* MyWirelessApp_05-EndDevice demo application
*
* (c) Copyright 2006, Freescale, Inc. All rights reserved.
*
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale Semiconductor Danmark A/S.
*
*****************************************************************************/

#include "MApp.h"

/************************************************************************************
*************************************************************************************
* Private macros
*************************************************************************************
************************************************************************************/


/************************************************************************************
*************************************************************************************
* Private prototypes
*************************************************************************************
************************************************************************************/

/* Forward declarations of helper functions */
static uint8_t App_StartScan(uint8_t scanType);
static uint8_t App_HandleScanActiveConfirm(nwkMessage_t *pMsg);
static uint8_t App_WaitMsg(nwkMessage_t *pMsg, uint8_t msgType);
static uint8_t App_SendAssociateRequest(void);
static uint8_t App_HandleAssociateConfirm(nwkMessage_t *pMsg);
static void    App_HandleMcpsInput(mcpsToNwkMessage_t *pMsgIn);
static void    App_TransmitUartData(void);
static void    App_HandleKeys( key_event_t events );

/************************************************************************************
*************************************************************************************
* Private type definitions
*************************************************************************************
************************************************************************************/


/************************************************************************************
*************************************************************************************
* Private memory declarations
*************************************************************************************
************************************************************************************/

/* Information about the PAN we are part of */
static panDescriptor_t mCoordInfo;

/* This is either the short address assigned by the PAN coordinator
   during association, or our own extended MAC address. */
static uint8_t maMyAddress[8];
/* The devices address mode. If 2, then maMyAddress contains the short
   address assigned by the PAN coordinator. If 3, then maMyAddress is
   equal to the extended address. */
static uint8_t mAddrMode;

/* Data request packet for sending UART input to the coordinator */
static nwkToMcpsMessage_t *mpPacket;

/* The MSDU handle is a unique data packet identifier */
static uint8_t mMsduHandle;

/* Number of pending data packets */
static uint8_t mcPendingPackets;

/* Application input queues */
static anchor_t mMlmeNwkInputQueue;
static anchor_t mMcpsNwkInputQueue;


/************************************************************************************
*************************************************************************************
* Public memory declarations
*************************************************************************************
************************************************************************************/

/* The current state of the applications state machine */
uint8_t gState;

/************************************************************************************
*************************************************************************************
* Public functions
*************************************************************************************
************************************************************************************/

void DeepSleepWakeupStackProc(void);

/*****************************************************************************
* Initialization function for the App Task. This is called during
* initialization and should contain any application specific initialization
* (ie. hardware initialization/setup, table initialization, power up
* notificaiton.
*
* Interface assumptions: None
*
* Return value: None
*
*****************************************************************************/
void MApp_init(void)
{ 
  
  /* The initial application state */
  gState = stateInit;    
  /* Reset number of pending packets */
  mcPendingPackets = 0;
  /* Initialize the 802.15.4 stack */
  Init_802_15_4();
  /* Initialize the MAC 802.15.4 extended address */
  Init_MacExtendedAddress();
  /* Initialize the timer module. 
  This is done because the UART module uses the TMR module and this must be initialized
  If you use TMR module functionality for your own, you must not recall this function  */    
  TMR_Init();
  /* register keyboard callback function */
  KBD_Init(App_HandleKeys);
  /* initialize LCD Module */
  LCD_Init();
  /* Initialize the UART so that we can print out status messages */
  UartUtil_Init(gDefaultUartUtilBaudRate_c);
  /* Prepare input queues.*/
  MSG_InitQueue(&mMcpsNwkInputQueue); 
  MSG_InitQueue(&mMlmeNwkInputQueue); 
  /* Enable MCU interrupts */
  IrqControlLib_EnableAllIrqs();
  /*signal app ready*/  
  Led1Flashing;
  Led2Flashing;
  Led3Flashing;
  Led4Flashing;
    
  UartUtil_Print("\nPress any switch on board to start running the application.\n");  
  LCD_ClearDisplay();
  LCD_WriteString(1,"Press any key");
  LCD_WriteString(2,"to start.");
}

/*****************************************************************************
*Mac Application Task event processor.  This function is called to
* process all events for the task. Events include timers, messages and any
* other user defined events
*
* Interface assumptions: None
*
* Return value: None
*****************************************************************************/
void AppTask(event_t events) 
{ 
  /* Pointer for storing the messages from MLME, MCPS, and ASP. */
  void *pMsgIn;
  /* Stores the status code returned by some functions. */
  uint8_t rc;
  
  /* The application state machine */
  switch(gState)
  {
  case stateInit:    
    /* Print a welcome message to the UART */
    UartUtil_Print("MyWirelessApp_05-EndDevice demo application is initialized and ready.\n\n");            
    /* Goto Active Scan state. */
    gState = stateScanActiveStart;
    TS_SendEvent(gZappTaskID_c, evtStartScan);
    break;
    
  case stateScanActiveStart:
    /* Start the Active scan, and goto wait for confirm state. */
    UartUtil_Print("Start scanning for a PAN coordinator\n");
    /*print a message on the LCD also*/
    LCD_ClearDisplay();
    LCD_WriteString(1,"Start scanning");
    LCD_WriteString(2,"for coordinator");  
    rc = App_StartScan(gScanModeActive_c);
    if(rc == errorNoError)
    {
      gState = stateScanActiveWaitConfirm;
    }
    break;
    
  case stateScanActiveWaitConfirm:
    /* Stay in this state until the Scan confirm message
          arrives, and then goto the associate state. */
    if (events & evtMessageFromMLME)
    {
      pMsgIn = MSG_DeQueue(&mMlmeNwkInputQueue);
      if (pMsgIn)
      {              
        rc = App_WaitMsg(pMsgIn, gNwkScanCnf_c);
        if(rc == errorNoError)
        {
          /* ALWAYS free the beacon frame contained in the beacon notify indication.*/
          MSG_Free(((nwkMessage_t *)pMsgIn)->msgData.beaconNotifyInd.pBufferRoot);
          UartUtil_Print("Received an MLME-Beacon Notify Indication\n");
          rc = App_HandleScanActiveConfirm(pMsgIn);
          if(rc == errorNoError)
          {
            UartUtil_Print("Found a coordinator with the following properties:\n");
            UartUtil_Print("----------------------------------------------------");
            UartUtil_Print("\nAddress...........0x"); UartUtil_PrintHex(mCoordInfo.coordAddress, mCoordInfo.coordAddrMode == gAddrModeShort_c ? 2 : 8, 0);
            UartUtil_Print("\nPAN ID............0x"); UartUtil_PrintHex(mCoordInfo.coordPanId, 2, 0);
            UartUtil_Print("\nLogical Channel...0x"); UartUtil_PrintHex(&mCoordInfo.logicalChannel, 1, 0);
            UartUtil_Print("\nBeacon Spec.......0x"); UartUtil_PrintHex(mCoordInfo.superFrameSpec, 2, 0);
            UartUtil_Print("\nLink Quality......0x"); UartUtil_PrintHex(&mCoordInfo.linkQuality, 1, 0);
            UartUtil_Print("\n\n");

            gState = stateAssociate;
            TS_SendEvent(gZappTaskID_c, evtStartAssoc);
          }
          else
          {
            UartUtil_Print("Scan did not find a suitable coordinator\n");
            LCD_ClearDisplay();
            LCD_WriteString(1,"No coordinator");
            LCD_WriteString(2,"found");      
          }
        } 
        MSG_Free(pMsgIn);
      }
    }
    break;

  case stateAssociate:
    /* Associate to the PAN coordinator */
    UartUtil_Print("Associating to PAN coordinator on channel 0x");
    UartUtil_PrintHex(&(mCoordInfo.logicalChannel), 1, gPrtHexNewLine_c);
    /*print a message on the LCD also*/
    LCD_ClearDisplay();
    LCD_WriteString(1,"Associating to ");
    LCD_WriteString(2,"PAN coordinator");  
    rc = App_SendAssociateRequest();
    if(rc == errorNoError)
      gState = stateAssociateWaitConfirm;
    break; 

  case stateAssociateWaitConfirm:
    /* Stay in this state until the Associate confirm message
       arrives, and then goto the Listen state. */
    if (events & evtMessageFromMLME)
    {
      pMsgIn = MSG_DeQueue(&mMlmeNwkInputQueue);
      if (pMsgIn)
      {
        rc = App_WaitMsg(pMsgIn, gNwkAssociateCnf_c);
        if(rc == errorNoError)
        {
          rc = App_HandleAssociateConfirm(pMsgIn);
          if (rc == errorNoError)
          { 
	          UartUtil_Print("Successfully associated with the coordinator.\n");
	          UartUtil_Print("We were assigned the short address 0x");
	          UartUtil_PrintHex(maMyAddress, mAddrMode == gAddrModeShort_c ? 2 : 8, 0);
	          UartUtil_Print("\n\nReady to send data to the coordinator.\n\n");
	          /*print a message on the LCD also*/
	          LCD_ClearDisplay();
	          LCD_WriteString(1,"Ready to send");
	          LCD_WriteString(2,"data");
	          
	          gState = stateListen;      
	          TS_SendEvent(gZappTaskID_c, evtListen);      
          }        
          else 
          {
          
	          UartUtil_Print("\nAssociate Confirm wasn't successful... \n\n");
	          gState = stateScanActiveStart;
              TS_SendEvent(gZappTaskID_c, evtStartScan);
          }
        }
      MSG_Free(pMsgIn);
      }
    }
    break; 
    
  case stateListen:
    /* stay in this state forever. 
       Handle MCPS confirms and transmit data from UART */
    if (events & evtMessageFromMCPS)
    {      
      /* Get the message from MCPS */
      pMsgIn = MSG_DeQueue(&mMcpsNwkInputQueue);
      if (pMsgIn)
      {              
        /* Process it */
        App_HandleMcpsInput(pMsgIn);
        /* Messages from the MCPS must always be freed. */
        MSG_Free(pMsgIn);
      }
    }

    App_TransmitUartData();
    TS_SendEvent(gZappTaskID_c, evtListen);
    break;
  }
}


/************************************************************************************
*************************************************************************************
* Private functions
*************************************************************************************
************************************************************************************/


/******************************************************************************
* The App_StartScan(scanType) function will start the scan process of the
* specified type in the MAC. This is accomplished by allocating a MAC message,
* which is then assigned the desired scan parameters and sent to the MLME
* service access point.
* The function may return either of the following values:
*   errorNoError:          The Scan message was sent successfully.
*   errorInvalidParameter: The MLME service access point rejected the
*                          message due to an invalid parameter.
*   errorAllocFailed:      A message buffer could not be allocated.
*
******************************************************************************/
static uint8_t App_StartScan(uint8_t scanType)
{
  mlmeMessage_t *pMsg;
  mlmeScanReq_t *pScanReq;

  UartUtil_Print("Sending the MLME-Scan Request message to the MAC...");

  /* Allocate a message for the MLME (We should check for NULL). */
  pMsg = MSG_AllocType(mlmeMessage_t);
  if(pMsg != NULL)
  {
    /* This is a MLME-START.req command */
    pMsg->msgType = gMlmeScanReq_c;
    /* Create the Start request message data. */
    pScanReq = &pMsg->msgData.scanReq;
    /* gScanModeED_c, gScanModeActive_c, gScanModePassive_c, or gScanModeOrphan_c */
    pScanReq->scanType = scanType;
    /* ChannelsToScan & 0xFF - LSB, always 0x00 */
    pScanReq->scanChannels[0] = (uint8_t)((mDefaultValueOfChannel_c)     & 0xFF);
    /* ChannelsToScan>>8 & 0xFF  */
    pScanReq->scanChannels[1] = (uint8_t)((mDefaultValueOfChannel_c>>8)  & 0xFF);
    /* ChannelsToScan>>16 & 0xFF  */
    pScanReq->scanChannels[2] = (uint8_t)((mDefaultValueOfChannel_c>>16) & 0xFF);
    /* ChannelsToScan>>24 & 0xFF - MSB */
    pScanReq->scanChannels[3] = (uint8_t)((mDefaultValueOfChannel_c>>24) & 0xFF);
    /* Duration per channel 0-14 (dc). T[sec] = (16*960*((2^dc)+1))/1000000.
       A scan duration of 3 on 16 channels approximately takes 2 secs. */
    pScanReq->scanDuration = 3;
    
    /* Send the Scan request to the MLME. */
    if(MSG_Send(NWK_MLME, pMsg) == gSuccess_c)
    {
      UartUtil_Print("Done\n");
      return errorNoError;
    }
    else
    {
      UartUtil_Print("Invalid parameter!\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -