/* / _____) _ | | ( (____ _____ ____ _| |_ _____ ____| |__ \____ \| ___ | (_ _) ___ |/ ___) _ \ _____) ) ____| | | || |_| ____( (___| | | | (______/|_____)_|_|_| \__)_____)\____)_| |_| (C)2013 Semtech-Cycleo Description: Minimum test program for the loragw_spi 'library' Use logic analyser to check the results. License: Revised BSD License, see LICENSE.TXT file include in the project Maintainer: Sylvain Miermont */ /* -------------------------------------------------------------------------- */ /* --- DEPENDANCIES --------------------------------------------------------- */ #include #include #include #include /* getopt_long */ #include "loragw_hal.h" #include "loragw_spi.h" /* -------------------------------------------------------------------------- */ /* --- PRIVATE MACROS ------------------------------------------------------- */ #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) /* -------------------------------------------------------------------------- */ /* --- PRIVATE CONSTANTS ---------------------------------------------------- */ #define BURST_TEST_SIZE 2500 /* >> LGW_BURST_CHUNK */ #define TIMING_REPEAT 1 /* repeat transactions multiple times for timing characterisation */ /* -------------------------------------------------------------------------- */ /* --- MAIN FUNCTION -------------------------------------------------------- */ int main(int argc, char **argv) { int i; void *spi_target = NULL; uint8_t data = 0; uint8_t dataout[BURST_TEST_SIZE]; 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 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"); 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) { 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) { 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) { 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 */ 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; } /* --- EOF ------------------------------------------------------------------ */