Skip to content

Commit

Permalink
Merge pull request #1114 from Ant-ON/cmd_line_arg
Browse files Browse the repository at this point in the history
[feature] Extended set of cmd line arguments for st-info and st-util
  • Loading branch information
Nightwalker-87 authored Mar 26, 2021
2 parents 82435be + b387c93 commit cfff13c
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 112 deletions.
7 changes: 4 additions & 3 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
| Option | Tool | Description | Available<br />since |
| --------------------- | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------- |
| --flash=n[k][m] | st-flash | One can specify `--flash=128k` for example, to override the default value of 64k for the STM32F103C8T6<br />to assume 128k of flash being present. This option accepts decimal (128k), octal 0200k, or hex 0x80k values.<br />Leaving the multiplier out is equally valid, e.g.: `--flash=0x20000`. The size may be followed by an optional<br />"k" or "m" to multiply the given value by 1k (1024) or 1M respectively. | v1.4.0 |
| --freq=n[k][m] | st-flash | The frequency of the SWD/JTAG interface can be specified, to override the default 1800 kHz configuration.<br />This option solely accepts decimal values (5K or 1.8M) with the unit `Hz` being left out. Valid frequencies are:<br />`5K, 15K, 25K, 50K, 100K, 125K, 240K, 480K, 950K, 1200K(1.2M), 1800K(1.8M), 4000K(4M)`. | v1.6.1 |
| --freq=n[k][m] | st-info<br />st-flash<br />st-util | The frequency of the SWD/JTAG interface can be specified, to override the default 1800 kHz configuration.<br />This option solely accepts decimal values (5K or 1.8M) with the unit `Hz` being left out. Valid frequencies are:<br />`5K, 15K, 25K, 50K, 100K, 125K, 240K, 480K, 950K, 1200K, 1800K, 4000K(4M)`. | v1.6.1 |
| --opt | st-flash | Optimisation can be enabled in order to skip flashing empty (0x00 or 0xff) bytes at the end of binary file.<br />This may cause some garbage data left after a flash operation. This option was enabled by default in earlier releases. | v1.6.1 |
| --reset | st-flash | Trigger a reset both before and after flashing. The default uses the hardware reset through `NRST` pin.<br />The software reset is used if the hardware reset failed (`NRST` pin not connected). | v1.0.0 |
| --connect-under-reset | st-flash | Connect under reset. Option makes it possible to connect to the device before code execution. This is useful<br />when the target contains code that lets the device go to sleep, disables debug pins or other special code. | v1.6.1 |
| --connect-under-reset | st-info<br />st-flash | Connect under reset. Option makes it possible to connect to the device before code execution. This is useful<br />when the target contains code that lets the device go to sleep, disables debug pins or other special code. | v1.6.1 |
| --hot-plug | st-info<br />st-util | Connect to the target without reset. | v1.6.2 |
| --probe | st-info | Display hardware information about the connected programmer and target MCU. | v1.2.0 |
| --version | st-info<br />st-flash<br />st-util | Print version information. | v1.3.0 |
| --help | st-flash<br />st-util | Print list of available commands. | |
Expand Down Expand Up @@ -262,7 +263,7 @@ There are a few options:
-m, --multi
Set gdb server to extended mode.
st-util will continue listening for connections after disconnect.
-n, --no-reset
-n, --no-reset, --hot-plug
Do not reset board on connection.
```

Expand Down
11 changes: 5 additions & 6 deletions src/st-flash/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ static void cleanup(int signum) {
}

static void usage(void) {
puts("command line: ./st-flash [--debug] [--reset] [--connect-under-reset] [--opt] [--serial <serial>] [--format <format>] [--flash=<fsize>] [--freq=<Hz>] [--area=<area>] {read|write} [path] [addr] [size]");
puts("command line: ./st-flash [--debug] [--connect-under-reset] [--freq=<Hz>] [--serial <serial>] erase");
puts("command line: ./st-flash [--debug] [--freq=<Hz>] [--serial <serial>] reset");
puts("command line: ./st-flash [--debug] [--reset] [--connect-under-reset] [--opt] [--serial <serial>] [--format <format>] [--flash=<fsize>] [--freq=<KHz>] [--area=<area>] {read|write} [path] [addr] [size]");
puts("command line: ./st-flash [--debug] [--connect-under-reset] [--freq=<KHz>] [--serial <serial>] erase");
puts("command line: ./st-flash [--debug] [--freq=<KHz>] [--serial <serial>] reset");
puts(" <addr>, <serial> and <size>: Use hex format.");
puts(" <fsize>: Use decimal, octal or hex (prefix 0xXXX) format, optionally followed by k=KB, or m=MB (eg. --flash=128k)");
puts(" <format>: Can be 'binary' (default) or 'ihex', although <addr> must be specified for binary format only.");
Expand All @@ -52,6 +52,7 @@ int main(int ac, char** av) {
uint8_t * mem = NULL;

o.size = 0;
o.connect = CONNECT_NORMAL;

if (flash_get_opts(&o, ac - 1, av + 1) == -1) {
printf("invalid command line\n");
Expand All @@ -61,9 +62,7 @@ int main(int ac, char** av) {

printf("st-flash %s\n", STLINK_VERSION);

sl = stlink_open_usb(o.log_level,
o.connect_under_reset ? 2 : 1,
(char *)o.serial, o.freq);
sl = stlink_open_usb(o.log_level, o.connect, (char *)o.serial, o.freq);

if (sl == NULL) { return(-1); }

Expand Down
2 changes: 1 addition & 1 deletion src/st-flash/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct flash_opts {
size_t flash_size; // --flash=n[k][m]
int opt; // enable empty tail data drop optimization
int freq; // --freq=n[k][m] frequency of JTAG/SWD
bool connect_under_reset;
enum connect_type connect;
};

#define FLASH_OPTS_INITIALIZER {0, { 0 }, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Expand Down
57 changes: 15 additions & 42 deletions src/st-flash/flash_opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <stdio.h>
#include <string.h>

#include <helper.h>

#include "flash.h"

static bool starts_with(const char * str, const char * prefix) {
Expand Down Expand Up @@ -140,50 +142,21 @@ int flash_get_opts(struct flash_opts* o, int ac, char** av) {
return(-1);
}

} else if (strcmp(av[0], "--freq") == 0 || starts_with(av[0], "--freq=")) {
const char* freq;

if (strcmp(av[0], "--freq") == 0) {
ac--;
av++;

if (ac < 1) {
return(-1);
}
} else if (strcmp(av[0], "--freq") == 0) {
ac--;
av++;

freq = av[0];
} else {
freq = av[0] + strlen("--freq=");
if (ac < 1) {
return(-1);
}

if (strcmp(freq, "5K") == 0 || strcmp(freq, "5k") == 0) {
o->freq = 5;
} else if (strcmp(freq, "15K") == 0 || strcmp(freq, "15k") == 0) {
o->freq = 15;
} else if (strcmp(freq, "25K") == 0 || strcmp(freq, "25k") == 0) {
o->freq = 25;
} else if (strcmp(freq, "50K") == 0 || strcmp(freq, "50k") == 0) {
o->freq = 50;
} else if (strcmp(freq, "100K") == 0 || strcmp(freq, "100k") == 0) {
o->freq = 100;
} else if (strcmp(freq, "125K") == 0 || strcmp(freq, "125k") == 0) {
o->freq = 125;
} else if (strcmp(freq, "240K") == 0 || strcmp(freq, "240k") == 0) {
o->freq = 240;
} else if (strcmp(freq, "480K") == 0 || strcmp(freq, "480k") == 0) {
o->freq = 480;
} else if (strcmp(freq, "950K") == 0 || strcmp(freq, "950k") == 0) {
o->freq = 950;
} else if (strcmp(freq, "1200K") == 0 || strcmp(freq, "1200k") == 0 ||
strcmp(freq, "1.2M") == 0 || strcmp(freq, "1.2m") == 0) {
o->freq = 1200;
} else if (strcmp(freq, "1800K") == 0 || strcmp(freq, "1800k") == 0 ||
strcmp(freq, "1.8M") == 0 || strcmp(freq, "1.8m") == 0) {
o->freq = 1800;
} else if (strcmp(freq, "4000K") == 0 || strcmp(freq, "4000k") == 0 ||
strcmp(freq, "4M") == 0 || strcmp(freq, "4m") == 0) {
o->freq = 4000;
} else {
o->freq = arg_parse_freq(av[0]);
if (o->freq < 0) {
return(-1);
}
} else if (starts_with(av[0], "--freq=")) {
o->freq = arg_parse_freq(av[0] + strlen("--freq="));
if (o->freq < 0) {
return(-1);
}
} else if (strcmp(av[0], "--format") == 0 || starts_with(av[0], "--format=")) {
Expand Down Expand Up @@ -219,7 +192,7 @@ int flash_get_opts(struct flash_opts* o, int ac, char** av) {
o->flash_size = (size_t)flash_size;
}
} else if (strcmp(av[0], "--connect-under-reset") == 0) {
o->connect_under_reset = true;
o->connect = CONNECT_UNDER_RESET;
} else {
break; // non-option found

Expand Down
74 changes: 39 additions & 35 deletions src/st-info/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
#include <sys/types.h>

#include <stlink.h>
#include <helper.h>

static void usage(void) {
puts("st-info --version");
puts("st-info --probe");
puts("st-info --probe [--connect-under-reset] [--hot-plug] [--freq=<KHz>]");
puts("st-info --serial");
puts("st-info --flash [--connect-under-reset]");
puts("st-info --pagesize [--connect-under-reset]");
puts("st-info --sram [--connect-under-reset]");
puts("st-info --chipid [--connect-under-reset]");
puts("st-info --descr [--connect-under-reset]");
puts("st-info --flash [--connect-under-reset] [--hot-plug] [--freq=<KHz>]");
puts("st-info --pagesize [--connect-under-reset] [--hot-plug] [--freq=<KHz>]");
puts("st-info --sram [--connect-under-reset] [--hot-plug] [--freq=<KHz>]");
puts("st-info --chipid [--connect-under-reset] [--hot-plug] [--freq=<KHz>]");
puts("st-info --descr [--connect-under-reset] [--hot-plug] [--freq=<KHz>]");
}

static void stlink_print_version(stlink_t *sl) {
Expand Down Expand Up @@ -45,11 +46,11 @@ static void stlink_print_info(stlink_t *sl) {
if (params) { printf(" descr: %s\n", params->description); }
}

static void stlink_probe(void) {
static void stlink_probe(enum connect_type connect, int freq) {
stlink_t **stdevs;
size_t size;

size = stlink_probe_usb(&stdevs);
size = stlink_probe_usb(&stdevs, connect, freq);

printf("Found %u stlink programmers\n", (unsigned int)size);

Expand All @@ -61,44 +62,47 @@ static void stlink_probe(void) {
stlink_probe_usb_free(&stdevs, size);
}

static stlink_t *stlink_open_first(bool under_reset) {
static int print_data(int ac, char **av) {
stlink_t* sl = NULL;
sl = stlink_v1_open(0, 1);
enum connect_type connect = CONNECT_NORMAL;
int freq = 0;

if (sl == NULL) {
if (under_reset) {
sl = stlink_open_usb(0, 2, NULL, 0);
} else {
sl = stlink_open_usb(0, 1, NULL, 0);
}
if (strcmp(av[1], "--version") == 0) {
printf("v%s\n", STLINK_VERSION);
return(0);
}

return(sl);
}
for (int i=2; i<ac; i++) {

if (strcmp(av[i], "--connect-under-reset") == 0) {
connect = CONNECT_UNDER_RESET;
continue;
} else if (strcmp(av[i], "--hot-plug") == 0) {
connect = CONNECT_HOT_PLUG;
continue;
} else if (strcmp(av[i], "--freq") == 0) {
if (++i < ac) {
freq = arg_parse_freq(av[i]);
if (freq >= 0) { continue; }
}
} else if (strncmp(av[i], "--freq=", 7) == 0) {
freq = arg_parse_freq(av[i] + 7);
if (freq >= 0) { continue; }
}

static int print_data(int ac, char **av) {
stlink_t* sl = NULL;
bool under_reset = false;
printf("Incorrect argument: %s\n\n", av[i]);
usage();
return(-1);
}

// probe needs all devices unclaimed
if (strcmp(av[1], "--probe") == 0) {
stlink_probe();
return(0);
} else if (strcmp(av[1], "--version") == 0) {
printf("v%s\n", STLINK_VERSION);
stlink_probe(connect, freq);
return(0);
}

if (ac == 3) {
if (strcmp(av[2], "--connect-under-reset") == 0) {
under_reset = true;
} else {
usage();
return(-1);
}
}

sl = stlink_open_first(under_reset);
// open first st-link device
sl = stlink_open_usb(0, connect, NULL, freq);

if (sl == NULL) { return(-1); }

Expand Down
Loading

0 comments on commit cfff13c

Please sign in to comment.