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

📄 routermain.c

📁 Zigbee 路由协议源代码(版本V3.0)
💻 C
字号:
/*********************************************************************

 Router Node for use with SerialCoordV3/SerialExpV3

 
 R.B.Reese (reese@ece.msstate.edu), September 2005

 ********************************************************************/

// Uncomment ENABLE_DEBUG line to enable debug mode for this file.
// Or you may also globally enable debug by defining this macro
// in zigbee.h file or from compiler command-line.
#ifndef ENABLE_DEBUG
//#define ENABLE_DEBUG
#endif


#include <string.h>

#include "zigbee.h"         // Zigbee defs
#if defined(MCHP_C18)
#include <stdio.h>
#endif

#include "config.h"
#include "msstate.h"

#include "sralloc.h"

#include "Console.h"
#include "bugfix.h"
#include "board.h"


#include "router.h"


// When to update lamp status.
#define MAX_LAMP_UPDATE_PERIOD      (1*TICK_SECOND)

// This defines how LEDs will be blinked when unexpected RESET is detected.
#define FATAL_ERROR_BLINK_PERIOD    (TICK_SECOND/2)

#if defined(ENABLE_DEBUG)
    #define BUILD_MODE  " (Debug Build)"
#else
    #define BUILD_MODE  " (Non-debug Build)"
#endif

// Various informational messages as part of configuration process.
ROM char * const startUpMsg      = "  SerialCoord/RFD V3 Demo, 28 Sep  2005\r\n";

ROM char * const resetMsg              = "******Unexpected reset occurred.******\r\n";
ROM char * const nextChannelMsg        = "Now operating in next channel...\r\n";
ROM char * const bindingTableCleared   = "Neighbor/Binding Table cleared, reseting network\n\r";


#include "app.h"
APPFLAGS appFlags;

// Private helper functions
static void InitializeBoard(void);

#define MAX_CLEARBIND_TIME (TICK_SECOND*10) 


//these are input buffers and flags for the application code
#define INPUT_BUF_SIZE 40
volatile char buf_1[INPUT_BUF_SIZE], buf_2[INPUT_BUF_SIZE];
volatile unsigned char ebufptr,bufptr;
volatile unsigned char buf_flag, send_buffer_flag, overrun_flag;
volatile unsigned char echoptr, ebuf_flag, last_char;

void main(void){

  BYTE c, exit_flag;

  // To keep track of time.
  TICK lastTick;
  TICK S2High;    //use for clearing binding table

  APP_STATE smApp;

  // Enable watchdog
  ENABLE_WDT();

  // Clear all flags in the begining.
  appFlags.Val = 0x00;

  // Initialize the board hardware.
  InitializeBoard();

  // This is to check some errant/programming mistakes.
  if ( TO == 0 ) {
    // Clear watchdog to set TO bit.
    CLRWDT();
    // Clear flags so that we can detect it next time.
    STKPTR &= 0x3F;
    RCON |= 0x1F;
    // Display message on terminal.
    ConsolePutROMString(resetMsg);

    // Prepare for alternating flashing LED sequence.
    D1 = 0;
    D2 = 1;
    TickInit();  //start TICK timer
    DISABLE_WDT();
    // On this error, blink LEDs.
    while(1)
    {
      lastTick = TickGet();
      while (TickGetDiff(TickGet(), lastTick) < (TICK_SECOND>>1)) {
	TickUpdate();   //update tick count
      }

      D1 ^= 1;
      D2 ^= 1;
    }
  }

  ConsolePutROMString(startUpMsg);
  PrintStackConfig();
  PrintOurShortAddress();

  InitRxAlive();
  InitializeNetwork();
  APLPermitAssociation();  //permit nodes to associate with us.

  lastTick = TickGet();

  // main loop basically does nothing except call Stack code
  while( 1 ) {
    // Toggle RA4 to indicate how long we execute this loop
    LATA4 ^= 1;

    // Keep 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();
    KeepRxAlive();  

    // see if neighbor/binding table should be cleared
    if (S2) S2High = TickGet();
    else if ( !S2 && (TickGetDiff(TickGet(), S2High) > MAX_CLEARBIND_TIME)){
	//S2 switch has been held down long enough
	// clear binding table, and lets restart
	DISABLE_WDT();
	//PrintBindingTable();  //debug
	ClearNeighborTable();  
	ConsolePutROMString(bindingTableCleared);
	//PrintBindingTable();  //debug
	while( !ConsoleIsPutReady() ) { CLRWDT(); };
	RESET();
      }
  }
} //end main


