kopia lustrzana https://github.com/stlink-org/stlink
* Merge st-probe tool into st-info
* Fixup #318 serial print in stlink probe with trailing zeros * Refactor stlink-usb.h with some doxygen-style comments * Refactor some pieces of stlink_open_usbpull/398/head
rodzic
a86d51b469
commit
db5f0297bf
|
@ -1,3 +1,4 @@
|
|||
build
|
||||
INSTALL
|
||||
Makefile.in
|
||||
aclocal.m4
|
||||
|
|
|
@ -72,16 +72,13 @@ target_link_libraries(st-flash ${PROJECT_NAME})
|
|||
add_executable(st-info src/st-info.c)
|
||||
target_link_libraries(st-info ${PROJECT_NAME})
|
||||
|
||||
add_executable(st-probe src/st-probe.c)
|
||||
target_link_libraries(st-probe ${PROJECT_NAME})
|
||||
|
||||
add_executable(st-util gdbserver/gdb-remote.c
|
||||
gdbserver/gdb-remote.h
|
||||
gdbserver/gdb-server.c
|
||||
gdbserver/gdb-server.h)
|
||||
target_link_libraries(st-util ${PROJECT_NAME})
|
||||
|
||||
install(TARGETS ${PROJECT_NAME} st-flash st-util st-info st-probe
|
||||
install(TARGETS ${PROJECT_NAME} st-flash st-util st-info
|
||||
RUNTIME DESTINATION bin
|
||||
ARCHIVE DESTINATION lib
|
||||
)
|
||||
|
|
|
@ -5,9 +5,9 @@ SUBDIRS = . $(MAYBE_GUI)
|
|||
AUTOMAKE_OPTIONS = subdir-objects
|
||||
|
||||
if MINGW
|
||||
bin_PROGRAMS = st-flash st-util st-info st-probe
|
||||
bin_PROGRAMS = st-flash st-util st-info
|
||||
else
|
||||
bin_PROGRAMS = st-flash st-util st-term st-info st-probe
|
||||
bin_PROGRAMS = st-flash st-util st-term st-info
|
||||
endif
|
||||
|
||||
noinst_LIBRARIES = libstlink.a
|
||||
|
@ -15,15 +15,13 @@ noinst_LIBRARIES = libstlink.a
|
|||
st_flash_SOURCES = flash/main.c
|
||||
st_term_SOURCES = src/st-term.c
|
||||
st_info_SOURCES = src/st-info.c
|
||||
st_probe_SOURCES = src/st-probe.c
|
||||
st_util_SOURCES = gdbserver/gdb-remote.c gdbserver/gdb-remote.h gdbserver/gdb-server.c mingw/mingw.c mingw/mingw.h
|
||||
|
||||
CFILES = \
|
||||
src/stlink-common.c \
|
||||
src/stlink-usb.c \
|
||||
src/stlink-sg.c \
|
||||
src/uglylogging.c \
|
||||
src/st-info.c
|
||||
src/uglylogging.c
|
||||
|
||||
if !MINGW
|
||||
CFILES += src/st-term.c
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
/* simple wrapper around the stlink_flash_write function */
|
||||
|
||||
// TODO - this should be done as just a simple flag to the st-util command line...
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include "stlink-common.h"
|
||||
|
||||
#include <stlink-common.h>
|
||||
|
||||
static void usage(void)
|
||||
{
|
||||
|
@ -16,9 +12,52 @@ static void usage(void)
|
|||
puts("st-info --descr");
|
||||
puts("st-info --pagesize");
|
||||
puts("st-info --chipid");
|
||||
puts("st-info --serial");
|
||||
puts("st-info --probe");
|
||||
}
|
||||
|
||||
static int print_data(stlink_t* sl, char** av)
|
||||
static void stlink_print_info(stlink_t *sl)
|
||||
{
|
||||
const chip_params_t *params = NULL;
|
||||
|
||||
if (!sl)
|
||||
return;
|
||||
|
||||
for (int n = 0; n < sl->serial_size; n++)
|
||||
printf("%02x", sl->serial[n]);
|
||||
printf("\n");
|
||||
|
||||
printf("\t flash: %zu (pagesize: %zu)\n", sl->flash_size, sl->flash_pgsz);
|
||||
printf("\t sram: %zu\n", sl->sram_size);
|
||||
printf("\tchipid: 0x%.4x\n", sl->chip_id);
|
||||
|
||||
for (size_t i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
|
||||
if (devices[i].chip_id == sl->chip_id) {
|
||||
params = &devices[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (params)
|
||||
printf("\t descr: %s\n", params->description);
|
||||
}
|
||||
|
||||
static void stlink_probe(void)
|
||||
{
|
||||
stlink_t **stdevs;
|
||||
size_t size;
|
||||
|
||||
size = stlink_probe_usb(&stdevs);
|
||||
|
||||
printf("Found %zu stlink programmers\n", size);
|
||||
|
||||
for (size_t n = 0; n < size; n++)
|
||||
stlink_print_info(stdevs[n]);
|
||||
|
||||
stlink_probe_usb_free(&stdevs, size);
|
||||
}
|
||||
|
||||
static int print_data(stlink_t *sl, char **av)
|
||||
{
|
||||
int ret = 0;
|
||||
if (strcmp(av[1], "--flash") == 0)
|
||||
|
@ -29,7 +68,13 @@ static int print_data(stlink_t* sl, char** av)
|
|||
printf("0x%zx\n", sl->flash_pgsz);
|
||||
else if (strcmp(av[1], "--chipid") == 0)
|
||||
printf("0x%.4x\n", sl->chip_id);
|
||||
else if (strcmp(av[1], "--descr")==0) {
|
||||
else if (strcmp(av[1], "--probe") == 0)
|
||||
stlink_probe();
|
||||
else if (strcmp(av[1], "--serial") == 0) {
|
||||
for (int n = 0; n < sl->serial_size; n++)
|
||||
printf("%02x", sl->serial[n]);
|
||||
printf("\n");
|
||||
} else if (strcmp(av[1], "--descr") == 0) {
|
||||
const chip_params_t *params = NULL;
|
||||
for (size_t i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
|
||||
if(devices[i].chip_id == sl->chip_id) {
|
||||
|
@ -51,7 +96,7 @@ stlink_t* open_sl(void)
|
|||
stlink_t* sl;
|
||||
sl = stlink_v1_open(0, 1);
|
||||
if (sl == NULL)
|
||||
sl = stlink_open_usb(0, 1, NULL);
|
||||
sl = stlink_open_usb(0, 1, NULL);
|
||||
return sl;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include "stlink-common.h"
|
||||
|
||||
void stlink_print_info(stlink_t *sl)
|
||||
{
|
||||
const chip_params_t *params = NULL;
|
||||
|
||||
if (!sl)
|
||||
return;
|
||||
|
||||
for (size_t n = 0; n < sizeof(sl->serial); n++)
|
||||
printf("%02x", sl->serial[n]);
|
||||
printf("\n");
|
||||
|
||||
printf("\t flash: %zu (pagesize: %zu)\n", sl->flash_size, sl->flash_pgsz);
|
||||
printf("\t sram: %zu\n", sl->sram_size);
|
||||
printf("\tchipid: 0x%.4x\n", sl->chip_id);
|
||||
|
||||
for (size_t i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
|
||||
if(devices[i].chip_id == sl->chip_id) {
|
||||
params = &devices[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (params)
|
||||
printf("\t descr: %s\n", params->description);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
stlink_t **stdevs;
|
||||
size_t size;
|
||||
|
||||
size = stlink_probe_usb(&stdevs);
|
||||
|
||||
printf("Found %zu stlink programmers\n", size);
|
||||
|
||||
for (size_t n = 0; n < size; n++)
|
||||
stlink_print_info(stdevs[n]);
|
||||
|
||||
stlink_probe_usb_free(&stdevs, size);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
|
@ -641,7 +641,8 @@ extern "C" {
|
|||
uint32_t chip_id;
|
||||
int core_stat;
|
||||
|
||||
char serial[13];
|
||||
char serial[16];
|
||||
int serial_size;
|
||||
|
||||
#define STM32_FLASH_PGSZ 1024
|
||||
#define STM32L_FLASH_PGSZ 256
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
#include "stlink-common.h"
|
||||
#include "stlink-usb.h"
|
||||
#include "uglylogging.h"
|
||||
|
||||
enum SCSI_Generic_Direction {SG_DXFER_TO_DEV=0, SG_DXFER_FROM_DEV=0x80};
|
||||
|
||||
|
@ -714,17 +713,19 @@ stlink_backend_t _stlink_usb_backend = {
|
|||
_stlink_usb_target_voltage
|
||||
};
|
||||
|
||||
|
||||
stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) {
|
||||
stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16])
|
||||
{
|
||||
stlink_t* sl = NULL;
|
||||
struct stlink_libusb* slu = NULL;
|
||||
int error = -1;
|
||||
int ret = -1;
|
||||
int config;
|
||||
|
||||
sl = calloc(1, sizeof (stlink_t));
|
||||
slu = calloc(1, sizeof (struct stlink_libusb));
|
||||
if (sl == NULL) goto on_malloc_error;
|
||||
if (slu == NULL) goto on_malloc_error;
|
||||
if (sl == NULL)
|
||||
goto on_malloc_error;
|
||||
if (slu == NULL)
|
||||
goto on_malloc_error;
|
||||
|
||||
ugly_init(verbose);
|
||||
sl->backend = &_stlink_usb_backend;
|
||||
|
@ -754,26 +755,32 @@ stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) {
|
|||
devAddr=atoi(c);
|
||||
ILOG("bus %03d dev %03d\n",devBus, devAddr);
|
||||
}
|
||||
|
||||
while (cnt--){
|
||||
libusb_get_device_descriptor( list[cnt], &desc );
|
||||
if (desc.idVendor!=USB_ST_VID) continue;
|
||||
if (desc.idVendor != USB_ST_VID)
|
||||
continue;
|
||||
|
||||
if (devBus && devAddr)
|
||||
if ((libusb_get_bus_number(list[cnt])!=devBus) || (libusb_get_device_address(list[cnt])!=devAddr)) continue;
|
||||
if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ){
|
||||
if ((p_usb_iserial != NULL)){
|
||||
struct libusb_device_handle* handle;
|
||||
if ((libusb_get_bus_number(list[cnt])!=devBus) || (libusb_get_device_address(list[cnt])!=devAddr))
|
||||
continue;
|
||||
|
||||
if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ) {
|
||||
if ((serial != NULL)) {
|
||||
struct libusb_device_handle *handle;
|
||||
|
||||
libusb_open(list[cnt], &handle);
|
||||
libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char *)sl->serial, sizeof(sl->serial));
|
||||
sl->serial_size = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char *)sl->serial, sizeof(sl->serial));
|
||||
libusb_close(handle);
|
||||
if (memcmp(p_usb_iserial,&sl->serial, sizeof(sl->serial) - 1) == 0){
|
||||
break;
|
||||
}else{
|
||||
if (sl->serial_size < 0)
|
||||
continue;
|
||||
}
|
||||
}else{
|
||||
else if (memcmp(serial, &sl->serial, sl->serial_size) == 0)
|
||||
break;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (desc.idProduct == USB_STLINK_PID) {
|
||||
slu->protocoll = 1;
|
||||
break;
|
||||
|
@ -784,23 +791,20 @@ stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) {
|
|||
WLOG ("Couldn't find %s ST-Link/V2 devices\n",(devBus && devAddr)?"matched":"any");
|
||||
goto on_error;
|
||||
} else {
|
||||
int error = libusb_open(list[cnt], &slu->usb_handle);
|
||||
if( error !=0 ) {
|
||||
WLOG("Error %d (%s) opening ST-Link/V2 device %03d:%03d\n",
|
||||
error, strerror (errno), libusb_get_bus_number(list[cnt]), libusb_get_device_address(list[cnt]));
|
||||
ret = libusb_open(list[cnt], &slu->usb_handle);
|
||||
if (ret != 0) {
|
||||
WLOG("Error %d (%s) opening ST-Link/V2 device %03d:%03d\n",
|
||||
ret, strerror (errno), libusb_get_bus_number(list[cnt]), libusb_get_device_address(list[cnt]));
|
||||
goto on_error;
|
||||
}
|
||||
}
|
||||
|
||||
libusb_free_device_list(list, 1);
|
||||
|
||||
|
||||
if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
|
||||
int r;
|
||||
|
||||
r = libusb_detach_kernel_driver(slu->usb_handle, 0);
|
||||
if (r<0) {
|
||||
WLOG("libusb_detach_kernel_driver(() error %s\n", strerror(-r));
|
||||
ret = libusb_detach_kernel_driver(slu->usb_handle, 0);
|
||||
if (ret < 0) {
|
||||
WLOG("libusb_detach_kernel_driver(() error %s\n", strerror(-ret));
|
||||
goto on_libusb_error;
|
||||
}
|
||||
}
|
||||
|
@ -837,8 +841,6 @@ stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) {
|
|||
// TODO - never used at the moment, always CMD_SIZE
|
||||
slu->cmd_len = (slu->protocoll == 1)? STLINK_SG_SIZE: STLINK_CMD_SIZE;
|
||||
|
||||
/* success */
|
||||
|
||||
if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
|
||||
ILOG("-- exit_dfu_mode\n");
|
||||
stlink_exit_dfu_mode(sl);
|
||||
|
@ -852,24 +854,28 @@ stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) {
|
|||
stlink_reset(sl);
|
||||
usleep(10000);
|
||||
}
|
||||
|
||||
stlink_version(sl);
|
||||
error = stlink_load_device_params(sl);
|
||||
ret = stlink_load_device_params(sl);
|
||||
|
||||
on_libusb_error:
|
||||
if (error == -1) {
|
||||
if (ret == -1) {
|
||||
stlink_close(sl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* success */
|
||||
return sl;
|
||||
|
||||
on_error:
|
||||
if( slu->libusb_ctx)
|
||||
if (slu->libusb_ctx)
|
||||
libusb_exit(slu->libusb_ctx);
|
||||
|
||||
on_malloc_error:
|
||||
if (sl != NULL) free(sl);
|
||||
if (slu != NULL) free(slu);
|
||||
if (sl != NULL)
|
||||
free(sl);
|
||||
if (slu != NULL)
|
||||
free(slu);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,15 +6,18 @@
|
|||
*/
|
||||
|
||||
#ifndef STLINK_USB_H
|
||||
#define STLINK_USB_H
|
||||
#define STLINK_USB_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <libusb.h>
|
||||
|
||||
#include "stlink-common.h"
|
||||
#include "uglylogging.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <libusb.h>
|
||||
#include "stlink-common.h"
|
||||
|
||||
#define STLINK_SG_SIZE 31
|
||||
#define STLINK_CMD_SIZE 16
|
||||
|
||||
|
@ -28,13 +31,21 @@ extern "C" {
|
|||
unsigned int cmd_len;
|
||||
};
|
||||
|
||||
stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial);
|
||||
/**
|
||||
* Open a stlink
|
||||
* @param verbose Verbosity loglevel
|
||||
* @param reset Reset stlink programmer
|
||||
* @param serial Serial number to search for, when NULL the first stlink found is opened (binary format)
|
||||
* @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[16]);
|
||||
size_t stlink_probe_usb(stlink_t **stdevs[]);
|
||||
void stlink_probe_usb_free(stlink_t **stdevs[], size_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* STLINK_USB_H */
|
||||
#endif /* STLINK_USB_H */
|
||||
|
||||
|
|
|
@ -9,14 +9,16 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define UDEBUG 90
|
||||
#define UINFO 50
|
||||
#define UWARN 30
|
||||
#define UERROR 20
|
||||
#define UFATAL 10
|
||||
enum ugly_loglevel {
|
||||
UDEBUG = 90,
|
||||
UINFO = 50,
|
||||
UWARN = 30,
|
||||
UERROR = 20,
|
||||
UFATAL = 10
|
||||
};
|
||||
|
||||
int ugly_init(int maximum_threshold);
|
||||
int ugly_log(int level, const char *tag, const char *format, ...);
|
||||
int ugly_init(int maximum_threshold);
|
||||
int ugly_log(int level, const char *tag, const char *format, ...);
|
||||
|
||||
#define DLOG(format, args...) ugly_log(UDEBUG, __FILE__, format, ## args)
|
||||
#define ILOG(format, args...) ugly_log(UINFO, __FILE__, format, ## args)
|
||||
|
|
Ładowanie…
Reference in New Issue