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

📄 hcswitchc.c

📁 能在TI公司的CC2420-ZIGBEE-RF芯片上运行的ZIGBEE应用程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*********************************************************************

                    Switch ZigBee Coordinator

Following the Home Control, Lighting profile, this file implements a
simple switch.  Only mandatory clusters are supported, and end device
binding is supported as per the profile requirements.

 *********************************************************************
 * FileName:        HCSwitchC.c
 * Dependencies:
 * Processor:       PIC18F
 * Complier:        MCC18 v2.30 or higher
 *                  HITECH PICC-18 V8.10PL1 or higher
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the 揅ompany? for its PICmicro?Microcontroller is intended and
 * supplied to you, the Company抯 customer, for use solely and
 * exclusively on Microchip PICmicro Microcontroller products. The
 * software is owned by the Company and/or its supplier, and is
 * protected under applicable copyright laws. All rights are reserved.
 * Any use in violation of the foregoing restrictions may subject the
 * user to criminal sanctions under applicable laws, as well as to
 * civil liability for the breach of the terms and conditions of this
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN 揂S IS?CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * HiTech PICC18 Compiler Options excluding device selection:
 *                  -FAKELOCAL -G -Zg -E -C
 *
 * Author               Date    Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * DF/KO                04/29/05 Microchip ZigBee Stack v1.0-2.0
 * DF/KO                07/18/05 Microchip ZigBee Stack v1.0-3.0
 * DF/KO                07/27/05 Microchip ZigBee Stack v1.0-3.1
 * DF/KO                08/19/05 Microchip ZigBee Stack v1.0-3.2
 * DF/KO                09/08/05 Microchip ZigBee Stack v1.0-3.3
 *******************************************************************/

// define ENABLE_DEBUG to see the console display.
#define ENABLE_DEBUG

#include <string.h>
#include "Compiler.h"
#include "generic.h"
#include "Debug.h"

#include "zigbee.h"         // Zigbee defs
#include "sralloc.h"

// Switches and LEDs locations.
#define BIND_SWITCH                 RB5
#define LIGHT_SWITCH                RB4

#define BIND_INDICATION             LATA0
#define MESSAGE_INDICATION          LATA1

#define BIND_STATE_BOUND            0
#define BIND_STATE_TOGGLE           1
#define BIND_STATE_UNBOUND          1
#define BIND_WAIT_DURATION          (6*TICK_SECOND)
#define COORD_SHORT_ADDR            0
#define LIGHT_OFF                   0x00
#define LIGHT_ON                    0xFF
#define LIGHT_TOGGLE                0xF0
#define MAX_MESSAGE_WAIT            (TICK_SECOND)

//******************************************************************************
// Configuration Bits
//******************************************************************************

// If other than 18F4620 device is used, you must define config values of your
// choice or set them via the MPLAB interface.

#if defined(MCHP_C18) && defined(__18F4620)
    #pragma romdata CONFIG1H = 0x300001
    const rom unsigned char config1H = 0b00000010;      // HSPLL oscillator

    #pragma romdata CONFIG2L = 0x300002
    const rom unsigned char config2L = 0b00011111;      // Brown-out Reset Enabled in hardware @ 2.0V, PWRTEN disabled

    #pragma romdata CONFIG2H = 0x300003
    const rom unsigned char config2H = 0b00010000;      // HW WD disabled, 1:128 prescaler

    #pragma romdata CONFIG3H = 0x300005
    const rom unsigned char config3H = 0b10000000;      // PORTB digital on RESET

    #pragma romdata CONFIG4L = 0x300006
    const rom unsigned char config4L = 0b10000001;      // DEBUG disabled,
                                                        // XINST disabled
                                                        // LVP disabled
                                                        // STVREN enabled

    #pragma romdata

#elif defined(HITECH_C18) && defined(_18F4620)
    // Set configuration fuses for HITECH compiler.
    __CONFIG(1, 0x0600);    // HSPLL oscillator
    __CONFIG(2, 0x101F);    // PWRTEN disabled, BOR enabled @ 2.0V, HW WD disabled, 1:128 prescaler
    __CONFIG(3, 0x8000);    // PORTB digital on RESET
    __CONFIG(4, 0x0081);    // DEBUG disabled,
                            // XINST disabled
                            // LVP disabled
                            // STVREN enabled
#endif

//------------------------------------------------------------------------------
// Typedefs
//------------------------------------------------------------------------------

typedef enum _SM_BIND
{
    SM_BIND_INIT,
    SM_BIND_CHECK_RESPONSE
} SM_BIND;

typedef enum _SM_STATE
{
    SM_APP_INIT,
    SM_APP_INIT_NETWORK,
    SM_APP_NORMAL_START,
    SM_APP_NORMAL_RUN,
    SM_APP_BIND_WAIT,
    SM_APP_RUN_SWITCH_TASK,
} SM_STATE;

typedef enum _SM_SWITCH
{
    SM_SWITCH_INIT,
    SM_SWITCH_UPDATE,
    SM_SWITCH_MAC_WAIT,
    SM_SWITCH_APS_WAIT,
    SM_SWITCH_AF_WAIT
} SM_SWITCH;

//------------------------------------------------------------------------------
// Application variables
//------------------------------------------------------------------------------

