📄 mapp.c
字号:
/*****************************************************************************
* MyWirelessApp_04-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 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;
/* 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;
/* 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)
{
/* 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_04-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");
/*print a message on the LCD also*/
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);
/*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("\nApplication enters now sleep mode.\n");
#endif /*gLpmIncluded_d*/
}
else
{
UartUtil_Print("\nAssociate Confirm wasn't successful... \n\n");
gState = stateScanActiveStart;
TS_SendEvent(gZappTaskID_c, evtStartScan);
}
}
MSG_Free(pMsgIn);
}
}
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*/
TS_SendEvent(gZappTaskID_c, evtTerminate);
}
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...");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -