* Merge st-probe tool into st-info
authorJerry Jacobs <jerry.jacobs@dualinventive.com>
Fri, 15 Apr 2016 21:09:45 +0000 (23:09 +0200)
committerJerry Jacobs <jerry.jacobs@dualinventive.com>
Fri, 15 Apr 2016 21:09:45 +0000 (23:09 +0200)
* 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_usb

.gitignore
CMakeLists.txt
Makefile.am
src/st-info.c
src/st-probe.c [deleted file]
src/stlink-common.h
src/stlink-usb.c
src/stlink-usb.h
src/uglylogging.h

index 09025703733b2850b0ba91acde3d6789864ee737..1c8296454b33e99a406d8ef91f3d22e06bfcd414 100644 (file)
@@ -1,3 +1,4 @@
+build
 INSTALL
 Makefile.in
 aclocal.m4
index 659218f7443448c19af1ea7896cea1a27d0607ba..cd329bb18e5fb32b4f2d4b027762eceaf0059d3a 100644 (file)
@@ -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
 )
index 9a0b387e21085eb2227f86383aa6fa7c3f40bb8a..e8927bd4a6d825a5d7dc451c74f60cc954b2cc51 100644 (file)
@@ -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
index 66d9fb391776556a0d2f30bf291b1d2709404dfc..2cea40e5c2b602a45aabde482d48f646e68df085 100644 (file)
@@ -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 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)
@@ -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;
 }
 
diff --git a/src/st-probe.c b/src/st-probe.c
deleted file mode 100644 (file)
index b58bb7a..0000000
+++ /dev/null
@@ -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;
-}
index 2b2d09b1a32417949a91ab765b99ad7cffdb0eb1..e2f7b75c2d5923e195f5f323eeaabe1b83874edb 100644 (file)
@@ -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
index 36ac9687226cc40935191ac8407b1af0154ed3be..6125ec3fa570491f67f060f3c7180bf9fdbce0e0 100644 (file)
@@ -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:
-    ifslu->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;
 }
 
index 801cafad3232b9f333b9fec5ac2e453584d74183..747d54c7480bdfb1d815c13657099d833b0299fa 100644 (file)
@@ -6,14 +6,17 @@
  */
 
 #ifndef STLINK_USB_H
-#define        STLINK_USB_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
+#define STLINK_USB_H
 
+#include <stdbool.h>
 #include <libusb.h>
+
 #include "stlink-common.h"
+#include "uglylogging.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 #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 */
 
index 7099d919ca5c943a6cea164440f4e878b010fff3..df816caedcf7dbc89382c752ee706aff0c77870f 100644 (file)
@@ -9,14 +9,16 @@
 extern "C" {
 #endif
 
-#define UDEBUG 90
-#define UINFO  50
-#define UWARN  30
-#define UERROR 20
-#define UFATAL 10
-
-    int ugly_init(int maximum_threshold);
-    int ugly_log(int level, const char *tag, const char *format, ...);
+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, ...);
 
 #define DLOG(format, args...)   ugly_log(UDEBUG, __FILE__, format, ## args)
 #define ILOG(format, args...)   ugly_log(UINFO, __FILE__, format, ## args)