2009-09-08 20:07:35 +00:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* CC2430 RF driver
|
|
|
|
* \author
|
|
|
|
* Zach Shelby <zach@sensinode.com>
|
2010-01-25 23:12:09 +00:00
|
|
|
*
|
|
|
|
* bankable code for cc2430 rf driver. this code can be placed in any bank.
|
|
|
|
*
|
2009-09-08 20:07:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "dev/radio.h"
|
|
|
|
#include "dev/cc2430_rf.h"
|
|
|
|
#include "cc2430_sfr.h"
|
|
|
|
#include "sys/clock.h"
|
|
|
|
|
2010-06-14 19:19:16 +00:00
|
|
|
#include "net/packetbuf.h"
|
2009-09-08 20:07:35 +00:00
|
|
|
#include "net/rime/rimestats.h"
|
|
|
|
|
2010-01-29 19:15:44 +00:00
|
|
|
extern void (* receiver_callback)(const struct radio_driver *);
|
2009-09-08 20:07:35 +00:00
|
|
|
#ifndef RF_DEFAULT_POWER
|
|
|
|
#define RF_DEFAULT_POWER 100
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef RF_DEFAULT_CHANNEL
|
|
|
|
#define RF_DEFAULT_CHANNEL 18
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CC2430_CONF_CHECKSUM
|
|
|
|
#define CC2430_CONF_CHECKSUM 0
|
|
|
|
#endif /* CC2420_CONF_CHECKSUM */
|
|
|
|
|
|
|
|
#if CC2430_CONF_CHECKSUM
|
|
|
|
#include "lib/crc16.h"
|
|
|
|
#define CHECKSUM_LEN 2
|
|
|
|
#else
|
2009-10-15 12:43:53 +00:00
|
|
|
#define CHECKSUM_LEN 2
|
2009-09-08 20:07:35 +00:00
|
|
|
#endif /* CC2430_CONF_CHECKSUM */
|
2010-01-29 19:15:44 +00:00
|
|
|
#if DEBUG_LEDS
|
|
|
|
/* moved leds code to BANK1 to make space for cc2430_rf_process in HOME */
|
|
|
|
/* can't call code in BANK1 from alternate banks unless it is marked with __banked */
|
|
|
|
#include "dev/leds.h"
|
2009-09-08 20:07:35 +00:00
|
|
|
#define RF_RX_LED_ON() leds_on(LEDS_RED);
|
|
|
|
#define RF_RX_LED_OFF() leds_off(LEDS_RED);
|
|
|
|
#define RF_TX_LED_ON() leds_on(LEDS_GREEN);
|
|
|
|
#define RF_TX_LED_OFF() leds_off(LEDS_GREEN);
|
2010-01-29 19:15:44 +00:00
|
|
|
#else
|
|
|
|
#define RF_RX_LED_ON()
|
|
|
|
#define RF_RX_LED_OFF()
|
|
|
|
#define RF_TX_LED_ON()
|
|
|
|
#define RF_TX_LED_OFF()
|
|
|
|
#endif
|
2010-04-10 19:59:37 +00:00
|
|
|
#define DEBUG 0
|
2009-09-08 20:07:35 +00:00
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define RX_ACTIVE 0x80
|
|
|
|
#define TX_ACK 0x40
|
|
|
|
#define TX_ON_AIR 0x20
|
|
|
|
#define RX_NO_DMA
|
|
|
|
|
|
|
|
#ifdef HAVE_RF_ERROR
|
|
|
|
uint8_t rf_error = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint8_t rf_initialized = 0;
|
|
|
|
uint8_t rf_tx_power;
|
|
|
|
uint8_t rf_flags;
|
|
|
|
uint8_t rf_channel = 0;
|
|
|
|
rf_address_mode_t rf_addr_mode;
|
|
|
|
uint16_t rf_manfid;
|
|
|
|
uint8_t rf_softack;
|
2009-09-11 08:07:12 +00:00
|
|
|
uint16_t rf_panid;
|
2009-09-08 20:07:35 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-01-29 19:15:44 +00:00
|
|
|
PROCESS_NAME(cc2430_rf_process);
|
2009-09-08 20:07:35 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
const struct radio_driver cc2430_rf_driver =
|
|
|
|
{
|
|
|
|
cc2430_rf_send,
|
|
|
|
cc2430_rf_read,
|
|
|
|
cc2430_rf_set_receiver,
|
|
|
|
cc2430_rf_on,
|
|
|
|
cc2430_rf_off,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-01-25 23:12:09 +00:00
|
|
|
cc2430_rf_init(void) __banked
|
2009-09-08 20:07:35 +00:00
|
|
|
{
|
|
|
|
if(rf_initialized) {
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("cc2430_rf_init called\n");
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
RFPWR &= ~RREG_RADIO_PD; /*make sure it's powered*/
|
|
|
|
while((RFPWR & ADI_RADIO_PD) == 1);
|
|
|
|
while((RFIF & IRQ_RREG_ON) == 0); /*wait for power up*/
|
|
|
|
SLEEP &= ~OSC_PD; /*Osc on*/
|
|
|
|
while((SLEEP & XOSC_STB) == 0); /*wait for power up*/
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
rf_flags = 0;
|
|
|
|
rf_softack = 0;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
FSMTC1 = 1; /*don't abort reception, if enable called, accept ack, auto rx after tx*/
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
MDMCTRL0H = 0x02; /* Generic client, standard hysteresis, decoder on 0x0a */
|
|
|
|
MDMCTRL0L = 0xE2; /* automatic ACK and CRC, standard CCA and preamble 0xf2 */
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
MDMCTRL1H = 0x30; /* Defaults */
|
|
|
|
MDMCTRL1L = 0x0;
|
|
|
|
|
|
|
|
RXCTRL0H = 0x32; /* RX tuning optimized */
|
|
|
|
RXCTRL0L = 0xf5;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
/* get ID for MAC */
|
|
|
|
rf_manfid = CHVER;
|
|
|
|
rf_manfid <<= 8;
|
|
|
|
rf_manfid += CHIPID;
|
|
|
|
cc2430_rf_channel_set(RF_DEFAULT_CHANNEL);
|
|
|
|
cc2430_rf_command(ISFLUSHTX);
|
|
|
|
cc2430_rf_command(ISFLUSHRX);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-11 08:07:12 +00:00
|
|
|
cc2430_rf_set_addr(0xffff, 0x0000, NULL);
|
|
|
|
cc2430_rf_address_decoder_mode(RF_DECODER_NONE);
|
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
RFIM = IRQ_FIFOP;
|
|
|
|
RFIF &= ~(IRQ_FIFOP);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
S1CON &= ~(RFIF_0 | RFIF_1);
|
|
|
|
IEN2 |= RFIE;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
RF_TX_LED_OFF();
|
|
|
|
RF_RX_LED_OFF();
|
|
|
|
rf_initialized = 1;
|
|
|
|
process_start(&cc2430_rf_process, NULL);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2010-01-25 23:12:09 +00:00
|
|
|
cc2430_rf_send_b(void *payload, unsigned short payload_len) __banked
|
2009-09-08 20:07:35 +00:00
|
|
|
{
|
|
|
|
uint8_t i, counter;
|
2010-01-25 23:12:09 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
if(rf_flags & TX_ACK) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if((rf_flags & RX_ACTIVE) == 0) {
|
|
|
|
cc2430_rf_rx_enable();
|
|
|
|
}
|
|
|
|
/* Check packet attributes */
|
|
|
|
/*printf("packetbuf_attr: txpower = %d\n", packetbuf_attr(PACKETBUF_ATTR_RADIO_TXPOWER));*/
|
|
|
|
/* Should set TX power according to this if > 0 */
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2010-01-25 23:12:09 +00:00
|
|
|
PRINTF("cc2430_rf: sending %ud byte payload\n", payload_len);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
RIMESTATS_ADD(lltx);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISFLUSHTX);
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("cc2430_rf: sent = ");
|
2009-10-15 12:43:53 +00:00
|
|
|
/* Send the phy length byte first */
|
|
|
|
RFD = payload_len+CHECKSUM_LEN; /* Payload plus FCS */
|
|
|
|
PRINTF("(%d)", payload_len+CHECKSUM_LEN);
|
2009-09-08 20:07:35 +00:00
|
|
|
for(i = 0 ; i < payload_len; i++) {
|
|
|
|
RFD = ((unsigned char*)(payload))[i];
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("%02X", ((unsigned char*)(payload))[i]);
|
2009-09-08 20:07:35 +00:00
|
|
|
}
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("\n");
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-10-15 12:43:53 +00:00
|
|
|
/* Leave space for the FCS */
|
|
|
|
RFD = 0;
|
|
|
|
RFD = 0;
|
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
if(cc2430_rf_cca_check(0,0) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
/* Start the transmission */
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
RFIF &= ~IRQ_TXDONE;
|
|
|
|
cc2430_rf_command(ISTXON);
|
|
|
|
counter = 0;
|
|
|
|
while(!(RFSTATUS & TX_ACTIVE) && (counter++ < 3)) {
|
|
|
|
clock_delay(10);
|
|
|
|
}
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
if(!(RFSTATUS & TX_ACTIVE)) {
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("cc2430_rf: TX never active.\n");
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISFLUSHTX);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
RF_RX_LED_OFF();
|
|
|
|
RF_TX_LED_ON();
|
|
|
|
// rf_flags |= TX_ON_AIR;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2010-01-25 23:12:09 +00:00
|
|
|
cc2430_rf_read_banked(void *buf, unsigned short bufsize) __banked
|
2009-09-08 20:07:35 +00:00
|
|
|
{
|
2009-10-15 12:43:53 +00:00
|
|
|
uint8_t i, len;
|
2009-09-08 20:07:35 +00:00
|
|
|
#if CC2420_CONF_CHECKSUM
|
|
|
|
uint16_t checksum;
|
|
|
|
#endif /* CC2420_CONF_CHECKSUM */
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
/* RX interrupt polled the cc2430_rf_process, now read the RX FIFO */
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
/* Check the length */
|
|
|
|
len = RFD;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
/* Check for validity */
|
|
|
|
if(len > CC2430_MAX_PACKET_LEN) {
|
|
|
|
/* Oops, we must be out of sync. */
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("error: bad sync\n");
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISFLUSHRX);
|
|
|
|
RIMESTATS_ADD(badsynch);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
if(len <= CC2430_MIN_PACKET_LEN) {
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("error: too short\n");
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISFLUSHRX);
|
|
|
|
RIMESTATS_ADD(tooshort);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
if(len - CHECKSUM_LEN > bufsize) {
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("error: too long\n");
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISFLUSHRX);
|
|
|
|
RIMESTATS_ADD(toolong);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-11 08:07:12 +00:00
|
|
|
/* Read the buffer */
|
|
|
|
PRINTF("cc2430_rf: read = ");
|
2009-10-15 12:43:53 +00:00
|
|
|
PRINTF("(%d)", len);
|
|
|
|
for(i = 0; i < (len - CHECKSUM_LEN); i++) {
|
2009-09-08 20:07:35 +00:00
|
|
|
((unsigned char*)(buf))[i] = RFD;
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("%02X", ((unsigned char*)(buf))[i]);
|
|
|
|
}
|
|
|
|
PRINTF("\n");
|
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
#if CC2430_CONF_CHECKSUM
|
|
|
|
/* Deal with the checksum */
|
2009-10-15 12:43:53 +00:00
|
|
|
checksum = RFD * 256;
|
|
|
|
checksum += RFD;
|
2009-09-08 20:07:35 +00:00
|
|
|
#endif /* CC2430_CONF_CHECKSUM */
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_RSSI, ((int8_t) RFD) - 45);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, RFD);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-11 08:07:12 +00:00
|
|
|
RFIF &= ~IRQ_FIFOP;
|
|
|
|
RFSTATUS &= ~FIFO;
|
|
|
|
cc2430_rf_command(ISFLUSHRX);
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISFLUSHRX);
|
2009-09-11 08:07:12 +00:00
|
|
|
RF_RX_LED_OFF();
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
RIMESTATS_ADD(llrx);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-11 08:07:12 +00:00
|
|
|
return (len - CHECKSUM_LEN);
|
2009-09-08 20:07:35 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Execute a single CSP command.
|
|
|
|
*
|
|
|
|
* \param command command to execute
|
|
|
|
*
|
|
|
|
*/
|
2010-01-25 23:12:09 +00:00
|
|
|
void cc2430_rf_command(uint8_t command) __banked
|
2009-09-08 20:07:35 +00:00
|
|
|
{
|
|
|
|
if(command >= 0xE0) { /*immediate strobe*/
|
|
|
|
uint8_t fifo_count;
|
|
|
|
switch(command) { /*hardware bug workaround*/
|
|
|
|
case ISRFOFF:
|
|
|
|
case ISRXON:
|
|
|
|
case ISTXON:
|
|
|
|
fifo_count = RXFIFOCNT;
|
|
|
|
RFST = command;
|
|
|
|
clock_delay(2);
|
|
|
|
if(fifo_count != RXFIFOCNT) {
|
|
|
|
RFST = ISFLUSHRX;
|
|
|
|
RFST = ISFLUSHRX;
|
|
|
|
}
|
|
|
|
break;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
default:
|
|
|
|
RFST = command;
|
|
|
|
}
|
|
|
|
} else if(command == SSTART) {
|
|
|
|
RFIF &= ~IRQ_CSP_STOP; /*clear IRQ flag*/
|
|
|
|
RFST = SSTOP; /*make sure there is a stop in the end*/
|
|
|
|
RFST = ISSTART; /*start execution*/
|
|
|
|
while((RFIF & IRQ_CSP_STOP) == 0);
|
|
|
|
} else {
|
|
|
|
RFST = command; /*write command*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-01-25 23:12:09 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
/**
|
|
|
|
* Select RF channel.
|
|
|
|
*
|
|
|
|
* \param channel channel number to select
|
|
|
|
*
|
|
|
|
* \return channel value or negative (invalid channel number)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* channel freqdiv = (2048 + FSCTRL(9:0)) / 4
|
|
|
|
freq = (2048 + FSCTRL(9:0)) MHz */
|
|
|
|
|
|
|
|
int8_t
|
|
|
|
cc2430_rf_channel_set(uint8_t channel)
|
|
|
|
{
|
|
|
|
uint16_t freq;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
if((channel < 11) || (channel > 26)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISSTOP); /*make sure CSP is not running*/
|
|
|
|
cc2430_rf_command(ISRFOFF);
|
|
|
|
/* Channel values: 11-26 */
|
|
|
|
freq = (uint16_t) channel - 11;
|
|
|
|
freq *= 5; /*channel spacing*/
|
|
|
|
freq += 357; /*correct channel range*/
|
|
|
|
freq |= 0x4000; /*LOCK_THR = 1*/
|
|
|
|
FSCTRLH = (freq >> 8);
|
|
|
|
FSCTRLL = (uint8_t)freq;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISRXON);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
rf_channel = channel;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
return (int8_t) channel;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*PA_LEVEL TXCTRL register Output Power [dBm] Current Consumption [mA]
|
|
|
|
31 0xA0FF 0 17.4
|
|
|
|
27 0xA0FB -1 16.5
|
|
|
|
23 0xA0F7 -3 15.2
|
|
|
|
19 0xA0F3 -5 13.9
|
|
|
|
15 0xA0EF -7 12.5
|
|
|
|
11 0xA0EB -10 11.2
|
|
|
|
7 0xA0E7 -15 9.9
|
|
|
|
3 0xA0E3 -25 8.5*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select RF transmit power.
|
|
|
|
*
|
|
|
|
* \param new_power new power level (in per cent)
|
|
|
|
*
|
|
|
|
* \return new level or negative (value out of range)
|
|
|
|
*/
|
|
|
|
|
|
|
|
int8_t
|
|
|
|
cc2430_rf_power_set(uint8_t new_power)
|
|
|
|
{
|
|
|
|
uint16_t power;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
if(new_power > 100) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
power = 31 * new_power;
|
|
|
|
power /= 100;
|
|
|
|
power += 0xA160;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
/* Set transmitter power */
|
|
|
|
TXCTRLH = (power >> 8);
|
|
|
|
TXCTRLL = (uint8_t)power;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
rf_tx_power = (int8_t) new_power;
|
|
|
|
return rf_tx_power;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Enable RF receiver.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* \return pdTRUE
|
|
|
|
* \return pdFALSE bus not free
|
|
|
|
*/
|
|
|
|
int8_t
|
2010-01-25 23:12:09 +00:00
|
|
|
cc2430_rf_rx_enable(void) __banked
|
2009-09-08 20:07:35 +00:00
|
|
|
{
|
2009-09-11 08:07:12 +00:00
|
|
|
PRINTF("cc2430_rf_rx_enable called\n");
|
2009-09-08 20:07:35 +00:00
|
|
|
if(!(rf_flags & RX_ACTIVE)) {
|
|
|
|
IOCFG0 = 0x7f; // Set the FIFOP threshold 127
|
|
|
|
RSSIH = 0xd2; /* -84dbm = 0xd2 default, 0xe0 -70 dbm */
|
|
|
|
rf_flags |= RX_ACTIVE;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
RFPWR &= ~RREG_RADIO_PD; /*make sure it's powered*/
|
|
|
|
while((RFIF & IRQ_RREG_ON) == 0); /*wait for power up*/
|
|
|
|
SLEEP &= ~OSC_PD; /*Osc on*/
|
|
|
|
while((SLEEP & XOSC_STB) == 0); /*wait for power up*/
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
cc2430_rf_command(ISRXON);
|
|
|
|
cc2430_rf_command(ISFLUSHRX);
|
|
|
|
}
|
2010-01-25 23:12:09 +00:00
|
|
|
PRINTF("cc2430_rf_rx_enable done\n");
|
2009-09-08 20:07:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Disable RF receiver.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* \return pdTRUE
|
|
|
|
* \return pdFALSE bus not free
|
|
|
|
*/
|
2010-01-25 23:12:09 +00:00
|
|
|
int8_t cc2430_rf_rx_disable(void) __banked
|
2009-09-08 20:07:35 +00:00
|
|
|
{
|
|
|
|
cc2430_rf_command(ISSTOP); /*make sure CSP is not running*/
|
|
|
|
cc2430_rf_command(ISRFOFF);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
RFPWR |= RREG_RADIO_PD; /*RF powerdown*/
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
rf_flags = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Enable RF transmitter.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* \return pdTRUE
|
|
|
|
* \return pdFALSE bus not free
|
|
|
|
*/
|
|
|
|
int8_t
|
|
|
|
cc2430_rf_tx_enable(void)
|
|
|
|
{
|
|
|
|
DMAARM = 0x80 + (1 << 0); /*ABORT + channel bit*/
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-09-11 08:07:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set MAC addresses
|
|
|
|
*
|
|
|
|
* \param pan The PAN address to set
|
|
|
|
* \param adde The short address to set
|
|
|
|
* \param ieee_addr The 64-bit IEEE address to set
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cc2430_rf_set_addr(unsigned pan, unsigned addr, const uint8_t *ieee_addr)
|
|
|
|
{
|
|
|
|
uint8_t f;
|
|
|
|
__xdata unsigned char *ptr;
|
|
|
|
|
|
|
|
rf_panid = pan;
|
|
|
|
PANIDH = pan >> 8;
|
|
|
|
PANIDL = pan & 0xff;
|
|
|
|
|
|
|
|
SHORTADDRH = addr >> 8;
|
|
|
|
SHORTADDRL = addr & 0xff;
|
|
|
|
|
|
|
|
if(ieee_addr != NULL) {
|
|
|
|
ptr = &IEEE_ADDR0;
|
|
|
|
/* LSB first, MSB last for 802.15.4 addresses in CC2420 */
|
|
|
|
for (f = 0; f < 8; f++) {
|
|
|
|
*ptr++ = ieee_addr[f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Set address decoder on/off.
|
|
|
|
*
|
|
|
|
* \param param 1=on 0=off.
|
|
|
|
* \return pdTRUE operation successful
|
|
|
|
*/
|
|
|
|
int8_t
|
|
|
|
cc2430_rf_address_decoder_mode(rf_address_mode_t mode)
|
|
|
|
{
|
|
|
|
int8_t retval = -1;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
rf_softack = 0;
|
|
|
|
/* set oscillator on*/
|
|
|
|
switch(mode) {
|
|
|
|
case RF_SOFTACK_MONITOR:
|
|
|
|
rf_softack = 1;
|
|
|
|
case RF_MONITOR:
|
|
|
|
MDMCTRL0H |= 0x10; /*Address-decode off , coordinator*/
|
|
|
|
MDMCTRL0L &= ~0x10; /*no automatic ACK */
|
|
|
|
break;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
case RF_DECODER_COORDINATOR:
|
|
|
|
MDMCTRL0H |= 0x18; /*Address-decode on , coordinator*/
|
|
|
|
MDMCTRL0L |= 0x10; /*automatic ACK */
|
|
|
|
break;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
case RF_DECODER_ON:
|
|
|
|
MDMCTRL0H |= 0x08; /*Address-decode on */
|
|
|
|
MDMCTRL0L &= ~0x10; /* no automatic ACK */
|
|
|
|
break;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
default:
|
|
|
|
MDMCTRL0H &= ~0x18; /* Generic client */
|
|
|
|
MDMCTRL0L &= ~0x10; /* no automatic ACK */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rf_addr_mode = mode;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
retval = 1;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Channel energy detect.
|
|
|
|
*
|
|
|
|
* Coordinator use this function detect best channel for PAN-network.
|
|
|
|
* \return RSSI-energy level dBm.
|
|
|
|
* \return 0 operation failed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int8_t
|
|
|
|
cc2430_rf_analyze_rssi(void)
|
|
|
|
{
|
|
|
|
int8_t retval = -128;
|
|
|
|
/*pause_us(128);*/
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
retval = (int8_t)RSSIL;
|
|
|
|
retval -= 45;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Clear channel assesment check.
|
|
|
|
*
|
|
|
|
* \return pdTRUE CCA clear
|
|
|
|
* \return pdFALSE CCA reserved
|
|
|
|
*/
|
|
|
|
int8_t
|
|
|
|
cc2430_rf_cca_check(uint8_t backoff_count, uint8_t slotted)
|
|
|
|
{
|
|
|
|
uint8_t counter, cca = 1;
|
|
|
|
int8_t retval = 1;
|
|
|
|
backoff_count;
|
|
|
|
cc2430_rf_command(ISRXON);
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
clock_delay(64);
|
|
|
|
switch(slotted) {
|
|
|
|
case 1:
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
if(RFSTATUS & CCA) {
|
|
|
|
counter = 0;
|
|
|
|
cca = 1;
|
|
|
|
while(cca != 0) {
|
|
|
|
if(counter > 1) {
|
|
|
|
cca = 0;
|
|
|
|
}
|
|
|
|
clock_delay(256);
|
|
|
|
if(!(RFSTATUS & CCA)) {
|
|
|
|
cca = 0;
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
break;
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
case 0:
|
|
|
|
if(!(RFSTATUS & CCA)) {
|
|
|
|
retval = -1;
|
|
|
|
} else {
|
2009-09-09 07:32:02 +00:00
|
|
|
|
2009-09-08 20:07:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Send ACK.
|
|
|
|
*
|
|
|
|
*\param pending set up pending flag if pending > 0.
|
|
|
|
*/
|
|
|
|
void
|
2010-01-25 23:12:09 +00:00
|
|
|
cc2430_rf_send_ack(uint8_t pending) __banked
|
2009-09-08 20:07:35 +00:00
|
|
|
{
|
|
|
|
if(pending) {
|
|
|
|
cc2430_rf_command(ISACKPEND);
|
|
|
|
} else {
|
|
|
|
cc2430_rf_command(ISACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|