Host driver/HAL to build a LoRa Picocell Gateway which communicates through USB with a concentrator board based on Semtech SX1308 multi-channel modem and SX1257/SX1255 RF transceivers.
util_boot/src/util_boot.c@0:102b50f941d0, 2018-04-11 (annotated)
- Committer:
- dgabino
- Date:
- Wed Apr 11 14:38:42 2018 +0000
- Revision:
- 0:102b50f941d0
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgabino | 0:102b50f941d0 | 1 | /* |
dgabino | 0:102b50f941d0 | 2 | / _____) _ | | |
dgabino | 0:102b50f941d0 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
dgabino | 0:102b50f941d0 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
dgabino | 0:102b50f941d0 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
dgabino | 0:102b50f941d0 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
dgabino | 0:102b50f941d0 | 7 | (C)2017 Semtech-Cycleo |
dgabino | 0:102b50f941d0 | 8 | |
dgabino | 0:102b50f941d0 | 9 | Description: |
dgabino | 0:102b50f941d0 | 10 | Utility to jump to the PicoCell MCU bootloader |
dgabino | 0:102b50f941d0 | 11 | |
dgabino | 0:102b50f941d0 | 12 | License: Revised BSD License, see LICENSE.TXT file include in the project |
dgabino | 0:102b50f941d0 | 13 | */ |
dgabino | 0:102b50f941d0 | 14 | |
dgabino | 0:102b50f941d0 | 15 | |
dgabino | 0:102b50f941d0 | 16 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 17 | /* --- DEPENDANCIES --------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 18 | |
dgabino | 0:102b50f941d0 | 19 | /* fix an issue between POSIX and C99 */ |
dgabino | 0:102b50f941d0 | 20 | #if __STDC_VERSION__ >= 199901L |
dgabino | 0:102b50f941d0 | 21 | #define _XOPEN_SOURCE 600 |
dgabino | 0:102b50f941d0 | 22 | #else |
dgabino | 0:102b50f941d0 | 23 | #define _XOPEN_SOURCE 500 |
dgabino | 0:102b50f941d0 | 24 | #endif |
dgabino | 0:102b50f941d0 | 25 | |
dgabino | 0:102b50f941d0 | 26 | #include <stdint.h> /* C99 types */ |
dgabino | 0:102b50f941d0 | 27 | #include <stdio.h> /* printf fprintf sprintf fopen fputs */ |
dgabino | 0:102b50f941d0 | 28 | #include <unistd.h> /* getopt access */ |
dgabino | 0:102b50f941d0 | 29 | #include <stdlib.h> /* EXIT_FAILURE */ |
dgabino | 0:102b50f941d0 | 30 | |
dgabino | 0:102b50f941d0 | 31 | #include "loragw_com.h" |
dgabino | 0:102b50f941d0 | 32 | |
dgabino | 0:102b50f941d0 | 33 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 34 | /* --- PRIVATE MACROS ------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 35 | |
dgabino | 0:102b50f941d0 | 36 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
dgabino | 0:102b50f941d0 | 37 | #define MSG(args...) fprintf(stderr, args) /* message that is destined to the user */ |
dgabino | 0:102b50f941d0 | 38 | |
dgabino | 0:102b50f941d0 | 39 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 40 | /* --- PRIVATE CONSTANTS ---------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 41 | |
dgabino | 0:102b50f941d0 | 42 | #define COM_PATH_DEFAULT "/dev/ttyACM0" |
dgabino | 0:102b50f941d0 | 43 | |
dgabino | 0:102b50f941d0 | 44 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 45 | /* --- PRIVATE VARIABLES (GLOBAL) ------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 46 | |
dgabino | 0:102b50f941d0 | 47 | void *lgw_com_target = NULL; /*! generic pointer to the COM device */ |
dgabino | 0:102b50f941d0 | 48 | |
dgabino | 0:102b50f941d0 | 49 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 50 | /* --- PRIVATE FUNCTIONS DECLARATION ---------------------------------------- */ |
dgabino | 0:102b50f941d0 | 51 | |
dgabino | 0:102b50f941d0 | 52 | void usage (void); |
dgabino | 0:102b50f941d0 | 53 | |
dgabino | 0:102b50f941d0 | 54 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 55 | /* --- PRIVATE FUNCTIONS DEFINITION ----------------------------------------- */ |
dgabino | 0:102b50f941d0 | 56 | |
dgabino | 0:102b50f941d0 | 57 | /* describe command line options */ |
dgabino | 0:102b50f941d0 | 58 | void usage(void) { |
dgabino | 0:102b50f941d0 | 59 | MSG("Available options:\n"); |
dgabino | 0:102b50f941d0 | 60 | MSG(" -h print this help\n"); |
dgabino | 0:102b50f941d0 | 61 | MSG(" -d <path> COM device to be used to access the concentrator board\n"); |
dgabino | 0:102b50f941d0 | 62 | MSG(" => default path: " COM_PATH_DEFAULT "\n"); |
dgabino | 0:102b50f941d0 | 63 | } |
dgabino | 0:102b50f941d0 | 64 | |
dgabino | 0:102b50f941d0 | 65 | /* -------------------------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 66 | /* --- MAIN FUNCTION -------------------------------------------------------- */ |
dgabino | 0:102b50f941d0 | 67 | |
dgabino | 0:102b50f941d0 | 68 | int main(int argc, char **argv) { |
dgabino | 0:102b50f941d0 | 69 | int i, x; |
dgabino | 0:102b50f941d0 | 70 | lgw_com_cmd_t cmd; |
dgabino | 0:102b50f941d0 | 71 | lgw_com_ans_t ans; |
dgabino | 0:102b50f941d0 | 72 | /* COM interfaces */ |
dgabino | 0:102b50f941d0 | 73 | const char com_path_default[] = COM_PATH_DEFAULT; |
dgabino | 0:102b50f941d0 | 74 | const char *com_path = com_path_default; |
dgabino | 0:102b50f941d0 | 75 | |
dgabino | 0:102b50f941d0 | 76 | while ((i = getopt (argc, argv, "hd:")) != -1) { |
dgabino | 0:102b50f941d0 | 77 | switch (i) { |
dgabino | 0:102b50f941d0 | 78 | case 'h': |
dgabino | 0:102b50f941d0 | 79 | usage(); |
dgabino | 0:102b50f941d0 | 80 | return EXIT_FAILURE; |
dgabino | 0:102b50f941d0 | 81 | break; |
dgabino | 0:102b50f941d0 | 82 | |
dgabino | 0:102b50f941d0 | 83 | case 'd': |
dgabino | 0:102b50f941d0 | 84 | if (optarg != NULL) { |
dgabino | 0:102b50f941d0 | 85 | com_path = optarg; |
dgabino | 0:102b50f941d0 | 86 | } |
dgabino | 0:102b50f941d0 | 87 | break; |
dgabino | 0:102b50f941d0 | 88 | |
dgabino | 0:102b50f941d0 | 89 | default: |
dgabino | 0:102b50f941d0 | 90 | MSG("ERROR: argument parsing use -h option for help\n"); |
dgabino | 0:102b50f941d0 | 91 | usage(); |
dgabino | 0:102b50f941d0 | 92 | return EXIT_FAILURE; |
dgabino | 0:102b50f941d0 | 93 | } |
dgabino | 0:102b50f941d0 | 94 | } |
dgabino | 0:102b50f941d0 | 95 | |
dgabino | 0:102b50f941d0 | 96 | /* Open communication bridge */ |
dgabino | 0:102b50f941d0 | 97 | x = lgw_com_open(&lgw_com_target, com_path); |
dgabino | 0:102b50f941d0 | 98 | if (x == LGW_COM_ERROR) { |
dgabino | 0:102b50f941d0 | 99 | printf("ERROR: FAIL TO CONNECT BOARD ON %s\n", com_path); |
dgabino | 0:102b50f941d0 | 100 | return -1; |
dgabino | 0:102b50f941d0 | 101 | } |
dgabino | 0:102b50f941d0 | 102 | |
dgabino | 0:102b50f941d0 | 103 | /* prepare command to jump to bootloader */ |
dgabino | 0:102b50f941d0 | 104 | cmd.id = 'n'; |
dgabino | 0:102b50f941d0 | 105 | cmd.len_msb = 0; |
dgabino | 0:102b50f941d0 | 106 | cmd.len_lsb = 0; |
dgabino | 0:102b50f941d0 | 107 | cmd.address = 0; |
dgabino | 0:102b50f941d0 | 108 | /* send command to MCU */ |
dgabino | 0:102b50f941d0 | 109 | x = lgw_com_send_command(lgw_com_target, cmd, &ans); |
dgabino | 0:102b50f941d0 | 110 | if (x == LGW_COM_ERROR) { |
dgabino | 0:102b50f941d0 | 111 | printf("ERROR: FAIL TO SEND COMMAND\n"); |
dgabino | 0:102b50f941d0 | 112 | return -1; |
dgabino | 0:102b50f941d0 | 113 | } |
dgabino | 0:102b50f941d0 | 114 | |
dgabino | 0:102b50f941d0 | 115 | /* Close communication bridge */ |
dgabino | 0:102b50f941d0 | 116 | x = lgw_com_close(lgw_com_target); |
dgabino | 0:102b50f941d0 | 117 | if (x == LGW_COM_ERROR) { |
dgabino | 0:102b50f941d0 | 118 | printf("ERROR: FAIL TO DISCONNECT BOARD\n"); |
dgabino | 0:102b50f941d0 | 119 | return -1; |
dgabino | 0:102b50f941d0 | 120 | } |
dgabino | 0:102b50f941d0 | 121 | |
dgabino | 0:102b50f941d0 | 122 | return EXIT_SUCCESS; |
dgabino | 0:102b50f941d0 | 123 | } |
dgabino | 0:102b50f941d0 | 124 | |
dgabino | 0:102b50f941d0 | 125 | /* --- EOF ------------------------------------------------------------------ */ |
dgabino | 0:102b50f941d0 | 126 | |
dgabino | 0:102b50f941d0 | 127 |