mapp.c
来自「freescale的基于802.15.4的无线通讯例程」· C语言 代码 · 共 483 行 · 第 1/2 页
C
483 行
/*****************************************************************************
* MyWirelessApp_03-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 bool_t App_Idle(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;
/* 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);
/* Initialize LowPowerModule if included*/
#if (gLpmIncluded_d==1)
PWR_CheckForAndEnterNewPowerState_Init();
#endif /*gLpmIncluded_d*/
/* 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");
/*print a message on the LCD also*/
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)
{
void *pMsgIn;
uint8_t rc;
/* Preset error to contain the success code */
rc = errorNoError;
switch(gState)
{
case stateInit:
/* Print a welcome message to the UART */
UartUtil_Print("MyWirelessApp_03-EndDevice demo application is initialized and ready.\n\n");
/* Goto Energy Detection 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;
}
else
{
UartUtil_Print("Error scanning\n");
/*print a message on the LCD also*/
LCD_ClearDisplay();
LCD_WriteString(1,"Error scanning");
}
break;
case stateScanActiveWaitConfirm:
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");
/*print a message on the LCD also*/
LCD_ClearDisplay();
LCD_WriteString(1,"Low power mode");
gState = stateTerminate;
TS_SendEvent(gZappTaskID_c, evtTerminate);
/* Print out that app will enter sleep mode, if LPM module included*/
#if (gLpmIncluded_d==1)
UartUtil_Print("Application enters now sleep mode.\n");
#endif /*gLpmIncluded_d*/
}
else
{
UartUtil_Print("Scan did not find a suitable coordinator\n");
/*print a message on the LCD also*/
LCD_ClearDisplay();
LCD_WriteString(1,"No coordinator");
LCD_WriteString(2,"found");
}
}
MSG_Free(pMsgIn);
}
else
UartUtil_Print("\nNULL Message\n");
}
break;
case stateTerminate:
/*Allow the device to enter to sleep if no activity on UART */
if (App_Idle() == TRUE)
{
#if (gLpmIncluded_d==1)
UartUtil_ConfigureStopMode(TRUE);
PWR_AllowDeviceToSleep();
#endif /*gLpmIncluded_d */
}
else
{
/*stay in this state until no activity on UART or MLME*/
TS_SendEvent(gZappTaskID_c, evtTerminate);
}
break;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?