void rf_security_enable(uint8_t *key)
{
    cc2420WriteReg(CC2420_SECCTRL0, 0x0306); // Enable CTR encryption with key 0
    cc2420WriteReg(CC2420_SECCTRL1, 0x0e0e); // Encrypt / Decrypt 18 bytes into header

security_enable=1;
}
/**********************************************************
 * set the radio into "normal" mode (buffered TXFIFO) and go into (data) receive */
void rf_data_mode() {
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_pend(radio_sem);
#endif
        cc2420Strobe(CC2420_SRFOFF); //stop radio
        cc2420WriteReg(CC2420_MDMCTRL1, 0x0500); // default MDMCTRL1 value
        cc2420WriteReg(CC2420_DACTST, 0); // default value
        rf_flush_rx_fifo();
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_post(radio_sem);
#endif
}
//-------------------------------------------------------------------------------------------------------
//	void halRfSetChannel(UINT8 Channel)
//
//	DESCRIPTION:
//		Programs CC2420 for a given IEEE 802.15.4 channel.
//		Note that SRXON, STXON or STXONCCA must be run for the new channel selection to take full effect.
//
//	PARAMETERS:
//		UINT8 channel
//			The channel number (11-26)
//-------------------------------------------------------------------------------------------------------
void halRfSetChannel(uint8_t channel) {
    uint16_t f;
    /*
    // Derive frequency programming from the given channel number
    f = (uint16_t) (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
    DISABLE_GLOBAL_INT();
    cc2420WriteReg(CC2420_FSCTRL, f);
    ENABLE_GLOBAL_INT();
    */
    f= cc2420ReadReg(CC2420_FSCTRL);

    // Mask bit 0-9 to 0
    f &= 0x3C00;

    // Set bit 0-9
    f |= 357 + ((channel - 11) * 5)+0x4000;

    DISABLE_GLOBAL_INT();
    cc2420WriteReg(CC2420_FSCTRL, f);
    ENABLE_GLOBAL_INT();

} // rfSetChannel
Beispiel #4
0
void cc2420SetChannel(u08 channel)
{
	u16 f;
	
	// Derive frequency programming from the given channel number
	f = (u16)(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
	cc2420WriteReg(CC2420_FSCTRL, f);
}
void rf_test_mode()
{

#ifdef RADIO_PRIORITY_CEILING
nrk_sem_pend(radio_sem);
#endif
        cc2420Strobe(CC2420_SRFOFF); //stop radio
	// NOTE ON SETTING CC2420_MDMCTRL1
	// 
	// RF studio" uses TX_MODE=3 (CC2420_MDMCTRL1=0x050C)
	// to send an unmodulated carrier; data sheet says TX_MODE 
	// can be 2 or 3. So it should not matter...
	// HOWEVER, using (TX_MODE=3) sometimes causes problems when 
	// going back to "data" mode!
	cc2420WriteReg(CC2420_MDMCTRL1, 0x0508); // MDMCTRL1 with TX_MODE=2
	cc2420WriteReg(CC2420_DACTST, 0x1800); // send unmodulated carrier
	rf_flush_rx_fifo();

#ifdef RADIO_PRIORITY_CEILING
nrk_sem_post(radio_sem);
#endif
}
void rf_tx_power(uint8_t pwr)
{
uint16_t tmp;
    //tmp=0x5070;
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_pend (radio_sem);
#endif
    tmp=0xA0E0;
    tmp=tmp | (pwr&0x1F);  
    cc2420WriteReg(CC2420_TXCTRL, tmp);   // Set the FIFOP threshold to maximum
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_post(radio_sem);
#endif
}
void rf_set_cca_thresh(int8_t t)
{
// default is -32
// Higher number is less sensitive
uint16_t val;
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_pend(radio_sem);
#endif

val=(t<<8) | 0x80;
cc2420WriteReg(CC2420_RSSI, val); 

#ifdef RADIO_PRIORITY_CEILING
nrk_sem_post(radio_sem);
#endif
}
//-------------------------------------------------------------------------------------------------------
//  void rf_init(RF_RX_INFO *pRRI, uint8_t channel, WORD panId, WORD myAddr)
//
//  DESCRIPTION:
//      Initializes CC2420 for radio communication via the basic RF library functions. Turns on the
//		voltage regulator, resets the CC2420, turns on the crystal oscillator, writes all necessary
//		registers and protocol addresses (for automatic address recognition). Note that the crystal
//		oscillator will remain on (forever).
//
//  ARGUMENTS:
//      RF_RX_INFO *pRRI
//          A pointer the RF_RX_INFO data structure to be used during the first packet reception.
//			The structure can be switched upon packet reception.
//      uint8_t channel
//          The RF channel to be used (11 = 2405 MHz to 26 = 2480 MHz)
//      WORD panId
//          The personal area network identification number
//      WORD myAddr
//          The 16-bit short address which is used by this node. Must together with the PAN ID form a
//			unique 32-bit identifier to avoid addressing conflicts. Normally, in a 802.15.4 network, the
//			short address will be given to associated nodes by the PAN coordinator.
//-------------------------------------------------------------------------------------------------------
void rf_init(RF_RX_INFO *pRRI, uint8_t channel, uint16_t panId, uint16_t myAddr) {
    uint8_t n;
    uint16_t f;
    uint8_t spiStatusByte;
#ifdef RADIO_PRIORITY_CEILING
   int8_t v;
    radio_sem = nrk_sem_create(1,RADIO_PRIORITY_CEILING);
    if (radio_sem == NULL)
      nrk_kernel_error_add (NRK_SEMAPHORE_CREATE_ERROR, nrk_get_pid ());

  v = nrk_sem_pend (radio_sem);
  if (v == NRK_ERROR) {
    nrk_kprintf (PSTR ("CC2420 ERROR:  Access to semaphore failed\r\n"));
  }
#endif

    // Make sure that the voltage regulator is on, and that the reset pin is inactive
    SET_VREG_ACTIVE();
    halWait(1000);
    SET_RESET_ACTIVE();
    halWait(1);
    SET_RESET_INACTIVE();
    halWait(100);

    // Initialize the FIFOP external interrupt
    //FIFOP_INT_INIT();
    //ENABLE_FIFOP_INT();

    // Turn off all interrupts while we're accessing the CC2420 registers
	DISABLE_GLOBAL_INT();

    // Register modifications
    cc2420Strobe(CC2420_SXOSCOFF);
    cc2420Strobe(CC2420_SXOSCON);
    mdmctrl0=0x02E2;
    cc2420WriteReg(CC2420_MDMCTRL0, mdmctrl0);  // Std Preamble, CRC, no auto ack, no hw addr decoding 
    //FASTSPI_SETREG(CC2420_MDMCTRL0, 0x0AF2);  // Turn on automatic packet acknowledgment
						// Turn on hw addre decoding 
    cc2420WriteReg(CC2420_MDMCTRL1, 0x0500); // Set the correlation threshold = 20
    cc2420WriteReg(CC2420_IOCFG0, 0x007F);   // Set the FIFOP threshold to maximum
    cc2420WriteReg(CC2420_SECCTRL0, 0x01C4); // Turn off "Security"
    cc2420WriteReg(CC2420_RXCTRL1, 0x1A56); // All default except
					    // reference bias current to RX
					    // bandpass filter is set to 3uA 

/*
    // FIXME: remove later for auto ack
    myAddr=MY_MAC;
    panId=0x02;
    FASTSPI_SETREG(CC2420_MDMCTRL0, 0x0AF2);  // Turn on automatic packet acknowledgment
//    FASTSPI_SETREG(CC2420_MDMCTRL0, 0x0AE2);  // Turn on automatic packet acknowledgment
    nrk_spin_wait_us(500);
    nrk_spin_wait_us(500);
    FASTSPI_WRITE_RAM_LE(&myAddr, CC2420RAM_SHORTADDR, 2, n);
    nrk_spin_wait_us(500);
    FASTSPI_WRITE_RAM_LE(&panId, CC2420RAM_PANID, 2, n);
    nrk_spin_wait_us(500);
    
   printf( "myAddr=%d\r\n",myAddr );
*/

    nrk_spin_wait_us(500);
    cc2420WriteRamLE((uint8 *)&panId, CC2420RAM_PANID, 2);
    nrk_spin_wait_us(500);

  	ENABLE_GLOBAL_INT();

    // Set the RF channel
    //halRfSetChannel(channel);
    
	
	
	
	
	f= cc2420ReadReg(CC2420_FSCTRL);

    // Mask bit 0-9 to 0
    f &= 0x3C00;

    // Set bit 0-9
    f |= 357 + ((channel - 11) * 5)+0x4000;

    DISABLE_GLOBAL_INT();
	cc2420WriteReg(CC2420_FSCTRL, f);
	ENABLE_GLOBAL_INT();

    // Turn interrupts back on
	ENABLE_GLOBAL_INT();

	// Set the protocol configuration
	rfSettings.pRxInfo = pRRI;
	rfSettings.panId = panId;
	rfSettings.myAddr = myAddr;
	rfSettings.txSeqNumber = 0;
        rfSettings.receiveOn = FALSE;

	// Wait for the crystal oscillator to become stable
    //halRfWaitForCrystalOscillator();
      

    // Poll the SPI status byte until the crystal oscillator is stable
    do {
	    DISABLE_GLOBAL_INT();
	    spiStatusByte=cc2420GetStatus();
	    ENABLE_GLOBAL_INT();
    } while (!(spiStatusByte & (BM(CC2420_XOSC16M_STABLE))));



	// Write the short address and the PAN ID to the CC2420 RAM (requires that the XOSC is on and stable)
   //	DISABLE_GLOBAL_INT();
//    FASTSPI_WRITE_RAM_LE(&myAddr, CC2420RAM_SHORTADDR, 2, n);
//    FASTSPI_WRITE_RAM_LE(&panId, CC2420RAM_PANID, 2, n);
  //	ENABLE_GLOBAL_INT();

#ifdef RADIO_PRIORITY_CEILING
  v = nrk_sem_post (radio_sem);
  if (v == NRK_ERROR) {
    nrk_kprintf (PSTR ("CC2420 ERROR:  Release of semaphore failed\r\n"));
    _nrk_errno_set (2);
  }
#endif

auto_ack_enable=0;
security_enable=0;
last_pkt_encrypted=0;
} // rf_init() 
void rf_auto_ack_disable()
{
    auto_ack_enable=0;
    mdmctrl0 &= (~0x0010);
    cc2420WriteReg(CC2420_MDMCTRL0, mdmctrl0);  
}
void rf_auto_ack_enable()
{
    auto_ack_enable=1;
    mdmctrl0 |= 0x0010;
    cc2420WriteReg(CC2420_MDMCTRL0, mdmctrl0); 
}
void rf_addr_decode_disable()
{
    mdmctrl0 &= (~0x0800);
    cc2420WriteReg(CC2420_MDMCTRL0, mdmctrl0);
}
void rf_addr_decode_enable()
{
    mdmctrl0 |= 0x0800;
    cc2420WriteReg(CC2420_MDMCTRL0, mdmctrl0);
}
void rf_security_disable()
{
  cc2420WriteReg(CC2420_SECCTRL0, 0x01C4); // Turn off "Security enable"
  security_enable=0;
}
/**********************************************************
 * Put the radio in serial TX mode, where data is sampled from the FIFO
 * pin to send after SFD, and timing is done using FIFOP
 * use rf_carrier_on() to start, set FIFO to first bit, then wait for it
 * to go up and down, then set next bit etc.
 * NOTE: You must set the FIFO pin to output mode in order to do this!
 * This can be undone by calling rf_data_mode()
 */
void rf_tx_set_serial()
{
    cc2420WriteReg(CC2420_MDMCTRL1, 0x0504); // set TXMODE to 1
    rf_flush_rx_fifo();
}
/**********************************************************
 * Set the radio into serial unbuffered RX mode
 * RX data is received through sampling the FIFO pin, timing is done using FIFOP 
 * Use rf_rx_on() to start rcv, then wait for SFD / FIFOP. Sample during each high edge of FIFOP
 * This can be undone by using rf_data_mode()
 */
void rf_rx_set_serial()
{
    cc2420Strobe(CC2420_SRFOFF);           // stop radio
    cc2420WriteReg(CC2420_MDMCTRL1, 0x0501); // Set RX_MODE to 1
    rf_flush_rx_fifo();
}