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

📄 copy of evboard.c

📁 zigbee 协议堆栈 stack(简化协议)
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
  V0.1 Initial Release   10/July/2006  RBR

  V0.1.2.1
  7/17/2006 Fixed problem with maximum length packet (0x7F) 
    reception in interrupt service function. Was checking for overflow,
  when I should not have been, since the RX fifo is flushed after
  reception anyway.  RBR.

  V0.2.2.  
  8/2/2006 Fixed problem with checking of CRC byte.  RBR

*/
#include "compiler.h"
#include "lrwpan_common_types.h"   //types common acrosss most files
#include "ieee_lrwpan_defs.h"
#include "hal.h"
#include "halstack.h"
#include "debug.h"
#include "evboard.h"
#include "evbRadio.h"
#include "memalloc.h"
#include "console.h"
#include "phy.h"
#include "mac.h"
#include "neighbor.h"

//configuration bits here.
#ifdef MCC18#pragma config OSC = HSPLL, FCMEN=OFF, PWRT = OFF , BOREN = OFF, WDT = OFF, WDTPS = 1024 , MCLRE=ON, LPT1OSC = OFF, PBADEN=OFF, CCP2MX=PORTBE, STVREN=ON, LVP = OFF, XINST=OFF, DEBUG = OFF#endif
#ifdef HI_TECH_C
__CONFIG(1,HSPLL);   // HSPLL oscillator__CONFIG(2, BORDIS & PWRTDIS & WDTDIS & WDTPS1K); // PWRTEN disabled, BOR disabled, WDT disabled, 1:256 WDT prescaler
__CONFIG(3,PBDIGITAL & CCP2RB3); //portB digital ports, CCP2 to RB3__CONFIG(4, DEBUGDIS & LVPDIS & XINSTDIS);  // Debug Disabled, lowVolt Program disabled, extended instructions disabled.

#endif


RADIO_FLAGS local_radio_flags;

EVB_SW_STATE sw_state;


void evbIntCallback(void){
//poll the switches
 sw_state.bits.s1_last_val = sw_state.bits.s1_val;
 sw_state.bits.s2_last_val = sw_state.bits.s2_val;
 sw_state.bits.s1_val = !(SW1_INPUT_VALUE()); //low true switch, so invert
 sw_state.bits.s2_val = !(SW2_INPUT_VALUE());//low true switch, so invert
 if (sw_state.bits.s1_last_val != sw_state.bits.s1_val) {
       sw_state.bits.s1_tgl = 1;
 }
 if (sw_state.bits.s2_last_val != sw_state.bits.s2_val) {
       sw_state.bits.s2_tgl = 1;
 }

 }

#define SW_POLL_TIME   MSECS_TO_MACTICKS(100)

UINT32 last_switch_poll;

void evbPoll(void){
//only do this if the slow timer not enabled since
//the slowtimer interrupt will handle the polling
#ifndef LRWPAN_ENABLE_SLOW_TIMER
// poll the switches
if ( halMACTimerNowDelta(last_switch_poll) > SW_POLL_TIME) {
    evbIntCallback();
    last_switch_poll = halGetMACTimer();
  }
#endif
}

//init the board
void evbInit(void){
   local_radio_flags.val = 0;
   sw_state.val = 0;
   last_switch_poll = halGetMACTimer();
    //configure SW1, SW2
   SW_CONFIG();
   LED_CONFIG();
   LED1_OFF();
   LED2_OFF();
  RADIO_PORT_CONFIG();
}

void evbLedSet(BYTE lednum, BOOL state) {
    switch(lednum) {
       case 1:    if (state) LED1_ON(); else LED1_OFF(); break;
       case 2:    if (state) LED2_ON(); else LED2_OFF(); break;
    }
}

BOOL evbLedGet(BYTE lednum){
 switch(lednum) {
       case 1:    return(LED1_STATE());
       case 2:    return(LED2_STATE());
    }
  return FALSE;
}



void halSetRadioPANID(UINT16 panid){ 
  UINT8 n,intStatus;
  SAVE_AND_DISABLE_GLOBAL_INTERRUPT(intStatus) ;
   FASTSPI_WRITE_RAM_LE(&panid, CC2420RAM_PANID, 2, n);
  RESTORE_GLOBAL_INTERRUPT(intStatus);	
}


void halSetRadioShortAddr(SADDR saddr){
   UINT8 n,intStatus;
   SAVE_AND_DISABLE_GLOBAL_INTERRUPT(intStatus) ;
   FASTSPI_WRITE_RAM_LE(&saddr, CC2420RAM_SHORTADDR, 2, n);
   RESTORE_GLOBAL_INTERRUPT(intStatus); 
}

void halSetRadioIEEEAddress(void) {
 UINT8 n,intStatus;
  BYTE buf[8];

 halGetProcessorIEEEAddress(buf);
 SAVE_AND_DISABLE_GLOBAL_INTERRUPT(intStatus) ;
 FASTSPI_WRITE_RAM_LE(&buf[0], CC2420RAM_IEEEADDR, 8, n);
 RESTORE_GLOBAL_INTERRUPT(intStatus); 
}

void halRfWaitForCrystalOscillator(void) {
    BYTE spiStatusByte,intStatus;

    
} // halRfWaitForCrystalOscillator

//TODO
void halDisableRadio(void) {
  DISABLE_FIFOP_INT();
 SET_VREG_INACTIVE();
  SET_RESET_ACTIVE();
  halWaitMs(10); 
}


