?? evboard.c
字號:
/*
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
V0.2.3
8/15/2006 Changed halSendPacket() packet so that TX FIFO is loaded
before STXONCCA is done.
V.02.3.4
Made change in halInitRadio() with regards to the CC2420_IOCFG0 if
dynamic PANIDs are used to accept all BCN frames
*/
#include "compiler.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
#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"
RADIO_FLAGS local_radio_flags;
EVB_SW_STATE sw_state;
static UINT16 random_seed;
void evbInitRandomSeed(void);
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();
evbInitRandomSeed();
//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;
}
//since PIC18 architecture has no good way of doing RANDOM nums, put this
//in the evboard file.
//random_seed variable is unitialized on purpose in RAM
//personalize random_seed by MAC address and TIMER0 value
void evbInitRandomSeed(void) {
random_seed = random_seed ^ TMR0L ^ aExtendedAddress_B0;
random_seed = random_seed << 8;
random_seed = random_seed ^ TMR0L ^ aExtendedAddress_B1;
if (random_seed == 0) {
random_seed = TMR0H;
random_seed = random_seed << 8;
random_seed = random_seed | TMR0L;
}
}
UINT8 halGetRandomByte(void) {
BYTE bit;
bit = 0;
if (random_seed & 0x8000) bit = bit ^ 1;
if (random_seed & 0x4000) bit = bit ^ 1;
if (random_seed & 0x1000) bit = bit ^ 1;
if (random_seed & 0x0008) bit = bit ^ 1;
random_seed = random_seed << 1;
if (bit) random_seed++;
return(random_seed & 0xFF);
}
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);
DEBUG_STRING(DBG_INFO,"RadioPanID: ");
DEBUG_UINT16(DBG_INFO,panid);
DEBUG_STRING(DBG_INFO,"\n");
}
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 {
halGetRandomByte(); //shift the LFSR to try to make PANID more random
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
#ifdef LRWPAN_COORDINATOR
FASTSPI_SETREG(CC2420_IOCFG0, 0x007F); // Set the FIFOP threshold to maximum
#else
#ifdef LRWPAN_USE_STATIC_PANID
FASTSPI_SETREG(CC2420_IOCFG0, 0x007F); // Set the FIFOP threshold to maximum
#else
FASTSPI_SETREG(CC2420_IOCFG0, 0x087F); // Set the FIFOP threshold to maximum, received all BCN frames
#endif
#endif
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);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -