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

📄 endpoint.c

📁 该代码为Jennic的基于IEEE802.15.4协议的Zigbee协议栈星型网源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************
 *
 * MODULE:             Demo endpoint code
 *
 * COMPONENT:          $RCSfile: Endpoint.c,v $
 *
 * VERSION:            $Name: JN-SW-4013-2v4 $
 *
 * REVISION:           $Revision: 1.8 $
 *
 * DATED:              $Date: 2006/02/17 09:48:36 $
 *
 * STATUS:             $State: Exp $
 *
 * AUTHOR:             CJG
 *
 * DESCRIPTION:
 * Endpoint for demonstrator. Synchronises to coordinator and sends
 * back data, sets up sensor reads.
 *
 * LAST MODIFIED BY:   $Author: cjg $
 *                     $Modtime: $
 *
 ****************************************************************************
 *
 *  (c) Copyright 2005 JENNIC Ltd
 *
 ****************************************************************************/

/****************************************************************************/
/***        Include files                                                 ***/
/****************************************************************************/
#include "jendefs.h"
#include "ALSdriver.h"
#include "HTSdriver.h"
#include "DemoConfig.h"
#include <AppHardwareApi.h>
#include <AppQueueApi.h>
#include <AppApi.h>
#include <mac_sap.h>
#include <mac_pib.h>
#include "LedControl.h"
#include "Button.h"
#include "gdb.h"

/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/
/* Timing values */
#define FIFTY_MS_IN_32KHZ     1600

/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/
/* Key (button) press values as used internally */
typedef enum
{
    E_KEY_0 = BUTTON_0_MASK,
    E_KEY_1 = BUTTON_1_MASK,
    E_KEYS_0_AND_1 = (BUTTON_0_MASK | BUTTON_1_MASK)
} teKeyValues;

/* State machine states */
typedef enum
{
    E_STATE_OFF,
    E_STATE_SCANNING,
    E_STATE_SYNCING,
    E_STATE_ASSOCIATING,
    E_STATE_RUNNING,
    E_STATE_TX_DATA,
    E_STATE_READ_SENSORS,
    E_STATE_ASSOC_TO_SCAN
} teState;
/* State machine is as follows:

      E_STATE_OFF
            |
            | Initialised
            V
      E_STATE_SCANNING
            |
            | Scan complete, expected beacon found
            V
      E_STATE_SYNCING
            |
            | Expected beacon seen
            V
      E_STATE_ASSOCIATING
            |
            | Association complete
            V
 +--> E_STATE_RUNNING
 |          |
 |          | Beacon seen
 |          V
 |    E_STATE_TX_DATA
 |          |
 |          | Transmission complete (success or fail)
 |          V
 |    E_STATE_READ_SENSORS
 |          |
 |          | Sensors read
 |          |
 +----------+

*/

/* All variables with scope throughout module are in one structure */
typedef struct
{
    /* Transceiver (basically anything TX/RX not covered elsewhere) */
    struct
    {
        uint8   u8CurrentTxHandle;
        uint8   u8PrevRxBsn;
    } sTransceiver;

    /* Controls (switch, light level alarm) */
    struct
    {
        uint8   u8Switch;
        uint8   u8LightAlarmLevel;
    } sControls;

    /* Sensor data, stored between read and going out in frame */
    struct
    {
        uint8   u8TempResult;
        uint8   u8HtsResult;
        uint8   u8AlsResult;
    } sSensors;

    /* System (state, assigned address, channel) */
    struct
    {
        teState eState;
        uint16  u16ShortAddr;
        uint8   u8ThisNode;
        uint8   u8Channel;
    } sSystem;
} tsDemoData;

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/
PRIVATE void   vInitSystem(void);
PRIVATE void   vInitEndpoint(void);
PRIVATE void   vStartScan(void);
PRIVATE bool_t bProcessKeyPress(void);
PRIVATE void   vProcessInterrupts(void);
PRIVATE void   vStartSync(void);
PRIVATE void   vProcessIncomingMcps(MAC_McpsDcfmInd_s *psMcpsInd);
PRIVATE void   vProcessIncomingMlme(MAC_MlmeDcfmInd_s *psMlmeInd);
PRIVATE void   vProcessRead(void);
PRIVATE void   vProcessRxBeacon(MAC_MlmeDcfmInd_s *psMlmeInd);
PRIVATE void   vProcessTxBlock(void);
PRIVATE uint8  u8FindMin(uint8 u8Val1, uint8 u8Val2);
PRIVATE void   vDisplayError(char *pcString, uint32 u32Data);
#ifdef UART0_DEBUG
PRIVATE void   vDebug(char *pcMessage);
PRIVATE void   vDisplayHex(uint32 u32Data, int iSize);
#endif

/****************************************************************************/
/***        Exported Variables                                            ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/

/* Handles from the MAC */
PRIVATE void *s_pvMac;
PRIVATE MAC_Pib_s *s_psMacPib;

