-/*
- * Chip IDs are explained in the appropriate programming manual for the
- * DBGMCU_IDCODE register (0xE0042000)
- */
-
-#define CORE_M3_R1 0x1BA00477
-#define CORE_M3_R2 0x4BA00477
-#define CORE_M4_R0 0x2BA01477
-
-struct chip_params {
- uint32_t chip_id;
- char* description;
- uint32_t flash_size_reg;
- uint32_t max_flash_size, flash_pagesize;
- uint32_t sram_size;
- uint32_t bootrom_base, bootrom_size;
-} const devices[] = {
- { 0x410, "F1 Medium-density device", 0x1ffff7e0,
- 0x20000, 0x400, 0x5000, 0x1ffff000, 0x800 }, // table 2, pm0063
- { 0x411, "F2 device", 0, /* No flash size register found in the docs*/
- 0x100000, 0x20000, 0x20000, 0x1fff0000, 0x7800 }, // table 1, pm0059
- { 0x412, "F1 Low-density device", 0x1ffff7e0,
- 0x8000, 0x400, 0x2800, 0x1ffff000, 0x800 }, // table 1, pm0063
- { 0x413, "F4 device", 0x1FFF7A10,
- 0x100000, 0x20000, 0x30000, 0x1fff0000, 0x7800 }, // table 1, pm0081
- { 0x414, "F1 High-density device", 0x1ffff7e0,
- 0x80000, 0x800, 0x10000, 0x1ffff000, 0x800 }, // table 3 pm0063
- // This ignores the EEPROM! (and uses the page erase size,
- // not the sector write protection...)
- { 0x416, "L1 Med-density device", 0x1FF8004C, // table 1, pm0062
- 0x20000, 0x100, 0x4000, 0x1ff00000, 0x1000 },
- { 0x418, "F1 Connectivity line device", 0x1ffff7e0,
- 0x40000, 0x800, 0x10000, 0x1fffb000, 0x4800 },
- { 0x420, "F1 Medium-density value line device", 0x1ffff7e0,
- 0x20000, 0x400, 0x2000, 0x1ffff000, 0x800 },
- { 0x428, "F1 High-density value line device", 0x1ffff7e0,
- 0x80000, 0x800, 0x8000, 0x1ffff000, 0x800 },
- { 0x430, "F1 XL-density device", 0x1ffff7e0, // pm0068
- 0x100000, 0x800, 0x18000, 0x1fffe000, 0x1800 },
- { 0 }
-};
-
-int serve(stlink_t *sl, int port);
-char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
-
-int main(int argc, char** argv) {
-
- stlink_t *sl = NULL;
- int port = 0;
-
- const char * HelpStr = "\nUsage:\n"
- "\tst-util [Arguments]\n"
- "\tArguments (no more than 2):\n"
- "\t\t<Port>: Port. Default: 4242.\n"
- "\t\t{usb|sgauto|/dev/sgX}: Transport, "
- "where X = {0, 1, 2, ...}. Default: USB.\n"
- "\tExamples:\n"
- "\t\tst-util 1234\n"
- "\t\tst-util sgauto\n"
- "\t\tst-util 1234 usb\n"
- "\t\tst-util /dev/sgX 1234\n"
- "\t\tst-util 1234 /dev/sgX\n";
-
-
- // Parsing the arguments of command line ...
-
- if (argc == 1 || argc > 3) {
- fprintf(stderr, HelpStr, NULL);
- return 1;
- }
-
- for(int a = 1; a < argc; a++) {
-
- // Port
- int p = atoi(argv[a]);
- if (p < 0 || p > 0xFFFF) {
- fprintf(stderr, "Invalid port\n");
- fprintf(stderr, HelpStr, NULL);
- return 1;
- }
- if (p > 0 && port == 0) {port = p; continue;}
-
- // if (p == 0) ...
-
- if (sl != NULL) {
- fprintf(stderr, "Invalid argumets\n");
- fprintf(stderr, HelpStr, NULL);
- return 1;
+typedef struct _st_state_t {
+ // things from command line, bleh
+ int stlink_version;
+ // "/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTE531X6-if00-port0" is only 58 chars
+ char devicename[100];
+ int logging_level;
+ int listen_port;
+ int persistent;
+ int reset;
+} st_state_t;
+
+
+int serve(stlink_t *sl, st_state_t *st);
+char* make_memory_map(stlink_t *sl);
+
+#ifndef __MINGW32__
+static void cleanup(int signal __attribute__((unused))) {
+ if (connected_stlink) {
+ /* Switch back to mass storage mode before closing. */
+ stlink_run(connected_stlink);
+ stlink_exit_debug_mode(connected_stlink);
+ stlink_close(connected_stlink);
+ }
+
+ exit(1);
+}
+#endif
+
+
+
+int parse_options(int argc, char** argv, st_state_t *st) {
+ static struct option long_options[] = {
+ {"help", no_argument, NULL, 'h'},
+ {"verbose", optional_argument, NULL, 'v'},
+ {"device", required_argument, NULL, 'd'},
+ {"stlink_version", required_argument, NULL, 's'},
+ {"stlinkv1", no_argument, NULL, '1'},
+ {"listen_port", required_argument, NULL, 'p'},
+ {"multi", optional_argument, NULL, 'm'},
+ {"no-reset", optional_argument, NULL, 'n'},
+ {0, 0, 0, 0},
+ };
+ const char * help_str = "%s - usage:\n\n"
+ " -h, --help\t\tPrint this help\n"
+ " -vXX, --verbose=XX\tspecify a specific verbosity level (0..99)\n"
+ " -v, --verbose\tspecify generally verbose logging\n"
+ " -d <device>, --device=/dev/stlink2_1\n"
+ "\t\t\tWhere is your stlink device connected?\n"
+ " -s X, --stlink_version=X\n"
+ "\t\t\tChoose what version of stlink to use, (defaults to 2)\n"
+ " -1, --stlinkv1\tForce stlink version 1\n"
+ " -p 4242, --listen_port=1234\n"
+ "\t\t\tSet the gdb server listen port. "
+ "(default port: " STRINGIFY(DEFAULT_GDB_LISTEN_PORT) ")\n"
+ " -m, --multi\n"
+ "\t\t\tSet gdb server to extended mode.\n"
+ "\t\t\tst-util will continue listening for connections after disconnect.\n"
+ " -n, --no-reset\n"
+ "\t\t\tDo not reset board on connection.\n"
+ ;
+
+
+ int option_index = 0;
+ int c;
+ int q;
+ while ((c = getopt_long(argc, argv, "hv::d:s:1p:mn", long_options, &option_index)) != -1) {
+ switch (c) {
+ case 0:
+ printf("XXXXX Shouldn't really normally come here, only if there's no corresponding option\n");
+ printf("option %s", long_options[option_index].name);
+ if (optarg) {
+ printf(" with arg %s", optarg);