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

📄 mapp.c

📁 freescale的基于802.15.4的无线通讯例程
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
* MyWirelessApp_03-Coordinator 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 type definitions
*************************************************************************************
************************************************************************************/


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

/* Forward declarations of helper functions */
static uint8_t App_StartScan(uint8_t scanType);
static uint8_t App_WaitMsg(nwkMessage_t *pMsg, uint8_t msgType);
static void    App_HandleScanEdConfirm(nwkMessage_t *pMsg);
static uint8_t App_StartCoordinator(void);
static void    App_HandleKeys(key_event_t events) ;

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

/* The short address and PAN ID of the coordinator*/
static const uint8_t maShortAddress[2] = { (mDefaultValueOfShortAddress_c & 0xff), (mDefaultValueOfShortAddress_c >> 8)};
static const uint8_t maPanId[2] = { (mDefaultValueOfPanId_c & 0xff), (mDefaultValueOfPanId_c >> 8)};

/* The current logical channel (frequency band) */
static uint8_t mLogicalChannel;

/* Application input queues */
static anchor_t mMlmeNwkInputQueue;

/************************************************************************************
*************************************************************************************
* 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;    
  /* 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(&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 */
  void *pMsgIn;
  /* Stores the error/success code returned by some functions. */
  uint8_t ret;
      
  /* The application state machine */
  switch(gState)
  {
  case stateInit:        
    /* Print a welcome message to the UART */
    UartUtil_Print("MyWirelessApp_03-Coordinator demo application is initialized and ready.\n\n");            
    /* Goto Energy Detection state. */
    gState = stateScanEdStart;
    TS_SendEvent(gZappTaskID_c, evtStartScan);
    break;
    
  case stateScanEdStart:
    /* Start the Energy Detection scan, and goto wait for confirm state. */
    UartUtil_Print("Initiating the Energy Detection Scan\n");
    /*print a message on the LCD also*/
    LCD_ClearDisplay();
    LCD_WriteString(1,"Starting Energy");
    LCD_WriteString(2,"Detection Scan");  
    ret = App_StartScan(gScanModeED_c);
    if(ret == errorNoError)
    {
      gState = stateScanEdWaitConfirm;
    }
    break;
    
  case stateScanEdWaitConfirm:
    /* Stay in this state until the MLME Scan confirm message arrives,
       and has been processed. Then goto Start Coordinator state. */
    if (events & evtMessageFromMLME)
    {
      pMsgIn = MSG_DeQueue(&mMlmeNwkInputQueue);
      if (pMsgIn)
      {       
        ret = App_WaitMsg(pMsgIn, gNwkScanCnf_c);
        if(ret == errorNoError)
        {
          /* Process the ED scan confirm. The logical
             channel is selected by this function. */
          App_HandleScanEdConfirm(pMsgIn);
          gState = stateStartCoordinator;
          TS_SendEvent(gZappTaskID_c, evtStartCoordinator);
        }
        MSG_Free(pMsgIn);
      }      
    }
    break;

  case stateStartCoordinator:
    /* Start up as a PAN Coordinator on the selected channel. */
    if (events & evtStartCoordinator)
    {
      UartUtil_Print("\nStarting as PAN coordinator on channel 0x");
      UartUtil_PrintHex(&mLogicalChannel, 1, FALSE);
      UartUtil_Print("\n");
      /*print a message on the LCD also*/
      LCD_ClearDisplay();
      LCD_WriteString(1,"Starting");
      LCD_WriteString(2,"PAN coordinator");    
      ret = App_StartCoordinator();
      if(ret == errorNoError)
      {
        /* If the Start request was sent successfully to
           the MLME, then goto Wait for confirm state. */
        gState = stateStartCoordinatorWaitConfirm;
      }
    }
    break; 

  case stateStartCoordinatorWaitConfirm:
    /* Stay in this state until the Start confirm message
       arrives, and then goto the Listen state. */
    if (events & evtMessageFromMLME)
    {
      pMsgIn = MSG_DeQueue(&mMlmeNwkInputQueue);
      if (pMsgIn)
      {                     
        ret = App_WaitMsg(pMsgIn, gNwkStartCnf_c);
        if(ret == errorNoError)
        {
          UartUtil_Print("Started the coordinator with PAN ID 0x");
          UartUtil_PrintHex((uint8_t *)maPanId, 2, 0);
          UartUtil_Print(", and short address 0x");
          UartUtil_PrintHex((uint8_t *)maShortAddress, 2, 0);
          UartUtil_Print(".\n");
          /*print a message on the LCD also*/
          LCD_ClearDisplay();
          LCD_WriteString(1,"PAN coordinator");    
          LCD_WriteString(2,"started");
        }
        MSG_Free(pMsgIn);
      }
    }
    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 */

⌨️ 快捷键说明

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