tsDemoData sDemoData;

/****************************************************************************/
/***        Exported Functions                                            ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: AppColdStart
 *
 * DESCRIPTION:
 * Entry point for application. Initialises system, starts scan then
 * processes interrupts.
 *
 * RETURNS:
 * void, never returns
 *
 ****************************************************************************/
PUBLIC void AppColdStart(void)
{
    /* Debug hooks: include these regardless of whether debugging or not */
    HAL_GDB_INIT();
    HAL_BREAKPOINT();

    /* General initialisation: reset hardware */
    vInitSystem();

    while (1)
    {
        /* Initialise software elements */
        vInitEndpoint();

        /* Perform scan to find demo coordinator */
        vStartScan();

        /* Run main loop. This processes interrupts util the 'reset' key
           combination is pressed */
        vProcessInterrupts();
    }
}

/****************************************************************************
 *
 * NAME: AppWarmStart
 *
 * DESCRIPTION:
 * Entry point for application from boot loader. Simply jumps to AppColdStart
 * as, in this instance, application will never warm start.
 *
 * RETURNS:
 * Never returns.
 *
 ****************************************************************************/
PUBLIC void AppWarmStart(void)
{
    AppColdStart();
}

/****************************************************************************/
/***        Local Functions                                               ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: vInitSystem
 *
 * DESCRIPTION:
 * Initialises stack and hardware. Also sets non-default values in the
 * 802.15.4 PIB and starts the first read of the light sensor. Subsequent
 * reads of this sensor occur automatically.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vInitSystem(void)
{
    /* Initialise stack and hardware interface, and register peripheral
       interrupts with AppQueueApi handler */
    (void)u32AppQApiInit(NULL, NULL, NULL);
    (void)u32AHI_Init();

    /* Set up the MAC handles. Must be called AFTER u32AppQApiInit() */
    s_pvMac = pvAppApiGetMacHandle();
    s_psMacPib = MAC_psPibGetHandle(s_pvMac);

    /* Set DIO for buttons and LEDs */
    vLedControl(0, TRUE);
    vLedControl(1, TRUE);
    vLedInitRfd();
    vButtonInitRfd();

    /* Enable interrupts for DIO buttons */
    vAHI_DioWakeEdge(0, BUTTON_ALL_MASK_RFD << BUTTON_BASE_BIT);
    vAHI_DioWakeEnable(BUTTON_ALL_MASK_RFD << BUTTON_BASE_BIT, 0);

    /* Set up peripheral hardware */
    vALSreset();
    vHTSreset();

#ifdef UART0_DEBUG
    /* Enable UART 0 for debug output */
    vAHI_UartEnable(0);
    vAHI_UartReset(0, TRUE, TRUE);
    vAHI_UartSetClockDivisor(0, E_AHI_UART_RATE_38400);
    vAHI_UartReset(0, FALSE, FALSE);

    vDebug("Running ");
#endif

#ifdef UART0_DEBUG
    vDebug("InitS ");
#endif

    /* Set Pan ID in PIB (also sets match register in hardware) */
    MAC_vPibSetPanId(s_pvMac, DEMO_PAN_ID);

    /* Start ALS now: it automatically keeps re-sampling after this */
    vALSstartReadChannel(0);
}

/****************************************************************************
 *
 * NAME: vInitEndpoint
 *
 * DESCRIPTION:
 * Initialises software structures and variables.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vInitEndpoint(void)
{
#ifdef UART0_DEBUG
    vDebug("InitE ");
#endif

    /* Set defaults for software */
    sDemoData.sTransceiver.u8CurrentTxHandle = 0;
    sDemoData.sControls.u8Switch = 0;
    sDemoData.sControls.u8LightAlarmLevel = 0;
    sDemoData.sSensors.u8TempResult = 0;
    sDemoData.sSensors.u8HtsResult = 0;
    sDemoData.sSensors.u8AlsResult = 0;
    sDemoData.sSystem.eState = E_STATE_OFF;
    sDemoData.sSystem.u16ShortAddr = 0xffff;
    sDemoData.sSystem.u8ThisNode = 0;
}

/****************************************************************************
 *
 * NAME: vStartScan
 *
 * DESCRIPTION:
 * Sends an MLME request to the 802.15.4 to start an actve scan. If the
 * returned confirmation is not 'deferred', a fatal error is assumed.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vStartScan(void)
{
    MAC_MlmeReqRsp_s  sMlmeReqRsp;
    MAC_MlmeSyncCfm_s sMlmeSyncCfm;

    sDemoData.sSystem.eState = E_STATE_SCANNING;

#ifdef UART0_DEBUG
    vDebug("Sc ");
#endif

    /* Request scan */
    sMlmeReqRsp.u8Type = MAC_MLME_REQ_SCAN;
    sMlmeReqRsp.u8ParamLength = sizeof(MAC_MlmeReqScan_s);

⌨️ 快捷键说明

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