summaryrefslogtreecommitdiff
path: root/libloragw
diff options
context:
space:
mode:
Diffstat (limited to 'libloragw')
-rw-r--r--libloragw/src/loragw_spi.native.c2
-rw-r--r--libloragw/tst/test_loragw_spi.c118
2 files changed, 102 insertions, 18 deletions
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_spi.c b/libloragw/tst/test_loragw_spi.c
index 872a075..9489d3c 100644
--- a/libloragw/tst/test_loragw_spi.c
+++ b/libloragw/tst/test_loragw_spi.c
@@ -20,7 +20,9 @@ Maintainer: Sylvain Miermont
#include <stdint.h>
#include <stdio.h>
+#include <getopt.h> /* getopt_long */
+#include "loragw_hal.h"
#include "loragw_spi.h"
/* -------------------------------------------------------------------------- */
@@ -37,7 +39,7 @@ Maintainer: Sylvain Miermont
/* -------------------------------------------------------------------------- */
/* --- MAIN FUNCTION -------------------------------------------------------- */
-int main()
+int main(int argc, char **argv)
{
int i;
void *spi_target = NULL;
@@ -46,37 +48,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;