static union
{
    struct
    {
        unsigned int bBindSwitchToggled     : 1;
        unsigned int bLightSwitchToggled    : 1;
        unsigned int bIsNotBound            : 1;
    } bits;
    BYTE Val;
} appFlags;
#define APP_FLAGS_INIT      0x04
#define TOGGLE_BOUND_FLAG   0x04

static struct
{
    TICK    requestStart;
    union
    {
        struct
        {
            unsigned int bResponseReceived  : 1;
            unsigned int bBindSuccessful    : 1;
        } bits;
        BYTE Val;
    } flags;
} endDeviceBindInfo;

EP_HANDLE           hLightSwitch;

//------------------------------------------------------------------------------
// Function Prototypes
//------------------------------------------------------------------------------

static BOOL BindingTask( void );
static void Initialize( void );
static void InitializeBoard(void);
static void LightSwitchInit(void);
static BOOL LightSwitchTask( void );
static BOOL SendEndDeviceBindReq( void );

//******************************************************************************
//******************************************************************************
// Main
//******************************************************************************
//******************************************************************************

void main(void)
{
    static SM_STATE     smApp = SM_APP_INIT;

    Initialize();

    // This is main Zigbee stack initialization call.  The stack is not enabled
    // until APLEnable is called, so the stack can be enabled and disabled
    // without reinitializing.
    APLInit();

    // Enable global interrupts.  This must be done after APLInit() is called.
    GIEL = 1;
    GIEH = 1;

    // Enable the stack.
    APLEnable();

    // Establish a network.
    APLNetworkInit();

    while( 1 )
    {
        // Toggle RA4 to indicate how long we execute this loop
        LATA4 ^= 1;

        // Keep the watchdog happy.
        CLRWDT();

        // This is the main stack task, responsible for Zigbee stack related functionality.
        // This function must be called before all Zigbee tasks.
        // Actual application task functions can be called in any order.
        APLTask();

        switch(smApp)
        {
            case SM_APP_INIT:
                // Initialize all local end point tasks
                LightSwitchInit();

                DEBUG_OUT( "Trying to start network...\r\n" );
                smApp = SM_APP_INIT_NETWORK;
                break;

            case SM_APP_INIT_NETWORK:
                // We must find an empty channel to establish network, or else
                // continue scanning new channels.
                if ( APLIsNetworkInitComplete() )
                {
                    // If there was any error, report/handle it.
                    if ( GetLastZError() == ZCODE_NO_ERROR )
                    {
                        // We have established a network, break out from this
                        // loop and continue with next logic.
                        DEBUG_OUT( "Network established.\r\n" );

                        // Form new network now - or do it after your
                        // application requirements are met
                        APLNetworkForm();

                        smApp = SM_APP_NORMAL_START;
                    }
                    else
                    {
                        // Actual application may want to go notify user.
                        smApp = SM_APP_INIT;
                    }
                }
                break;

            case SM_APP_NORMAL_START:
                // Permit new associations once we have established a network.
                APLPermitAssociation();

                smApp = SM_APP_NORMAL_RUN;
                break;

            case SM_APP_NORMAL_RUN:
                // See if the bind switch was pressed.  Note that the operation
                // will toggle between binding and unbinding.
                if ( appFlags.bits.bBindSwitchToggled )
                {
                    DEBUG_OUT( "Trying to do end device bind...\r\n" );
                    appFlags.bits.bBindSwitchToggled = FALSE;
                    smApp = SM_APP_BIND_WAIT;
                }

                // See if the light switch was pressed and the user is
                // requesting the light to be toggled.
                else if ( appFlags.bits.bLightSwitchToggled )
                {
                    DEBUG_OUT( "Sending light message...\r\n" );
                    appFlags.bits.bLightSwitchToggled = FALSE;
                    smApp = SM_APP_RUN_SWITCH_TASK;
                }

                break;

            case SM_APP_BIND_WAIT:
                if (BindingTask())
                {
                    DEBUG_OUT( "Binding task complete.\r\n" );
                    smApp = SM_APP_NORMAL_RUN;
                }
                break;

            case SM_APP_RUN_SWITCH_TASK:
                // Once the switch is toggled, we run the task until it is
                // complete.
                if ( LightSwitchTask() )
                {
                    DEBUG_OUT( "Light message done.\r\n" );
                    smApp = SM_APP_NORMAL_RUN;
                }
                break;
        }
    }
}

//------------------------------------------------------------------------------
// Initialize
//------------------------------------------------------------------------------

static void Initialize( void )
{
    BYTE        *p;

    // Enable watchdog
    ENABLE_WDT();

    // Initialize the flags.  We will turn on the LED when we are not bound, and
    // turn it off when we are, since the normal operating state should be bound,
    // and we will save power with that polarity.  Note that if we get reset,
    // bindings are retained, and the LED may not correctly indicate the bind state.
    appFlags.Val = APP_FLAGS_INIT;

    // Initialize the board hardware.
    InitializeBoard();

    // Initialize the tick (time) manager required for Zigbee stack modules.
    TickInit();

    // Initialize the LED's.
    BIND_INDICATION = appFlags.bits.bIsNotBound;

⌨️ 快捷键说明

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