Beispiel #1
0
numvar func_degf(void) {	
	rf_set_register(REG_ADC_CONFIG, 0);
	rf_set_register(REG_TEMP_SENSOR_CONTROL, (3<<TSRANGE | 1<<ENTSOFFS | 1<<ENTSTRIM));
	rf_set_register(REG_ADC_CONFIG, 1<<ADCSTART);
	while (!(rf_read_register(REG_ADC_CONFIG) & (1<<ADCDONE))) {;}
	return rf_read_register(0x11) - 40;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// void rf_set_as_rx(bool rx_active_mode)
//
// Description:
//  Sets the device as an RX, giving the user the option to go into active or standby mode
//
// Parameters:
//  bool rx_active_mode - true to take to RX active mode (if RX), false otherwise
//
// Return value:
//  None
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void rf_set_as_rx(bool rx_active_mode)
{
	unsigned char config;

	rf_read_register(RF_CONFIG, &config, 1); //Read the current CONFIG value

	//If the device is already configured as an RX, exit
	if((config & RF_CONFIG_PRIM_RX) != 0)
	{
		return;
	}

	//Set the PRIM_RX bit, then write the old value of CONFIG back to the device
	config |= RF_CONFIG_PRIM_RX;
	rf_write_register(RF_CONFIG, &config, 1);

	//Set the CE pin if active RX mode is requested, and clear it otherwise
	if(rx_active_mode != false)
	{
		rf_set_ce();
	}
	else
	{
		rf_clear_ce();
	}
}
Beispiel #3
0
//////////////////////
// rx_fetch_pkt: read packet into buffer if available
//
//	returns number of bytes transferred to pkt, 0 if no data available
//
byte rx_fetch_pkt(pkt_t *pkt) {

	// Does the radio have business for us?
	if (!rx_pkt_ready()) return 0;		// quick out if our packet interrupt hasn't fired

	if (!(rf_read_status() & (1<<IPKVALID))) {
#ifdef RADIO_DEBUG
		sp("rx: int no pkt!");
#endif
		return 0;
	}

#ifdef RADIO_DEBUG
	sp("rx_ got _packet");
#endif

	led_on();

	byte length = rf_read_register(REG_RECEIVED_PACKET_LENGTH);

	// If the packet length is bigger than our buffer We Have A Situation.
	// Clip to our max packet size; set_rx_mode() will clear the fifo,
	// silently discarding the excess.
	//
	if (length > RF_PACKET_SIZE) length = RF_PACKET_SIZE;

	rx_get_pkt(REG_FIFO, length, pkt);
	rx_packet_count++;						// got one? count it
	set_rx_mode();
	led_off();

	log_packet('R', pkt, length);

	return length;
}
Beispiel #4
0
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// void rf_set_as_tx()
//
// Description:
//  Sets the device as a TX
//
// Parameters:
//  None
//
// Return value:
//  None
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void rf_set_as_tx()
{
	uint8_t config;
	uint8_t ce_status = rf_get_ce();

	rf_read_register(RF_CONFIG, &config, 1); //Read the current CONFIG value

	//If the device is already configured as a TX, exit
	if((config & RF_CONFIG_PRIM_RX) == 0)
	{
		return;
	}

	rf_clear_ce(); //Clear the CE pin to enter TX mode

	//Clear the PRIM_RX bit, then write the old value of CONFIG back to the device
	config &= (~RF_CONFIG_PRIM_RX);
	rf_write_register(RF_CONFIG, &config, 1);

	//Set the CE pin if it was set coming into this function
	if(ce_status)
	{
		rf_set_ce();
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// bool rf_tx_fifo_is_full()
//
// Description:
//  Returns whether or not the TX FIFO is full
//
// Parameters:
//  None
//
// Return value:
//  bool - true if TX FIFO is full, false otherwise
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool rf_tx_fifo_is_full()
{
	uint8_t fifo_status;

	rf_read_register(RF_FIFO_STATUS, &fifo_status, 1);

	return (fifo_status & RF_FIFO_STATUS_TX_FULL) ? true : false;
}
Beispiel #6
0
/////////////////////////////////////
// initialize the radio interface
//
// stick with me, this goes on for a while
// "may you live with interesting radios"
//
void init_radio(void) {

	init_leds();

	// Add Bitlash functions specific to this radio
	//	addBitlashFunction("degf", (bitlash_function) func_degf);
	addBitlashFunction("freq", (bitlash_function) func_setfreq);
	addBitlashFunction("rfget", (bitlash_function) func_rfget);
	addBitlashFunction("rfset", (bitlash_function) func_rfset);


	// Set output mode for outputs
	// TODO: Ensure SS is set to output if L01_CSN moves off SS!
	L01_PORT_DD |= (1<<L01_CE | 1<<L01_CSN | 1<<MOSI | 1<<L01_SCK);

	// Set MISO as an input
	L01_PORT_DD &= ~(1<<MISO);
	L01_PORT &= ~(1<<MISO);		// turn off the internal pullup

	rf_end();		// Set -CSN high to deselect radio

	// Initialize SPI interface to the radio
#if F_CPU == 8000000
	// Enable SPI; be the Master; SPI mode 0; set up for 8/2 prescale for 4 MHz clock
	SPCR = 0<<SPIE | 1<<SPE | 1<<MSTR | 0<<CPOL | 0<<CPHA | 0<<SPR1 | 0<<SPR0;
	SPSR = 1<<SPI2X;		// set 2x mode to make /4 into /2, so 4 MHz

#elif F_CPU == 16000000
	//// Enable SPI; be the Master; SPI mode 0; set up for 16/4 prescale for 4 MHz clock
	SPCR = 0<<SPIE | 1<<SPE | 1<<MSTR | 0<<CPOL | 0<<CPHA | 0<<SPR1 | 0<<SPR0; 
	SPSR = 0<<SPI2X;		// 16 MHz does _not_ need 2X mode

#else
	unsupported F_CPU
#endif

	// clear the status and data registers
	int junk = SPSR; 
	junk = SPDR;

	// For RFM22: set CE low to enable radio  (it's ~STBY)
	rf_enable();

	// Got radio?
	for (;;) {
		rf_set_register(REG_OP_MODE_1, 1<<SWRES);		// Reset the radio
		delay(1000);

		// Verify we have a radio, and it is the right type
		byte device_type = rf_read_register(REG_DEVICE_TYPE);
		byte device_version = rf_read_register(REG_VERSION_CODE);

		//printHex(device_type); speol();
		//printHex(device_version); speol();

		if ((device_type == DEVICE_TYPE_RXTX) &&
			((device_version == DEVICE_RFM22_VER_2) || (device_version == DEVICE_RFM22B))) {
			sp("RFM22 go!"); speol();
			break;
		} else {
			sp("No radio."); speol();
		}
	}

	//
	// Set up radio registers for transmit and receive
	//

	// Disable ENPOR and ENCHIPRDY interrupt sources
	// which are enabled by default
	rf_set_register(REG_INTERRUPT_ENABLE_2, 0);

	//set_ready_mode();

	// Configure GPIO0-1 for tx-rx antenna switching
	rf_set_register(REG_GPIO0_CONFIG, (3<<GPIODRV | 1<<PUP | 0x12));	// GPIO0 is TX-State -> TX-Ant control
	rf_set_register(REG_GPIO1_CONFIG, (3<<GPIODRV | 1<<PUP | 0x15));	// GPIO1 is RX-State -> RX-Ant control

	// Configure GPIO2 for 10MHz system clock output
	rf_set_register(REG_MCLK_OUTPUT, MCLK_10MHZ);

	// Data Access Control setup
	// default for REG_DATA_ACCESS_CONTROL is fine: use the packet engine & crc checks
	//	rf_set_register(REG_DATA_ACCESS_CONTROL, (1<<ENPACRX | 1<<ENPACTX | 1<<ENCRC | CRC_CRC16);

	// Frequency setup
	//
	// For North America, the default frequency band "fb" is 0x15 or 21
	// so 21 * 20 MHz = 420 MHz offset + 480 Mhz base = 900 MHz band
	//
	// the default carrier frequency setting "fc" is 0xBB80 = 48000
	//		48000/64000 = 0.75 * 2 * 10000 = 15000
	// giving 900 + 15 = 915 MHz as default frequency
	//
	// TODO: ignoring frequency offset registers for the moment
	//
	// This is equivalent to the default setting:
	//
	// rf_set_register(REG_FREQUENCY_BAND_SELECT, (1<<SBSEL | 1<<HBSEL | 0x15));
	// rf_set_register(REG_NOMINAL_CARRIER_FREQUENCY_HI, 0xbb);
	// rf_set_register(REG_NOMINAL_CARRIER_FREQUENCY_LO, 0x80);
	// or: rfset(0x75,0x75); rfset(0x76,0xbb); rfset(0x77,0x80)
	//
	// Decoding the example from the sample transmit/receive code for 434 MHz
	// band select low band with offset of 19
	// 240 + 19*10 = 430
	//
	// the remaining 4 mhz is to be found in the r76/77 carrier center freq
	// 	0x6400 = 25600
	// 	25600/64000 = 0.4
	// 	0.4 * 10000 = 4000
	//
	// This is the equivalent code to set 434 MHz using our defines:
	//rf_set_register(REG_FREQUENCY_BAND_SELECT, (1<<SBSEL | 0<<HBSEL | 0x13));
	//rf_set_register(REG_NOMINAL_CARRIER_FREQUENCY_HI, 0x64);
	//rf_set_register(REG_NOMINAL_CARRIER_FREQUENCY_LO, 0x00);
	// or from the console: rfset(0x75,0x53); rfset(0x76,0x64); rfset(0x77,0x00)


	// TX Data Rate Setup
	//
	// this fragment from the example sets up for 4800 bps:
	//	spi_write(0x6e, 0x27); // Tx data rate 1
	//	spi_write(0x6f, 0x52); // Tx data rate 0
	//
	// for HBSEL=1, TX_DR = 1000 * txdr / 2^16
	// for HBSEL=0, TX_DR = 1000 * txdr / 2^21
	//
	// the default on this sample part is 0x0a3d => 39.9938 kbps, call it 40kbps
	//
	// inverting the formula:
	// 		value = (baud/1000)*(2^21)/1000	for baud <  30000
	// 		value = (baud/1000)*(2^16)/1000	for baud >= 30000
	//
	// these values are copied from the file doc/rfm-22-calculations.ods:
	//
	//		baud	divisor	divisor, hex
	//		1200	2516	09D4
	//		2400	5033	13A9
	//		4800	10066	2752
	//		9600	20132	4EA4
	//
	//		40000	2621	0A3D	<-- this is the default 6e/6f setting
	//		57600	3774	0EBE
	//		64000	4194	1062
	//		96000	6291	1893
	//		100000  6553    1999
	//		128000	8388	20C4
	//


// Configure the data rate
//
#define TX_DATA_RATE 40000
//#define TX_DATA_RATE 100000

#if TX_DATA_RATE == 40000

	// 40kbps is the factory setting;
	// take all the defaults but turn on FIFO and FSK
	rf_set_register(REG_MODULATION_MODE_CONTROL_2,
		(0<<TRCLK | MOD_FIFO<<DTMOD | 0<<ENINV | 0<<FD_8 | MOD_FSK<<MODTYP));

#elif  TX_DATA_RATE == 100000

	// Set data rate to 100000 bps
	// NOTE: don't forget to double check the TXDTRTSCALE bit in 0x70 if you set below 30k
	rf_set_register(REG_TX_DATA_RATE_HI, 0x19);
	rf_set_register(REG_TX_DATA_RATE_LO, 0x99);

	// Frequency Deviation setup
	//
	// value in increments of 625 Hz
	// default of 0x20 gives 0x20 = 32 * 625 Hz = 20 KHz
	// example uses 45k/625 = 72
	//

#define TX_DEVIATION 50000

#if TX_DEVIATION == 50000

	// for 100kbps, with deviation at 50 kHz = 50000/625 = 80 = 0x050
	// NOTE: high bit goes in REG_MODULATION_CONTROL_2 as FD_8
	rf_set_register(REG_FREQUENCY_DEVIATION, 0x50);

	// Set up for FIFO io and FSK modulation
	// NOTE: high bit of deviation is here!
	rf_set_register(REG_MODULATION_MODE_CONTROL_2, 
		(0<<TRCLK | MOD_FIFO<<DTMOD | 0<<ENINV | 0<<FD_8 | MOD_FSK<<MODTYP));

	// Set up RX Modem Configuration per Table 16
	//
	// For 100kbps/100kHz settings:
	rf_set_register(REG_IF_FILTER_BANDWIDTH, (1<<DWN3_BYPASS | 0<<NDEC_EXP | 0xf<<FILSET));
	rf_set_register(REG_CLOCK_RECOVERY_OVERSAMPLING_RATE, 0x78);
	rf_set_register(REG_CLOCK_RECOVERY_OFFSET_2, (0<<RXOSR_8_10 | 0<<STALLCTRL | 1<<NCOFF_16_19));
	rf_set_register(REG_CLOCK_RECOVERY_OFFSET_1,(0x11));
	rf_set_register(REG_CLOCK_RECOVERY_OFFSET_0,(0x11));
	rf_set_register(REG_CLOCK_RECOVERY_GAIN_1,(0x04));
	rf_set_register(REG_CLOCK_RECOVERY_GAIN_0,(0x46));

#elif TX_DEVIATION == 300000

	// for 100kbps, set deviation at 300 kHz = 300000/625 = 480 = 0x1e0
	// NOTE: high bit goes in REG_MODULATION_CONTROL_2 as FD_8
	rf_set_register(REG_FREQUENCY_DEVIATION, 0xe0);

	// Set up for FIFO io and FSK modulation
	// NOTE: high bit of deviation is here!
	rf_set_register(REG_MODULATION_MODE_CONTROL_2, 
		(0<<TRCLK | MOD_FIFO<<DTMOD | 0<<ENINV | 1<<FD_8 | MOD_FSK<<MODTYP));

	// Set up RX Modem Configuration per Table 16
	//
	// For 100kbps/300kHz settings:
	rf_set_register(REG_IF_FILTER_BANDWIDTH, (1<<DWN3_BYPASS | 0<<NDEC_EXP | 0xe<<FILSET));
	rf_set_register(REG_CLOCK_RECOVERY_OVERSAMPLING_RATE, 0x78);
	rf_set_register(REG_CLOCK_RECOVERY_OFFSET_2, (0<<RXOSR_8_10 | 0<<STALLCTRL | 1<<NCOFF_16_19));
	rf_set_register(REG_CLOCK_RECOVERY_OFFSET_1,(0x11));
	rf_set_register(REG_CLOCK_RECOVERY_OFFSET_0,(0x11));
	rf_set_register(REG_CLOCK_RECOVERY_GAIN_1,(0x00));
	rf_set_register(REG_CLOCK_RECOVERY_GAIN_0,(0xb8));
#else
	unsupported TX_DEVIATION
#endif

#else
	unsupported TX_DATA_RATE
#endif

	// Header Configuration
	//
	// Enable header checking and broadcast matching on all address bytes
	rf_set_register(REG_HEADER_CONTROL_1, (0xf<<BCEN | 0xf<<HDCH));

	// Set up for 4 byte header, variable packet length, 2 byte sync, 0 for MSB Preamble Length
	// 	(default is 0x22: HDLEN 010 Header 3 and 2; Sync Word 3 and 2)
	//
	rf_set_register(REG_HEADER_CONTROL_2, (4<<HDLEN | 0<<FIXPKLEN | 1<<SYNCLEN | 0<<PREALEN8));

	// Preamble Length
	// the default preamble length is 8 nibbles / 32 bits
	// the sample transmit / receive code provided by HopeRF uses 64 nibbles instead(!)
	// the table on page 43 recommends 40 bits (10 nibbles), so we use that here
	//
	// TODO: test with shorter preamble for performance
	//
	rf_set_register(REG_PREAMBLE_LENGTH, (40/4));

	// Preamble Detect Threshold
	//
	// DOCBUG: possible doc issue regarding preamble threshold:
	// 	rfget(0x35) is 0x20 at bootup; this is 4<<3, not 5<<3 per doc p.106
	//	(not to mention register 0x35 is missing from the summary table)
	// 	4 nibbles is 16 bits, but on page 42 it is recommended to use 20 bits
	// so we set PREATH to 20 bits here; that's 5 nibbles
	//
	rf_set_register(REG_PREAMBLE_DETECTION_CONTROL_1, (20/4)<<PREATH);

	// Sync Pattern and Length
	// we use the default sync pattern of 2dd4 with 2 byte check

	// Enable the radio so rf_set_rx_address is a pass-thru below
	radio_go = 1;		// mark the radio as UP

	// Receive address setup: listen on NOOB, and the broadcast address,
	// until setid() is called to provide an updated address.
	// If setid() was called before now, use the cached value.
	// (Tx address is set up per packet)
	//
	rf_set_rx_address((*nodeid) ? nodeid : (char *) DEFAULT_RX_ADDRESS);

	set_rx_mode();		// engage the receiver and off we go
}
Beispiel #7
0
byte rf_read_status(void) {
	rfstat1 = rf_read_register(REG_INTERRUPT_STATUS_1);
	rfstat2 = rf_read_register(REG_INTERRUPT_STATUS_2);
	return rfstat1;
}