static void InitializeBoard(void)
{
    // This is a RS232 console - you may replace it with any console you like.
    ConsoleInit();

    // Switches S2 and S3 are on RB5 and RB4 respectively. We want interrupt-on-change
    INTCON = 0x00;

    // There is no external pull-up resistors on S2 and S3. We will use internal pull-ups.
    // The CC2420 provides SFD (Start of Frame Detect) signal on RB2. We are using
    // the falling edge of that signal to generate INT2
    // Enable PORTB internal pullups, INT2 on falling edge
    INTCON2 = 0x00;

    // Enable INT2 interrupt - SFD from CC2420 or IRQ from ZMD44101
    INTCON3 = 0xD0;

    // CC2420 I/O assignments with respect to PIC:
    // RB0 <- FIFO      (Input)
    // RB1 <- CCA       (Input - Not used in this version of stack)
    // RB2 <- SFD       (Input - Generates interrupt on falling edge)
    // RB3 <- FIFOP     (Input - Used to detect overflow)
    // RC0 -> CSn       (Output - to select CC2420 SPI slave)
    // RC1 -> VREG_EN   (Output - to enable CC2420 voltage regulator)
    // RC2 -> RESETn    (Output - to reset CC2420)
    // RC3 - > SCK      (Output - SPI Clock to CC2420)
    // RC4 <- SO        (Input - SPI data from CC2420)
    // RC5 -> SI        (Output - SPI data to CC2420)

    // Make PORTB as input - this is the RESET default
    TRISB = 0xff;

    // Set PORTC control signal direction and initial states
    // Start with CC2420 disabled and not selected
    LATC = 0xfd;

    // Set the SPI module for use by Stack
    TRISC = 0xD0;

    // Set the SPI module
    SSPSTAT = 0xC0;
    SSPCON1 = 0x20;


    // D1 and D2 are on RA0 and RA1 respectively, and CS of TC77 is on RA2.
    // Make PORTA as digital I/O.
    // The TC77 temp sensor CS is on RA2.
    ADCON1 = 0x0F;

    // Deselect TC77 (RA2)
    LATA = 0x04;

    // Make RA0, RA1, RA2 and RA4 as outputs.
    TRISA = 0xE0;

    // Set up the interrupt to read in a data packet.
    // set to capture on rising edge
    CCP2CON = 0b00000101;
    // set high priority for RX packet detection
    CCP2IP = 1;
    CCP2IF = 0;
    CCP2IE = 1;

}

#if defined(MCHP_C18)
#pragma interruptlow LowISR
void LowISR(void)
#elif defined(HITECH_C18)
    void interrupt low_priority LowISR(void)
#else
    void LowISR(void)
#endif

{
    return;
}


// NOTE: Several PICs, including the PIC18F4620 revision A3 have a RETFIE FAST/MOVFF bug
// The interruptlow keyword is used to work around the bug when using C18
// To work around the bug on PICC-18, configure the compiler to Compile for ICD
#if defined(MCHP_C18)
    #pragma interruptlow HighISR save=section(".tmpdata"),section("MATH_DATA"),PROD
    void HighISR(void)
#elif defined(HITECH_C18)
    void interrupt HighISR(void)
#else
    void HighISR(void)
#endif
{
    // This is a quick way to display MAC TX/RX status on RD1 LED.
    // TMR0IF is also used by TickUpdate, so we will let it clear the flag.
  // D2 turned on during transmission
  if ( TMR0IF )   D2 = 0;

    // Must call this so that MAC module can determine if a transmission is in progress or not.
    APL_ISR();

    // Must call this so that tick manager can update its tick count.
    TickUpdate();

    // Is this a interrupt-on-change interrupt?
    if ( RBIF == 1 )
    {
        // Clear mismatch condition and flag
        if ( S2 == 0 )
            appFlags.bits.S2Toggled = TRUE;
        if ( S3 == 0 )
            appFlags.bits.S3Toggled = TRUE;

        // Clear mis-match condition and reset the interrupt flag
        LATB = PORTB;
        RBIF = 0;

    }
}

#if defined(MCHP_C18)
#pragma code highVector=0x08
void HighVector (void)
{
    _asm goto HighISR _endasm
}
#pragma code /* return to default code section */
#endif

#if defined(MCHP_C18)
#pragma code lowhVector=0x18
void LowVector (void)
{
    _asm goto LowISR _endasm
}
#pragma code /* return to default code section */
#endif




// Callback to application to notify of a frame transmission.
void AppMACFrameTransmitted(void){
  // Turn on LED to indicate activity. TMR0IF interrupt will turn it off.
  // See interrupt handler.
  D2 = 1;
}

// Callback to application to notify of a frame reception
void AppMACFrameReceived(void)
{
    // Turn on LED to indicate activity. TMR0IF interrupt will turn it off.
    // See interrupt handler.
  D2 = 1;
}

// Callback to application to notify of an ack timeout
void AppMACFrameTimeOutOccurred(void)
{
}

// Callback to application to notify that a route discovery attempt failed.
extern void AppRouteDiscoveryFailed( SHORT_ADDR shortAddr )
{
}




⌨️ 快捷键说明

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