summaryrefslogtreecommitdiff
path: root/libloragw
diff options
context:
space:
mode:
authorJason Reiss <jreiss@multitech.com>2020-03-20 09:11:25 -0500
committerJason Reiss <jreiss@multitech.com>2020-03-20 09:11:25 -0500
commitea028e8048d55367a52365087f9e3928746c55a5 (patch)
treed3c28fb745071653bcf35bff7f05c93051796390 /libloragw
parentb3a4461b3dfc5588111a88fd735d058888b8d3d4 (diff)
parentdea57d6f29246434173c33c56850708c51fc5b23 (diff)
downloadlora_gateway_mtac_full-ea028e8048d55367a52365087f9e3928746c55a5.tar.gz
lora_gateway_mtac_full-ea028e8048d55367a52365087f9e3928746c55a5.tar.bz2
lora_gateway_mtac_full-ea028e8048d55367a52365087f9e3928746c55a5.zip
Merge branch 'master' of gitlab.multitech.net:lora_enterprise/lora_gateway_mtac_full5.0.1-mts-5
Diffstat (limited to 'libloragw')
-rw-r--r--libloragw/Makefile6
-rw-r--r--libloragw/inc/loragw_fpga.h3
-rw-r--r--libloragw/inc/loragw_hal.h10
-rw-r--r--libloragw/inc/loragw_reg.h11
-rw-r--r--libloragw/src/loragw_fpga.c29
-rw-r--r--libloragw/src/loragw_gps.c3
-rw-r--r--libloragw/src/loragw_hal.c39
-rw-r--r--libloragw/src/loragw_reg.c38
-rw-r--r--libloragw/src/loragw_spi.native.c2
-rw-r--r--libloragw/tst/test_loragw_cal.c44
-rw-r--r--libloragw/tst/test_loragw_gps.c4
-rw-r--r--libloragw/tst/test_loragw_hal.c31
-rw-r--r--libloragw/tst/test_loragw_reg.c59
-rw-r--r--libloragw/tst/test_loragw_spi.c119
14 files changed, 305 insertions, 93 deletions
diff --git a/libloragw/Makefile b/libloragw/Makefile
index 4575bdc..7e95eca 100644
--- a/libloragw/Makefile
+++ b/libloragw/Makefile
@@ -1,6 +1,6 @@
### get external defined data
-LIBLORAGW_VERSION := `cat ../VERSION`
+LIBLORAGW_VERSION := $(shell git describe)
include library.cfg
### constant symbols
@@ -10,7 +10,7 @@ CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc
AR := $(CROSS_COMPILE)ar
-CFLAGS := -O2 -Wall -Wextra -std=c99 -Iinc -I. -isystem =/usr/include/gps
+CFLAGS := -O2 -Wall -Wextra -std=c99 -Iinc -I. -isystem =/usr/include/gps -DLIBLORAGW_VERSION=\"$(LIBLORAGW_VERSION)\"
OBJDIR = obj
INCLUDES = $(wildcard inc/*.h)
@@ -31,7 +31,7 @@ clean:
### transpose library.cfg into a C header file : config.h
-inc/config.h: ../VERSION library.cfg
+inc/config.h: library.cfg
@echo "*** Checking libloragw library configuration ***"
@rm -f $@
#File initialization
diff --git a/libloragw/inc/loragw_fpga.h b/libloragw/inc/loragw_fpga.h
index 46246b6..722438c 100644
--- a/libloragw/inc/loragw_fpga.h
+++ b/libloragw/inc/loragw_fpga.h
@@ -135,7 +135,6 @@ int lgw_fpga_reg_wb(uint16_t register_id, uint8_t *data, uint16_t size);
*/
int lgw_fpga_reg_rb(uint16_t register_id, uint8_t *data, uint16_t size);
-int lgw_set_attenuation(float attenuation);
-
+int lgw_set_attenuation(uint8_t *attn);
#endif
/* --- EOF ------------------------------------------------------------------ */
diff --git a/libloragw/inc/loragw_hal.h b/libloragw/inc/loragw_hal.h
index e1e4644..f6dc8dc 100644
--- a/libloragw/inc/loragw_hal.h
+++ b/libloragw/inc/loragw_hal.h
@@ -177,6 +177,7 @@ enum lgw_radio_type_e {
struct lgw_conf_board_s {
bool lorawan_public; /*!> Enable ONLY for *public* networks using the LoRa MAC protocol */
uint8_t clksrc; /*!> Index of RF chain which provides clock to concentrator */
+ uint8_t max_tx_power; /*!> Max power limit for transmitting packets */
};
/**
@@ -259,7 +260,7 @@ struct lgw_pkt_tx_s {
uint8_t tx_mode; /*!> select on what event/time the TX is triggered */
uint32_t count_us; /*!> timestamp or delay in microseconds for TX trigger */
uint8_t rf_chain; /*!> through which RF chain will the packet be sent */
- int8_t rf_power; /*!> TX power, in dBm */
+ float rf_power; /*!> TX power, in dBm */
uint8_t modulation; /*!> modulation to use for the packet */
uint8_t bandwidth; /*!> modulation bandwidth (LoRa only) */
uint32_t datarate; /*!> TX datarate (baudrate for FSK, SF for LoRa) */
@@ -416,13 +417,6 @@ const char* lgw_version_info(void);
*/
uint32_t lgw_time_on_air(struct lgw_pkt_tx_s *packet);
-/**
-@brief Set the attenuation for sending packets to get the exact power level
-@param attenuation is in dB, it can be between 0-31.75
-@return LGW_HAL_ERROR if the operation failed, LGW_HAL_SUCCESS else
-*/
-int lgw_set_attenuation(float attenuation);
-
#endif
/* --- EOF ------------------------------------------------------------------ */
diff --git a/libloragw/inc/loragw_reg.h b/libloragw/inc/loragw_reg.h
index 1e98efa..4dd9edb 100644
--- a/libloragw/inc/loragw_reg.h
+++ b/libloragw/inc/loragw_reg.h
@@ -403,14 +403,17 @@ uint8_t read_fpga_version();
@param version number provided to check through the validated list
@return status true/false
*/
-bool check_fpga_version(uint8_t version);
+bool fpga_version_supported();
+
+/**
+@brief Check if the LoRa FPGA uses an attenuator for transmitting packets
+@return status true/false
+*/
+bool fpga_supports_attenuator();
/**
@brief Connect LoRa concentrator by opening SPI link
-@param spi_only indicates if we only want to create the SPI connexion to the
concentrator, or if we also want to reset it and configure the FPGA (if present)
-@param tx_notch_filter TX notch filter frequency to be set in the FPGA (only
-used with SX1301AP2 reference design).
@return status of register operation (LGW_REG_SUCCESS/LGW_REG_ERROR)
*/
int lgw_connect(bool spi_only, uint32_t tx_notch_freq);
diff --git a/libloragw/src/loragw_fpga.c b/libloragw/src/loragw_fpga.c
index ce1c9c6..63c6d3b 100644
--- a/libloragw/src/loragw_fpga.c
+++ b/libloragw/src/loragw_fpga.c
@@ -175,9 +175,14 @@ 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 for the full card*/
- if (fpga_version == 32) {
- lgw_fpga_reg_w(LGW_FPGA_RF_ATTN_MODE, 0);
+ }
+
+ /* Set Attenuator mode to 1 to allow it to use it*/
+ if (fpga_supports_attenuator()) {
+ x = lgw_fpga_reg_w(LGW_FPGA_RF_ATTN_MODE, 1);
+ if (x != LGW_REG_SUCCESS) {
+ DEBUG_MSG("ERROR: Failed to configure Attenuator mode\n");
+ return LGW_REG_ERROR;
}
}
@@ -361,17 +366,21 @@ int lgw_fpga_reg_rb(uint16_t register_id, uint8_t *data, uint16_t size) {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-int lgw_set_attenuation(float attenuation) {
- int i;
- int32_t val;
- if (attenuation > 31.75 || attenuation < 0) {
+int lgw_set_attenuation(uint8_t *attn) {
+ if (*attn > 127) {
return LGW_HAL_ERROR;
}
- i = lgw_fpga_reg_w(LGW_FPGA_RF_ATTN_VALUE, (uint8_t)(attenuation * LGW_RF_ATTN_CONV_CONST));
+ int i;
+ /* The max value allowed for 7 bits of a register */
+ uint8_t max_attn = 127;
+
+ /* Shifting the float value to apply get the adjusted integer */
+ uint8_t shifted_attn = (uint8_t)(*attn * LGW_RF_ATTN_CONV_CONST);
- i = lgw_fpga_reg_r(LGW_FPGA_RF_ATTN_VALUE, &val);
+ /* The attenuator value is bit 0-6 of the register hence must be limited to 127 */
+ uint8_t reg_attn = max_attn ^ ((shifted_attn ^ max_attn) & -(shifted_attn < max_attn));
+ i = lgw_fpga_reg_w(LGW_FPGA_RF_ATTN_VALUE, reg_attn);
if (i == LGW_REG_SUCCESS) {
- DEBUG_PRINTF("INFO: Attenuator set to %u \n", (uint8_t)val);
return LGW_HAL_SUCCESS;
} else {
return LGW_HAL_ERROR;
diff --git a/libloragw/src/loragw_gps.c b/libloragw/src/loragw_gps.c
index 3aad031..19c2f83 100644
--- a/libloragw/src/loragw_gps.c
+++ b/libloragw/src/loragw_gps.c
@@ -89,8 +89,6 @@ static bool gps_lock_ok = false;
static char gps_mod = 'N'; /* GPS mode (N no fix, A autonomous, D differential) */
static short gps_sat = 0; /* number of satellites used for fix */
-static struct termios ttyopt_restore;
-
/* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS DECLARATION ---------------------------------------- */
@@ -255,7 +253,6 @@ int str_chop(char *s, int buff_size, char separator, int *idx_ary, int max_idx)
int lgw_gps_enable(struct gps_data_t *gpsdata, struct fixsource_t *source) {
unsigned int flags;
- fd_set fds;
flags = WATCH_ENABLE;
flags |= WATCH_RAW;
flags |= WATCH_NMEA;
diff --git a/libloragw/src/loragw_hal.c b/libloragw/src/loragw_hal.c
index d77c693..c723be9 100644
--- a/libloragw/src/loragw_hal.c
+++ b/libloragw/src/loragw_hal.c
@@ -135,6 +135,7 @@ 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 uint8_t max_tx_power = 32; /* default uncalibrated max tx power */
static struct lgw_tx_gain_lut_s txgain_lut = {
.size = 2,
@@ -423,8 +424,12 @@ int lgw_board_setconf(struct lgw_conf_board_s conf) {
/* set internal config according to parameters */
lorawan_public = conf.lorawan_public;
rf_clkout = conf.clksrc;
-
- DEBUG_PRINTF("Note: board configuration; lorawan_public:%d, clksrc:%d\n", lorawan_public, rf_clkout);
+ if (fpga_supports_attenuator()) {
+ max_tx_power = conf.max_tx_power;
+ DEBUG_PRINTF("Note: board configuration; lorawan_public:%d, clksrc:%d, max_tx_power:%d\n", lorawan_public, rf_clkout, max_tx_power);
+ } else {
+ DEBUG_PRINTF("Note: board configuration; lorawan_public:%d, clksrc:%d \n", lorawan_public, rf_clkout);
+ }
return LGW_HAL_SUCCESS;
}
@@ -1414,27 +1419,30 @@ int lgw_send(struct lgw_pkt_tx_s pkt_data) {
/* interpretation of TX power */
- if (read_fpga_version() == 32) {
- for (pow_index = 0; pow_index < txgain_lut.size-1; pow_index++) {
- if (txgain_lut.lut[pow_index].rf_power >= pkt_data.rf_power) {
- x = lgw_set_attenuation((float)(txgain_lut.lut[pow_index].rf_power - pkt_data.rf_power));
- if (x != LGW_HAL_SUCCESS) {
- DEBUG_MSG("ERROR: Failed to set attenuation value\n");
- return LGW_HAL_ERROR;
- }
- break;
- }
+ if (fpga_supports_attenuator()) {
+ /* Power is set to max and the attenuator brings down the level to match the packet's request */
+ target_mix_gain = 15; /* Mixer gain is not used for setting power*/
+ lgw_reg_w(LGW_TX_GAIN, 0); /* Dig gain is not used for setting power */
+ uint8_t attn = (uint8_t)(max_tx_power - pkt_data.rf_power);
+ x = lgw_set_attenuation(&attn);
+ if (x != LGW_HAL_SUCCESS) {
+ DEBUG_MSG("ERROR: Failed to set attenuation value\n");
+ return LGW_HAL_ERROR;
}
} else {
+ /* Power is matched from the txgain_lut */
for (pow_index = txgain_lut.size-1; pow_index > 0; pow_index--) {
if (txgain_lut.lut[pow_index].rf_power <= pkt_data.rf_power) {
break;
}
}
+
+ /* loading TX imbalance correction */
+ target_mix_gain = txgain_lut.lut[pow_index].mix_gain;
+ /* Set digital gain from LUT */
+ lgw_reg_w(LGW_TX_GAIN, txgain_lut.lut[pow_index].dig_gain);
}
- /* loading TX imbalance correction */
- target_mix_gain = txgain_lut.lut[pow_index].mix_gain;
if (pkt_data.rf_chain == 0) { /* use radio A calibration table */
lgw_reg_w(LGW_TX_OFFSET_I, cal_offset_a_i[target_mix_gain - 8]);
lgw_reg_w(LGW_TX_OFFSET_Q, cal_offset_a_q[target_mix_gain - 8]);
@@ -1443,9 +1451,6 @@ int lgw_send(struct lgw_pkt_tx_s pkt_data) {
lgw_reg_w(LGW_TX_OFFSET_Q, cal_offset_b_q[target_mix_gain - 8]);
}
- /* Set digital gain from LUT */
- lgw_reg_w(LGW_TX_GAIN, txgain_lut.lut[pow_index].dig_gain);
-
/* 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 e707838..5991384 100644
--- a/libloragw/src/loragw_reg.c
+++ b/libloragw/src/loragw_reg.c
@@ -48,7 +48,8 @@ Maintainer: Sylvain Miermont
#define PAGE_ADDR 0x00
#define PAGE_MASK 0x03
-const uint8_t FPGA_VERSION[] = { 28, 31, 32, 33 , 34, 35, 37 }; /* several versions could be supported */
+const uint8_t FPGA_VERSIONS_SUPPORTED[] = { 28, 31, 32, 33, 34, 35, 37 }; /* several versions could be supported */
+const uint8_t FPGA_VERSIONS_SUPPORTING_ATTENUATOR[] = { 32, 34, 35, 37 }; /* defines lgw_send power interpretation */
/*
auto generated register mapping for C code : 11-Jul-2013 13:20:40
@@ -488,21 +489,34 @@ int reg_r_align32(void *spi_target, uint8_t spi_mux_mode, uint8_t spi_mux_target
/* Read the FPGA version */
uint8_t read_fpga_version() {
- uint8_t u = 0;
- uint8_t spi_stat = lgw_spi_r(lgw_spi_target, LGW_SPI_MUX_MODE1, LGW_SPI_MUX_TARGET_FPGA, loregs[LGW_VERSION].addr, &u);
- if (spi_stat != LGW_SPI_SUCCESS) {
- DEBUG_MSG("ERROR READING VERSION REGISTER\n");
- return LGW_REG_ERROR;
- }
- return u;
+ uint8_t u = 0;
+ uint8_t spi_stat = lgw_spi_r(lgw_spi_target, LGW_SPI_MUX_MODE1, LGW_SPI_MUX_TARGET_FPGA, loregs[LGW_VERSION].addr, &u);
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ DEBUG_MSG("ERROR READING VERSION REGISTER\n");
+ return LGW_REG_ERROR;
+ }
+ return u;
}
/* Verify the FPGA version is supported */
-bool check_fpga_version(uint8_t version) {
+bool fpga_version_supported() {
+ int i;
+
+ for (i = 0; i < (int)(sizeof FPGA_VERSIONS_SUPPORTED); i++) {
+ if (FPGA_VERSIONS_SUPPORTED[i] == read_fpga_version() ) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/* Check if the FPGA uses an attenuator for transmitting packets*/
+bool fpga_supports_attenuator() {
int i;
- for (i = 0; i < (int)(sizeof FPGA_VERSION); i++) {
- if (FPGA_VERSION[i] == version ) {
+ for (i = 0; i < (int)(sizeof FPGA_VERSIONS_SUPPORTING_ATTENUATOR); i++) {
+ if (FPGA_VERSIONS_SUPPORTING_ATTENUATOR[i] == read_fpga_version() ) {
return true;
}
}
@@ -537,7 +551,7 @@ int lgw_connect(bool spi_only, uint32_t tx_notch_freq) {
DEBUG_MSG("ERROR READING VERSION REGISTER\n");
return LGW_REG_ERROR;
}
- if (check_fpga_version(u) != true) {
+ if (fpga_version_supported(u) != true) {
/* We failed to read expected FPGA version, so let's assume there is no FPGA */
DEBUG_PRINTF("INFO: no FPGA detected or version not supported (v%u)\n", u);
lgw_spi_mux_mode = LGW_SPI_MUX_MODE0;
diff --git a/libloragw/src/loragw_spi.native.c b/libloragw/src/loragw_spi.native.c
index 2380ecb..a26fc22 100644
--- a/libloragw/src/loragw_spi.native.c
+++ b/libloragw/src/loragw_spi.native.c
@@ -73,7 +73,7 @@ int lgw_spi_set_path(const char *path) {
}
}
-
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* SPI initialization and configuration */
int lgw_spi_open(void **spi_target_ptr) {
diff --git a/libloragw/tst/test_loragw_cal.c b/libloragw/tst/test_loragw_cal.c
index 533d189..60c5ce7 100644
--- a/libloragw/tst/test_loragw_cal.c
+++ b/libloragw/tst/test_loragw_cal.c
@@ -31,12 +31,13 @@ Maintainer: Sylvain Miermont
#include <signal.h> /* sigaction */
#include <math.h> /* cos */
#include <unistd.h> /* getopt access */
+#include <getopt.h> /* getopt_long */
#include "loragw_hal.h"
#include "loragw_reg.h"
#include "loragw_aux.h"
#include "loragw_radio.h"
-
+#include "loragw_spi.h"
/* -------------------------------------------------------------------------- */
/* --- PRIVATE MACROS ------------------------------------------------------- */
@@ -99,13 +100,14 @@ void usage (void);
void usage(void) {
printf("Library version information: %s\n", lgw_version_info());
printf( "Available options:\n");
- printf( " -h print this help\n");
- printf( " -a <float> Radio A frequency in MHz\n");
- printf( " -b <float> Radio B frequency in MHz\n");
- printf( " -r <int> Radio type (SX1255:1255, SX1257:1257)\n");
- printf( " -n <uint> Number of calibration iterations\n");
- printf( " -k <int> Concentrator clock source (0:radio_A, 1:radio_B(default))\n");
- printf( " -t <int> Radio to run TX calibration on (0:None(default), 1:radio_A, 2:radio_B, 3:both)\n");
+ printf( " -h print this help\n");
+ printf( " -a <float> Radio A frequency in MHz\n");
+ printf( " -b <float> Radio B frequency in MHz\n");
+ printf( " -r <int> Radio type (SX1255:1255, SX1257:1257)\n");
+ printf( " -n <uint> Number of calibration iterations\n");
+ printf( " -k <int> Concentrator clock source (0:radio_A, 1:radio_B(default))\n");
+ printf( " -t <int> Radio to run TX calibration on (0:None(default), 1:radio_A, 2:radio_B, 3:both)\n");
+ printf(" --path <string> path of SPIDEV e.g. /dev/spidev0.0\n");
}
/* -------------------------------------------------------------------------- */
@@ -142,8 +144,16 @@ int main(int argc, char **argv)
uint8_t tx_enable = 0;
int nb_cal = 5;
+ /* Parameter parsing */
+ int option_index = 0;
+ static struct option long_options[] = {
+ {"path", 1, 0, 0},
+ {0, 0, 0, 0}
+ };
+ char arg_s[64];
+
/* parse command line options */
- while ((i = getopt (argc, argv, "ha:b:r:n:k:t:")) != -1) {
+ while ((i = getopt_long (argc, argv, "ha:b:r:n:k:t:", long_options, &option_index)) != -1) {
switch (i) {
case 'h':
usage();
@@ -190,6 +200,22 @@ int main(int argc, char **argv)
sscanf(optarg, "%i", &xi);
tx_enable = (uint8_t)xi;
break;
+ case 0:
+ if (strcmp(long_options[option_index].name,"path") == 0) {
+ i = sscanf(optarg, "%s", arg_s);
+ if ((i != 1) || (strncmp(arg_s, "/dev/", 5 ) != 0)) {
+ printf("ERROR: argument parsing of --path argument. Use -h to print help\n");
+ return -1;
+ }
+ else {
+ lgw_spi_set_path(arg_s);
+ }
+ }
+ else {
+ printf("ERROR: argument parsing options. Use -h to print help\n");
+ return -1;
+ }
+ break;
default:
printf("ERROR: argument parsing\n");
usage();
diff --git a/libloragw/tst/test_loragw_gps.c b/libloragw/tst/test_loragw_gps.c
index 6e644f8..fd8c61b 100644
--- a/libloragw/tst/test_loragw_gps.c
+++ b/libloragw/tst/test_loragw_gps.c
@@ -146,11 +146,9 @@ int main()
/* serial variables */
char serial_buff[128]; /* buffer to receive GPS data */
- size_t wr_idx = 0; /* pointer to end of chars in buffer */
- int gps_tty_dev; /* file descriptor to the serial port of the GNSS module */
/* NMEA/UBX variables */
- enum gps_msg latest_msg; /* keep track of latest NMEA/UBX message parsed */
+ enum gps_msg latest_msg = UNKNOWN; /* keep track of latest NMEA/UBX message parsed */
fd_set fds;
char delim[4] = "$";
diff --git a/libloragw/tst/test_loragw_hal.c b/libloragw/tst/test_loragw_hal.c
index e2fee5e..37e8b42 100644
--- a/libloragw/tst/test_loragw_hal.c
+++ b/libloragw/tst/test_loragw_hal.c
@@ -30,7 +30,9 @@ Maintainer: Sylvain Miermont
#include <string.h> /* memset */
#include <signal.h> /* sigaction */
#include <unistd.h> /* getopt access */
+#include <getopt.h> /* getopt_long */
+#include "loragw_spi.h"
#include "loragw_hal.h"
#include "loragw_reg.h"
#include "loragw_aux.h"
@@ -78,6 +80,7 @@ void usage(void) {
printf( " -t <float> Radio TX frequency in MHz\n");
printf( " -r <int> Radio type (SX1255:1255, SX1257:1257)\n");
printf( " -k <int> Concentrator clock source (0: radio_A, 1: radio_B(default))\n");
+ printf(" --path <string> path of SPIDEV e.g. /dev/spidev0.0\n");
}
/* -------------------------------------------------------------------------- */
@@ -107,8 +110,16 @@ int main(int argc, char **argv)
double xd = 0.0;
int xi = 0;
+ /* Parameter parsing */
+ int option_index = 0;
+ static struct option long_options[] = {
+ {"path", 1, 0, 0},
+ {0, 0, 0, 0}
+ };
+ char arg_s[64];
+
/* parse command line options */
- while ((i = getopt (argc, argv, "ha:b:t:r:k:")) != -1) {
+ while ((i = getopt_long (argc, argv, "ha:b:r:n:k:t:", long_options, &option_index)) != -1) {
switch (i) {
case 'h':
usage();
@@ -145,6 +156,22 @@ int main(int argc, char **argv)
sscanf(optarg, "%i", &xi);
clocksource = (uint8_t)xi;
break;
+ case 0:
+ if (strcmp(long_options[option_index].name,"path") == 0) {
+ i = sscanf(optarg, "%s", arg_s);
+ if ((i != 1) || (strncmp(arg_s, "/dev/", 5 ) != 0)) {
+ printf("ERROR: argument parsing of --path argument. Use -h to print help\n");
+ return -1;
+ }
+ else {
+ lgw_spi_set_path(arg_s);
+ }
+ }
+ else {
+ printf("ERROR: argument parsing options. Use -h to print help\n");
+ return -1;
+ }
+ break;
default:
printf("ERROR: argument parsing\n");
usage();
@@ -307,7 +334,7 @@ int main(int argc, char **argv)
if (i == LGW_HAL_SUCCESS) {
printf("*** Concentrator started ***\n");
} else {
- printf("*** Impossible to start concentrator ***\n");
+ printf("*** Unable to start concentrator ***\n");
return -1;
}
diff --git a/libloragw/tst/test_loragw_reg.c b/libloragw/tst/test_loragw_reg.c
index 37a6f5a..6248a1b 100644
--- a/libloragw/tst/test_loragw_reg.c
+++ b/libloragw/tst/test_loragw_reg.c
@@ -19,15 +19,27 @@ Maintainer: Sylvain Miermont
#include <stdint.h>
#include <stdio.h>
+#include <string.h>
+#include <getopt.h> /* getopt_long */
+#include "loragw_hal.h"
#include "loragw_reg.h"
+#include "loragw_spi.h"
/* -------------------------------------------------------------------------- */
/* --- MAIN FUNCTION -------------------------------------------------------- */
#define BURST_TEST_LENGTH 8192
-int main()
+/* describe command line options */
+void usage(void) {
+ printf("Library version information: %s\n", lgw_version_info());
+ printf( "Available options:\n");
+ printf( " -h print this help\n");
+ printf(" --path <string> path of SPIDEV e.g. /dev/spidev0.0\n");
+}
+
+int main(int argc, char **argv)
{
int32_t read_value, test_value;
uint16_t lfsr;
@@ -35,9 +47,52 @@ int main()
uint8_t burst_buffin[BURST_TEST_LENGTH];
int i;
+ /* Parameter parsing */
+ int option_index = 0;
+ static struct option long_options[] = {
+ {"path", 1, 0, 0},
+ {0, 0, 0, 0}
+ };
+ char arg_s[64];
+
+ /* parse command line options */
+ while ((i = getopt_long (argc, argv, "h", long_options, &option_index)) != -1) {
+ switch (i) {
+ case 'h':
+ usage();
+ return -1;
+ break;
+ case 0:
+ if (strcmp(long_options[option_index].name,"path") == 0) {
+ i = sscanf(optarg, "%s", arg_s);
+ if ((i != 1) || (strncmp(arg_s, "/dev/", 5 ) != 0)) {
+ printf("ERROR: argument parsing of --path argument. Use -h to print help\n");
+ return -1;
+ }
+ else {
+ lgw_spi_set_path(arg_s);
+ }
+ }
+ else {
+ printf("ERROR: argument parsing options. Use -h to print help\n");
+ return -1;
+ }
+ break;
+ default:
+ printf("ERROR: argument parsing\n");
+ usage();
+ return -1;
+ }
+ }
+
+
printf("Beginning of test for loragw_reg.c\n");
- lgw_connect(false, 129E3);
+ int result = lgw_connect(false, 129E3);
+ if (result == -1) {
+ printf("ERROR: Failed to connect to the board\n");
+ return -1;
+ }
/* 2 SPI transactions:
-> 0x80 0x00 <- 0x00 0x00 forcing page 0
-> 0x01 0x00 <- 0x00 0x64 checking version
diff --git a/libloragw/tst/test_loragw_spi.c b/libloragw/tst/test_loragw_spi.c
index 872a075..4d06eaa 100644
--- a/libloragw/tst/test_loragw_spi.c
+++ b/libloragw/tst/test_loragw_spi.c
@@ -19,8 +19,11 @@ Maintainer: Sylvain Miermont
/* --- DEPENDANCIES --------------------------------------------------------- */
#include <stdint.h>
+#include <string.h>
#include <stdio.h>
+#include <getopt.h> /* getopt_long */
+#include "loragw_hal.h"
#include "loragw_spi.h"
/* -------------------------------------------------------------------------- */
@@ -37,7 +40,7 @@ Maintainer: Sylvain Miermont
/* -------------------------------------------------------------------------- */
/* --- MAIN FUNCTION -------------------------------------------------------- */
-int main()
+int main(int argc, char **argv)
{
int i;
void *spi_target = NULL;
@@ -46,37 +49,119 @@ int main()
uint8_t datain[BURST_TEST_SIZE];
uint8_t spi_mux_mode = LGW_SPI_MUX_MODE0;
+ /* Parameter parsing */
+ int option_index = 0;
+ static struct option long_options[] = {
+ {"path", 1, 0, 0},
+ {0, 0, 0, 0}
+ };
+ char arg_s[64];
+
+ while ((i = getopt_long (argc, argv, "h", long_options, &option_index)) != -1) {
+ switch (i) {
+ case 'h':
+ printf("~~~ Library version string~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
+ printf(" %s\n", lgw_version_info());
+ printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
+ printf(" --path <string> Path of SPIDEV e.g. /dev/spidev0.0\n");
+ printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
+ return 0;
+ break;
+
+ case 0:
+ if (strcmp(long_options[option_index].name,"path") == 0) {
+ i = sscanf(optarg, "%s", arg_s);
+ if ((i != 1) || (strncmp(arg_s, "/dev/", 5 ) != 0)) {
+ printf("ERROR: argument parsing of --path argument. Use -h to print help\n");
+ return LGW_SPI_ERROR;
+ }
+ else {
+ lgw_spi_set_path(arg_s);
+ }
+ }
+ else {
+ printf("ERROR: argument parsing options. Use -h to print help\n");
+ return LGW_SPI_ERROR;
+ }
+ break;
+
+ default:
+ printf("ERROR: argument parsing options. Use -h to print help\n");
+ return LGW_SPI_ERROR;
+ }
+ }
+
for (i = 0; i < BURST_TEST_SIZE; ++i) {
dataout[i] = 0x30 + (i % 10); /* ASCCI code for 0 -> 9 */
datain[i] = 0x23; /* garbage data, to be overwritten by received data */
}
printf("Beginning of test for loragw_spi.c\n");
- lgw_spi_open(&spi_target);
+
+ int spi_stat = lgw_spi_open(&spi_target);
+
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ printf("ERROR: Unable to connect to concentrator\n");
+ return LGW_SPI_ERROR;
+ }
/* normal R/W test */
- for (i = 0; i < TIMING_REPEAT; ++i)
- lgw_spi_w(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0xAA, 0x96);
- for (i = 0; i < TIMING_REPEAT; ++i)
- lgw_spi_r(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, &data);
+ for (i = 0; i < TIMING_REPEAT; ++i) {
+ spi_stat = lgw_spi_w(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0xAA, 0x96);
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ printf("ERROR: Failed write normal R/W test\n");
+ return LGW_SPI_ERROR;
+ }
+ }
+ for (i = 0; i < TIMING_REPEAT; ++i) {
+ spi_stat = lgw_spi_r(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, &data);
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ printf("ERROR: Failed read normal R/W test\n");
+ return LGW_SPI_ERROR;
+ }
+ }
/* burst R/W test, small bursts << LGW_BURST_CHUNK */
- for (i = 0; i < TIMING_REPEAT; ++i)
- lgw_spi_wb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, dataout, 16);
- for (i = 0; i < TIMING_REPEAT; ++i)
- lgw_spi_rb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, datain, 16);
+ for (i = 0; i < TIMING_REPEAT; ++i) {
+ spi_stat = lgw_spi_wb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, dataout, 16);
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ printf("ERROR: Failed write small burst R/W test\n");
+ return LGW_SPI_ERROR;
+ }
+ }
+ for (i = 0; i < TIMING_REPEAT; ++i) {
+ spi_stat = lgw_spi_rb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, datain, 16);
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ printf("ERROR: Failed read small burst R/W test\n");
+ return LGW_SPI_ERROR;
+ }
+ }
/* burst R/W test, large bursts >> LGW_BURST_CHUNK */
- for (i = 0; i < TIMING_REPEAT; ++i)
- lgw_spi_wb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x5A, dataout, ARRAY_SIZE(dataout));
- for (i = 0; i < TIMING_REPEAT; ++i)
- lgw_spi_rb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x5A, datain, ARRAY_SIZE(datain));
+ for (i = 0; i < TIMING_REPEAT; ++i) {
+ spi_stat = lgw_spi_wb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x5A, dataout, ARRAY_SIZE(dataout));
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ printf("ERROR: Failed write large burst R/W test\n");
+ return LGW_SPI_ERROR;
+ }
+ }
+ for (i = 0; i < TIMING_REPEAT; ++i) {
+ spi_stat = lgw_spi_rb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x5A, datain, ARRAY_SIZE(datain));
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ printf("ERROR: Failed read large burst R/W test\n");
+ return LGW_SPI_ERROR;
+ }
+ }
/* last read (blocking), just to be sure no to quit before the FTDI buffer is flushed */
- lgw_spi_r(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, &data);
- printf("data received (simple read): %d\n",data);
-
+ spi_stat = lgw_spi_r(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, &data);
+ if (spi_stat != LGW_SPI_SUCCESS) {
+ printf("ERROR: Failed to read last block\n");
+ return LGW_SPI_ERROR;
+ }
lgw_spi_close(spi_target);
+
+ printf("data received (simple read): %d\n",data);
printf("End of test for loragw_spi.c\n");
return 0;