-# make ... for both libusb and libsg
-#
-# make CONFIG_USE_LIBSG=0 ...
-# for just libusb
-#
+# make ... for both stlink v1 and stlink v2 support
+##
VPATH=src
-SOURCES_LIB=stlink-common.c stlink-usb.c
+SOURCES_LIB=stlink-common.c stlink-usb.c stlink-sg.c uglylogging.c
OBJS_LIB=$(SOURCES_LIB:.c=.o)
-TEST_PROGRAMS=test_usb
+TEST_PROGRAMS=test_usb test_sg
- LDFLAGS=-lusb-1.0 -L. -lstlink
+ LDFLAGS=-L. -lstlink -lusb-1.0
-ifeq ($(CONFIG_USE_LIBSG),)
-CONFIG_USE_LIBSG=1
-endif
-
-ifneq ($(CONFIG_USE_LIBSG),0)
-SOURCES_LIB+=stlink-sg.c
-CFLAGS+=-DCONFIG_USE_LIBSG=1
-LDFLAGS+=-lsgutils2
-TEST_PROGRAMS+=test_sg
-endif
-
CFLAGS+=-g
-CFLAGS+=-DCONFIG_USE_LIBUSB=1
CFLAGS+=-DDEBUG=1
CFLAGS+=-std=gnu99
CFLAGS+=-Wall -Wextra
#include "stlink-common.h"
+#include "uglylogging.h"
-void D(stlink_t *sl, char *txt) {
- if (sl->verbose > 1)
- fputs(txt, stderr);
-}
-
-void DD(stlink_t *sl, char *format, ...) {
- if (sl->verbose > 0) {
- va_list list;
- va_start(list, format);
- vfprintf(stderr, format, list);
- va_end(list);
- }
-}
-
+#define LOG_TAG __FILE__
+#define DLOG(format, args...) ugly_log(UDEBUG, LOG_TAG, format, ## args)
+#define ILOG(format, args...) ugly_log(UINFO, LOG_TAG, format, ## args)
+#define WLOG(format, args...) ugly_log(UWARN, LOG_TAG, format, ## args)
+#define fatal(format, args...) ugly_log(UFATAL, LOG_TAG, format, ## args)
+ /* todo: stm32l15xxx flash memory, pm0062 manual */
- /* FPEC flash controller interface, pm0063 manual
- */
+ /* stm32f FPEC flash controller interface, pm0063 manual */
#define FLASH_REGS_ADDR 0x40022000
#define FLASH_REGS_SIZE 0x28
sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
- /* flash memory settings */
- sl->flash_base = STM32_FLASH_BASE;
- sl->flash_size = STM32_FLASH_SIZE;
- sl->flash_pgsz = STM32_FLASH_PGSZ;
-
- /* system memory */
- sl->sys_base = STM32_SYSTEM_BASE;
- sl->sys_size = STM32_SYSTEM_SIZE;
-
- /* sram memory settings */
- sl->sram_base = STM32_SRAM_BASE;
- sl->sram_size = STM32L_SRAM_SIZE;
-
if (libusb_init(&(slu->libusb_ctx))) {
- fprintf(stderr, "failed to init libusb context, wrong version of libraries?\n");
- goto on_error;
+ WLOG("failed to init libusb context, wrong version of libraries?\n");
+ goto on_error;
}
- count = libusb_get_device_list(slu->libusb_ctx, &devs);
- if (count < 0) {
- printf("libusb_get_device_list\n");
- goto on_libusb_error;
- }
-
- for (i = 0; i < count; ++i) {
- dev = devs[i];
- slu->protocoll = is_stlink_device(dev, 0);
- if (slu->protocoll > 0) break;
- }
- if (i == count) goto on_libusb_error;
-
- if (libusb_open(dev, &(slu->usb_handle))) {
- printf("libusb_open()\n");
- goto on_libusb_error;
+ slu->usb_handle = libusb_open_device_with_vid_pid(slu->libusb_ctx, USB_ST_VID, USB_STLINK_32L_PID);
+ if (slu->usb_handle == NULL) {
+ // TODO - free usb context too...
+ free(slu);
+ WLOG("Couldn't find any ST-Link/V2 devices");
+ return NULL;
}
- if (iSerial) {
- unsigned char serial[256];
- struct libusb_device_descriptor desc;
- int r;
-
- r = libusb_get_device_descriptor(dev, &desc);
- if (r<0) {
- printf("Can't get descriptor to match Iserial\n");
- goto on_libusb_error;
- }
- r = libusb_get_string_descriptor_ascii
- (slu->usb_handle, desc.iSerialNumber, serial, 256);
- if (r<0) {
- printf("Can't get Serialnumber to match Iserial\n");
- goto on_libusb_error;
- }
- if (strcmp((char*)serial, iSerial)) {
- printf("Mismatch in serial numbers, dev %s vs given %s\n",
- serial, iSerial);
- goto on_libusb_error;
- }
- }
-
if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
int r;
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);
}
+
+ if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) {
+ stlink_enter_swd_mode(sl);
+ }
+
stlink_version(sl);
+
+ /* per device family initialization */
+ stlink_core_id(sl);
+ if (sl->core_id == STM32L_CORE_ID) {
+
+ /* flash memory settings */
+ sl->flash_base = STM32_FLASH_BASE;
+ sl->flash_size = STM32_FLASH_SIZE;
+ sl->flash_pgsz = STM32L_FLASH_PGSZ;
+
+ /* system memory */
+ sl->sys_base = STM32_SYSTEM_BASE;
+ sl->sys_size = STM32_SYSTEM_SIZE;
+
+ /* sram memory settings */
+ sl->sram_base = STM32_SRAM_BASE;
+ sl->sram_size = STM32L_SRAM_SIZE;
+
+ } else if (sl->core_id == STM32VL_CORE_ID) {
+
+ /* flash memory settings */
+ sl->flash_base = STM32_FLASH_BASE;
+ sl->flash_size = STM32_FLASH_SIZE;
+ sl->flash_pgsz = STM32_FLASH_PGSZ;
+
+ /* system memory */
+ sl->sys_base = STM32_SYSTEM_BASE;
+ sl->sys_size = STM32_SYSTEM_SIZE;
+
+ /* sram memory settings */
+ sl->sram_base = STM32_SRAM_BASE;
+ sl->sram_size = STM32_SRAM_SIZE;
+
+ } else {
+
+ fprintf(stderr, "unknown coreid: %x\n", sl->core_id);
+ goto on_libusb_error;
+
+ }
+
error = 0;
on_libusb_error: