Skip to content

Commit

Permalink
add RSSI evaluation to packet handler, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
astuder committed Jan 23, 2014
1 parent 0e08276 commit af3e453
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 13 deletions.
14 changes: 11 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,20 @@ int main(void)
uart_send_string(str_output_buffer);
uart_send_string("dBm\r\n");
uart_send_string("error: ");
if (error == PH_ERROR_NOEND)
switch (error) {
case PH_ERROR_NOEND:
uart_send_string("no end flag");
else if (error == PH_ERROR_STUFFBIT)
break;
case PH_ERROR_STUFFBIT:
uart_send_string("invalid stuff bit");
else if (error == PH_ERROR_CRC)
break;
case PH_ERROR_CRC:
uart_send_string("CRC error");
break;
case PH_ERROR_RSSI_DROP:
uart_send_string("RSSI drop");
break;
}
uart_send_string("\r\n");
ph_loop(); // house keeping, sending over UART takes time
}
Expand Down
35 changes: 32 additions & 3 deletions packet_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
#define LED1_OFF P1OUT &= ~LED1
#define LED1_TOGGLE P1OUT ^= LED1

// sync word for AIS
// sync word for AIS - only used for test
#define AIS_SYNC_WORD 0x7e

// paramters for package detection
#define PH_PREAMBLE_LENGTH 8 // minimum number of alternating bits we need for a valid preamble
#define PH_SYNC_TIMEOUT 16 // number of bits we wait for a preamble to start before changing channel
#define PH_RSSI_THRESHOLD -95 // threshold in dBm for valid signal, comment out to ignore signal strength

