CC2520: Added offset to calculate correct RSSI value

This commit is contained in:
Sumankumar Panchal 2016-08-24 00:11:24 +05:30
parent 6947fc7381
commit 25dea7264a

View file

@ -55,12 +55,19 @@
#define FOOTER1_CRC_OK 0x80 #define FOOTER1_CRC_OK 0x80
#define FOOTER1_CORRELATION 0x7f #define FOOTER1_CORRELATION 0x7f
#ifdef CC2520_CONF_RSSI_OFFSET
#define RSSI_OFFSET CC2520_CONF_RSSI_OFFSET
#else /* CC2520_CONF_RSSI_OFFSET */
/* The RSSI_OFFSET is approximate 76 (see CC2520 specification) */
#define RSSI_OFFSET 76
#endif /* CC2520_CONF_RSSI_OFFSET */
#include <stdio.h> #include <stdio.h>
#define DEBUG 0 #define DEBUG 0
#if DEBUG #if DEBUG
#define PRINTF(...) printf(__VA_ARGS__) #define PRINTF(...) printf(__VA_ARGS__)
#else #else
#define PRINTF(...) do {} while (0) #define PRINTF(...) do {} while(0)
#endif #endif
#if 0 && DEBUG #if 0 && DEBUG
@ -97,7 +104,6 @@ static volatile uint16_t last_packet_timestamp;
PROCESS(cc2520_process, "CC2520 driver"); PROCESS(cc2520_process, "CC2520 driver");
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int cc2520_on(void); int cc2520_on(void);
int cc2520_off(void); int cc2520_off(void);
@ -141,7 +147,6 @@ get_value(radio_param_t param, radio_value_t *value)
return RADIO_RESULT_NOT_SUPPORTED; return RADIO_RESULT_NOT_SUPPORTED;
} }
} }
static radio_result_t static radio_result_t
set_value(radio_param_t param, radio_value_t value) set_value(radio_param_t param, radio_value_t value)
{ {
@ -166,21 +171,18 @@ set_value(radio_param_t param, radio_value_t value)
return RADIO_RESULT_NOT_SUPPORTED; return RADIO_RESULT_NOT_SUPPORTED;
} }
} }
static radio_result_t static radio_result_t
get_object(radio_param_t param, void *dest, size_t size) get_object(radio_param_t param, void *dest, size_t size)
{ {
return RADIO_RESULT_NOT_SUPPORTED; return RADIO_RESULT_NOT_SUPPORTED;
} }
static radio_result_t static radio_result_t
set_object(radio_param_t param, const void *src, size_t size) set_object(radio_param_t param, const void *src, size_t size)
{ {
return RADIO_RESULT_NOT_SUPPORTED; return RADIO_RESULT_NOT_SUPPORTED;
} }
const struct radio_driver cc2520_driver = const struct radio_driver cc2520_driver =
{ {
cc2520_init, cc2520_init,
cc2520_prepare, cc2520_prepare,
cc2520_transmit, cc2520_transmit,
@ -197,7 +199,7 @@ const struct radio_driver cc2520_driver =
set_value, set_value,
get_object, get_object,
set_object set_object
}; };
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -269,7 +271,9 @@ off(void)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define GET_LOCK() locked++ #define GET_LOCK() locked++
static void RELEASE_LOCK(void) { static void
RELEASE_LOCK(void)
{
if(locked == 1) { if(locked == 1) {
if(lock_on) { if(lock_on) {
on(); on();
@ -340,7 +344,7 @@ cc2520_init(void)
/* correlation threshold = 20, RX bandpass filter = 1.3uA.*/ /* correlation threshold = 20, RX bandpass filter = 1.3uA.*/
setreg(CC2520_TXCTRL, 0x94); setreg(CC2520_TXCTRL, 0x94);
setreg(CC2520_TXPOWER, 0x13); // Output power 1 dBm setreg(CC2520_TXPOWER, 0x13); /* Output power 1 dBm */
/* /*
@ -357,21 +361,21 @@ cc2520_init(void)
0xF2 -> 3 dBm 0xF2 -> 3 dBm
0xF7 -> 5 dBm 0xF7 -> 5 dBm
*/ */
setreg(CC2520_CCACTRL0, 0xF8); // CCA treshold -80dBm setreg(CC2520_CCACTRL0, 0xF8); /* CCA treshold -80dBm */
// Recommended RX settings /* Recommended RX settings */
setreg(CC2520_MDMCTRL0, 0x84); // Controls modem setreg(CC2520_MDMCTRL0, 0x84); /* Controls modem */
setreg(CC2520_MDMCTRL1, 0x14); // Controls modem setreg(CC2520_MDMCTRL1, 0x14); /* Controls modem */
setreg(CC2520_RXCTRL, 0x3F); // Adjust currents in RX related analog modules setreg(CC2520_RXCTRL, 0x3F); /* Adjust currents in RX related analog modules */
setreg(CC2520_FSCTRL, 0x5A); // Adjust currents in synthesizer. setreg(CC2520_FSCTRL, 0x5A); /* Adjust currents in synthesizer. */
setreg(CC2520_FSCAL1, 0x2B); // Adjust currents in VCO setreg(CC2520_FSCAL1, 0x2B); /* Adjust currents in VCO */
setreg(CC2520_AGCCTRL1, 0x11); // Adjust target value for AGC control loop setreg(CC2520_AGCCTRL1, 0x11); /* Adjust target value for AGC control loop */
setreg(CC2520_AGCCTRL2, 0xEB); setreg(CC2520_AGCCTRL2, 0xEB);
// Disable external clock /* Disable external clock */
setreg(CC2520_EXTCLOCK, 0x00); setreg(CC2520_EXTCLOCK, 0x00);
// Tune ADC performance /* Tune ADC performance */
setreg(CC2520_ADCTEST0, 0x10); setreg(CC2520_ADCTEST0, 0x10);
setreg(CC2520_ADCTEST1, 0x0E); setreg(CC2520_ADCTEST1, 0x0E);
setreg(CC2520_ADCTEST2, 0x03); setreg(CC2520_ADCTEST2, 0x03);
@ -379,7 +383,7 @@ cc2520_init(void)
/* Set auto CRC on frame. */ /* Set auto CRC on frame. */
#if CC2520_CONF_AUTOACK #if CC2520_CONF_AUTOACK
setreg(CC2520_FRMCTRL0, AUTOCRC | AUTOACK); setreg(CC2520_FRMCTRL0, AUTOCRC | AUTOACK);
setreg(CC2520_FRMFILT0, FRAME_MAX_VERSION|FRAME_FILTER_ENABLE); setreg(CC2520_FRMFILT0, FRAME_MAX_VERSION | FRAME_FILTER_ENABLE);
#else #else
/* setreg(CC2520_FRMCTRL0, 0x60); */ /* setreg(CC2520_FRMCTRL0, 0x60); */
setreg(CC2520_FRMCTRL0, AUTOCRC); setreg(CC2520_FRMCTRL0, AUTOCRC);
@ -430,7 +434,7 @@ cc2520_transmit(unsigned short payload_len)
#if WITH_SEND_CCA #if WITH_SEND_CCA
strobe(CC2520_INS_SRXON); strobe(CC2520_INS_SRXON);
BUSYWAIT_UNTIL(status() & BV(CC2520_RSSI_VALID) , RTIMER_SECOND / 10); BUSYWAIT_UNTIL(status() & BV(CC2520_RSSI_VALID), RTIMER_SECOND / 10);
strobe(CC2520_INS_STXONCCA); strobe(CC2520_INS_STXONCCA);
#else /* WITH_SEND_CCA */ #else /* WITH_SEND_CCA */
strobe(CC2520_INS_STXON); strobe(CC2520_INS_STXON);
@ -462,11 +466,11 @@ cc2520_transmit(unsigned short payload_len)
ENERGEST_ON(ENERGEST_TYPE_TRANSMIT); ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
/* We wait until transmission has ended so that we get an /* We wait until transmission has ended so that we get an
accurate measurement of the transmission time.*/ accurate measurement of the transmission time.*/
//BUSYWAIT_UNTIL(getreg(CC2520_EXCFLAG0) & TX_FRM_DONE , RTIMER_SECOND / 100); /* BUSYWAIT_UNTIL(getreg(CC2520_EXCFLAG0) & TX_FRM_DONE , RTIMER_SECOND / 100); */
BUSYWAIT_UNTIL(!(status() & BV(CC2520_TX_ACTIVE)), RTIMER_SECOND / 10); BUSYWAIT_UNTIL(!(status() & BV(CC2520_TX_ACTIVE)), RTIMER_SECOND / 10);
#ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS #ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS
ENERGEST_OFF_LEVEL(ENERGEST_TYPE_TRANSMIT,cc2520_get_txpower()); ENERGEST_OFF_LEVEL(ENERGEST_TYPE_TRANSMIT, cc2520_get_txpower());
#endif #endif
ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT); ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
if(receive_on) { if(receive_on) {
@ -641,15 +645,14 @@ cc2520_set_pan_addr(unsigned pan,
tmp[1] = pan >> 8; tmp[1] = pan >> 8;
CC2520_WRITE_RAM(&tmp, CC2520RAM_PANID, 2); CC2520_WRITE_RAM(&tmp, CC2520RAM_PANID, 2);
tmp[0] = addr & 0xff; tmp[0] = addr & 0xff;
tmp[1] = addr >> 8; tmp[1] = addr >> 8;
CC2520_WRITE_RAM(&tmp, CC2520RAM_SHORTADDR, 2); CC2520_WRITE_RAM(&tmp, CC2520RAM_SHORTADDR, 2);
if(ieee_addr != NULL) { if(ieee_addr != NULL) {
int f; int f;
uint8_t tmp_addr[8]; uint8_t tmp_addr[8];
// LSB first, MSB last for 802.15.4 addresses in CC2520 /* LSB first, MSB last for 802.15.4 addresses in CC2520 */
for (f = 0; f < 8; f++) { for(f = 0; f < 8; f++) {
tmp_addr[7 - f] = ieee_addr[f]; tmp_addr[7 - f] = ieee_addr[f];
} }
CC2520_WRITE_RAM(tmp_addr, CC2520RAM_IEEEADDR, 8); CC2520_WRITE_RAM(tmp_addr, CC2520RAM_IEEEADDR, 8);
@ -737,15 +740,13 @@ cc2520_read(void *buf, unsigned short bufsize)
getrxdata(footer, FOOTER_LEN); getrxdata(footer, FOOTER_LEN);
if(footer[1] & FOOTER1_CRC_OK) { if(footer[1] & FOOTER1_CRC_OK) {
cc2520_last_rssi = footer[0]; cc2520_last_rssi = footer[0] - RSSI_OFFSET;
cc2520_last_correlation = footer[1] & FOOTER1_CORRELATION; cc2520_last_correlation = footer[1] & FOOTER1_CORRELATION;
packetbuf_set_attr(PACKETBUF_ATTR_RSSI, cc2520_last_rssi); packetbuf_set_attr(PACKETBUF_ATTR_RSSI, cc2520_last_rssi);
packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, cc2520_last_correlation); packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, cc2520_last_correlation);
RIMESTATS_ADD(llrx); RIMESTATS_ADD(llrx);
} else { } else {
RIMESTATS_ADD(badcrc); RIMESTATS_ADD(badcrc);
len = FOOTER_LEN; len = FOOTER_LEN;
@ -809,6 +810,7 @@ cc2520_rssi(void)
BUSYWAIT_UNTIL(status() & BV(CC2520_RSSI_VALID), RTIMER_SECOND / 100); BUSYWAIT_UNTIL(status() & BV(CC2520_RSSI_VALID), RTIMER_SECOND / 100);
rssi = (int)((signed char)getreg(CC2520_RSSI)); rssi = (int)((signed char)getreg(CC2520_RSSI));
rssi -= RSSI_OFFSET;
if(radio_was_off) { if(radio_was_off) {
cc2520_off(); cc2520_off();
@ -818,12 +820,12 @@ cc2520_rssi(void)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* /*
static int static int
detected_energy(void) detected_energy(void)
{ {
return cc2520_rssi(); return cc2520_rssi();
} }
*/ */
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int int
cc2520_cca_valid(void) cc2520_cca_valid(void)