Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Added connect under reset to stlink_open_usb( ) #963

Merged
merged 3 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/stlink/tools/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ 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;
};

#define FLASH_OPTS_INITIALIZER {0, { 0 }, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
#define FLASH_OPTS_INITIALIZER {0, { 0 }, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

int flash_get_opts(struct flash_opts* o, int ac, char** av);

Expand Down
3 changes: 3 additions & 0 deletions src/tools/flash_opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ int flash_get_opts(struct flash_opts* o, int ac, char** av) {
if (result != 0) return bad_arg ("--flash");
else o->flash_size = (size_t) flash_size;
}
else if (strcmp(av[0],"--connect-under-reset")== 0){
o->connect_under_reset = true;
}
else {
break; // non-option found
}
Expand Down
39 changes: 27 additions & 12 deletions src/tools/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ static void usage(void)
puts("st-info --probe");
puts("st-info --serial");
puts("st-info --hla-serial");
puts("st-info --flash");
puts("st-info --pagesize");
puts("st-info --sram");
puts("st-info --chipid");
puts("st-info --descr");
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]");
}

/* Print normal or OpenOCD hla_serial with newline */
Expand Down Expand Up @@ -76,19 +76,25 @@ static void stlink_probe(void)
stlink_probe_usb_free(&stdevs, size);
}

static stlink_t *stlink_open_first(void)
static stlink_t *stlink_open_first(bool under_reset)
{
stlink_t* sl = NULL;
sl = stlink_v1_open(0, 1);
if (sl == NULL)
sl = stlink_open_usb(0, 1, NULL, 0);

if (sl == NULL) {
if (under_reset) {
sl = stlink_open_usb(0, 2, NULL, 0);
} else {
sl = stlink_open_usb(0, 1, NULL, 0);
}
}

return sl;
}

static int print_data(char **av)
static int print_data(int ac, char **av)
{
stlink_t* sl = NULL;
bool under_reset = false;

// Probe needs all devices unclaimed
if (strcmp(av[1], "--probe") == 0) {
Expand All @@ -99,7 +105,16 @@ static int print_data(char **av)
return 0;
}

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

sl = stlink_open_first(under_reset);

if (sl == NULL) {
return -1;
Expand Down Expand Up @@ -149,7 +164,7 @@ int main(int ac, char** av)
return -1;
}

err = print_data(av);
err = print_data(ac,av);

return err;
}
13 changes: 11 additions & 2 deletions src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ static stlink_backend_t _stlink_usb_backend = {
_stlink_usb_set_swdclk
};

stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq) {
stlink_t *stlink_open_usb(enum ugly_loglevel verbose, int reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq) {
stlink_t* sl = NULL;
struct stlink_libusb* slu = NULL;
int ret = -1;
Expand Down Expand Up @@ -1145,9 +1145,18 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[ST
break;
}

if (reset == 2) {
stlink_jtag_reset(sl,0);
if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) stlink_enter_swd_mode(sl);
stlink_force_debug(sl);
stlink_jtag_reset(sl,1);
usleep(10000);
}


if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) stlink_enter_swd_mode(sl);

if (reset) {
if (reset == 1) {
if ( sl->version.stlink_v > 1) stlink_jtag_reset(sl, 2);
stlink_reset(sl);
usleep(10000);
Expand Down
2 changes: 1 addition & 1 deletion src/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extern "C" {
* @retval NULL Error while opening the stlink
* @retval !NULL Stlink found and ready to use
*/
stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq);
stlink_t *stlink_open_usb(enum ugly_loglevel verbose, int reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq);
size_t stlink_probe_usb(stlink_t **stdevs[]);
void stlink_probe_usb_free(stlink_t **stdevs[], size_t size);

Expand Down
23 changes: 20 additions & 3 deletions tests/usb.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
#include <stdio.h>

#include <stlink.h>
#include <string.h>

static void usage(void)
{
puts("test-usb --reset");
puts("test-usb --no-reset");
}

int main(int ac, char** av) {
(void)ac;
(void)av;

stlink_t* sl;
struct stlink_reg regs;
int reset = 0;

if (ac == 2) {
if (strcmp(av[1], "--reset") == 0)
reset = 2;
if (strcmp(av[1], "--no-reset") == 0)
reset = 1;
}
if (reset == 0) {
usage();
return 0;
}

sl = stlink_open_usb(10, 1, NULL, 0);
sl = stlink_open_usb(10, reset, NULL, 0);
if (sl != NULL) {
printf("-- version\n");
stlink_version(sl);
Expand Down