From b55d02b1b9620b2bd736df88d9f987d61b52168d Mon Sep 17 00:00:00 2001 From: Harsh Sharma Date: Tue, 31 Jul 2018 15:08:46 -0500 Subject: Added function to setup fpga attenuation and tx lut variable --- libloragw/src/loragw_fpga.c | 15 ++++++++++++++- libloragw/src/loragw_hal.c | 25 +++++++++++++++++++++---- libloragw/src/loragw_reg.c | 4 ++-- 3 files changed, 37 insertions(+), 7 deletions(-) (limited to 'libloragw/src') diff --git a/libloragw/src/loragw_fpga.c b/libloragw/src/loragw_fpga.c index 832152b..c2cb305 100644 --- a/libloragw/src/loragw_fpga.c +++ b/libloragw/src/loragw_fpga.c @@ -130,7 +130,7 @@ float lgw_fpga_get_tx_notch_delay(void) { /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -int lgw_fpga_configure(uint32_t tx_notch_freq) { +int lgw_fpga_configure(uint32_t tx_notch_freq, bool fpga_attn_used) { int x; int32_t val; bool spectral_scan_support, lbt_support; @@ -175,6 +175,19 @@ int lgw_fpga_configure(uint32_t tx_notch_freq) { DEBUG_MSG("ERROR: Failed to configure FPGA polarity\n"); return LGW_REG_ERROR; } + /* Set Attenuator mode to be used or not depending on if a full card with valid conf settings are given*/ + if (fpga_version == 32) { + printf("INFO: Full Card with FPGA version %u detected\n", fpga_version); + if (fpga_attn_used == true) { + lgw_reg_w(LGW_FPGA_RF_ATTN_MODE, 0); + printf("INFO: Valid config detected. Using Attenuator mode\n"); + } else { + printf("INFO: Invalid config detected. Using Legacy mode\n"); + lgw_reg_w(LGW_FPGA_RF_ATTN_MODE, 1); + } + } else { + lgw_reg_w(LGW_FPGA_RF_ATTN_MODE, 1); + } } /* Configure TX notch filter */ diff --git a/libloragw/src/loragw_hal.c b/libloragw/src/loragw_hal.c index 8103751..3e6975b 100644 --- a/libloragw/src/loragw_hal.c +++ b/libloragw/src/loragw_hal.c @@ -136,13 +136,14 @@ static uint64_t fsk_sync_word= 0xC194C1; /* default FSK sync word (ALIGNED RIGHT static bool lorawan_public = false; static uint8_t rf_clkout = 0; -static struct lgw_tx_gain_lut_s txgain_lut = { +struct lgw_tx_gain_lut_s txgain_lut = { .size = 2, .lut[0] = { .dig_gain = 0, .pa_gain = 2, .dac_gain = 3, .mix_gain = 10, + .attenuation = -1.0, .rf_power = 14 }, .lut[1] = { @@ -150,6 +151,7 @@ static struct lgw_tx_gain_lut_s txgain_lut = { .pa_gain = 3, .dac_gain = 3, .mix_gain = 14, + .attenuation = -1.0, .rf_power = 27 }}; @@ -679,12 +681,16 @@ int lgw_txgain_setconf(struct lgw_tx_gain_lut_s *conf) { DEBUG_MSG("ERROR: TX gain LUT: External PA gain must not exceed 3\n"); return LGW_HAL_ERROR; } - + if (conf->lut[i].attenuation > 31.75) { + DEBUG_MSG("ERROR: TX gain LUT: External Attenuation gain must not exceed 31.75\n"); + return LGW_HAL_ERROR; + } /* Set internal LUT */ txgain_lut.lut[i].dig_gain = conf->lut[i].dig_gain; txgain_lut.lut[i].dac_gain = conf->lut[i].dac_gain; txgain_lut.lut[i].mix_gain = conf->lut[i].mix_gain; txgain_lut.lut[i].pa_gain = conf->lut[i].pa_gain; + txgain_lut.lut[i].attenuation = conf->lut[i].attenuation; txgain_lut.lut[i].rf_power = conf->lut[i].rf_power; } @@ -710,8 +716,8 @@ int lgw_start(void) { if (lgw_is_started == true) { DEBUG_MSG("Note: LoRa concentrator already started, restarting it now\n"); } - - reg_stat = lgw_connect(false, rf_tx_notch_freq[rf_tx_enable[1]?1:0]); + printf("Attenuation value %f\n", txgain_lut.lut[0].attenuation); + reg_stat = lgw_connect(false, rf_tx_notch_freq[rf_tx_enable[1]?1:0], txgain_lut.lut[0].attenuation > -1.0?true:false); if (reg_stat == LGW_REG_ERROR) { DEBUG_MSG("ERROR: FAIL TO CONNECT BOARD\n"); return LGW_HAL_ERROR; @@ -1433,6 +1439,17 @@ int lgw_send(struct lgw_pkt_tx_s pkt_data) { /* Set digital gain from LUT */ lgw_reg_w(LGW_TX_GAIN, txgain_lut.lut[pow_index].dig_gain); + int32_t read_value; + lgw_reg_r(LGW_FPGA_RF_ATTN_MODE, &read_value); + if (read_value == 0) { + lgw_reg_w(LGW_FPGA_RF_ATTN_VALUE, txgain_lut.lut[pow_index].attenuation * 4); + printf("dig_gain for sent packet %d\n", txgain_lut.lut[pow_index].dig_gain); + printf("mix_gain for sent packet %d\n", txgain_lut.lut[pow_index].mix_gain); + printf("pa_gain for sent packet %d\n", txgain_lut.lut[pow_index].pa_gain); + printf("Power for sent packet %d\n", txgain_lut.lut[pow_index].rf_power); + printf("Attenuation for sent packet %f\n", txgain_lut.lut[pow_index].attenuation); + } + /* fixed metadata, useful payload and misc metadata compositing */ transfer_size = TX_METADATA_NB + pkt_data.size; /* */ payload_offset = TX_METADATA_NB; /* start the payload just after the metadata */ diff --git a/libloragw/src/loragw_reg.c b/libloragw/src/loragw_reg.c index 4d689da..e040942 100644 --- a/libloragw/src/loragw_reg.c +++ b/libloragw/src/loragw_reg.c @@ -511,7 +511,7 @@ bool check_fpga_version(uint8_t version) { } /* Concentrator connect */ -int lgw_connect(bool spi_only, uint32_t tx_notch_freq) { +int lgw_connect(bool spi_only, uint32_t tx_notch_freq, bool fpga_attn_used) { int spi_stat = LGW_SPI_SUCCESS; uint8_t u = 0; int x; @@ -548,7 +548,7 @@ int lgw_connect(bool spi_only, uint32_t tx_notch_freq) { lgw_spi_w(lgw_spi_target, lgw_spi_mux_mode, LGW_SPI_MUX_TARGET_FPGA, 0, 1); lgw_spi_w(lgw_spi_target, lgw_spi_mux_mode, LGW_SPI_MUX_TARGET_FPGA, 0, 0); /* FPGA configure */ - x = lgw_fpga_configure(tx_notch_freq); + x = lgw_fpga_configure(tx_notch_freq, fpga_attn_used); if (x != LGW_REG_SUCCESS) { DEBUG_MSG("ERROR CONFIGURING FPGA\n"); return LGW_REG_ERROR; -- cgit v1.2.3