LRWPAN_STATUS_ENUM halSetChannel(BYTE channel){
    UINT16 f,intStatus;

	// Derive frequency programming from the given channel number
	f = (UINT16) (channel - 11); // Subtract the base channel 
	f = f + (f << 2);    		 // Multiply with 5, which is the channel spacing
	f = f + 357 + 0x4000;		 // 357 is 2405-2048, 0x4000 is LOCK_THR = 1
	
    // Write it to the CC2420
	 SAVE_AND_DISABLE_GLOBAL_INTERRUPT(intStatus) ;
	FASTSPI_SETREG(CC2420_FSCTRL, f);
    RESTORE_GLOBAL_INTERRUPT(intStatus);  
  return(LRWPAN_STATUS_SUCCESS);
}

//this is a desperation move to restart a stuck radio
//called by the SendPacket
void halResetRadio(void){
  SET_VREG_INACTIVE();
  halWaitMs(10); 
  halInitRadio(phy_pib.phyCurrentFrequency, phy_pib.phyCurrentChannel, local_radio_flags);
 //write our PANID, our short address
 halSetRadioPANID(mac_pib.macPANID);
 halSetRadioShortAddr(macGetShortAddr());
}


#define CRYSTAL_TIMEOUT 100  //in ms
LRWPAN_STATUS_ENUM halInitRadio(PHY_FREQ_ENUM frequency, BYTE channel, RADIO_FLAGS radio_flags)
{
      BYTE intStatus;
      UINT8 tmp;
     UINT32 start_tick;
     BYTE spiStatusByte;

  	// Make sure that the voltage regulator is on, and that the reset pin is inactive
      SET_RESET_ACTIVE();
      SET_VREG_ACTIVE();
      halWaitMs(1);   
     SET_RESET_INACTIVE();
     halWaitUs(10);

    SAVE_AND_DISABLE_GLOBAL_INTERRUPT(intStatus) ;
  
    // Register modifications
      
    // Poll the SPI status byte until the crystal oscillator is stable
   start_tick = halGetMACTimer();
    do {
	    FASTSPI_STROBE(CC2420_SXOSCON);
	    FASTSPI_UPD_STATUS(spiStatusByte);
          if (! (!(spiStatusByte & (BM(CC2420_XOSC16M_STABLE))))) break;	   
    } while (halMACTimerNowDelta(start_tick) < MSECS_TO_MACTICKS(CRYSTAL_TIMEOUT ) );

     if (!(spiStatusByte & (BM(CC2420_XOSC16M_STABLE)))) {
          DEBUG_STRING(DBG_ERR,"halInitRadio: Crystal failed to stabilize\n");
          RESTORE_GLOBAL_INTERRUPT(intStatus); 
           return(LRWPAN_STATUS_PHY_RADIO_INIT_FAILED);
        }
   

#define PAN_COORDINATOR     0x10
#define ADR_DECODE               0x08
#define AUTO_CRC                   0x20
#define AUTO_ACK                   0x10

   // set some registers
  SPI_ENABLE();
 //with auto ack, auto_crc, pan_coor, the MDMCTRL0 reg should be a value of 0x1AF2
  // high byte MDCTRL0
        
        FASTSPI_TX_ADDR(CC2420_MDMCTRL0);
       //first, MSB
        tmp = 0x02;      //CCA hystersis, mid range
        if (radio_flags.bits.pan_coordinator) {
           tmp  =   tmp | PAN_COORDINATOR;
         }
      if (!radio_flags.bits.listen_mode) {
          // Turning on Address Decoding
       tmp = tmp | ADR_DECODE;
      }
      FASTSPI_TX(tmp);
      //now, LSB
      tmp = 0xC2;
       

    if (!radio_flags.bits.listen_mode) {
       //turn on autoCRC and autoACK, address decode
       tmp = tmp | AUTO_CRC | AUTO_ACK ;
     } 
      FASTSPI_TX(tmp);
   
    SPI_DISABLE();
    local_radio_flags = radio_flags;   //save these if we need to do a reset

    //set the rest
    FASTSPI_SETREG(CC2420_MDMCTRL1, 0x0500); // Set the correlation threshold = 20
    FASTSPI_SETREG(CC2420_IOCFG0, 0x007F);   // Set the FIFOP threshold to maximum
    FASTSPI_SETREG(CC2420_SECCTRL0, 0x01C4); // Turn off "Security enable"

    //have to set our Long address

      halSetRadioIEEEAddress();

    // Set the RF channel
    halSetChannel(channel);

   
      DEBUG_STRING(DBG_INFO, "Radio configured\n");
     //enable the receive

	FASTSPI_STROBE(CC2420_SRXON);
	FASTSPI_STROBE(CC2420_SFLUSHRX);
      // Initialize the FIFOP external interrupt
     FIFOP_INT_INIT();
     ENABLE_FIFOP_INT();

     RESTORE_GLOBAL_INTERRUPT(intStatus); 
	return(LRWPAN_STATUS_SUCCESS);
}

void halFlushRXFIFO(void){
 BYTE intStatus;

   SAVE_AND_DISABLE_GLOBAL_INTERRUPT(intStatus) ;
   FASTSPI_STROBE(CC2420_SFLUSHRX);
   FASTSPI_STROBE(CC2420_SFLUSHRX);
  RESTORE_GLOBAL_INTERRUPT(intStatus); 
}

//regardless of what happens here, we will try TXONCCA after this returns.
void  doIEEE_backoff(void) {
     BYTE be, nb, tmp, rannum;
    UINT32  delay, start_tick;
  
   be = aMinBE;
   nb = 0;
  do {
      if (be) {
        //do random delay
        tmp = be;

⌨️ 快捷键说明

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