// pins that packet handler uses to receive data
#define PH_DATA_CLK_PIN BIT2 // 2.2 RX data clock
Expand Down Expand Up @@ -79,8 +81,14 @@ void ph_setup(void)
// start packet handler operation, including ISR
void ph_start(void)
{
// start radio, wait until it's spun up

#ifndef TEST
#ifdef PH_RSSI_THRESHOLD
// set radio RSSI threshold
radio_set_property(0x20, 0x4a, RADIO_DBM_TO_RSSI(PH_RSSI_THRESHOLD));
#endif

// start radio, wait until it's spun up
radio_start_rx(ph_radio_channel, 0, 0, RADIO_STATE_NO_CHANGE, RADIO_STATE_NO_CHANGE, RADIO_STATE_NO_CHANGE);
radio_wait_for_CTS();
#endif
Expand Down Expand Up @@ -195,14 +203,22 @@ __interrupt void ph_irq_handler(void)

case PH_SYNC_FLAG: // sub-state: start flag detection
rx_sync_count--; // count down bits
#ifndef TEST
#ifdef PH_RSSI_THRESHOLD
if (!RADIO_SIGNAL) { // if we don't have a stable signal
ph_state = PH_STATE_RESET; // abort sync and reset state machine
break;
}
#endif
#endif
if (rx_sync_count != 0) { // if this is not the last bit
if (!rx_bit) // we expect a 1, 0 is an error
rx_sync_state = PH_SYNC_RESET; // restart preamble detection
} else { // if this is the last bit
if (!rx_bit) { // we expect a 0
#ifndef TEST
radio_frr_read('A', 1); // read fetched RSSI from FRR
ph_rssi = ((int) radio_buffer.data[0] >> 1) - 0x40 - 70; // calculate dBm: RSSI / 2 - RSSI_COMP - 70
ph_rssi = RADIO_RSSI_TO_DBM(radio_buffer.data[0]); // convert RSSI into dBm
#endif
rx_bit_count = 0; // reset bit counter
ph_state = PH_STATE_PREFETCH; // next state: start receiving packet
Expand All @@ -217,6 +233,13 @@ __interrupt void ph_irq_handler(void)

case PH_STATE_PREFETCH: // state: pre-fill receive buffer with 8 bits
rx_bit_count++; // increase bit counter

if (!RADIO_SIGNAL) { // if we don't have a stable signal
ph_last_error = PH_ERROR_RSSI_DROP; // report error
ph_state = PH_STATE_RESET; // abort package
break;
}

if (rx_bit_count == 8) { // after 8 bits arrived
rx_bit_count = 0; // reset bit counter
rx_one_count = 0; // reset counter for stuff bits
Expand All @@ -230,6 +253,12 @@ __interrupt void ph_irq_handler(void)
break; // do nothing for the first 8 bits to fill buffer

case PH_STATE_RECEIVE_PACKET: // state: receiving packet data
if (!RADIO_SIGNAL) { // if we don't have a stable signal
ph_last_error = PH_ERROR_RSSI_DROP; // report error
ph_state = PH_STATE_RESET; // abort package
break;
}

rx_bit = rx_bitstream & 0x80; // extract data bit for processing

if (rx_one_count == 5) { // if we expect a stuff-bit..
Expand Down
4 changes: 2 additions & 2 deletions packet_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ enum PH_STATE {
// packet handler errors
enum PH_ERROR {
PH_ERROR_NONE = 0,
PH_ERROR_NOSTART, // no start flag after preamble
PH_ERROR_STUFFBIT, // invalid stuff-bit
PH_ERROR_NOEND, // no end flag after more than 1020 bits, message too long
PH_ERROR_CRC // CRC error
PH_ERROR_CRC, // CRC error
PH_ERROR_RSSI_DROP // signal strength fell below threshold
};

uint8_t ph_get_state(void); // get current state of packet handler
Expand Down
24 changes: 21 additions & 3 deletions radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@

#define CMD_NOP 0x00
#define CMD_PART_INFO 0x01
#define CMD_POWER_UP 0x02
#define CMD_FUNC_INFO 0x10
#define CMD_POWER_UP 0x12
#define CMD_SET_PROPERTY 0x11
#define CMD_GET_PROPERTY 0x12
#define CMD_GPIO_PIN_CONFIG 0x13
#define CMD_GET_ADC_READING 0x14
#define CMD_FIFO_INFO 0x15
#define CMD_PACKET_INFO 0x16
#define CMD_IRCAL 0x17
#define CMD_PROTOCOL_CFG 0x18
#define CMD_GET_INT_STATUS 0x20
#define CMD_GET_PH_STATUS 0x21
#define CMD_GET_MODEM_STATUS 0x22
#define CMD_GET_CHIP_STATUS 0x23
#define CMD_START_RX 0x32
#define CMD_REQUEST_DEVICE_STATE 0x33
#define CMD_CHANGE_STATE 0x34
#define CMD_RX_HOP 0x36
#define CMD_READ_CMD_BUFF 0x44
#define CMD_FRR_A_READ 0x50
#define CMD_READ_RX_FIFO 0x77
Expand All @@ -44,8 +51,8 @@ const uint8_t radio_ircal_sequence_fine[] = { 0x13, 0x10, 0xCA, 0xF0 };

union radio_buffer_u radio_buffer;

void send_command(uint8_t cmd, const uint8_t *send_buffer, uint8_t send_length, uint8_t response_length);
int receive_result(uint8_t length);
static void send_command(uint8_t cmd, const uint8_t *send_buffer, uint8_t send_length, uint8_t response_length);
static int receive_result(uint8_t length);

// configure I/O pins used for radio
void radio_setup(void)
Expand Down Expand Up @@ -102,6 +109,17 @@ void radio_configure(void)
return;
}

// directly set individual radio property
void radio_set_property(uint8_t prop_group, uint8_t prop_num, uint8_t value)
{
radio_buffer.data[0] = prop_group;
radio_buffer.data[1] = 1;
radio_buffer.data[2] = prop_num;
radio_buffer.data[3] = value;
send_command(CMD_SET_PROPERTY, radio_buffer.data, 4, 0);
return;
}

// invoke radio image rejection self-calibration
void radio_calibrate_ir(void)
{
Expand Down
18 changes: 17 additions & 1 deletion radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define RADIO_GPIO_2 BIT2 // 2.2 configurable, e.g. RX data clock
#define RADIO_GPIO_3 BIT3 // 2.3 configurable, e.g. RX data
#define RADIO_SDN BIT4 // 2.4 chip shutdown, set high for 1us to reset radio, pulled low by 100k resistor
#define RADIO_NIRQ BIT5 // 2.5 configurable, e.g. preamble, high when detected (for debug only, use sync word for actual package detection)
#define RADIO_NIRQ BIT5 // 2.5 configurable, e.g. CCA, high when RSSI exceeding threshold

#define RADIO_CTS RADIO_GPIO_1 // when low, chip is busy/not ready
#ifndef TEST
Expand All @@ -23,6 +23,17 @@
#define RADIO_READY (1)
#endif

#define RADIO_CCA RADIO_NIRQ // when high, signal strength exceeds RSSI threshold
#ifndef TEST
#define RADIO_SIGNAL (P2IN & RADIO_CCA)
#else
#define RADIO_SIGNAL (1)
#endif

// convert RSSI to dBm: RSSI / 2 - RSSI_COMP - 70 and vice versa
#define RADIO_RSSI_TO_DBM(val) (((int) val >> 1) - 0x40 - 70)
#define RADIO_DBM_TO_RSSI(val) ((val + 70 + 0x40) << 1)

// functions to start up / reset chip
void radio_setup(void); // set up MSP430 pins and SPI for interfacing w/ radio
void radio_configure(void); // configure radio using radio_config_Si4362.h
Expand Down Expand Up @@ -75,6 +86,11 @@ void radio_frr_read( // read fast read registers, results in radio_buffer
uint8_t frr, // start register 'A', 'B', 'C' or 'D'
uint8_t count); // number of registers to read (1-4)

void radio_set_property( // directly set individual radio property
uint8_t prop_group, // property group, e.g. 0x20 for MODEM
uint8_t prop_num, // property number, e.g. 0x4a for RSSI threshold
uint8_t value); // property value, e.g. RADIO_DBM_TO_RSSI(-80)

// data structures of various responses, access via radio_buffer.* after calling respective radio_get_* function

struct part_info_s {
Expand Down
2 changes: 1 addition & 1 deletion radio_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
// MODEM_RSSI_CONTROL - Selects if the RSSI value is latched, and at what point in the packet it is latched. The Latched RSSI value may be read by a Fast Response Register, or returned by the GET_MODEM_STATUS command. Selects whether the RSSI value is updated every 1*Tb bit period, or whether the RSSI value is averaged over the previous 4*Tb bit periods. Selects if the Latched RSSI value is compared against the MODEM_RSSI_THRESH value, for the purpose of exiting to the RXTIMEOUT_STATE if below threshold.
*/
//#define RF_MODEM_OOK_CNT1_11 0x11, 0x20, 0x0B, 0x42, 0x84, 0x03, 0xD6, 0x83, 0x00, 0xA4, 0x01, 0x80, 0xFF, 0x0C, 0x02 // 02 = latch RSSI when sync word detected
#define RF_MODEM_OOK_CNT1_11 0x11, 0x20, 0x0B, 0x42, 0x84, 0x03, 0xD6, 0x83, 0x00, 0xA4, 0x01, 0x80, 0x58, 0x0C, 0x03 // 03 = latch RSSI 4*Tb, RSSI threshold at -100db
#define RF_MODEM_OOK_CNT1_11 0x11, 0x20, 0x0B, 0x42, 0x84, 0x03, 0xD6, 0x83, 0x00, 0xA4, 0x01, 0x80, 0x58, 0x0C, 0x03 // 03 = latch RSSI 4*Tb, RSSI threshold at -90dBm

/*
// Set properties: RF_MODEM_RSSI_COMP_1
Expand Down

1 comment on commit af3e453

@astuder
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closes #5

Please sign in to comment.