From e7d9e0c175c073190efefa8c04f477504a237318 Mon Sep 17 00:00:00 2001 From: Aaron Shaw Date: Fri, 8 Oct 2021 03:29:13 +0100 Subject: [PATCH] add modified chip_id.c - add a modified version of chip_id.c that allows us to check the chip ID - this allows us to determine if the chip is sx1302 or sx1303 --- buildfiles/compileSX1302.sh | 3 +- sx1302fixes/chip_id.c | 236 ++++++++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 sx1302fixes/chip_id.c diff --git a/buildfiles/compileSX1302.sh b/buildfiles/compileSX1302.sh index cf3c52e..90dcdfe 100755 --- a/buildfiles/compileSX1302.sh +++ b/buildfiles/compileSX1302.sh @@ -17,9 +17,10 @@ rm libloragw/src/loragw_ad5338r.c -f #Copy new files cp ../sx1302fixes/loragw_hal.c libloragw/src/loragw_hal.c -f cp ../sx1302fixes/Makefile libloragw/Makefile -f -cp ../sx1302fixes/lora_pkt_fwd.c packet_forwarder/src/lora_pkt_fwd.c +cp ../sx1302fixes/lora_pkt_fwd.c packet_forwarder/src/lora_pkt_fwd.c -f cp ../sx1302fixes/test_loragw_gps_uart.c libloragw/tst/test_loragw_gps.c -f cp ../sx1302fixes/test_loragw_gps_i2c.c libloragw/tst/test_loragw_gps_i2c.c -f +cp ../sx1302fixes/chip_id.c util_chip_id/src/chip_id.c -f make clean make -j 4 diff --git a/sx1302fixes/chip_id.c b/sx1302fixes/chip_id.c new file mode 100644 index 0000000..7f8df14 --- /dev/null +++ b/sx1302fixes/chip_id.c @@ -0,0 +1,236 @@ +/* + ______ _ + / _____) _ | | +( (____ _____ ____ _| |_ _____ ____| |__ + \____ \| ___ | (_ _) ___ |/ ___) _ \ + _____) ) ____| | | || |_| ____( (___| | | | +(______/|_____)_|_|_| \__)_____)\____)_| |_| + (C)2019 Semtech + +Description: + Utility to get SX1302 chip EUI + +License: Revised BSD License, see LICENSE.TXT file include in the project +*/ + + +/* -------------------------------------------------------------------------- */ +/* --- DEPENDANCIES --------------------------------------------------------- */ + +/* fix an issue between POSIX and C99 */ +#if __STDC_VERSION__ >= 199901L + #define _XOPEN_SOURCE 600 +#else + #define _XOPEN_SOURCE 500 +#endif + +#include +#include +#include +#include /* PRIx64, PRIu64... */ +#include +#include +#include +#include /* sigaction */ +#include /* getopt_long */ + +#include "loragw_hal.h" +#include "loragw_reg.h" +#include "loragw_aux.h" +#include "loragw_sx1302.h" + +/* -------------------------------------------------------------------------- */ +/* --- PRIVATE MACROS ------------------------------------------------------- */ + +#define RAND_RANGE(min, max) (rand() % (max + 1 - min) + min) + +/* -------------------------------------------------------------------------- */ +/* --- PRIVATE CONSTANTS ---------------------------------------------------- */ + +#define COM_TYPE_DEFAULT LGW_COM_SPI +#define COM_PATH_DEFAULT "/dev/spidev0.0" + +#define DEFAULT_CLK_SRC 0 +#define DEFAULT_FREQ_HZ 868500000U + +/* -------------------------------------------------------------------------- */ +/* --- PRIVATE VARIABLES ---------------------------------------------------- */ + +/* -------------------------------------------------------------------------- */ +/* --- PRIVATE FUNCTIONS ---------------------------------------------------- */ + +/* 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(" -u Set COM type as USB (default is SPI)\n"); + printf(" -d [path] Path to the COM interface\n"); + printf(" => default path: " COM_PATH_DEFAULT "\n"); + printf(" -k Concentrator clock source (Radio A or Radio B) [0..1]\n"); + printf(" -r Radio type (1255, 1257, 1250)\n"); +} + +/* -------------------------------------------------------------------------- */ +/* --- MAIN FUNCTION -------------------------------------------------------- */ + +int main(int argc, char **argv) +{ + int i, x; + unsigned int arg_u; + uint8_t clocksource = 0; + lgw_radio_type_t radio_type = LGW_RADIO_TYPE_SX1250; + sx1302_model_id_t model_id; + + struct lgw_conf_board_s boardconf; + struct lgw_conf_rxrf_s rfconf; + uint64_t eui; + + /* SPI interfaces */ + const char com_path_default[] = COM_PATH_DEFAULT; + const char * com_path = com_path_default; + lgw_com_type_t com_type = COM_TYPE_DEFAULT; + + /* Parameter parsing */ + int option_index = 0; + static struct option long_options[] = { + {0, 0, 0, 0} + }; + + /* parse command line options */ + while ((i = getopt_long (argc, argv, "hud:k:r:", long_options, &option_index)) != -1) { + switch (i) { + case 'h': + usage(); + return -1; + break; + + case 'u': + com_type = LGW_COM_USB; + break; + + case 'd': + com_path = optarg; + break; + + case 'r': /* Radio type */ + i = sscanf(optarg, "%u", &arg_u); + if ((i != 1) || ((arg_u != 1255) && (arg_u != 1257) && (arg_u != 1250))) { + printf("ERROR: argument parsing of -r argument. Use -h to print help\n"); + return EXIT_FAILURE; + } else { + switch (arg_u) { + case 1255: + radio_type = LGW_RADIO_TYPE_SX1255; + break; + case 1257: + radio_type = LGW_RADIO_TYPE_SX1257; + break; + default: /* 1250 */ + radio_type = LGW_RADIO_TYPE_SX1250; + break; + } + } + break; + + case 'k': /* Clock Source */ + i = sscanf(optarg, "%u", &arg_u); + if ((i != 1) || (arg_u > 1)) { + printf("ERROR: argument parsing of -k argument. Use -h to print help\n"); + return EXIT_FAILURE; + } else { + clocksource = (uint8_t)arg_u; + } + break; + + default: + printf("ERROR: argument parsing\n"); + usage(); + return -1; + } + } + + if (com_type == LGW_COM_SPI) { + /* Board reset */ + if (system("./reset_lgw.sh start") != 0) { + printf("ERROR: failed to reset SX1302, check your reset_lgw.sh script\n"); + exit(EXIT_FAILURE); + } + } + + /* Configure the gateway */ + memset(&boardconf, 0, sizeof boardconf); + boardconf.lorawan_public = true; + boardconf.clksrc = clocksource; + boardconf.full_duplex = false; + boardconf.com_type = com_type; + strncpy(boardconf.com_path, com_path, sizeof boardconf.com_path); + boardconf.com_path[sizeof boardconf.com_path - 1] = '\0'; /* ensure string termination */ + if (lgw_board_setconf(&boardconf) != LGW_HAL_SUCCESS) { + printf("ERROR: failed to configure board\n"); + return EXIT_FAILURE; + } + + memset(&rfconf, 0, sizeof rfconf); + rfconf.enable = true; /* rf chain 0 needs to be enabled for calibration to work on sx1257 */ + rfconf.freq_hz = 868500000; /* dummy */ + rfconf.type = radio_type; + rfconf.tx_enable = false; + rfconf.single_input_mode = false; + if (lgw_rxrf_setconf(0, &rfconf) != LGW_HAL_SUCCESS) { + printf("ERROR: failed to configure rxrf 0\n"); + return EXIT_FAILURE; + } + + memset(&rfconf, 0, sizeof rfconf); + rfconf.enable = (clocksource == 1) ? true : false; + rfconf.freq_hz = 868500000; /* dummy */ + rfconf.type = radio_type; + rfconf.tx_enable = false; + rfconf.single_input_mode = false; + if (lgw_rxrf_setconf(1, &rfconf) != LGW_HAL_SUCCESS) { + printf("ERROR: failed to configure rxrf 1\n"); + return EXIT_FAILURE; + } + + x = lgw_start(); + if (x != 0) { + printf("ERROR: failed to start the gateway\n"); + return EXIT_FAILURE; + } + + /* get the concentrator chip model */ + x = sx1302_get_model_id(&model_id); + if (x != LGW_HAL_SUCCESS) { + printf("ERROR: failed to get concentrator chip model\n"); + } else { + printf("\nINFO: concentrator chip model ID: 0x%02X\n", model_id); + } + + /* get the concentrator EUI */ + x = lgw_get_eui(&eui); + if (x != LGW_HAL_SUCCESS) { + printf("ERROR: failed to get concentrator EUI\n"); + } else { + printf("\nINFO: concentrator EUI: 0x%016" PRIx64 "\n\n", eui); + } + + /* Stop the gateway */ + x = lgw_stop(); + if (x != 0) { + printf("ERROR: failed to stop the gateway\n"); + return EXIT_FAILURE; + } + + if (com_type == LGW_COM_SPI) { + /* Board reset */ + if (system("./reset_lgw.sh stop") != 0) { + printf("ERROR: failed to reset SX1302, check your reset_lgw.sh script\n"); + exit(EXIT_FAILURE); + } + } + + return 0; +} + +/* --- EOF ------------------------------------------------------------------ */