summaryrefslogtreecommitdiff
path: root/libloragw/tst/test_loragw_spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'libloragw/tst/test_loragw_spi.c')
-rw-r--r--libloragw/tst/test_loragw_spi.c119
1 files changed, 102 insertions, 17 deletions
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;