* HAL: Fixed scheduled downlink time precision by taking the tx start delay into
account.
* HAL: Fixed timestamp correction calculation for BW250 & BW500
* HAL: Fixed possible buffer overflow in lgw_receive() function
* HAL: Keep packet received in RX buffer when the buffer allocated to receive
the packets is too small. Remaining packets will be fetched on the next
lgw_receive calls (aligned on SX1301 behaviour).
This commit is contained in:
Michael Coracin 2019-09-20 11:06:37 +02:00
commit 5942224602
10 changed files with 217 additions and 122 deletions

View file

@ -31,6 +31,7 @@ License: Revised BSD License, see LICENSE.TXT file include in the project
#include <time.h>
#include <unistd.h> /* symlink, unlink */
#include <fcntl.h>
#include <inttypes.h>
#include "loragw_reg.h"
#include "loragw_hal.h"
@ -451,7 +452,7 @@ int lgw_rxif_setconf(uint8_t if_chain, struct lgw_conf_rxif_s * conf) {
CONTEXT_FSK.sync_word_size = conf->sync_word_size;
CONTEXT_FSK.sync_word = conf->sync_word;
}
DEBUG_PRINTF("Note: FSK if_chain %d configuration; en:%d freq:%d bw:%d dr:%d (%d real dr) sync:0x%0*llX\n", if_chain,
DEBUG_PRINTF("Note: FSK if_chain %d configuration; en:%d freq:%d bw:%d dr:%d (%d real dr) sync:0x%0*" PRIu64 "\n", if_chain,
CONTEXT_IF_CHAIN[if_chain].enable,
CONTEXT_IF_CHAIN[if_chain].freq_hz,
CONTEXT_FSK.bandwidth,
@ -772,9 +773,9 @@ int lgw_stop(void) {
int lgw_receive(uint8_t max_pkt, struct lgw_pkt_rx_s *pkt_data) {
int res;
uint16_t sz = 0;
uint8_t nb_pkt_fetched = 0;
uint16_t nb_pkt_found = 0;
uint16_t nb_pkt_dropped = 0;
uint16_t nb_pkt_left = 0;
float current_temperature, rssi_temperature_offset;
/* Check that AGC/ARB firmwares are not corrupted, and update internal counter */
@ -785,48 +786,46 @@ int lgw_receive(uint8_t max_pkt, struct lgw_pkt_rx_s *pkt_data) {
}
/* Get packets from SX1302, if any */
res = sx1302_fetch(&sz);
res = sx1302_fetch(&nb_pkt_fetched);
if (res != LGW_REG_SUCCESS) {
printf("ERROR: failed to fetch packets from SX1302\n");
return LGW_HAL_ERROR;
}
if (sz == 0) {
if (nb_pkt_fetched == 0) {
return 0;
}
if (nb_pkt_fetched > max_pkt) {
nb_pkt_left = nb_pkt_fetched - max_pkt;
printf("WARNING: not enough space allocated, fetched %d packet(s), %d will be left in RX buffer\n", nb_pkt_fetched, nb_pkt_left);
}
/* Get the current temperature for further RSSI compensation : TODO */
/* Apply RSSI temperature compensation */
res = stts751_get_temperature(ts_fd, ts_addr, &current_temperature);
if (res != LGW_I2C_SUCCESS) {
printf("ERROR: failed to get current temperature\n");
return LGW_HAL_ERROR;
}
DEBUG_PRINTF("INFO: current temperature is %f C\n", current_temperature);
/* Iterate on the RX buffer to get parsed packets */
res = LGW_REG_SUCCESS;
while ((res == LGW_REG_SUCCESS) && (nb_pkt_found <= max_pkt)) {
for (nb_pkt_found = 0; nb_pkt_found < ((nb_pkt_fetched <= max_pkt) ? nb_pkt_fetched : max_pkt); nb_pkt_found++) {
/* Get packet and move to next one */
res = sx1302_parse(&lgw_context, &pkt_data[nb_pkt_found]);
if (res == LGW_REG_SUCCESS) {
/* we found a packet and parsed it */
if ((nb_pkt_found + 1) > max_pkt) {
printf("WARNING: no space left, dropping packet\n");
nb_pkt_dropped += 1;
continue;
}
/* Appli RSSI offset calibrated for the board */
pkt_data[nb_pkt_found].rssic += CONTEXT_RF_CHAIN[pkt_data[nb_pkt_found].rf_chain].rssi_offset;
pkt_data[nb_pkt_found].rssis += CONTEXT_RF_CHAIN[pkt_data[nb_pkt_found].rf_chain].rssi_offset;
/* Apply RSSI temperature compensation */
rssi_temperature_offset = sx1302_rssi_get_temperature_offset(&CONTEXT_RF_CHAIN[pkt_data[nb_pkt_found].rf_chain].rssi_tcomp, current_temperature);
pkt_data[nb_pkt_found].rssic += rssi_temperature_offset;
pkt_data[nb_pkt_found].rssis += rssi_temperature_offset;
DEBUG_PRINTF("INFO: RSSI temperature offset applied: %.3f dB\n", rssi_temperature_offset);
/* Next packet */
nb_pkt_found += 1;
if (res != LGW_REG_SUCCESS) {
printf("ERROR: failed to parse fetched packet %d, aborting...\n", nb_pkt_found);
return LGW_HAL_ERROR;
}
/* Appli RSSI offset calibrated for the board */
pkt_data[nb_pkt_found].rssic += CONTEXT_RF_CHAIN[pkt_data[nb_pkt_found].rf_chain].rssi_offset;
pkt_data[nb_pkt_found].rssis += CONTEXT_RF_CHAIN[pkt_data[nb_pkt_found].rf_chain].rssi_offset;
rssi_temperature_offset = sx1302_rssi_get_temperature_offset(&CONTEXT_RF_CHAIN[pkt_data[nb_pkt_found].rf_chain].rssi_tcomp, current_temperature);
pkt_data[nb_pkt_found].rssic += rssi_temperature_offset;
pkt_data[nb_pkt_found].rssis += rssi_temperature_offset;
DEBUG_PRINTF("INFO: RSSI temperature offset applied: %.3f dB (current temperature %.1f C)\n", rssi_temperature_offset, current_temperature);
}
DEBUG_PRINTF("INFO: nb pkt found:%u dropped:%u\n", nb_pkt_found, nb_pkt_dropped);
DEBUG_PRINTF("INFO: nb pkt found:%u left:%u\n", nb_pkt_found, nb_pkt_left);
return nb_pkt_found;
}

View file

@ -210,6 +210,9 @@ void sx1302_init(struct lgw_conf_timestamp_s *conf_ts) {
if (conf_ts != NULL) {
timestamp_counter_mode(conf_ts->enable_precision_ts, conf_ts->max_ts_metrics, conf_ts->nb_symbols);
}
/* Initialize RX buffer */
rx_buffer_new(&rx_buffer);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
@ -1570,25 +1573,30 @@ int sx1302_arb_start(uint8_t version) {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int sx1302_fetch(uint16_t * nb_bytes) {
int sx1302_fetch(uint8_t * nb_pkt) {
int err;
/* Initialize RX buffer */
err = rx_buffer_new(&rx_buffer);
if (err != LGW_REG_SUCCESS) {
printf("ERROR: Failed to initialize RX buffer\n");
return LGW_REG_ERROR;
/* Fetch packets from sx1302 if no more left in RX buffer */
if (rx_buffer.buffer_pkt_nb == 0) {
/* Initialize RX buffer */
err = rx_buffer_new(&rx_buffer);
if (err != LGW_REG_SUCCESS) {
printf("ERROR: Failed to initialize RX buffer\n");
return LGW_REG_ERROR;
}
/* Fetch RX buffer if any data available */
err = rx_buffer_fetch(&rx_buffer);
if (err != LGW_REG_SUCCESS) {
printf("ERROR: Failed to fetch RX buffer\n");
return LGW_REG_ERROR;
}
} else {
printf("Note: remaining %u packets in RX buffer, do not fetch sx1302 yet...\n", rx_buffer.buffer_pkt_nb);
}
/* Fetch RX buffer if any data available */
err = rx_buffer_fetch(&rx_buffer);
if (err != LGW_REG_SUCCESS) {
printf("ERROR: Failed to fetch RX buffer\n");
return LGW_REG_ERROR;
}
/* Return the number of bytes fetched */
*nb_bytes = rx_buffer.buffer_size;
/* Return the number of packet fetched */
*nb_pkt = rx_buffer.buffer_pkt_nb;
return LGW_REG_SUCCESS;
}
@ -1833,7 +1841,7 @@ uint16_t sx1302_lora_payload_crc(const uint8_t * data, uint8_t size) {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int sx1302_tx_set_start_delay(uint8_t rf_chain, lgw_radio_type_t radio_type, uint8_t modulation, uint8_t bandwidth) {
int sx1302_tx_set_start_delay(uint8_t rf_chain, lgw_radio_type_t radio_type, uint8_t modulation, uint8_t bandwidth, uint16_t * delay) {
uint16_t tx_start_delay = TX_START_DELAY_DEFAULT * 32;
uint16_t radio_bw_delay = 0;
uint16_t filter_delay = 0;
@ -1842,6 +1850,8 @@ int sx1302_tx_set_start_delay(uint8_t rf_chain, lgw_radio_type_t radio_type, uin
int32_t val;
uint8_t chirp_low_pass = 0;
CHECK_NULL(delay);
/* Adjust with radio type and bandwidth */
switch (radio_type) {
case LGW_RADIO_TYPE_SX1250:
@ -1896,6 +1906,9 @@ int sx1302_tx_set_start_delay(uint8_t rf_chain, lgw_radio_type_t radio_type, uin
lgw_reg_w(SX1302_REG_TX_TOP_TX_START_DELAY_MSB_TX_START_DELAY(rf_chain), (uint8_t)(tx_start_delay >> 8));
lgw_reg_w(SX1302_REG_TX_TOP_TX_START_DELAY_LSB_TX_START_DELAY(rf_chain), (uint8_t)(tx_start_delay >> 0));
/* return tx_start_delay */
*delay = tx_start_delay;
return LGW_REG_SUCCESS;
}
@ -2013,6 +2026,7 @@ int sx1302_send(lgw_radio_type_t radio_type, struct lgw_tx_gain_lut_s * tx_lut,
uint8_t pow_index;
uint8_t mod_bw;
uint8_t pa_en;
uint16_t tx_start_delay;
/* CHeck input parameters */
CHECK_NULL(tx_lut);
@ -2244,7 +2258,7 @@ int sx1302_send(lgw_radio_type_t radio_type, struct lgw_tx_gain_lut_s * tx_lut,
}
/* Set TX start delay */
sx1302_tx_set_start_delay(pkt_data->rf_chain, radio_type, pkt_data->modulation, pkt_data->bandwidth);
sx1302_tx_set_start_delay(pkt_data->rf_chain, radio_type, pkt_data->modulation, pkt_data->bandwidth, &tx_start_delay);
/* Write payload in transmit buffer */
lgw_reg_w(SX1302_REG_TX_TOP_TX_CTRL_WRITE_BUFFER(pkt_data->rf_chain), 0x01);
@ -2265,8 +2279,8 @@ int sx1302_send(lgw_radio_type_t radio_type, struct lgw_tx_gain_lut_s * tx_lut,
lgw_reg_w(SX1302_REG_TX_TOP_TX_TRIG_TX_TRIG_IMMEDIATE(pkt_data->rf_chain), 0x01);
break;
case TIMESTAMPED:
count_us = pkt_data->count_us * 32;
DEBUG_PRINTF("--> programming trig delay at %u (%u)\n", pkt_data->count_us, count_us);
count_us = pkt_data->count_us * 32 - tx_start_delay;
DEBUG_PRINTF("--> programming trig delay at %u (%u)\n", pkt_data->count_us - (tx_start_delay / 32), count_us);
lgw_reg_w(SX1302_REG_TX_TOP_TIMER_TRIG_BYTE0_TIMER_DELAYED_TRIG(pkt_data->rf_chain), (uint8_t)((count_us >> 0) & 0x000000FF));
lgw_reg_w(SX1302_REG_TX_TOP_TIMER_TRIG_BYTE1_TIMER_DELAYED_TRIG(pkt_data->rf_chain), (uint8_t)((count_us >> 8) & 0x000000FF));

View file

@ -108,6 +108,7 @@ int rx_buffer_new(rx_buffer_t * self) {
memset(self->buffer, 0, sizeof self->buffer);
self->buffer_size = 0;
self->buffer_index = 0;
self->buffer_pkt_nb = 0;
return LGW_REG_SUCCESS;
}
@ -121,6 +122,7 @@ int rx_buffer_del(rx_buffer_t * self) {
/* Reset index & size */
self->buffer_size = 0;
self->buffer_index = 0;
self->buffer_pkt_nb = 0;
return LGW_REG_SUCCESS;
}
@ -160,6 +162,29 @@ int rx_buffer_fetch(rx_buffer_t * self) {
}
/* Parse buffer to get number of packet fetched */
uint8_t payload_len;
uint16_t next_pkt_idx;
int idx = 0;
while (idx < self->buffer_size) {
if ((self->buffer[idx] != SX1302_PKT_SYNCWORD_BYTE_0) || (self->buffer[idx + 1] != SX1302_PKT_SYNCWORD_BYTE_1)) {
printf("ERROR: syncword not found in rx_buffer\n");
return LGW_REG_ERROR;
}
/* One packet found in the buffer */
self->buffer_pkt_nb += 1;
/* Compute the number of bytes for thsi packet */
payload_len = SX1302_PKT_PAYLOAD_LENGTH(self->buffer, idx);
next_pkt_idx = SX1302_PKT_HEAD_METADATA +
payload_len +
SX1302_PKT_TAIL_METADATA +
(2 * SX1302_PKT_NUM_TS_METRICS(self->buffer, idx + payload_len));
/* Move to next packet */
idx += (int)next_pkt_idx;
}
/* Initialize the current buffer index to iterate on */
self->buffer_index = 0;
@ -295,9 +320,12 @@ int rx_buffer_pop(rx_buffer_t * self, rx_packet_t * pkt) {
/* Parse & copy payload in packet struct */
memcpy((void *)pkt->payload, (void *)(&(self->buffer[self->buffer_index + SX1302_PKT_HEAD_METADATA])), pkt->rxbytenb_modem);
/* move buffer index toward next message */
/* Move buffer index toward next message */
self->buffer_index += (SX1302_PKT_HEAD_METADATA + pkt->rxbytenb_modem + SX1302_PKT_TAIL_METADATA + (2 * pkt->num_ts_metrics_stored));
/* Update the umber of packets currently stored in the rx_buffer */
self->buffer_pkt_nb -= 1;
return LGW_REG_SUCCESS;
}

View file

@ -191,42 +191,31 @@ int timestamp_counter_mode(bool enable_precision_ts, uint8_t max_ts_metrics, uin
uint32_t timestamp_counter_correction(int ifmod, uint8_t bandwidth, uint8_t datarate, uint8_t coderate, uint32_t crc_en, uint16_t payload_length) {
int32_t val;
uint32_t sf = (uint32_t)datarate, cr = (uint32_t)coderate, bw_pow, ppm;
uint32_t sf = (uint32_t)datarate, cr = (uint32_t)coderate, bw_pow;
uint32_t clk_period;
uint32_t nb_nibble, nb_nibble_in_hdr, nb_nibble_in_last_block;
uint32_t dft_peak_en, nb_iter;
uint32_t demap_delay, decode_delay, fft_delay_state3, fft_delay, delay_x;
uint32_t timestamp_correction;
uint32_t ppm = SET_PPM_ON(bandwidth, datarate) ? 1 : 0;
/* determine if 'PPM mode' is on */
if (SET_PPM_ON(bandwidth, datarate)) {
ppm = 1;
} else {
ppm = 0;
}
/* timestamp correction code, base delay */
switch (bandwidth)
{
case BW_125KHZ:
bw_pow = 1;
delay_x = 16000000 / bw_pow + 2031250;
break;
case BW_250KHZ:
bw_pow = 2;
delay_x = 16000000 / bw_pow + 2031250;
break;
case BW_500KHZ:
bw_pow = 4;
delay_x = 16000000 / bw_pow + 2031250;
break;
default:
DEBUG_PRINTF("ERROR: UNEXPECTED VALUE %d IN SWITCH STATEMENT\n", bandwidth);
delay_x = 0;
bw_pow = 0;
break;
return 0;
}
clk_period = 250000;
clk_period = 250000 / bw_pow;
delay_x = 16000000 / bw_pow + 2031250;
nb_nibble = (payload_length + 2 * crc_en) * 2 + 5;