Skip to content

Commit

Permalink
Merge pull request #398 from xor-gate/refactoring
Browse files Browse the repository at this point in the history
Refactoring some code, merge st-probe and fixup #318 serial print
  • Loading branch information
texane committed Apr 16, 2016
2 parents 17e3571 + 0e22df9 commit 6fe3a02
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 124 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
INSTALL
Makefile.in
aclocal.m4
Expand Down
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
8 changes: 3 additions & 5 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ 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

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
Expand Down
63 changes: 54 additions & 9 deletions src/st-info.c
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -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 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)
static int print_data(stlink_t *sl, char **av)
{
int ret = 0;
if (strcmp(av[1], "--flash") == 0)
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down
48 changes: 0 additions & 48 deletions src/st-probe.c

This file was deleted.

3 changes: 2 additions & 1 deletion src/stlink-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 52 additions & 41 deletions src/stlink-usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -754,26 +755,37 @@ 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--){

while (cnt--) {
libusb_get_device_descriptor( list[cnt], &desc );
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;
libusb_open(list[cnt], &handle);
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{
continue;
}
}else{
break;
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)) {
struct libusb_device_handle *handle;

libusb_open(list[cnt], &handle);
sl->serial_size = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber,
(unsigned char *)sl->serial, sizeof(sl->serial));
libusb_close(handle);

if (sl->serial_size < 0)
continue;
if (serial == NULL)
break;
if (memcmp(serial, &sl->serial, sl->serial_size) == 0)
break;

continue;
}

if (desc.idProduct == USB_STLINK_PID) {
slu->protocoll = 1;
break;
Expand All @@ -784,23 +796,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;
}
}
Expand Down Expand Up @@ -837,8 +846,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);
Expand All @@ -852,24 +859,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;
}

Expand Down
Loading

0 comments on commit 6fe3a02

Please sign in to comment.