Merge branch 'master' of https://github.com/texane/stlink
authorKarl Palsson <karlp@tweak.net.au>
Fri, 4 Nov 2011 02:51:15 +0000 (02:51 +0000)
committerKarl Palsson <karlp@tweak.net.au>
Fri, 4 Nov 2011 02:51:15 +0000 (02:51 +0000)
Conflicts:
Makefile
doc/tutorial/tutorial.pdf
example/blink/Makefile
flash/main.c
src/stlink-common.c
src/stlink-usb.c

Mostly whitespace conflicts it seems.

35 files changed:
.gitignore
10-stlink.rules [deleted file]
49-stlinkv1.rules [new file with mode: 0644]
49-stlinkv2.rules [new file with mode: 0644]
49-stm32l-discovery.rules [deleted file]
Makefile
README
doc/tutorial/tutorial.pdf
doc/tutorial/tutorial.tex
example/blink/Makefile
example/blink/default_bootloader.bin [deleted file]
example/blink/disasm.sh [deleted file]
example/blink/main.c
example/blink/o.bin [deleted file]
example/lcd/Makefile
example/lcd/stm32l_discovery_lcd.c
example/lcd/stm32l_discovery_lcd.h
flash/main.c
gdbserver/gdb-server.c
nbproject/Package-Default.bash [new file with mode: 0644]
nbproject/Package-gdbserver.bash [deleted file]
nbproject/Package-library_cli.bash [deleted file]
nbproject/configurations.xml
nbproject/project.xml
src/stlink-common.c
src/stlink-common.h
src/stlink-sg.c
src/stlink-sg.h
src/stlink-usb.c
src/stlink-usb.h
src/test_sg.c
src/uglylogging.c [new file with mode: 0644]
src/uglylogging.h [new file with mode: 0644]
stlink.modprobe.conf [deleted file]
stlink_v1.modprobe.conf [new file with mode: 0644]

index 44d2e4ddf67af28c37518032934fa9acb818bbf0..0a541c78bc18ec24dcbef429eab3dad0f13e0983 100644 (file)
@@ -1,6 +1,9 @@
 /nbproject/private/
 *.o
+*.elf
+doc/tutorial/*.log
+doc/tutorial/*.aux
 libstlink.a
 test_usb
 test_sg
-gdbserver/st-utils
\ No newline at end of file
+gdbserver/st-utils
diff --git a/10-stlink.rules b/10-stlink.rules
deleted file mode 100644 (file)
index 2413acd..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-# This file was taken from arm-utilites project, located at
-# http://code.google.com/p/arm-utilities/, which is licensed under GPL3.
-# An explicit permission to include any code from that project in
-# this one under BSD license was granted by arm-utilites author, Donald Becker.
-
-# This file watches for a STMicro ST-Link or STM32VLDiscovery board
-# and creates a device named /dev/stlink
-# See udev(7) for syntax.
-#
-# Written 2010,2011 by Donald Becker
-#
-# The STLink on the Discovery has a USB ID 0483:3744 and presents itself
-# as a mass storage (i.e. SCSI) device.  The SCSI emulation is signficantly
-# broken, and the kernel spews error reports for a while until it is
-# accepted.  Further problems are encountered when if it is automatically
-# mounted.
-#
-# Options that may prevent the mount are
-# ENV{UDISKS_PRESENTATION_HIDE}:="1",
-# ENV{UDISKS_PRESENTATION_NOPOLICY}:="1",
-# ENV{DM_UDEV_DISABLE_DISK_RULES_FLAG}:="1"
-
-ACTION!="add|change", GOTO="stlink_rules_end"
-
-SUBSYSTEMS=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3744", \
-   MODE="0664", GROUP="tape", NAME="stlinkusb%n", ENV{STLINK}="1", \
-   ENV{UDISKS_PRESENTATION_HIDE}:="1", \
-   ENV{UDISKS_PRESENTATION_NOPOLICY}:="1", \
-   ENV{DM_UDEV_DISABLE_DISK_RULES_FLAG}:="1", \
-   OPTIONS="last_rule"
-
-# Other possible settings:
-# OPTIONS="last_rule", ATTRS{vendor}=="STM32"
-
-KERNEL=="sg[0-9]*", MODE="0664", GROUP:="tape", \
-  NAME+="stlink-sg%n", SYMLINK+="stlink", \
-  ENV{UDISKS_PRESENTATION_HIDE}:="1", \
-  ENV{UDISKS_PRESENTATION_NOPOLICY}:="1", \
-  ENV{DM_UDEV_DISABLE_DISK_RULES_FLAG}:="1"
-
-SUBSYSTEM=="scsi", ATTR{vendor}=="STM32", MODE="0664", GROUP="tape", NAME="stlinksg-scsi%n", SYMLINK+="stlinkscsi", OPTIONS="last_rule"
-
-LABEL="stlink_rules_end"
diff --git a/49-stlinkv1.rules b/49-stlinkv1.rules
new file mode 100644 (file)
index 0000000..d474d6a
--- /dev/null
@@ -0,0 +1,11 @@
+# stm32 discovery boards, with onboard st/linkv1
+# ie, STM32VL
+
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3744", \
+    MODE:="0666", \
+    SYMLINK+="stlinkv1_%n"
+
+# If you share your linux system with other users, or just don't like the
+# idea of write permission for everybody, you can replace MODE:="0666" with
+# OWNER:="yourusername" to create the device owned by you, or with
+# GROUP:="somegroupname" and mange access using standard unix groups.
diff --git a/49-stlinkv2.rules b/49-stlinkv2.rules
new file mode 100644 (file)
index 0000000..a11215c
--- /dev/null
@@ -0,0 +1,12 @@
+# stm32 discovery boards, with onboard st/linkv2
+# ie, STM32L, STM32F4.
+# STM32VL has st/linkv1, which is quite different
+
+SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", \
+    MODE:="0666", \
+    SYMLINK+="stlinkv2_%n"
+
+# If you share your linux system with other users, or just don't like the
+# idea of write permission for everybody, you can replace MODE:="0666" with
+# OWNER:="yourusername" to create the device owned by you, or with
+# GROUP:="somegroupname" and mange access using standard unix groups.
diff --git a/49-stm32l-discovery.rules b/49-stm32l-discovery.rules
deleted file mode 100644 (file)
index 1f3ce77..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-# stm32l discovery board, with onboard st/linkv2
-#
-SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", \
-    MODE:="0666", \
-    SYMLINK+="stm32l_stlink%n"
-#
-# If you share your linux system with other users, or just don't like the
-# idea of write permission for everybody, you can replace MODE:="0666" with
-# OWNER:="yourusername" to create the device owned by you, or with
-# GROUP:="somegroupname" and mange access using standard unix groups.
index 943302e0487fdfcb20358f82179e05d5137464db..d35b3c4f1b57739972fdabb0ba8ec86e0ee34532 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,28 +1,13 @@
-# 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=-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
@@ -63,7 +48,7 @@ distclean: clean
        $(MAKE) -C gdbserver clean
        
 flash:
-       $(MAKE) -C flash CONFIG_USE_LIBSG="$(CONFIG_USE_LIBSG)" 
+       $(MAKE) -C flash
 
 gdbserver:
        $(MAKE) -C gdbserver
diff --git a/README b/README
index 429b8511f7dc7060a6a522279ad3fe0b379b635d..470c52a5977741d648e0660201dd417efe74116b 100644 (file)
--- a/README
+++ b/README
@@ -11,22 +11,47 @@ called stlink and there are 2 versions:
 . STLINKv1 uses SCSI passthru commands over USB,
 . STLINKv2 uses raw USB commands.
 
-It means that if you are using a STM32VL board, you have to install and load
-SCSI related software. First, load the sg kernel module:
-# modprobe sg
+Common requirements
+~~~~~~~~~~~~~~~~~~~
 
-Then, you need to install the package libsgutils2-dev. On Ubuntu:
-# sudo apt-get install libsgutils2-dev
+libusb-1.0  (You probably already have this, but you'll need the 
+development version to compile)
 
-LIBUSB is required for both cases.
+IF YOU HAVE AN STLINKv1
+~~~~~~~~~~~~~~~~~~~~~~~
+The STLINKv1's SCSI emulation is very broken, so the best thing to do
+is tell your operating system to completely ignore it.
+
+Options (do one of these before you plug it in)
+   *) modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:i
+or *)1. add "options usb-storage quirks=483:3744:i" to /etc/modprobe.conf
+   *)2. modprobe -r usb-storage && modprobe usb-storage
+or *)1. cp stlink_v1.modprobe.conf /etc/modprobe.d
+   *)2. modprobe -r usb-storage && modprobe usb-storage
+
+IF YOU HAVE AN STLINKv2
+~~~~~~~~~~~~~~~~~~~~~~~
+
+You're ready to go :)
 
 To run the gdb server, do (you do not need sudo if you have set up
 permissions correctly):
-$ make -C build && sudo ./build/st-util [/dev/sgX]
+$ make && [sudo] ./gdbserver/st-util 
+
+There are a few options:
+
+./gdbserver/st-util - usage:
 
-Currently, the GDB server listening port is hardcoded to 4242:
+  -h, --help        Print this help
+  -vXX, --verbose=XX    specify a specific verbosity level (0..99)
+  -v, --verbose specify generally verbose logging
+  -s X, --stlink_version=X
+            Choose what version of stlink to use, (defaults to 2)
+  -1, --stlinkv1    Force stlink version 1
+  -p 4242, --listen_port=1234
+            Set the gdb server listen port. (default port: 4242)
 
-Then, in gdb:
+Then, in gdb: (remember, you need to run an _ARM_ gdb, not an x86 gdb)
 (gdb) target remote :4242
 
 Have fun!
@@ -50,13 +75,14 @@ for GDB.
 Setting up udev rules
 =====================
 
-For convenience, you may install udev rules file, 10-stlink.rules, located
+For convenience, you may install udev rules file, 49-stlinkv*.rules, located
 in the root of repository. You will need to copy it to /etc/udev/rules.d,
 and then either reboot or execute
 $ udevadm control --reload-rules
 
-Udev will now create a /dev/stlink file, which will point at appropriate
-/dev/sgX device. Good to not accidentally start debugging your flash drive.
+Udev will now create a /dev/stlinkv2_XX or /dev/stlinkv1_XX file, with the appropriate permissions.
+This is currently all the device is for, (only one stlink of each version is supported at 
+any time presently)
 
 Running programs from SRAM
 ==========================
index 648138732a728963e6a538c2a9a5825a42055e45..856977bc7ce42517c50a390be589c18d46fb9b8e 100644 (file)
Binary files a/doc/tutorial/tutorial.pdf and b/doc/tutorial/tutorial.pdf differ
index 0437119c1351ed910d1d02a78078b0ab6f80c602..c3615df7818a1dad9f4d08a35ae8639217d9b42d 100644 (file)
@@ -24,7 +24,7 @@
 \section{Overview}
 \paragraph{}
 This guide details the use of STMicroelectronics STM32 discovery kits in
-an opensource environment.
+an open source environment.
 
 
 \newpage
@@ -48,18 +48,18 @@ This documentation assumes the toolchains is installed in a \$TOOLCHAIN\_PATH.
 
 \section{Installing STLINK}
 \paragraph{}
-STLINK is an opensource software to program and debug the discovery kits. Those
+STLINK is open source software to program and debug ST's STM32 Discovery kits. Those
 kits have an onboard chip that translates USB commands sent by the host PC into
-JTAG commands. This chip is called STLINK, which is confusing since the software
-has the same name. It comes into 2 versions (STLINK v1 and v2). From a software
+JTAG/SWD commands. This chip is called STLINK, (yes, isn't that confusing? suggest a better
+name!)  and comes in 2 versions (STLINK v1 and v2). From a software
 point of view, those versions differ only in the transport layer used to communicate
-(v1 uses SCSI passthru commands, while v2 uses raw USB).
+(v1 uses SCSI passthru commands, while v2 uses raw USB).  From a user point of view, they 
+are identical. 
 
 \paragraph{}
 Before continuing, the following dependencies must be met:
 \begin{itemize}
 \item libusb-1.0
-\item libsgutils2 (optionnal)
 \end{itemize}
 
 \paragraph{}
@@ -78,7 +78,7 @@ Everything can be built from the top directory:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
 $> cd stlink.git
-$> make CONFIG_USE_LIBSG=0
+$> make 
 \end{lstlisting}
 \end{small}
 It includes:
@@ -95,23 +95,34 @@ It includes:
 A simple LED blinking example is provided in the example directory. It is built using:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
-# update the make option accordingly to your architecture
 cd stlink.git/example/blink ;
-PATH=$TOOLCHAIN_PATH/bin:$PATH make CONFIG_STM32L_DISCOVERY=1;
+PATH=$TOOLCHAIN_PATH/bin:$PATH make
 \end{lstlisting}
 \end{small}
+This builds three files, one for each of the Discovery boards currently
+available, linked to run from SRAM. (So no risk of overwriting anything you didn't mean to) 
+These blink examples can safely be used to verify that:
+
+\begin{itemize}
+\item Your installed toolchain is capable of compiling for cortex M3/M4 targets
+\item stlink is functional
+\item Your arm-none-eabi-gdb is functional
+\item Your board is functional
+\end{itemize}
 
 \paragraph{}
-A GDB server must be start to interact with the STM32. Depending on the discovery kit you
+A GDB server must be started to interact with the STM32. Depending on the discovery kit you
 are using, you must run one of the 2 commands:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
-# STM32VL discovery kit
-$> sudo ./st-util /dev/sg2
+# STM32VL discovery kit (onboard ST-link)
+$> ./st-util --stlinkv1
 
-# STM32L discovery kit
-# 2 dummy command line arguments needed, will be fixed soon
-$> sudo ./st-util fu bar
+# STM32L or STM32F4 discovery kit (onboard ST-link/V2)
+$> ./st-util 
+
+# Full help for other options (listen port, version)
+$> ./st-util --help
 \end{lstlisting}
 \end{small}
 
@@ -136,7 +147,8 @@ By default, the program was linked such that the base address is 0x20000000. Fro
 memory map, GDB knows this address belongs to SRAM. To load the program in SRAM, simply use:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
-$> load blink.elf
+$> # Choose one as appropriate for your Discovery kit
+$> load blink_32L.elf | load blink_32VL.elf | load blink_F4.elf
 \end{lstlisting}
 \end{small}
 
@@ -150,8 +162,7 @@ $> continue
 \end{small}
 
 \paragraph{}
-The board BLUE and GREEN leds should be blinking (those leds are near the user and reset buttons).
-
+All the LEDs on the board should now be blinking in time (those leds are near the user and reset buttons).
 
 \newpage
 \section{Building and flashing a program}
@@ -163,13 +174,13 @@ FLASH memory reading and writing is done by a separate tool, as shown below:\\
 $> cd stlink.git/flash ;
 
 # stlinkv1 command to read 4096 from flash into out.bin
-$> ./flash read /dev/sg2 out.bin 0x8000000 4096
+$> ./flash read v1 out.bin 0x8000000 4096
 
 # stlinkv2 command
 $> ./flash read out.bin 0x8000000 4096
 
 # stlinkv1 command to write the file in.bin into flash
-$> ./flash write /dev/sg2 in.bin 0x8000000
+$> ./flash write v1 in.bin 0x8000000
 
 # stlinkv2 command
 $> ./flash write in.bin 0x8000000
@@ -263,16 +274,6 @@ $> make
 \end{lstlisting}
 \end{small}
 
-\subsection{STM32VL support}
-\paragraph{}
-It seems support for STM32VL is quite broken. If it does not work, try build STLINK using libsg:
-\begin{small}
-\begin{lstlisting}[frame=tb]
-$> cd stlink.git
-$> make CONFIG_USE_LIBSG=1
-\end{lstlisting}
-\end{small}
-
 
 \newpage
 \section{References}
index 1906b9159e6f0e2b9cc6f22d8ef94e46170aa6d3..cf7221da81cb33c234d59978c9872f2d7e56a1c7 100644 (file)
@@ -1,35 +1,32 @@
-EXECUTABLE=blink.elf
-BIN_IMAGE=blink.bin
-
 CC=arm-none-eabi-gcc
 OBJCOPY=arm-none-eabi-objcopy
 
-CFLAGS=-g -O2 -mlittle-endian -mthumb
-ifeq ($(CONFIG_STM32L_DISCOVERY), 1)
-       CFLAGS+=-mcpu=cortex-m3 -DCONFIG_STM32L_DISCOVERY
-else ifeq ($(CONFIG_STM32VL_DISCOVERY), 1)
-       CFLAGS+=-mcpu=cortex-m3 -DCONFIG_STM32VL_DISCOVERY=1
-else ifeq ($(CONFIG_STM32F4_DISCOVERY), 1)
-       CFLAGS+=-mcpu=cortex-m4 -DCONFIG_STM32F4_DISCOVERY=1    
-endif
-CFLAGS+=-ffreestanding -nostdlib -nostdinc
+DEF_CFLAGS=-g -O2 -mlittle-endian -mthumb -ffreestanding -nostdlib -nostdinc
 
 # to run from SRAM
-CFLAGS+=-Wl,-Ttext,0x20000000 -Wl,-e,0x20000000
+DEF_CFLAGS+=-Wl,-Ttext,0x20000000 -Wl,-e,0x20000000
 
 # to write to flash then run
-# CFLAGS+=-Wl,-Ttext,0x08000000 -Wl,-e,0x08000000
+# DEF_CFLAGS+=-Wl,-Ttext,0x08000000 -Wl,-e,0x08000000
+
+CFLAGS_VL=$(DEF_CFLAGS) -mcpu=cortex-m3 -DCONFIG_STM32VL_DISCOVERY=1
+CFLAGS_L=$(DEF_CFLAGS) -mcpu=cortex-m3 -DCONFIG_STM32L_DISCOVERY
+CFLAGS_F4=$(DEF_CFLAGS) -mcpu=cortex-m4 -DCONFIG_STM32F4_DISCOVERY=1
 
-all: $(BIN_IMAGE)
+all: blink_32VL.elf blink_32L.elf blink_F4.elf
 
-$(BIN_IMAGE): $(EXECUTABLE)
+%.bin: %.elf
        $(OBJCOPY) -O binary $^ $@
 
-$(EXECUTABLE): main.c
-       $(CC) $(CFLAGS) $^ -o $@
+blink_32VL.elf: main.c
+       $(CC) $(CFLAGS_VL) $^ -o $@
+blink_32L.elf: main.c
+       $(CC) $(CFLAGS_L) $^ -o $@
+blink_F4.elf: main.c
+       $(CC) $(CFLAGS_F4) $^ -o $@
 
 clean:
-       rm -rf $(EXECUTABLE)
-       rm -rf $(BIN_IMAGE)
+       rm -rf *.elf
+       rm -rf *.bin
 
 .PHONY: all clean
diff --git a/example/blink/default_bootloader.bin b/example/blink/default_bootloader.bin
deleted file mode 100755 (executable)
index 0e31371..0000000
Binary files a/example/blink/default_bootloader.bin and /dev/null differ
diff --git a/example/blink/disasm.sh b/example/blink/disasm.sh
deleted file mode 100755 (executable)
index b3b46da..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/usr/bin/env sh
-#/home/texane/sat/bin/arm-none-eabi-objdump -marm -Mforce-thumb -EL -b binary -D /tmp/barfoo
-/home/texane/sat/bin/arm-none-eabi-objdump -marm -EL -b binary -D /tmp/barfoo
index 0889b81619a4850b2514db5e3d27ec46af051594..e0438a0dbdec1678b7ff34d823588dceb9e6919d 100644 (file)
@@ -7,36 +7,30 @@ typedef unsigned int uint32_t;
 
 #if CONFIG_STM32VL_DISCOVERY
 
-# define GPIOC 0x40011000 /* port C */
-# define GPIOC_CRH (GPIOC + 0x04) /* port configuration register high */
-# define GPIOC_ODR (GPIOC + 0x0c) /* port output data register */
+#define GPIOC 0x40011000 /* port C */
+#define GPIOC_CRH (GPIOC + 0x04) /* port configuration register high */
+#define LED_PORT_ODR (GPIOC + 0x0c) /* port output data register */
 
-# define LED_BLUE (1 << 8) /* port C, pin 8 */
-# define LED_GREEN (1 << 9) /* port C, pin 9 */
+#define LED_BLUE (1 << 8) /* port C, pin 8 */
+#define LED_GREEN (1 << 9) /* port C, pin 9 */
+#define LED_ORANGE 0
+#define LED_RED 0
 
 static inline void setup_leds(void)
 {
   *(volatile uint32_t*)GPIOC_CRH = 0x44444411;
 }
 
-static inline void switch_leds_on(void)
-{
-  *(volatile uint32_t*)GPIOC_ODR = LED_BLUE | LED_GREEN;
-}
-
-static inline void switch_leds_off(void)
-{
-  *(volatile uint32_t*)GPIOC_ODR = 0;
-}
-
 #elif CONFIG_STM32L_DISCOVERY
 
-# define GPIOB 0x40020400 /* port B */
-# define GPIOB_MODER (GPIOB + 0x00) /* port mode register */
-# define GPIOB_ODR (GPIOB + 0x14) /* port output data register */
+#define GPIOB 0x40020400 /* port B */
+#define GPIOB_MODER (GPIOB + 0x00) /* port mode register */
+#define LED_PORT_ODR (GPIOB + 0x14) /* port output data register */
 
-# define LED_BLUE (1 << 6) /* port B, pin 6 */
-# define LED_GREEN (1 << 7) /* port B, pin 7 */
+#define LED_BLUE (1 << 6) /* port B, pin 6 */
+#define LED_GREEN (1 << 7) /* port B, pin 7 */
+#define LED_ORANGE 0
+#define LED_RED 0
 
 static inline void setup_leds(void)
 {
@@ -44,26 +38,16 @@ static inline void setup_leds(void)
   *(volatile uint32_t*)GPIOB_MODER |= (1 << (7 * 2)) | (1 << (6 * 2));
 }
 
-static inline void switch_leds_on(void)
-{
-  *(volatile uint32_t*)GPIOB_ODR = LED_BLUE | LED_GREEN;
-}
-
-static inline void switch_leds_off(void)
-{
-  *(volatile uint32_t*)GPIOB_ODR = 0;
-}
-
 #elif CONFIG_STM32F4_DISCOVERY
 
 #define GPIOD 0x40020C00 /* port D */
-# define GPIOD_MODER (GPIOD + 0x00) /* port mode register */
-# define GPIOD_ODR (GPIOD + 0x14) /* port output data register */
+#define GPIOD_MODER (GPIOD + 0x00) /* port mode register */
+#define LED_PORT_ODR (GPIOD + 0x14) /* port output data register */
 
-# define LED_GREEN (1 << 12) /* port B, pin 12 */
-# define LED_ORANGE (1 << 13) /* port B, pin 13 */
-# define LED_RED (1 << 14) /* port B, pin 14 */
-# define LED_BLUE (1 << 15) /* port B, pin 15 */
+#define LED_GREEN (1 << 12) /* port D, pin 12 */
+#define LED_ORANGE (1 << 13) /* port D, pin 13 */
+#define LED_RED (1 << 14) /* port D, pin 14 */
+#define LED_BLUE (1 << 15) /* port D, pin 15 */
 
 static inline void setup_leds(void)
 {
@@ -71,22 +55,20 @@ static inline void setup_leds(void)
        (1 << (13 * 2)) | (1 << (14 * 2)) | (1 << (15 * 2));
 }
 
+#else
+#error "Architecture must be defined!"
+#endif /* otherwise, error */
 
 static inline void switch_leds_on(void)
 {
-  *(volatile uint32_t*)GPIOD_ODR = LED_GREEN | LED_ORANGE | LED_RED | LED_BLUE;
+  *(volatile uint32_t*)LED_PORT_ODR = LED_BLUE | LED_GREEN | LED_ORANGE | LED_RED;
 }
 
 static inline void switch_leds_off(void)
 {
-  *(volatile uint32_t*)GPIOD_ODR = 0;
+  *(volatile uint32_t*)LED_PORT_ODR = 0;
 }
 
-#else
-#error "Architecture must be defined!"
-#endif /* otherwise, error */
-
-
 #define delay()                                                \
 do {                                                   \
   register unsigned int i;                             \
diff --git a/example/blink/o.bin b/example/blink/o.bin
deleted file mode 100755 (executable)
index 76324f7..0000000
Binary files a/example/blink/o.bin and /dev/null differ
index 0febe491283e4cfb9b6b913f002ba8eb9df1e91a..0f31ae68b0d2b83a74ab0c398dfe5a2413d74670 100644 (file)
@@ -2,7 +2,7 @@ ELF=lcd.elf
 
 CC=arm-none-eabi-gcc
 
-CFLAGS=-O2 -mlittle-endian -mthumb
+CFLAGS=-O2 -mlittle-endian -mthumb -g
 CFLAGS+=-mcpu=cortex-m3 -ffreestanding -nostdlib -nostdinc
 
 CFLAGS+=-I.
index 64163de40ce14542aeb9f4e782997b2d759ad972..982a5f2594bb02fa86a4a3ec18910ae8bfacbdac 100644 (file)
@@ -281,7 +281,7 @@ static void LCD_Conv_Char_Seg(uint8_t* c,bool point,bool column, uint8_t* digit)
       ch = star;\r
       break;\r
                   \r
-    case 'µ' :\r
+    case 'µ' :\r
       ch = C_UMAP;\r
       break;\r
     \r
@@ -301,7 +301,7 @@ static void LCD_Conv_Char_Seg(uint8_t* c,bool point,bool column, uint8_t* digit)
       ch = C_slatch;\r
       break;  \r
       \r
-    case '°' :\r
+    case '°' :\r
       ch = C_percent_1;\r
       break;  \r
     case '%' :\r
index d496b84003a9db420a32d7456bc3a974fcba2999..bb8b0d742a4337bfe0a2a2cf6ac4c46f21595e3a 100644 (file)
@@ -85,7 +85,7 @@ MSB   { 1 , 1 , 0 , 0   }
 #define BAR3_ON t_bar[0]  |= 2 \r
 #define BAR3_OFF t_bar[0] &= ~2 \r
 \r
-/* code for 'µ' character */\r
+/* code for 'µ' character */\r
 #define C_UMAP 0x6084\r
 \r
 /* code for 'm' character */\r
@@ -103,7 +103,7 @@ MSB   { 1 , 1 , 0 , 0   }
 /* constant code for '/' */\r
 #define C_slatch  0x00c0\r
 \r
-/* constant code for Â° */\r
+/* constant code for Ã‚° */\r
 #define C_percent_1 0xec00\r
 \r
 /* constant code  for small o */\r
index 46a8ce1711e295651cf2c16e1bfae3c08bc6dd09..44157816e9084b93359b3ae19bcba82725cafbfd 100644 (file)
@@ -1,5 +1,7 @@
 /* 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>
@@ -86,18 +88,12 @@ int main(int ac, char** av)
 
   if (o.devname != NULL) /* stlinkv1 */
   {
-#if CONFIG_USE_LIBSG
-    static const int scsi_verbose = 2;
-    sl = stlink_quirk_open(o.devname, scsi_verbose);
+    sl = stlink_v1_open(100);
     if (sl == NULL) goto on_error;
-#else
-    printf("not compiled for use with STLink/V1");
-    goto on_error;
-#endif
   }
   else /* stlinkv2 */
   {
-    sl = stlink_open_usb(1);
+    sl = stlink_open_usb(100);
     if (sl == NULL) goto on_error;
   }
 
index 67f0be0e61d4edd42992b72865b393c0ff3c186a..bcb3c974b1e86d171482bff50a61d00dc15d7ea6 100644 (file)
@@ -6,6 +6,7 @@
  license that can be found in the LICENSE file.
 */
 
+#include <getopt.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
 #include "gdb-remote.h"
 
+#define DEFAULT_LOGGING_LEVEL 100
+#define DEFAULT_GDB_LISTEN_PORT 4242
+
+#define STRINGIFY_inner(name) #name
+#define STRINGIFY(name) STRINGIFY_inner(name)
+
 #define FLASH_BASE 0x08000000
 #define FLASH_PAGE (sl->flash_pgsz)
 #define FLASH_PAGE_MASK (~((1 << 10) - 1))
 #define FLASH_SIZE (FLASH_PAGE * 128)
 
-volatile int do_exit = 0;
-void ctrl_c(int sig)
-{
-  do_exit = 1;
-}
-
 static const char hex[] = "0123456789abcdef";
 
 static const char* current_memory_map = NULL;
@@ -77,88 +78,136 @@ struct chip_params {
        { 0 }
 };
 
-int serve(stlink_t *sl, int port);
-char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
+typedef struct _st_state_t {
+    // things from command line, bleh
+    int stlink_version;
+    // "/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTE531X6-if00-port0" is only 58 chars
+    char devicename[100];
+    int logging_level;
+       int listen_port;
+} st_state_t;
 
-int main(int argc, char** argv) {
-
-       stlink_t *sl = NULL;
 
-       const char * HelpStr =  "Usage:\n"
-                                                               "\t st-util port [/dev/sgX]\n"
-                                                               "\t st-util [port]\n"
-                                                               "\t st-util --help\n";
+int serve(stlink_t *sl, int port);
+char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
 
-       switch(argc) {
 
-               case 3 : {
-                       //sl = stlink_quirk_open(argv[2], 0);
-                       // FIXME - hardcoded to usb....
-                       sl = stlink_open_usb(10);
-                       if(sl == NULL) return 1;
+int parse_options(int argc, char** argv, st_state_t *st) {
+    static struct option long_options[] = {
+        {"help", no_argument, NULL, 'h'},
+        {"verbose", optional_argument, NULL, 'v'},
+        {"device", required_argument, NULL, 'd'},
+        {"stlink_version", required_argument, NULL, 's'},
+        {"stlinkv1", no_argument, NULL, '1'},
+               {"listen_port", required_argument, NULL, 'p'},
+        {0, 0, 0, 0},
+    };
+       const char * help_str = "%s - usage:\n\n"
+       "  -h, --help\t\tPrint this help\n"
+       "  -vXX, --verbose=XX\tspecify a specific verbosity level (0..99)\n"
+       "  -v, --verbose\tspecify generally verbose logging\n"
+       "  -d <device>, --device=/dev/stlink2_1\n"
+       "\t\t\tWhere is your stlink device connected?\n"
+       "  -s X, --stlink_version=X\n"
+       "\t\t\tChoose what version of stlink to use, (defaults to 2)\n"
+       "  -1, --stlinkv1\tForce stlink version 1\n"
+       "  -p 4242, --listen_port=1234\n"
+       "\t\t\tSet the gdb server listen port. "
+       "(default port: " STRINGIFY(DEFAULT_GDB_LISTEN_PORT) ")\n"
+       ;
+
+
+    int option_index = 0;
+    int c;
+    int q;
+    while ((c = getopt_long(argc, argv, "hv::d:s:1p:", long_options, &option_index)) != -1) {
+        switch (c) {
+        case 0:
+            printf("XXXXX Shouldn't really normally come here, only if there's no corresponding option\n");
+            printf("option %s", long_options[option_index].name);
+            if (optarg) {
+                printf(" with arg %s", optarg);
+            }
+            printf("\n");
+            break;
+        case 'h':
+            printf(help_str, argv[0]);
+            exit(EXIT_SUCCESS);
+            break;
+        case 'v':
+            if (optarg) {
+                st->logging_level = atoi(optarg);
+            } else {
+                st->logging_level = DEFAULT_LOGGING_LEVEL;
+            }
+            break;
+        case 'd':
+            if (strlen(optarg) > sizeof (st->devicename)) {
+                fprintf(stderr, "device name too long: %zd\n", strlen(optarg));
+            } else {
+                strcpy(st->devicename, optarg);
+            }
+            break;
+               case '1':
+                       st->stlink_version = 1;
                        break;
-               }
-
-               case 2 : {
-                       if (strcmp(argv[1], "--help") == 0) {
-                               fprintf(stdout, HelpStr, NULL);
-                               return 1;
-                       }
-               }
-
-#if CONFIG_USE_LIBSG
-               case 1 : { // Search ST-LINK (from /dev/sg0 to /dev/sg99)
-                       const int DevNumMax = 99;
-                       int ExistDevCount = 0;
-
-                       for(int DevNum = 0; DevNum <= DevNumMax; DevNum++)
-                       {
-                               if(DevNum < 10) {
-                                       char DevName[] = "/dev/sgX";
-                                       const int X_index = 7;
-                                       DevName[X_index] = DevNum + '0';
-                                       if ( !access(DevName, F_OK) ) {
-                                               sl = stlink_quirk_open(DevName, 0);
-                                               ExistDevCount++;
-                                       }
-                               }
-                               else if(DevNum < 100) {
-                                       char DevName[] = "/dev/sgXY";
-                                       const int X_index = 7;
-                                       const int Y_index = 8;
-                                       DevName[X_index] = DevNum/10 + '0';
-                                       DevName[Y_index] = DevNum%10 + '0';
-                                       if ( !access(DevName, F_OK) ) {
-                                               sl = stlink_quirk_open(DevName, 0);
-                                               ExistDevCount++;
-                                       }
-                               }
-                               if(sl != NULL) break;
+               case 's':
+                       sscanf(optarg, "%i", &q);
+                       if (q < 0 || q > 2) {
+                               fprintf(stderr, "stlink version %d unknown!\n", q);
+                               exit(EXIT_FAILURE);
                        }
-
-                       if(sl == NULL) {
-                               fprintf(stdout, "\nNumber of /dev/sgX devices found: %i \n",
-                                               ExistDevCount);
-                               fprintf(stderr, "ST-LINK not found\n");
-                               return 1;
+                       st->stlink_version = q;
+                       break;
+               case 'p':
+                       sscanf(optarg, "%i", &q);
+                       if (q < 0) {
+                               fprintf(stderr, "Can't use a negative port to listen on: %d\n", q);
+                               exit(EXIT_FAILURE);
                        }
+                       st->listen_port = q;
                        break;
-               }
-#endif
+        }
+    }
+
+    if (optind < argc) {
+        printf("non-option ARGV-elements: ");
+        while (optind < argc)
+            printf("%s ", argv[optind++]);
+        printf("\n");
+    }
+    return 0;
+}
 
-               default: {
-                       fprintf(stderr, HelpStr, NULL);
-                       return 1;
-               }
-       }
 
-       if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
-               stlink_exit_dfu_mode(sl);
-       }
+int main(int argc, char** argv) {
 
-       if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) {
-         stlink_enter_swd_mode(sl);
-       }
+       stlink_t *sl = NULL;
+
+       st_state_t state;
+       memset(&state, 0, sizeof(state));
+       // set defaults...
+       state.stlink_version = 2;
+       state.logging_level = DEFAULT_LOGGING_LEVEL;
+       state.listen_port = DEFAULT_GDB_LISTEN_PORT;
+       parse_options(argc, argv, &state);
+       switch (state.stlink_version) {
+       case 2:
+               sl = stlink_open_usb(state.logging_level);
+               if(sl == NULL) return 1;
+               break;
+       case 1:
+               sl = stlink_v1_open(state.logging_level);
+               if(sl == NULL) return 1;
+               break;
+    }
+
+    if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) {
+        if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
+            stlink_exit_dfu_mode(sl);
+        }
+        stlink_enter_swd_mode(sl);
+    }
 
        uint32_t chip_id = stlink_chip_id(sl);
        uint32_t core_id = stlink_core_id(sl);
@@ -200,9 +249,7 @@ int main(int argc, char** argv) {
        // memory map is in 1k blocks.
        current_memory_map = make_memory_map(params, flash_size * 0x400);
 
-       int port = 4242;
-
-       while(serve(sl, port) == 0);
+       while(serve(sl, state.listen_port) == 0);
 
        /* Switch back to mass storage mode before closing. */
        stlink_run(sl);
@@ -603,7 +650,6 @@ int serve(stlink_t *sl, int port) {
 
        printf("Listening at *:%d...\n", port);
 
-       (void) signal (SIGINT, ctrl_c);
        int client = accept(sock, NULL, NULL);
        signal (SIGINT, SIG_DFL);
        if(client < 0) {
diff --git a/nbproject/Package-Default.bash b/nbproject/Package-Default.bash
new file mode 100644 (file)
index 0000000..f6abfb4
--- /dev/null
@@ -0,0 +1,75 @@
+#!/bin/bash -x
+
+#
+# Generated - do not edit!
+#
+
+# Macros
+TOP=`pwd`
+CND_PLATFORM=GNU-Linux-x86
+CND_CONF=Default
+CND_DISTDIR=dist
+CND_BUILDDIR=build
+NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging
+TMPDIRNAME=tmp-packaging
+OUTPUT_PATH=gdbserver/st-util
+OUTPUT_BASENAME=st-util
+PACKAGE_TOP_DIR=stlink/
+
+# Functions
+function checkReturnCode
+{
+    rc=$?
+    if [ $rc != 0 ]
+    then
+        exit $rc
+    fi
+}
+function makeDirectory
+# $1 directory path
+# $2 permission (optional)
+{
+    mkdir -p "$1"
+    checkReturnCode
+    if [ "$2" != "" ]
+    then
+      chmod $2 "$1"
+      checkReturnCode
+    fi
+}
+function copyFileToTmpDir
+# $1 from-file path
+# $2 to-file path
+# $3 permission
+{
+    cp "$1" "$2"
+    checkReturnCode
+    if [ "$3" != "" ]
+    then
+        chmod $3 "$2"
+        checkReturnCode
+    fi
+}
+
+# Setup
+cd "${TOP}"
+mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package
+rm -rf ${NBTMPDIR}
+mkdir -p ${NBTMPDIR}
+
+# Copy files and create directories and links
+cd "${TOP}"
+makeDirectory "${NBTMPDIR}/stlink/bin"
+copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755
+
+
+# Generate tar file
+cd "${TOP}"
+rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/stlink.tar
+cd ${NBTMPDIR}
+tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/stlink.tar *
+checkReturnCode
+
+# Cleanup
+cd "${TOP}"
+rm -rf ${NBTMPDIR}
diff --git a/nbproject/Package-gdbserver.bash b/nbproject/Package-gdbserver.bash
deleted file mode 100644 (file)
index 722b818..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-#!/bin/bash -x
-
-#
-# Generated - do not edit!
-#
-
-# Macros
-TOP=`pwd`
-CND_PLATFORM=GNU-Linux-x86
-CND_CONF=gdbserver
-CND_DISTDIR=dist
-CND_BUILDDIR=build
-NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging
-TMPDIRNAME=tmp-packaging
-OUTPUT_PATH=gdbserver/st-util
-OUTPUT_BASENAME=st-util
-PACKAGE_TOP_DIR=stlink/
-
-# Functions
-function checkReturnCode
-{
-    rc=$?
-    if [ $rc != 0 ]
-    then
-        exit $rc
-    fi
-}
-function makeDirectory
-# $1 directory path
-# $2 permission (optional)
-{
-    mkdir -p "$1"
-    checkReturnCode
-    if [ "$2" != "" ]
-    then
-      chmod $2 "$1"
-      checkReturnCode
-    fi
-}
-function copyFileToTmpDir
-# $1 from-file path
-# $2 to-file path
-# $3 permission
-{
-    cp "$1" "$2"
-    checkReturnCode
-    if [ "$3" != "" ]
-    then
-        chmod $3 "$2"
-        checkReturnCode
-    fi
-}
-
-# Setup
-cd "${TOP}"
-mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package
-rm -rf ${NBTMPDIR}
-mkdir -p ${NBTMPDIR}
-
-# Copy files and create directories and links
-cd "${TOP}"
-makeDirectory "${NBTMPDIR}/stlink/bin"
-copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755
-
-
-# Generate tar file
-cd "${TOP}"
-rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/stlink.tar
-cd ${NBTMPDIR}
-tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/stlink.tar *
-checkReturnCode
-
-# Cleanup
-cd "${TOP}"
-rm -rf ${NBTMPDIR}
diff --git a/nbproject/Package-library_cli.bash b/nbproject/Package-library_cli.bash
deleted file mode 100644 (file)
index 5a64472..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-#!/bin/bash -x
-
-#
-# Generated - do not edit!
-#
-
-# Macros
-TOP=`pwd`
-CND_PLATFORM=GNU-Linux-x86
-CND_CONF=library_cli
-CND_DISTDIR=dist
-CND_BUILDDIR=build
-NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging
-TMPDIRNAME=tmp-packaging
-OUTPUT_PATH=test_usb
-OUTPUT_BASENAME=test_usb
-PACKAGE_TOP_DIR=stlink/
-
-# Functions
-function checkReturnCode
-{
-    rc=$?
-    if [ $rc != 0 ]
-    then
-        exit $rc
-    fi
-}
-function makeDirectory
-# $1 directory path
-# $2 permission (optional)
-{
-    mkdir -p "$1"
-    checkReturnCode
-    if [ "$2" != "" ]
-    then
-      chmod $2 "$1"
-      checkReturnCode
-    fi
-}
-function copyFileToTmpDir
-# $1 from-file path
-# $2 to-file path
-# $3 permission
-{
-    cp "$1" "$2"
-    checkReturnCode
-    if [ "$3" != "" ]
-    then
-        chmod $3 "$2"
-        checkReturnCode
-    fi
-}
-
-# Setup
-cd "${TOP}"
-mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package
-rm -rf ${NBTMPDIR}
-mkdir -p ${NBTMPDIR}
-
-# Copy files and create directories and links
-cd "${TOP}"
-makeDirectory "${NBTMPDIR}/stlink/bin"
-copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755
-
-
-# Generate tar file
-cd "${TOP}"
-rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/stlink.tar
-cd ${NBTMPDIR}
-tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/stlink.tar *
-checkReturnCode
-
-# Cleanup
-cd "${TOP}"
-rm -rf ${NBTMPDIR}
index 3d02ea8313f6ebdceb9253b70edbb625cd2f4cd7..e129c44f64fc1278bd07bc79b2dd56ffc28e6566 100644 (file)
@@ -2,7 +2,105 @@
 <configurationDescriptor version="79">
   <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
     <df name="stlink" root=".">
+      <df name="doc">
+        <df name="tutorial">
+        </df>
+      </df>
       <df name="example">
+        <df name="blink">
+          <in>main.c</in>
+        </df>
+        <df name="blink_flash">
+          <in>main.c</in>
+          <in>startup_stm32l1xx_md.s</in>
+          <in>system_stm32l1xx.c</in>
+        </df>
+        <df name="lcd">
+          <in>discover_board.h</in>
+          <in>main.c</in>
+          <in>stm32l_discovery_lcd.c</in>
+          <in>stm32l_discovery_lcd.h</in>
+        </df>
+        <df name="libstm32l_discovery">
+          <df name="build">
+          </df>
+          <df name="inc">
+            <df name="base">
+              <in>stdint.h</in>
+            </df>
+            <df name="core_support">
+              <in>core_cm3.c</in>
+              <in>core_cm3.h</in>
+            </df>
+            <df name="device_support">
+              <in>stm32l1xx.h</in>
+              <in>system_stm32l1xx.h</in>
+            </df>
+            <in>misc.h</in>
+            <in>stm32l1xx_adc.h</in>
+            <in>stm32l1xx_comp.h</in>
+            <in>stm32l1xx_crc.h</in>
+            <in>stm32l1xx_dac.h</in>
+            <in>stm32l1xx_dbgmcu.h</in>
+            <in>stm32l1xx_dma.h</in>
+            <in>stm32l1xx_exti.h</in>
+            <in>stm32l1xx_flash.h</in>
+            <in>stm32l1xx_gpio.h</in>
+            <in>stm32l1xx_i2c.h</in>
+            <in>stm32l1xx_iwdg.h</in>
+            <in>stm32l1xx_lcd.h</in>
+            <in>stm32l1xx_pwr.h</in>
+            <in>stm32l1xx_rcc.h</in>
+            <in>stm32l1xx_rtc.h</in>
+            <in>stm32l1xx_spi.h</in>
+            <in>stm32l1xx_syscfg.h</in>
+            <in>stm32l1xx_tim.h</in>
+            <in>stm32l1xx_usart.h</in>
+            <in>stm32l1xx_wwdg.h</in>
+          </df>
+          <df name="src">
+            <in>misc.c</in>
+            <in>stm32l1xx_adc.c</in>
+            <in>stm32l1xx_comp.c</in>
+            <in>stm32l1xx_crc.c</in>
+            <in>stm32l1xx_dac.c</in>
+            <in>stm32l1xx_dbgmcu.c</in>
+            <in>stm32l1xx_dma.c</in>
+            <in>stm32l1xx_exti.c</in>
+            <in>stm32l1xx_flash.c</in>
+            <in>stm32l1xx_flash_ramfunc.c</in>
+            <in>stm32l1xx_gpio.c</in>
+            <in>stm32l1xx_i2c.c</in>
+            <in>stm32l1xx_iwdg.c</in>
+            <in>stm32l1xx_lcd.c</in>
+            <in>stm32l1xx_pwr.c</in>
+            <in>stm32l1xx_rcc.c</in>
+            <in>stm32l1xx_rtc.c</in>
+            <in>stm32l1xx_spi.c</in>
+            <in>stm32l1xx_syscfg.c</in>
+            <in>stm32l1xx_tim.c</in>
+            <in>stm32l1xx_usart.c</in>
+            <in>stm32l1xx_wwdg.c</in>
+          </df>
+        </df>
+        <df name="original_fw">
+          <in>discover_board.h</in>
+          <in>discover_functions.c</in>
+          <in>discover_functions.h</in>
+          <in>icc_measure.c</in>
+          <in>icc_measure.h</in>
+          <in>icc_measure_Ram.c</in>
+          <in>main.c</in>
+          <in>stm32_tsl_conf.h</in>
+          <in>stm32l1xx_conf.h</in>
+          <in>stm32l1xx_it.c</in>
+          <in>stm32l1xx_it.h</in>
+          <in>stm32l_discovery_lcd.c</in>
+          <in>stm32l_discovery_lcd.h</in>
+          <in>system_stm32l1xx.c</in>
+        </df>
+      </df>
+      <df name="flash">
         <in>main.c</in>
       </df>
       <df name="gdbserver">
         <in>stlink-usb.h</in>
         <in>test_sg.c</in>
         <in>test_usb.c</in>
+        <in>uglylogging.c</in>
+        <in>uglylogging.h</in>
       </df>
-      <df name="stm32l_notes">
+      <df name="toremove">
+        <df name="stm32l">
+          <df name="src">
+            <in>stlink-hw.h</in>
+          </df>
+        </df>
+        <df name="stm32l_notes">
+        </df>
       </df>
     </df>
     <logicalFolder name="ExternalFiles"
                    displayName="Important Files"
                    projectFiles="false"
                    kind="IMPORTANT_FILES_FOLDER">
-      <itemPath>build/Makefile</itemPath>
+      <itemPath>Makefile</itemPath>
     </logicalFolder>
   </logicalFolder>
   <sourceFolderFilter>^(nbproject)$</sourceFolderFilter>
   <sourceRootList>
     <Elem>.</Elem>
   </sourceRootList>
-  <projectmakefile>build/Makefile</projectmakefile>
+  <projectmakefile>Makefile</projectmakefile>
   <confs>
-    <conf name="library_cli" type="0">
+    <conf name="Default" type="0">
       <toolsSet>
         <remote-sources-mode>LOCAL_SOURCES</remote-sources-mode>
         <compilerSet>default</compilerSet>
       </toolsSet>
       <makefileType>
         <makeTool>
-          <buildCommandWorkingDir></buildCommandWorkingDir>
+          <buildCommandWorkingDir>.</buildCommandWorkingDir>
           <buildCommand>${MAKE} -f Makefile</buildCommand>
           <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
-          <executablePath>test_usb</executablePath>
+          <executablePath>gdbserver/st-util</executablePath>
           <cTool>
             <incDir>
-              <pElem>stm32l/src</pElem>
-              <pElem>/usr/include/libusb-1.0</pElem>
-              <pElem>stm32l/build</pElem>
-              <pElem>src</pElem>
+              <pElem>.</pElem>
             </incDir>
             <preprocessorList>
-              <Elem>CONFIG_USE_LIBUSB=1</Elem>
-              <Elem>__GNUC_STDC_INLINE__=1</Elem>
-              <Elem>__OPTIMIZE__=1</Elem>
-              <Elem>__REGISTER_PREFIX__=</Elem>
-              <Elem>__STDC_VERSION__=199901L</Elem>
-              <Elem>__USER_LABEL_PREFIX__=</Elem>
+              <Elem>DEBUG=1</Elem>
             </preprocessorList>
           </cTool>
         </makeTool>
       </makefileType>
+      <item path="example/blink/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/blink_flash/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/blink_flash/startup_stm32l1xx_md.s"
+            ex="true"
+            tool="4"
+            flavor="0">
+      </item>
+      <item path="example/blink_flash/system_stm32l1xx.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/lcd/discover_board.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/lcd/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/lcd/stm32l_discovery_lcd.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/lcd/stm32l_discovery_lcd.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/base/stdint.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/core_support/core_cm3.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/core_support/core_cm3.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/device_support/stm32l1xx.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/device_support/system_stm32l1xx.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/misc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_adc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_comp.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_crc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dac.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dbgmcu.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dma.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_exti.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_flash.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_gpio.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_i2c.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_iwdg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_lcd.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_pwr.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_rcc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_rtc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_spi.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_syscfg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_tim.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_usart.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_wwdg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/misc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_adc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_comp.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_crc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dac.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dbgmcu.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dma.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_exti.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_flash.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_flash_ramfunc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_gpio.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_i2c.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_iwdg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_lcd.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_pwr.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_rcc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_rtc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_spi.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_syscfg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_tim.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_usart.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_wwdg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/discover_board.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/discover_functions.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/discover_functions.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure_Ram.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32_tsl_conf.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_conf.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_it.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_it.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l_discovery_lcd.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l_discovery_lcd.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/system_stm32l1xx.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <folder path="stlink/gdbserver">
+        <cTool>
+          <incDir>
+            <pElem>src</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>CONFIG_USE_LIBSG=1</Elem>
+            <Elem>CONFIG_USE_LIBUSB=1</Elem>
+          </preprocessorList>
+        </cTool>
+      </folder>
+      <item path="flash/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="gdbserver/gdb-remote.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="gdbserver/gdb-server.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="toremove/stm32l/src/stlink-hw.h" ex="true" tool="3" flavor="0">
+      </item>
     </conf>
     <conf name="gdbserver" type="0">
       <toolsSet>
           <buildCommandWorkingDir>gdbserver</buildCommandWorkingDir>
           <buildCommand>${MAKE} -f Makefile</buildCommand>
           <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
-          <executablePath>gdbserver/st-util</executablePath>
+          <executablePath></executablePath>
+        </makeTool>
+      </makefileType>
+      <item path="example/blink/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/blink_flash/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/blink_flash/startup_stm32l1xx_md.s"
+            ex="true"
+            tool="4"
+            flavor="0">
+      </item>
+      <item path="example/blink_flash/system_stm32l1xx.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/lcd/discover_board.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/lcd/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/lcd/stm32l_discovery_lcd.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/lcd/stm32l_discovery_lcd.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/base/stdint.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/core_support/core_cm3.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/core_support/core_cm3.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/device_support/stm32l1xx.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/device_support/system_stm32l1xx.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/misc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_adc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_comp.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_crc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dac.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dbgmcu.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dma.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_exti.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_flash.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_gpio.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_i2c.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_iwdg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_lcd.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_pwr.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_rcc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_rtc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_spi.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_syscfg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_tim.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_usart.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_wwdg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/misc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_adc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_comp.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_crc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dac.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dbgmcu.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dma.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_exti.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_flash.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_flash_ramfunc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_gpio.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_i2c.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_iwdg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_lcd.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_pwr.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_rcc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_rtc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_spi.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_syscfg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_tim.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_usart.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_wwdg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/discover_board.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/discover_functions.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/discover_functions.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure_Ram.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32_tsl_conf.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_conf.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_it.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_it.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l_discovery_lcd.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l_discovery_lcd.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/system_stm32l1xx.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <folder path="stlink/gdbserver">
+        <cTool>
+          <incDir>
+            <pElem>src</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>CONFIG_USE_LIBSG=1</Elem>
+            <Elem>CONFIG_USE_LIBUSB=1</Elem>
+          </preprocessorList>
+        </cTool>
+      </folder>
+      <folder path="stlink/src">
+        <cTool>
+          <incDir>
+            <pElem>.</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>DEBUG=1</Elem>
+          </preprocessorList>
+        </cTool>
+      </folder>
+      <item path="flash/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="toremove/stm32l/src/stlink-hw.h" ex="true" tool="3" flavor="0">
+      </item>
+    </conf>
+    <conf name="flash" type="0">
+      <toolsSet>
+        <remote-sources-mode>LOCAL_SOURCES</remote-sources-mode>
+        <compilerSet>default</compilerSet>
+      </toolsSet>
+      <makefileType>
+        <makeTool>
+          <buildCommandWorkingDir>flash</buildCommandWorkingDir>
+          <buildCommand>${MAKE} -f Makefile</buildCommand>
+          <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
+          <executablePath></executablePath>
           <cTool>
-            <incDir>
-              <pElem>stm32l/src</pElem>
-              <pElem>/usr/include/libusb-1.0</pElem>
-              <pElem>stm32l/build</pElem>
-              <pElem>src</pElem>
-            </incDir>
             <preprocessorList>
-              <Elem>CONFIG_USE_LIBUSB=1</Elem>
-              <Elem>__GNUC_STDC_INLINE__=1</Elem>
-              <Elem>__OPTIMIZE__=1</Elem>
-              <Elem>__REGISTER_PREFIX__=</Elem>
-              <Elem>__STDC_VERSION__=199901L</Elem>
-              <Elem>__USER_LABEL_PREFIX__=</Elem>
+              <Elem>DEBUG</Elem>
             </preprocessorList>
           </cTool>
         </makeTool>
       </makefileType>
+      <item path="example/blink/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/blink_flash/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/blink_flash/startup_stm32l1xx_md.s"
+            ex="true"
+            tool="4"
+            flavor="0">
+      </item>
+      <item path="example/blink_flash/system_stm32l1xx.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/lcd/discover_board.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/lcd/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/lcd/stm32l_discovery_lcd.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/lcd/stm32l_discovery_lcd.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/base/stdint.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/core_support/core_cm3.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/core_support/core_cm3.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/device_support/stm32l1xx.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/device_support/system_stm32l1xx.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/misc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_adc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_comp.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_crc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dac.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dbgmcu.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_dma.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_exti.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_flash.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_gpio.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_i2c.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_iwdg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_lcd.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_pwr.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_rcc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_rtc.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_spi.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_syscfg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_tim.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_usart.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/inc/stm32l1xx_wwdg.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/misc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_adc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_comp.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_crc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dac.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dbgmcu.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_dma.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_exti.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_flash.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_flash_ramfunc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_gpio.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_i2c.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_iwdg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_lcd.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_pwr.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_rcc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_rtc.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_spi.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_syscfg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_tim.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_usart.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/libstm32l_discovery/src/stm32l1xx_wwdg.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/discover_board.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/discover_functions.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/discover_functions.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/icc_measure_Ram.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/main.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32_tsl_conf.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_conf.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_it.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l1xx_it.h" ex="true" tool="3" flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l_discovery_lcd.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/stm32l_discovery_lcd.h"
+            ex="true"
+            tool="3"
+            flavor="0">
+      </item>
+      <item path="example/original_fw/system_stm32l1xx.c"
+            ex="true"
+            tool="0"
+            flavor="0">
+      </item>
+      <folder path="stlink/flash">
+        <cTool>
+          <incDir>
+            <pElem>src</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>CONFIG_USE_LIBSG=1</Elem>
+            <Elem>CONFIG_USE_LIBUSB=1</Elem>
+          </preprocessorList>
+        </cTool>
+      </folder>
+      <folder path="stlink/gdbserver">
+        <cTool>
+          <incDir>
+            <pElem>src</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>CONFIG_USE_LIBSG=1</Elem>
+            <Elem>CONFIG_USE_LIBUSB=1</Elem>
+          </preprocessorList>
+        </cTool>
+      </folder>
+      <folder path="stlink/src">
+        <cTool>
+          <incDir>
+            <pElem>.</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>DEBUG=1</Elem>
+          </preprocessorList>
+        </cTool>
+      </folder>
+      <item path="gdbserver/gdb-remote.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="gdbserver/gdb-server.c" ex="true" tool="0" flavor="0">
+      </item>
+      <item path="toremove/stm32l/src/stlink-hw.h" ex="true" tool="3" flavor="0">
+      </item>
     </conf>
   </confs>
 </configurationDescriptor>
index 660863635e1b53907920e28dbcf8df0cd5c99155..946c06f6fd9e2e0115d4ef8d3019dfc60cefdc04 100644 (file)
             </sourceRootList>
             <confList>
                 <confElem>
-                    <name>library_cli</name>
+                    <name>Default</name>
                     <type>0</type>
                 </confElem>
                 <confElem>
                     <name>gdbserver</name>
                     <type>0</type>
                 </confElem>
+                <confElem>
+                    <name>flash</name>
+                    <type>0</type>
+                </confElem>
             </confList>
         </data>
     </configuration>
index a44e6e020031682e4f3e37ec6ee6a8c2aa515fa9..f58c34405e01f1b2e0fe4d04009122ed4650b184 100644 (file)
 
 
 #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 */
 
@@ -256,37 +248,38 @@ static void disable_flash_read_protection(stlink_t *sl) {
 // Delegates to the backends...
 
 void stlink_close(stlink_t *sl) {
-    D(sl, "\n*** stlink_close ***\n");
+    DLOG("*** stlink_close ***\n");
     sl->backend->close(sl);
     free(sl);
 }
 
 void stlink_exit_debug_mode(stlink_t *sl) {
-    D(sl, "\n*** stlink_exit_debug_mode ***\n");
+    DLOG("*** stlink_exit_debug_mode ***\n");
     sl->backend->exit_debug_mode(sl);
 }
 
 void stlink_enter_swd_mode(stlink_t *sl) {
-    D(sl, "\n*** stlink_enter_swd_mode ***\n");
+    DLOG("*** stlink_enter_swd_mode ***\n");
     sl->backend->enter_swd_mode(sl);
 }
 
 // Force the core into the debug mode -> halted state.
 void stlink_force_debug(stlink_t *sl) {
-    D(sl, "\n*** stlink_force_debug_mode ***\n");
+    DLOG("*** stlink_force_debug_mode ***\n");
     sl->backend->force_debug(sl);
 }
 
 void stlink_exit_dfu_mode(stlink_t *sl) {
-    D(sl, "\n*** stlink_exit_dfu_mode ***\n");
+    DLOG("*** stlink_exit_dfu_mode ***\n");
     sl->backend->exit_dfu_mode(sl);
 }
 
 uint32_t stlink_core_id(stlink_t *sl) {
-    D(sl, "\n*** stlink_core_id ***\n");
+    DLOG("*** stlink_core_id ***\n");
     sl->backend->core_id(sl);
     if (sl->verbose > 2)
         stlink_print_data(sl);
+    DLOG("core_id = 0x%08x\n", sl->core_id);
     return sl->core_id;
 }
 
@@ -313,17 +306,17 @@ void stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid) {
 }
 
 void stlink_reset(stlink_t *sl) {
-    D(sl, "\n*** stlink_reset ***\n");
+    DLOG("*** stlink_reset ***\n");
     sl->backend->reset(sl);
 }
 
 void stlink_run(stlink_t *sl) {
-    D(sl, "\n*** stlink_run ***\n");
+    DLOG("*** stlink_run ***\n");
     sl->backend->run(sl);
 }
 
 void stlink_status(stlink_t *sl) {
-    D(sl, "\n*** stlink_status ***\n");
+    DLOG("*** stlink_status ***\n");
     sl->backend->status(sl);
     stlink_core_stat(sl);
 }
@@ -354,26 +347,25 @@ void _parse_version(stlink_t *sl, stlink_version_t *slv) {
 }
 
 void stlink_version(stlink_t *sl) {
-    D(sl, "*** looking up stlink version\n");
-    stlink_version_t slv;
+    DLOG("*** looking up stlink version\n");
     sl->backend->version(sl);
-    _parse_version(sl, &slv);
+    _parse_version(sl, &sl->version);
     
-    DD(sl, "st vid         = 0x%04x (expect 0x%04x)\n", slv.st_vid, USB_ST_VID);
-    DD(sl, "stlink pid     = 0x%04x\n", slv.stlink_pid);
-    DD(sl, "stlink version = 0x%x\n", slv.stlink_v);
-    DD(sl, "jtag version   = 0x%x\n", slv.jtag_v);
-    DD(sl, "swim version   = 0x%x\n", slv.swim_v);
-    if (slv.jtag_v == 0) {
-        DD(sl, "    notice: the firmware doesn't support a jtag/swd interface\n");
+    DLOG("st vid         = 0x%04x (expect 0x%04x)\n", sl->version.st_vid, USB_ST_VID);
+    DLOG("stlink pid     = 0x%04x\n", sl->version.stlink_pid);
+    DLOG("stlink version = 0x%x\n", sl->version.stlink_v);
+    DLOG("jtag version   = 0x%x\n", sl->version.jtag_v);
+    DLOG("swim version   = 0x%x\n", sl->version.swim_v);
+    if (sl->version.jtag_v == 0) {
+        DLOG("    notice: the firmware doesn't support a jtag/swd interface\n");
     }
-    if (slv.swim_v == 0) {
-        DD(sl, "    notice: the firmware doesn't support a swim interface\n");
+    if (sl->version.swim_v == 0) {
+        DLOG("    notice: the firmware doesn't support a swim interface\n");
     }
 }
 
 void stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
-    D(sl, "\n*** stlink_write_mem32 ***\n");
+    DLOG("*** stlink_write_mem32 ***\n");
     if (len % 4 != 0) {
         fprintf(stderr, "Error: Data length doesn't have a 32 bit alignment: +%d byte.\n", len % 4);
         return;
@@ -382,7 +374,7 @@ void stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
 }
 
 void stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
-    D(sl, "\n*** stlink_read_mem32 ***\n");
+    DLOG("*** stlink_read_mem32 ***\n");
     if (len % 4 != 0) { // !!! never ever: fw gives just wrong values
         fprintf(stderr, "Error: Data length doesn't have a 32 bit alignment: +%d byte.\n",
                 len % 4);
@@ -392,23 +384,23 @@ void stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
 }
 
 void stlink_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
-    D(sl, "\n*** stlink_write_mem8 ***\n");
+    DLOG("*** stlink_write_mem8 ***\n");
     sl->backend->write_mem8(sl, addr, len);
 }
 
 void stlink_read_all_regs(stlink_t *sl, reg *regp) {
-    D(sl, "\n*** stlink_read_all_regs ***\n");
+    DLOG("*** stlink_read_all_regs ***\n");
     sl->backend->read_all_regs(sl, regp);
 }
 
 void stlink_write_reg(stlink_t *sl, uint32_t reg, int idx) {
-    D(sl, "\n*** stlink_write_reg\n");
+    DLOG("*** stlink_write_reg\n");
     sl->backend->write_reg(sl, reg, idx);
 }
 
 void stlink_read_reg(stlink_t *sl, int r_idx, reg *regp) {
-    D(sl, "\n*** stlink_read_reg\n");
-    DD(sl, " (%d) ***\n", r_idx);
+    DLOG("*** stlink_read_reg\n");
+    DLOG(" (%d) ***\n", r_idx);
 
     if (r_idx > 20 || r_idx < 0) {
         fprintf(stderr, "Error: register index must be in [0..20]\n");
@@ -425,7 +417,7 @@ unsigned int is_core_halted(stlink_t *sl) {
 }
 
 void stlink_step(stlink_t *sl) {
-    D(sl, "\n*** stlink_step ***\n");
+    DLOG("*** stlink_step ***\n");
     sl->backend->step(sl);
 }
 
@@ -433,16 +425,16 @@ int stlink_current_mode(stlink_t *sl) {
     int mode = sl->backend->current_mode(sl);
     switch (mode) {
         case STLINK_DEV_DFU_MODE:
-            DD(sl, "stlink current mode: dfu\n");
+            DLOG("stlink current mode: dfu\n");
             return mode;
         case STLINK_DEV_DEBUG_MODE:
-            DD(sl, "stlink current mode: debug (jtag or swd)\n");
+            DLOG("stlink current mode: debug (jtag or swd)\n");
             return mode;
         case STLINK_DEV_MASS_MODE:
-            DD(sl, "stlink current mode: mass\n");
+            DLOG("stlink current mode: mass\n");
             return mode;
     }
-    DD(sl, "stlink mode: unknown!\n");
+    DLOG("stlink mode: unknown!\n");
     return STLINK_DEV_UNKNOWN_MODE;
 }
 
@@ -493,11 +485,11 @@ void stlink_core_stat(stlink_t *sl) {
     switch (sl->q_buf[0]) {
         case STLINK_CORE_RUNNING:
             sl->core_stat = STLINK_CORE_RUNNING;
-            DD(sl, "  core status: running\n");
+            DLOG("  core status: running\n");
             return;
         case STLINK_CORE_HALTED:
             sl->core_stat = STLINK_CORE_HALTED;
-            DD(sl, "  core status: halted\n");
+            DLOG("  core status: halted\n");
             return;
         default:
             sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
index 2fe04e188f3830ae0f3e5026955ae6b62318fcde..bcd60aae1b204bac1a70d97b8f5295b6a50fe135 100644 (file)
@@ -185,12 +185,9 @@ extern "C" {
         stm32_addr_t sram_base;
         size_t sram_size;
 
+        struct stlink_version_ version;
     };
 
-    // some quick and dirty logging...
-    void D(stlink_t *sl, char *txt);
-    void DD(stlink_t *sl, char *format, ...);
-
     //stlink_t* stlink_quirk_open(const char *dev_name, const int verbose);
 
     // delegated functions...
index b8220de133bf2c26a5f6b92338c87b288bb8e09c..2bbbaf9d957a80d060316c05e1d302d8ea7f02ca 100644 (file)
@@ -12,7 +12,6 @@
  The stlink related constants kindly provided by Oliver Spencer (OpenOCD)
  for use in a GPL compatible license.
 
- Code format ~ TAB = 8, K&R, linux kernel source, golang oriented
  Tested compatibility: linux, gcc >= 4.3.3
 
  The communication is based on standard USB mass storage device
  CBW           - Command Block Wrapper
  CSW           - Command Status Wrapper
  RFU           - Reserved for Future Use
- scsi_pt       - SCSI pass-through
- sg            - SCSI generic
 
- * usb-storage.quirks
+ Originally, this driver used scsi pass through commands, which required the
+ usb-storage module to be loaded, providing the /dev/sgX links.  The USB mass
+ storage implementation on the STLinkv1 is however terribly broken, and it can 
+ take many minutes for the kernel to give up.
+ However, in Nov 2011, the scsi pass through was replaced by raw libusb, so 
+ instead of having to let usb-storage struggle with the device, and also greatly 
+ limiting the portability of the driver, you can now tell usb-storage to simply
+ ignore this device completely.
+   
+ usb-storage.quirks
  http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob_plain;f=Documentation/kernel-parameters.txt
  Each entry has the form VID:PID:Flags where VID and PID are Vendor and Product
  ID values (4-digit hex numbers) and Flags is a set of characters, each corresponding
 
  Example: quirks=0419:aaf5:rl,0421:0433:rc
  http://permalink.gmane.org/gmane.linux.usb.general/35053
+ For the stlinkv1, you just want the following
 
- modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:l
+ modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:i
 
  Equivalently, you can add a line saying
 
- options usb-storage quirks=483:3744:l
+ options usb-storage quirks=483:3744:i
 
  to your /etc/modprobe.conf or /etc/modprobe.d/local.conf (or add the "quirks=..."
  part to an existing options line for usb-storage).
-
- https://wiki.kubuntu.org/Kernel/Debugging/USB explains the protocoll and 
- would allow to replace the sg access to pure libusb access
  */
 
 
 #define __USE_GNU
+#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
 #include <sys/mman.h>
 
 #include "stlink-common.h"
-
-#if CONFIG_USE_LIBSG
-// sgutils2 (apt-get install libsgutils2-dev)
-#include <scsi/sg_lib.h>
-#include <scsi/sg_pt.h>
 #include "stlink-sg.h"
-#endif
+#include "uglylogging.h"
 
+#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)
 
 // Suspends execution of the calling process for
 // (at least) ms milliseconds.
@@ -106,28 +113,195 @@ static void clear_cdb(struct stlink_libsg *sl) {
     sl->q_data_dir = Q_DATA_IN;
 }
 
-// E.g. make the valgrind happy.
-
-static void clear_buf(stlink_t *sl) {
-    DD(sl, "*** clear_buf ***\n");
-    for (size_t i = 0; i < sizeof (sl->q_buf); i++)
-        sl->q_buf[i] = 0;
-
-}
-
 // close the device, free the allocated memory
 
 void _stlink_sg_close(stlink_t *sl) {
     if (sl) {
+#if FINISHED_WITH_SG
         struct stlink_libsg *slsg = sl->backend_data;
         scsi_pt_close_device(slsg->sg_fd);
         free(slsg);
+#endif
+    }
+}
+
+static int get_usb_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint32_t *tag)
+{
+    unsigned char csw[13];
+    memset(csw, 0, sizeof(csw));
+    int transferred;
+    int ret;
+    int try = 0;
+    do {
+        ret = libusb_bulk_transfer(handle, endpoint, (unsigned char *)&csw, sizeof(csw),
+                                   &transferred, SG_TIMEOUT_MSEC);
+        if (ret == LIBUSB_ERROR_PIPE) {
+            libusb_clear_halt(handle, endpoint);
+        }
+        try++;
+    } while ((ret == LIBUSB_ERROR_PIPE) && (try < 3));
+    if (ret != LIBUSB_SUCCESS) {
+        fprintf(stderr, "%s: receiving failed: %d\n", __func__, ret);
+        return -1;
+    }
+    if (transferred != sizeof(csw)) {
+        fprintf(stderr, "%s: received unexpected amount: %d\n", __func__, transferred);
+        return -1;
+    }
+
+    uint32_t rsig = read_uint32(csw, 0);
+    uint32_t rtag = read_uint32(csw, 4);
+    uint32_t residue = read_uint32(csw, 8);
+#define USB_CSW_SIGNATURE 0x53425355  // 'U' 'S' 'B' 'S' (reversed)
+    if (rsig != USB_CSW_SIGNATURE) {
+        WLOG("status signature was invalid: %#x\n", rsig);
+        return -1;
+    }
+    DLOG("residue was= %#x\n", residue);
+    *tag = rtag;
+    uint8_t rstatus = csw[12];
+    DLOG("rstatus = %x\n", rstatus);
+    return rstatus;
+}
+
+static int dump_CDB_command(uint8_t *cdb, uint8_t cdb_len) {
+    char dbugblah[100];
+    char *dbugp = dbugblah;
+    dbugp += sprintf(dbugp, "Sending CDB [");
+    for (uint8_t i = 0; i < cdb_len; i++) {
+        dbugp += sprintf(dbugp, " %#02x", (unsigned int) cdb[i]);
+    }
+    sprintf(dbugp, "]\n");
+    DLOG(dbugblah);
+    return 0;
+}
+
+/**
+ * Wraps a CDB mass storage command in the appropriate gunk to get it down
+ * @param handle
+ * @param endpoint
+ * @param cdb
+ * @param cdb_length
+ * @param lun
+ * @param flags
+ * @param expected_rx_size
+ * @return 
+ */
+int send_usb_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint_out,
+                              uint8_t *cdb, uint8_t cdb_length,
+                              uint8_t lun, uint8_t flags, uint32_t expected_rx_size) {
+    DLOG("Sending usb m-s cmd: cdblen:%d, rxsize=%d\n", cdb_length, expected_rx_size);
+    dump_CDB_command(cdb, cdb_length);
+
+    static uint32_t tag;
+    if (tag == 0) {
+        tag = 1;
+    }
+
+    int try = 0;
+    int ret = 0;
+    int real_transferred;
+    int i = 0;
+
+    uint8_t c_buf[STLINK_SG_SIZE];
+    // tag is allegedly ignored... TODO - verify
+    c_buf[i++] = 'U';
+    c_buf[i++] = 'S';
+    c_buf[i++] = 'B';
+    c_buf[i++] = 'C';
+    write_uint32(&c_buf[i], tag);
+    uint32_t this_tag = tag++;
+    write_uint32(&c_buf[i+4], expected_rx_size);
+    i+= 8;
+    c_buf[i++] = flags;
+    c_buf[i++] = lun;
+
+    c_buf[i++] = cdb_length;
+
+    // Now the actual CDB request
+    assert(cdb_length <= CDB_SL);
+    memcpy(&(c_buf[i]), cdb, cdb_length);
+    
+    int sending_length = STLINK_SG_SIZE;
+    DLOG("sending length set to: %d\n", sending_length);
+    
+    // send....
+    do {
+        DLOG("attempting tx...\n");
+        ret = libusb_bulk_transfer(handle, endpoint_out, c_buf, sending_length,
+                                   &real_transferred, SG_TIMEOUT_MSEC);
+        if (ret == LIBUSB_ERROR_PIPE) {
+            libusb_clear_halt(handle, endpoint_out);
+        }
+        try++;
+    } while ((ret == LIBUSB_ERROR_PIPE) && (try < 3));
+    if (ret != LIBUSB_SUCCESS) {
+        WLOG("sending failed: %d\n", ret);
+        return -1;
+    }
+    DLOG("Actually sent: %d, returning tag: %d\n", real_transferred, tag);
+    return this_tag;
+}
+
+
+/**
+ * Straight from stm8 stlink code...
+ * @param handle
+ * @param endpoint_in
+ * @param endpoint_out
+ */
+static void
+get_sense(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out)
+{
+    DLOG("Fetching sense...\n");
+    uint8_t cdb[16];
+    memset(cdb, 0, sizeof(cdb));
+#define REQUEST_SENSE 0x03
+#define REQUEST_SENSE_LENGTH 18
+    cdb[0] = REQUEST_SENSE;
+    cdb[4] = REQUEST_SENSE_LENGTH;
+    uint32_t tag = send_usb_mass_storage_command(handle, endpoint_out, cdb, sizeof(cdb), 0,
+                                                 LIBUSB_ENDPOINT_IN, REQUEST_SENSE_LENGTH);
+    if (tag == 0) {
+        WLOG("refusing to send request sense with tag 0\n");
+        return;
+    }
+    unsigned char sense[REQUEST_SENSE_LENGTH];
+    int transferred;
+    int ret;
+    int try = 0;
+    do {
+        ret = libusb_bulk_transfer(handle, endpoint_in, sense, sizeof(sense),
+                                   &transferred, SG_TIMEOUT_MSEC);
+        if (ret == LIBUSB_ERROR_PIPE) {
+            libusb_clear_halt(handle, endpoint_in);
+        }
+        try++;
+    } while ((ret == LIBUSB_ERROR_PIPE) && (try < 3));
+    if (ret != LIBUSB_SUCCESS) {
+        WLOG("receiving sense failed: %d\n", ret);
+        return;
+    }
+    if (transferred != sizeof(sense)) {
+        WLOG("received unexpected amount of sense: %d != %d\n", transferred, sizeof(sense));
+    }
+    uint32_t received_tag;
+    int status = get_usb_mass_storage_status(handle, endpoint_in, &received_tag);
+    if (status != 0) {
+        WLOG("receiving sense failed with status: %02x\n", status);
+        return;
+    }
+    if (sense[0] != 0x70 && sense[0] != 0x71) {
+        WLOG("No sense data\n");
+    } else {
+        WLOG("Sense KCQ: %02X %02X %02X\n", sense[2] & 0x0f, sense[12], sense[13]);
     }
 }
 
 
 //TODO rewrite/cleanup, save the error in sl
 
+#if FINISHED_WITH_SG
 static void stlink_confirm_inq(stlink_t *stl, struct sg_pt_base *ptvp) {
     struct stlink_libsg *sl = stl->backend_data;
     const int e = sl->do_scsi_pt_err;
@@ -144,7 +318,7 @@ static void stlink_confirm_inq(stlink_t *stl, struct sg_pt_base *ptvp) {
     }
     const int duration = get_scsi_pt_duration_ms(ptvp);
     if ((stl->verbose > 1) && (duration >= 0))
-        DD(stl, "      duration=%d ms\n", duration);
+        DLOG("      duration=%d ms\n", duration);
 
     // XXX stlink fw sends broken residue, so ignore it and use the known q_len
     // "usb-storage quirks=483:3744:r"
@@ -161,7 +335,7 @@ static void stlink_confirm_inq(stlink_t *stl, struct sg_pt_base *ptvp) {
     switch (cat) {
         case SCSI_PT_RESULT_GOOD:
             if (stl->verbose && (resid > 0))
-                DD(stl, "      notice: requested %d bytes but "
+                DLOG("      notice: requested %d bytes but "
                     "got %d bytes, ignore [broken] residue = %d\n",
                     stl->q_len, dsize, resid);
             break;
@@ -170,7 +344,7 @@ static void stlink_confirm_inq(stlink_t *stl, struct sg_pt_base *ptvp) {
                 sg_get_scsi_status_str(
                         get_scsi_pt_status_response(ptvp), sizeof (buf),
                         buf);
-                DD(stl, "  scsi status: %s\n", buf);
+                DLOG("  scsi status: %s\n", buf);
             }
             return;
         case SCSI_PT_RESULT_SENSE:
@@ -178,11 +352,11 @@ static void stlink_confirm_inq(stlink_t *stl, struct sg_pt_base *ptvp) {
             if (stl->verbose) {
                 sg_get_sense_str("", sl->sense_buf, slen, (stl->verbose
                         > 1), sizeof (buf), buf);
-                DD(stl, "%s", buf);
+                DLOG("%s", buf);
             }
             if (stl->verbose && (resid > 0)) {
                 if ((stl->verbose) || (stl->q_len > 0))
-                    DD(stl, "    requested %d bytes but "
+                    DLOG("    requested %d bytes but "
                         "got %d bytes\n", stl->q_len, dsize);
             }
             return;
@@ -195,13 +369,13 @@ static void stlink_confirm_inq(stlink_t *stl, struct sg_pt_base *ptvp) {
                 // The 'host_status' field has the following values:
                 //     [0x07] Internal error detected in the host adapter.
                 // This may not be fatal (and the command may have succeeded).
-                DD(stl, "  transport: %s", buf);
+                DLOG("  transport: %s", buf);
             }
             return;
         case SCSI_PT_RESULT_OS_ERR:
             if (stl->verbose) {
                 get_scsi_pt_os_err_str(ptvp, sizeof (buf), buf);
-                DD(stl, "  os: %s", buf);
+                DLOG("  os: %s", buf);
             }
             return;
         default:
@@ -209,14 +383,121 @@ static void stlink_confirm_inq(stlink_t *stl, struct sg_pt_base *ptvp) {
                     "category (%d)\n", cat);
     }
 }
+#endif
 
-void stlink_q(stlink_t *sl) {
+/**
+ * Just send a buffer on an endpoint, no questions asked.
+ * Handles repeats, and time outs.  Also handles reading status reports and sense
+ * @param handle libusb device *
+ * @param endpoint_out sends 
+ * @param endpoint_in used to read status reports back in 
+ * @param cbuf  what to send
+ * @param length how much to send
+ * @return number of bytes actually sent, or -1 for failures.
+ */
+int send_usb_data_only(libusb_device_handle *handle, unsigned char endpoint_out,
+    unsigned char endpoint_in, unsigned char *cbuf, unsigned int length) {
+    int ret;
+    int real_transferred;
+    int try;
+    do {
+        DLOG("attempting tx...\n");
+        ret = libusb_bulk_transfer(handle, endpoint_out, cbuf, length,
+                                   &real_transferred, SG_TIMEOUT_MSEC);
+        if (ret == LIBUSB_ERROR_PIPE) {
+            libusb_clear_halt(handle, endpoint_out);
+        }
+        try++;
+    } while ((ret == LIBUSB_ERROR_PIPE) && (try < 3));
+    if (ret != LIBUSB_SUCCESS) {
+        WLOG("sending failed: %d\n", ret);
+        return -1;
+    }
+    DLOG("Actually sent: %d\n", real_transferred);
+    
+    // now, swallow up the status, so that things behave nicely...
+    uint32_t received_tag;
+    // -ve is for my errors, 0 is good, +ve is libusb sense status bytes
+    int status = get_usb_mass_storage_status(handle, endpoint_in, &received_tag);
+    if (status < 0) {
+        WLOG("receiving status failed: %d\n", status);
+        return -1;
+    }
+    if (status != 0) {
+        WLOG("receiving status not passed :(: %02x\n", status);
+    }
+    if (status == 1) {
+        get_sense(handle, endpoint_in, endpoint_out);
+        return -1;
+    }
+    
+    return real_transferred;
+}
+
+
+int stlink_q(stlink_t *sl) {
     struct stlink_libsg* sg = sl->backend_data;
-    DD(sl, "CDB[");
-    for (int i = 0; i < CDB_SL; i++)
-        DD(sl, " 0x%02x", (unsigned int) sg->cdb_cmd_blk[i]);
-    DD(sl, "]\n");
+    //uint8_t cdb_len = 6;  // FIXME varies!!!
+    uint8_t cdb_len = 10;  // FIXME varies!!!
+    uint8_t lun = 0;  // always zero...
+    uint32_t tag = send_usb_mass_storage_command(sg->usb_handle, sg->ep_req, 
+        sg->cdb_cmd_blk, cdb_len, lun, LIBUSB_ENDPOINT_IN, sl->q_len);
+    
+    
+    // now wait for our response...
+    // length copied from stlink-usb...
+    int rx_length = sl->q_len;
+    int try = 0;
+    int real_transferred;
+    int ret;
+    if (rx_length > 0) {
+        do {
+            DLOG("attempting rx\n");
+            ret = libusb_bulk_transfer(sg->usb_handle, sg->ep_rep, sl->q_buf, rx_length, 
+                &real_transferred, SG_TIMEOUT_MSEC);
+            if (ret == LIBUSB_ERROR_PIPE) {
+                libusb_clear_halt(sg->usb_handle, sg->ep_req);
+            }
+            try++;
+        } while ((ret == LIBUSB_ERROR_PIPE) && (try < 3));
+
+        if (ret != LIBUSB_SUCCESS) {
+            WLOG("Receiving failed: %d\n", ret);
+            return -1;
+        }
+
+        if (real_transferred != rx_length) {
+            WLOG("received unexpected amount: %d != %d\n", real_transferred, rx_length);
+        }
+    }
 
+    uint32_t received_tag;
+    // -ve is for my errors, 0 is good, +ve is libusb sense status bytes
+    int status = get_usb_mass_storage_status(sg->usb_handle, sg->ep_rep, &received_tag);
+    if (status < 0) {
+        WLOG("receiving status failed: %d\n", status);
+        return -1;
+    }
+    if (status != 0) {
+        WLOG("receiving status not passed :(: %02x\n", status);
+    }
+    if (status == 1) {
+        get_sense(sg->usb_handle, sg->ep_rep, sg->ep_req);
+        return -1;
+    }
+    if (received_tag != tag) {
+        WLOG("received tag %d but expected %d\n", received_tag, tag);
+        //return -1;
+    }
+    if (rx_length > 0 && real_transferred != rx_length) {
+        return -1;
+    }
+    return 0;
+
+        
+    DLOG("Actually received: %d\n", real_transferred);
+
+#if FINISHED_WITH_SG
     // Get control command descriptor of scsi structure,
     // (one object per command!!)
     struct sg_pt_base *ptvp = construct_scsi_pt_obj();
@@ -245,6 +526,7 @@ void stlink_q(stlink_t *sl) {
     stlink_confirm_inq(sl, ptvp);
     // TODO recycle: clear_scsi_pt_obj(struct sg_pt_base * objp);
     destruct_scsi_pt_obj(ptvp);
+#endif
 }
 
 // TODO thinking, cleanup
@@ -257,55 +539,27 @@ void stlink_stat(stlink_t *stl, char *txt) {
 
     switch (stl->q_buf[0]) {
         case STLINK_OK:
-            DD(stl, "  %s: ok\n", txt);
+            DLOG("  %s: ok\n", txt);
             return;
         case STLINK_FALSE:
-            DD(stl, "  %s: false\n", txt);
+            DLOG("  %s: false\n", txt);
             return;
         default:
-            DD(stl, "  %s: unknown\n", txt);
+            DLOG("  %s: unknown\n", txt);
     }
 }
 
 
-static void parse_version(stlink_t *stl) {
-  struct stlink_libsg *sl = stl->backend_data;
-
-  sl->st_vid = 0;
-  sl->stlink_pid = 0;
-
-  if (stl->q_len <= 0) {
-    fprintf(stderr, "Error: could not parse the stlink version");
-    return;
-  }
-
-  uint32_t b0 = stl->q_buf[0]; //lsb
-  uint32_t b1 = stl->q_buf[1];
-  uint32_t b2 = stl->q_buf[2];
-  uint32_t b3 = stl->q_buf[3];
-  uint32_t b4 = stl->q_buf[4];
-  uint32_t b5 = stl->q_buf[5]; //msb
-
-  // b0 b1 || b2 b3 | b4 b5
-  // 4b | 6b | 6b || 2B | 2B
-  // stlink_v | jtag_v | swim_v || st_vid | stlink_pid
-
-  sl->stlink_v = (b0 & 0xf0) >> 4;
-  sl->jtag_v = ((b0 & 0x0f) << 2) | ((b1 & 0xc0) >> 6);
-  sl->swim_v = b1 & 0x3f;
-  sl->st_vid = (b3 << 8) | b2;
-  sl->stlink_pid = (b5 << 8) | b4;
-}
-
 void _stlink_sg_version(stlink_t *stl) {
     struct stlink_libsg *sl = stl->backend_data;
-    D(stl, "\n*** stlink_version ***\n");
+    DLOG("\n*** stlink_version ***\n");
     clear_cdb(sl);
     sl->cdb_cmd_blk[0] = STLINK_GET_VERSION;
     stl->q_len = 6;
     sl->q_addr = 0;
     stlink_q(stl);
-    parse_version(stl);
+    // HACK use my own private version right now...
+    
 }
 
 // Get stlink mode:
@@ -338,7 +592,7 @@ void _stlink_sg_enter_swd_mode(stlink_t *sl) {
 
 void _stlink_sg_enter_jtag_mode(stlink_t *sl) {
     struct stlink_libsg *sg = sl->backend_data;
-    D(sl, "\n*** stlink_enter_jtag_mode ***\n");
+    DLOG("\n*** stlink_enter_jtag_mode ***\n");
     clear_cdb(sg);
     sg->cdb_cmd_blk[1] = STLINK_DEBUG_ENTER;
     sg->cdb_cmd_blk[2] = STLINK_DEBUG_ENTER_JTAG;
@@ -350,7 +604,7 @@ void _stlink_sg_enter_jtag_mode(stlink_t *sl) {
 
 void _stlink_sg_exit_dfu_mode(stlink_t *sl) {
     struct stlink_libsg *sg = sl->backend_data;
-    D(sl, "\n*** stlink_exit_dfu_mode ***\n");
+    DLOG("\n*** stlink_exit_dfu_mode ***\n");
     clear_cdb(sg);
     sg->cdb_cmd_blk[0] = STLINK_DFU_COMMAND;
     sg->cdb_cmd_blk[1] = STLINK_DFU_EXIT;
@@ -427,7 +681,7 @@ void _stlink_sg_reset(stlink_t *sl) {
 
 void _stlink_sg_status(stlink_t *sl) {
     struct stlink_libsg *sg = sl->backend_data;
-    D(sl, "\n*** stlink_status ***\n");
+    DLOG("\n*** stlink_status ***\n");
     clear_cdb(sg);
     sg->cdb_cmd_blk[1] = STLINK_DEBUG_GETSTATUS;
     sl->q_len = 2;
@@ -439,7 +693,7 @@ void _stlink_sg_status(stlink_t *sl) {
 
 void _stlink_sg_force_debug(stlink_t *sl) {
     struct stlink_libsg *sg = sl->backend_data;
-    D(sl, "\n*** stlink_force_debug ***\n");
+    DLOG("\n*** stlink_force_debug ***\n");
     clear_cdb(sg);
     sg->cdb_cmd_blk[1] = STLINK_DEBUG_FORCEDEBUG;
     sl->q_len = 2;
@@ -470,7 +724,7 @@ void _stlink_sg_read_all_regs(stlink_t *sl, reg *regp) {
     for (int i = 0; i < 16; i++) {
         sg->reg.r[i] = read_uint32(sl->q_buf, 4 * i);
         if (sl->verbose > 1)
-            DD(sl, "r%2d = 0x%08x\n", i, sg->reg.r[i]);
+            DLOG("r%2d = 0x%08x\n", i, sg->reg.r[i]);
     }
     sg->reg.xpsr = read_uint32(sl->q_buf, 64);
     sg->reg.main_sp = read_uint32(sl->q_buf, 68);
@@ -480,11 +734,11 @@ void _stlink_sg_read_all_regs(stlink_t *sl, reg *regp) {
     if (sl->verbose < 2)
         return;
 
-    DD(sl, "xpsr       = 0x%08x\n", sg->reg.xpsr);
-    DD(sl, "main_sp    = 0x%08x\n", sg->reg.main_sp);
-    DD(sl, "process_sp = 0x%08x\n", sg->reg.process_sp);
-    DD(sl, "rw         = 0x%08x\n", sg->reg.rw);
-    DD(sl, "rw2        = 0x%08x\n", sg->reg.rw2);
+    DLOG("xpsr       = 0x%08x\n", sg->reg.xpsr);
+    DLOG("main_sp    = 0x%08x\n", sg->reg.main_sp);
+    DLOG("process_sp = 0x%08x\n", sg->reg.process_sp);
+    DLOG("rw         = 0x%08x\n", sg->reg.rw);
+    DLOG("rw2        = 0x%08x\n", sg->reg.rw2);
 }
 
 // Read an arm-core register, the index must be in the range 0..20.
@@ -505,7 +759,7 @@ void _stlink_sg_read_reg(stlink_t *sl, int r_idx, reg *regp) {
     stlink_print_data(sl);
 
     uint32_t r = read_uint32(sl->q_buf, 0);
-    DD(sl, "r_idx (%2d) = 0x%08x\n", r_idx, r);
+    DLOG("r_idx (%2d) = 0x%08x\n", r_idx, r);
 
     switch (r_idx) {
         case 16:
@@ -552,7 +806,7 @@ void _stlink_sg_write_reg(stlink_t *sl, uint32_t reg, int idx) {
 
 void stlink_write_dreg(stlink_t *sl, uint32_t reg, uint32_t addr) {
     struct stlink_libsg *sg = sl->backend_data;
-    D(sl, "\n*** stlink_write_dreg ***\n");
+    DLOG("\n*** stlink_write_dreg ***\n");
     clear_cdb(sg);
     sg->cdb_cmd_blk[1] = STLINK_DEBUG_WRITEDEBUGREG;
     // 2-5: address of reg of the debug module
@@ -569,7 +823,7 @@ void stlink_write_dreg(stlink_t *sl, uint32_t reg, uint32_t addr) {
 
 void _stlink_sg_run(stlink_t *sl) {
     struct stlink_libsg *sg = sl->backend_data;
-    D(sl, "\n*** stlink_run ***\n");
+    DLOG("\n*** stlink_run ***\n");
     clear_cdb(sg);
     sg->cdb_cmd_blk[1] = STLINK_DEBUG_RUNCORE;
     sl->q_len = 2;
@@ -594,7 +848,7 @@ void _stlink_sg_step(stlink_t *sl) {
 // see Cortex-M3 Technical Reference Manual
 // TODO make delegate!
 void stlink_set_hw_bp(stlink_t *sl, int fp_nr, uint32_t addr, int fp) {
-    D(sl, "\n*** stlink_set_hw_bp ***\n");
+    DLOG("\n*** stlink_set_hw_bp ***\n");
     struct stlink_libsg *sg = sl->backend_data;
     clear_cdb(sg);
     sg->cdb_cmd_blk[1] = STLINK_DEBUG_SETFP;
@@ -615,7 +869,7 @@ void stlink_set_hw_bp(stlink_t *sl, int fp_nr, uint32_t addr, int fp) {
 // TODO make delegate!
 void stlink_clr_hw_bp(stlink_t *sl, int fp_nr) {
     struct stlink_libsg *sg = sl->backend_data;
-    D(sl, "\n*** stlink_clr_hw_bp ***\n");
+    DLOG("\n*** stlink_clr_hw_bp ***\n");
     clear_cdb(sg);
     sg->cdb_cmd_blk[1] = STLINK_DEBUG_CLEARFP;
     sg->cdb_cmd_blk[2] = fp_nr;
@@ -658,11 +912,10 @@ void _stlink_sg_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
     write_uint32(sg->cdb_cmd_blk + 2, addr);
     write_uint16(sg->cdb_cmd_blk + 6, len);
 
-    // data_out 0-len
-    sl->q_len = len;
-    sg->q_addr = addr;
-    sg->q_data_dir = Q_DATA_OUT;
-    stlink_q(sl);
+    // this sends the command...
+    send_usb_mass_storage_command(sg->usb_handle, sg->ep_req, sg->cdb_cmd_blk, CDB_SL, 0, 0, 0);
+    // This sends the data...
+    send_usb_data_only(sg->usb_handle, sg->ep_req, sg->ep_rep, sl->q_buf, len);
     stlink_print_data(sl);
 }
 
@@ -677,11 +930,11 @@ void _stlink_sg_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
     write_uint32(sg->cdb_cmd_blk + 2, addr);
     write_uint16(sg->cdb_cmd_blk + 6, len);
 
-    // data_out 0-0x40-...-len
-    sl->q_len = len;
-    sg->q_addr = addr;
-    sg->q_data_dir = Q_DATA_OUT;
-    stlink_q(sl);
+    // this sends the command...
+    send_usb_mass_storage_command(sg->usb_handle, sg->ep_req, sg->cdb_cmd_blk, CDB_SL, 0, 0, 0);
+    // This sends the data...
+    send_usb_data_only(sg->usb_handle, sg->ep_req, sg->ep_rep, sl->q_buf, len);
+
     stlink_print_data(sl);
 }
 
@@ -760,30 +1013,95 @@ stlink_backend_t _stlink_sg_backend = {
     _stlink_sg_force_debug
 };
 
-stlink_t* stlink_open(const char *dev_name, const int verbose) {
-    fprintf(stderr, "\n*** stlink_open [%s] ***\n", dev_name);
-    int sg_fd = scsi_pt_open_device(dev_name, RDWR, verbose);
-    if (sg_fd < 0) {
-        fprintf(stderr, "error opening device: %s: %s\n", dev_name,
-                safe_strerror(-sg_fd));
-        return NULL;
-    }
-
+static stlink_t* stlink_open(const int verbose) {
+    
     stlink_t *sl = malloc(sizeof (stlink_t));
+    memset(sl, 0, sizeof(stlink_t));
     struct stlink_libsg *slsg = malloc(sizeof (struct stlink_libsg));
     if (sl == NULL || slsg == NULL) {
-        fprintf(stderr, "Couldn't malloc stlink and stlink_sg structures out of memory!\n");
+        WLOG("Couldn't malloc stlink and stlink_sg structures out of memory!\n");
         return NULL;
     }
+    
+    if (libusb_init(&(slsg->libusb_ctx))) {
+        WLOG("failed to init libusb context, wrong version of libraries?\n");
+        free(sl);
+        free(slsg);
+        return NULL;
+    }
+    
+    libusb_set_debug(slsg->libusb_ctx, 3);
+    
+    slsg->usb_handle = libusb_open_device_with_vid_pid(slsg->libusb_ctx, USB_ST_VID, USB_STLINK_PID);
+    if (slsg->usb_handle == NULL) {
+        WLOG("Failed to find an stlink v1 by VID:PID\n");
+        libusb_close(slsg->usb_handle);
+        free(sl);
+        free(slsg);
+        return NULL;
+    }
+    
+    // TODO 
+    // Could read the interface config descriptor, and assert lots of the assumptions
+    
+    // assumption: numInterfaces is always 1...
+    if (libusb_kernel_driver_active(slsg->usb_handle, 0) == 1) {
+        int r = libusb_detach_kernel_driver(slsg->usb_handle, 0);
+        if (r < 0) {
+            WLOG("libusb_detach_kernel_driver(() error %s\n", strerror(-r));
+            libusb_close(slsg->usb_handle);
+            free(sl);
+            free(slsg);
+            return NULL;
+        }
+        DLOG("Kernel driver was successfully detached\n");
+    }
+
+    int config;
+    if (libusb_get_configuration(slsg->usb_handle, &config)) {
+        /* this may fail for a previous configured device */
+        WLOG("libusb_get_configuration()\n");
+        libusb_close(slsg->usb_handle);
+        free(sl);
+        free(slsg);
+        return NULL;
+
+    }
+    
+    // assumption: bConfigurationValue is always 1
+    if (config != 1) {
+        WLOG("Your stlink got into a real weird configuration, trying to fix it!\n");
+        DLOG("setting new configuration (%d -> 1)\n", config);
+        if (libusb_set_configuration(slsg->usb_handle, 1)) {
+            /* this may fail for a previous configured device */
+            WLOG("libusb_set_configuration() failed\n");
+            libusb_close(slsg->usb_handle);
+            free(sl);
+            free(slsg);
+            return NULL;
+        }
+    }
+
+    if (libusb_claim_interface(slsg->usb_handle, 0)) {
+        WLOG("libusb_claim_interface() failed\n");
+        libusb_close(slsg->usb_handle);
+        free(sl);
+        free(slsg);
+        return NULL;
+    }
+
+    // assumption: endpoint config is fixed mang. really.
+    slsg->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
+    slsg->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
+    
+    DLOG("Successfully opened stlinkv1 by libusb :)\n");
+    
     sl->verbose = verbose;
     sl->backend_data = slsg;
     sl->backend = &_stlink_sg_backend;
 
-    slsg->sg_fd = sg_fd;
     sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
-    slsg->core_id = 0;
     slsg->q_addr = 0;
-    clear_buf(sl);
 
     /* flash memory settings */
     sl->flash_base = STM32_FLASH_BASE;
@@ -803,28 +1121,23 @@ stlink_t* stlink_open(const char *dev_name, const int verbose) {
 
 
 
-stlink_t* stlink_quirk_open(const char *dev_name, const int verbose) {
-
-    stlink_t *sl = stlink_open(dev_name, verbose);
+stlink_t* stlink_v1_open(const int verbose) {
+    ugly_init(verbose);
+    stlink_t *sl = stlink_open(verbose);
     if (sl == NULL) {
         fputs("Error: could not open stlink device\n", stderr);
         return NULL;
     }
 
     stlink_version(sl);
-    struct stlink_libsg *sg = sl->backend_data;
 
-    if ((sg->st_vid != USB_ST_VID) || (sg->stlink_pid != USB_STLINK_PID)) {
-        fprintf(stderr, "Error: the device %s is not a stlink\n",
-                dev_name);
-        fprintf(stderr, "       VID: got %04x expect %04x \n",
-                sg->st_vid, USB_ST_VID);
-        fprintf(stderr, "       PID: got %04x expect %04x \n",
-                sg->stlink_pid, USB_STLINK_PID);
+    if ((sl->version.st_vid != USB_ST_VID) || (sl->version.stlink_pid != USB_STLINK_PID)) {
+        ugly_log(UERROR, LOG_TAG, 
+            "WTF? successfully opened, but unable to read version details. BROKEN!\n");
         return NULL;
     }
 
-    D(sl, "\n*** stlink_force_open ***\n");
+    DLOG("Reading current mode...\n");
     switch (stlink_current_mode(sl)) {
         case STLINK_DEV_MASS_MODE:
             return sl;
@@ -832,15 +1145,18 @@ stlink_t* stlink_quirk_open(const char *dev_name, const int verbose) {
             // TODO go to mass?
             return sl;
     }
-    DD(sl, "\n*** switch the stlink to mass mode ***\n");
+
+    DLOG("Attempting to exit DFU mode\n");
     _stlink_sg_exit_dfu_mode(sl);
+    
     // exit the dfu mode -> the device is gone
-    DD(sl, "\n*** reopen the stlink device ***\n");
+    DLOG("\n*** reopen the stlink device ***\n");
     delay(1000);
     stlink_close(sl);
     delay(5000);
 
-    sl = stlink_open(dev_name, verbose);
+    DLOG("Attempting to reopen the stlink...\n");
+    sl = stlink_open(verbose);
     if (sl == NULL) {
         fputs("Error: could not open stlink device\n", stderr);
         return NULL;
@@ -849,18 +1165,3 @@ stlink_t* stlink_quirk_open(const char *dev_name, const int verbose) {
     stlink_version(sl);
     return sl;
 }
-
-static void __attribute__((unused)) mark_buf(stlink_t *sl) {
-    clear_buf(sl);
-    sl->q_buf[0] = 0x12;
-    sl->q_buf[1] = 0x34;
-    sl->q_buf[2] = 0x56;
-    sl->q_buf[3] = 0x78;
-    sl->q_buf[4] = 0x90;
-    sl->q_buf[15] = 0x42;
-    sl->q_buf[16] = 0x43;
-    sl->q_buf[63] = 0x42;
-    sl->q_buf[64] = 0x43;
-    sl->q_buf[1024 * 6 - 1] = 0x42; //6kB
-    sl->q_buf[1024 * 8 - 1] = 0x42; //8kB
-}
index 523b7d8836927df90385f94df8ca1613307bd390..d4d7723b84a7327fb608586ef889f3c1c6920c9f 100644 (file)
@@ -19,6 +19,7 @@ extern "C" {
 #define RDWR           0
 #define RO             1
 #define SG_TIMEOUT_SEC 1 // actually 1 is about 2 sec
+#define SG_TIMEOUT_MSEC        3 * 1000
     // Each CDB can be a total of 6, 10, 12, or 16 bytes, later version
     // of the SCSI standard also allow for variable-length CDBs (min. CDB is 6).
     // the stlink needs max. 10 bytes.
@@ -40,8 +41,12 @@ extern "C" {
 
 
 
-#if defined(CONFIG_USE_LIBUSB)
     struct stlink_libsg {
+        libusb_context* libusb_ctx;
+        libusb_device_handle *usb_handle;
+        unsigned ep_rep;
+        unsigned ep_req;
+        
         int sg_fd;
         int do_scsi_pt_err;
 
@@ -52,22 +57,13 @@ extern "C" {
         uint32_t q_addr;
 
         // Sense (error information) data
+        // obsolete, this was fed to the scsi tools
         unsigned char sense_buf[SENSE_BUF_LEN];
 
-        uint32_t st_vid;
-        uint32_t stlink_pid;
-        uint32_t stlink_v;
-        uint32_t jtag_v;
-        uint32_t swim_v;
-        uint32_t core_id;
-
         reg reg;
     };
-#else
-    struct stlink_libsg {};
-#endif
 
-    stlink_t* stlink_quirk_open(const char *dev_name, const int verbose);
+    stlink_t* stlink_v1_open(const int verbose);
 
 #ifdef __cplusplus
 }
index 4fb3a1c3204d91dc81f9e67b5a75b89eaae4d439..785f7beca46647fa9eebf99b146eddebab292a73 100644 (file)
@@ -5,8 +5,17 @@
 #include <time.h>
 #include <sys/types.h>
 #include <libusb-1.0/libusb.h>
+
 #include "stlink-common.h"
 #include "stlink-usb.h"
+#include "uglylogging.h"
+
+#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)
+
 
 enum SCSI_Generic_Direction {SG_DXFER_TO_DEV=0, SG_DXFER_FROM_DEV=0x80};
 
@@ -116,7 +125,10 @@ ssize_t send_recv(struct stlink_libusb* handle, int terminate,
         if (submit_wait(handle, handle->rep_trans)) return -1;
         res = handle->rep_trans->actual_length;
     }
+    
     if ((handle->protocoll == 1) && terminate) {
+        fprintf(stderr, "This is never used....\n");
+        exit(EXIT_FAILURE);
         /* Read the SG reply */
         unsigned char sg_buf[13];
         libusb_fill_bulk_transfer
@@ -172,6 +184,7 @@ static int fill_command
     int i = 0;
     memset(cmd, 0, sizeof (sl->c_buf));
     if(slu->protocoll == 1) {
+        fprintf(stderr, "This is never used....\n");
         cmd[i++] = 'U';
         cmd[i++] = 'S';
         cmd[i++] = 'B';
@@ -468,11 +481,11 @@ void _stlink_usb_read_all_regs(stlink_t *sl, reg *regp) {
     if (sl->verbose < 2)
         return;
 
-    DD(sl, "xpsr       = 0x%08x\n", read_uint32(sl->q_buf, 64));
-    DD(sl, "main_sp    = 0x%08x\n", read_uint32(sl->q_buf, 68));
-    DD(sl, "process_sp = 0x%08x\n", read_uint32(sl->q_buf, 72));
-    DD(sl, "rw         = 0x%08x\n", read_uint32(sl->q_buf, 76));
-    DD(sl, "rw2        = 0x%08x\n", read_uint32(sl->q_buf, 80));
+    DLOG("xpsr       = 0x%08x\n", read_uint32(sl->q_buf, 64));
+    DLOG("main_sp    = 0x%08x\n", read_uint32(sl->q_buf, 68));
+    DLOG("process_sp = 0x%08x\n", read_uint32(sl->q_buf, 72));
+    DLOG("rw         = 0x%08x\n", read_uint32(sl->q_buf, 76));
+    DLOG("rw2        = 0x%08x\n", read_uint32(sl->q_buf, 80));
 }
 
 void _stlink_usb_read_reg(stlink_t *sl, int r_idx, reg *regp) {
@@ -495,7 +508,7 @@ void _stlink_usb_read_reg(stlink_t *sl, int r_idx, reg *regp) {
     sl->q_len = (size_t) size;
     stlink_print_data(sl);
     r = read_uint32(sl->q_buf, 0);
-    DD(sl, "r_idx (%2d) = 0x%08x\n", r_idx, r);
+    DLOG("r_idx (%2d) = 0x%08x\n", r_idx, r);
     
     switch (r_idx) {
     case 16:
@@ -580,70 +593,38 @@ stlink_t* stlink_open_usb(const int verbose) {
     memset(sl, 0, sizeof (stlink_t));
     memset(slu, 0, sizeof (struct stlink_libusb));
 
-    sl->verbose = verbose;
+    ugly_init(verbose);
     sl->backend = &_stlink_usb_backend;
     sl->backend_data = slu;
     
     sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
 
     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;
         
         r = libusb_detach_kernel_driver(slu->usb_handle, 0);
-        if (r<0)
-            printf("libusb_detach_kernel_driver(() error %s\n", strerror(-r));
-        goto on_libusb_error;
+        if (r<0) {
+            WLOG("libusb_detach_kernel_driver(() error %s\n", strerror(-r));
+            goto on_libusb_error;
+        }
     }
 
     if (libusb_get_configuration(slu->usb_handle, &config)) {
         /* this may fail for a previous configured device */
-        printf("libusb_get_configuration()\n");
+        WLOG("libusb_get_configuration()\n");
         goto on_libusb_error;
     }
 
@@ -651,37 +632,39 @@ stlink_t* stlink_open_usb(const int verbose) {
         printf("setting new configuration (%d -> 1)\n", config);
         if (libusb_set_configuration(slu->usb_handle, 1)) {
             /* this may fail for a previous configured device */
-            printf("libusb_set_configuration()\n");
+            WLOG("libusb_set_configuration() failed\n");
             goto on_libusb_error;
         }
     }
 
     if (libusb_claim_interface(slu->usb_handle, 0)) {
-        printf("libusb_claim_interface()\n");
+        WLOG("libusb_claim_interface() failed\n");
         goto on_libusb_error;
     }
 
     slu->req_trans = libusb_alloc_transfer(0);
     if (slu->req_trans == NULL) {
-        printf("libusb_alloc_transfer\n");
+        WLOG("libusb_alloc_transfer failed\n");
         goto on_libusb_error;
     }
 
     slu->rep_trans = libusb_alloc_transfer(0);
     if (slu->rep_trans == NULL) {
-        printf("libusb_alloc_transfer\n");
+        WLOG("libusb_alloc_transfer failed\n");
         goto on_libusb_error;
     }
-
+    // TODO - could use the scanning techniq from stm8 code here...
     slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
     slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
 
     slu->sg_transfer_idx = 0;
+    // 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);
     }
 
index c632a8c4163da3257a61014cf1b614b30e7cc884..2f3b8cc3ed7f84c17994debaaa68da442aaf86bd 100644 (file)
@@ -18,7 +18,6 @@ extern "C" {
 #define STLINK_SG_SIZE 31
 #define STLINK_CMD_SIZE 16
 
-#if defined(CONFIG_USE_LIBUSB)
     struct stlink_libusb {
         libusb_context* libusb_ctx;
         libusb_device_handle* usb_handle;
@@ -30,11 +29,6 @@ extern "C" {
         unsigned int sg_transfer_idx;
         unsigned int cmd_len;
     };
-#else
-#error "it's all bad!"
-    struct stlink_libusb {};
-#endif
-
     
     stlink_t* stlink_open_usb(const int verbose);
 
index 34fbe540caa000c2665a87582b7578487b2f1816..357f4366523fe5e4d4788a9bf3d7739c38685860 100644 (file)
@@ -6,45 +6,54 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#if CONFIG_USE_LIBSG
-#include <scsi/sg_lib.h>
-#include <scsi/sg_pt.h>
-#endif
+#include <string.h>
 #include "stlink-common.h"
+#include "uglylogging.h"
+
+#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)
+
+static void __attribute__((unused)) mark_buf(stlink_t *sl) {
+    memset(sl->q_buf, 0, sizeof(sl->q_buf));
+    sl->q_buf[0] = 0xaa;
+    sl->q_buf[1] = 0xbb;
+    sl->q_buf[2] = 0xcc;
+    sl->q_buf[3] = 0xdd;
+    sl->q_buf[4] = 0x11;
+    sl->q_buf[15] = 0x22;
+    sl->q_buf[16] = 0x33;
+    sl->q_buf[63] = 0x44;
+    sl->q_buf[64] = 0x69;
+    sl->q_buf[1024 * 6 - 1] = 0x42; //6kB
+    sl->q_buf[1024 * 8 - 1] = 0x42; //8kB
+}
+
 
 int main(int argc, char *argv[]) {
        // set scpi lib debug level: 0 for no debug info, 10 for lots
-       const int scsi_verbose = 2;
-       char *dev_name;
 
        switch (argc) {
        case 1:
                fputs(
-                       "\nUsage: stlink-access-test /dev/sg0, sg1, ...\n"
+                       "\nUsage: stlink-access-test [anything at all] ...\n"
                                "\n*** Notice: The stlink firmware violates the USB standard.\n"
-                               "*** If you plug-in the discovery's stlink, wait a several\n"
-                               "*** minutes to let the kernel driver swallow the broken device.\n"
-                               "*** Watch:\ntail -f /var/log/messages\n"
-                               "*** This command sequence can shorten the waiting time and fix some issues.\n"
+                               "*** Because we just use libusb, we can just tell the kernel's\n"
+                               "*** driver to simply ignore the device...\n"
                                "*** Unplug the stlink and execute once as root:\n"
-                               "modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:lrwsro\n\n",
+                               "modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:i\n\n",
                        stderr);
                return EXIT_FAILURE;
-       case 2:
-               dev_name = argv[1];
-               break;
        default:
-               return EXIT_FAILURE;
+        break;
        }
 
-       fputs("*** stlink access test ***\n", stderr);
-       fprintf(stderr, "Using sg_lib %s : scsi_pt %s\n", sg_lib_version(),
-               scsi_pt_version());
-
-       stlink_t *sl = stlink_quirk_open(dev_name, scsi_verbose);
+       stlink_t *sl = stlink_v1_open(99);
        if (sl == NULL)
                return EXIT_FAILURE;
-
+    
        // we are in mass mode, go to swd
        stlink_enter_swd_mode(sl);
        stlink_current_mode(sl);
@@ -55,14 +64,14 @@ int main(int argc, char *argv[]) {
        //stlink_force_debug(sl);
        stlink_reset(sl);
        stlink_status(sl);
-#if 0
        // core system control block
        stlink_read_mem32(sl, 0xe000ed00, 4);
-       DD(sl, "cpu id base register: SCB_CPUID = got 0x%08x expect 0x411fc231", read_uint32(sl->q_buf, 0));
+       DLOG("cpu id base register: SCB_CPUID = got 0x%08x expect 0x411fc231\n", read_uint32(sl->q_buf, 0));
        // no MPU
        stlink_read_mem32(sl, 0xe000ed90, 4);
-       DD(sl, "mpu type register: MPU_TYPER = got 0x%08x expect 0x0", read_uint32(sl->q_buf, 0));
+       DLOG("mpu type register: MPU_TYPER = got 0x%08x expect 0x0\n", read_uint32(sl->q_buf, 0));
 
+#if 0
        stlink_read_mem32(sl, 0xe000edf0, 4);
        DD(sl, "DHCSR = 0x%08x", read_uint32(sl->q_buf, 0));
 
@@ -80,23 +89,23 @@ int main(int argc, char *argv[]) {
 #define LED_GREEN      (1<<9) // pin 9
        stlink_read_mem32(sl, GPIOC_CRH, 4);
        uint32_t io_conf = read_uint32(sl->q_buf, 0);
-       DD(sl, "GPIOC_CRH = 0x%08x", io_conf);
+       DLOG("GPIOC_CRH = 0x%08x\n", io_conf);
 
        // set: general purpose output push-pull, output mode, max speed 10 MHz.
        write_uint32(sl->q_buf, 0x44444411);
        stlink_write_mem32(sl, GPIOC_CRH, 4);
 
-       clear_buf(sl);
+       memset(sl->q_buf, 0, sizeof(sl->q_buf));
        for (int i = 0; i < 100; i++) {
                write_uint32(sl->q_buf, LED_BLUE | LED_GREEN);
                stlink_write_mem32(sl, GPIOC_ODR, 4);
                /* stlink_read_mem32(sl, 0x4001100c, 4); */
                /* DD(sl, "GPIOC_ODR = 0x%08x", read_uint32(sl->q_buf, 0)); */
-               delay(100);
+               usleep(100 * 1000);
 
-               clear_buf(sl);
+               memset(sl->q_buf, 0, sizeof(sl->q_buf));
                stlink_write_mem32(sl, GPIOC_ODR, 4); // PC lo
-               delay(100);
+        usleep(100 * 1000);
        }
        write_uint32(sl->q_buf, io_conf); // set old state
 
@@ -120,21 +129,19 @@ int main(int argc, char *argv[]) {
 #if 0
        // sram 0x20000000 8kB
        fputs("\n++++++++++ read/write 8bit, sram at 0x2000 0000 ++++++++++++++++\n\n", stderr);
-       clear_buf(sl);
-       stlink_write_mem8(sl, 0x20000000, 16);
+    memset(sl->q_buf, 0, sizeof(sl->q_buf));
+    mark_buf(sl);
+       //stlink_write_mem8(sl, 0x20000000, 16);
 
-       mark_buf(sl);
-       stlink_write_mem8(sl, 0x20000000, 1);
-       stlink_write_mem8(sl, 0x20000001, 1);
+       //stlink_write_mem8(sl, 0x20000000, 1);
+       //stlink_write_mem8(sl, 0x20000001, 1);
        stlink_write_mem8(sl, 0x2000000b, 3);
        stlink_read_mem32(sl, 0x20000000, 16);
 #endif
 #if 0
        // a not aligned mem32 access doesn't work indeed
        fputs("\n++++++++++ read/write 32bit, sram at 0x2000 0000 ++++++++++++++++\n\n", stderr);
-       clear_buf(sl);
-       stlink_write_mem8(sl, 0x20000000, 32);
-
+    memset(sl->q_buf, 0, sizeof(sl->q_buf));
        mark_buf(sl);
        stlink_write_mem32(sl, 0x20000000, 1);
        stlink_read_mem32(sl, 0x20000000, 16);
@@ -152,6 +159,7 @@ int main(int argc, char *argv[]) {
 #if 0
        // sram 0x20000000 8kB
        fputs("++++++++++ read/write 32bit, sram at 0x2000 0000 ++++++++++++\n", stderr);
+    memset(sl->q_buf, 0, sizeof(sl->q_buf));
        mark_buf(sl);
        stlink_write_mem8(sl, 0x20000000, 64);
        stlink_read_mem32(sl, 0x20000000, 64);
@@ -161,13 +169,14 @@ int main(int argc, char *argv[]) {
        stlink_read_mem32(sl, 0x20000000, 1024 * 6);
        stlink_read_mem32(sl, 0x20000000 + 1024 * 6, 1024 * 2);
 #endif
-#if 0
-       stlink_read_all_regs(sl);
+#if 1
+    reg regs;
+       stlink_read_all_regs(sl, &regs);
        stlink_step(sl);
        fputs("++++++++++ write r0 = 0x12345678\n", stderr);
        stlink_write_reg(sl, 0x12345678, 0);
-       stlink_read_reg(sl, 0);
-       stlink_read_all_regs(sl);
+       stlink_read_reg(sl, 0, &regs);
+       stlink_read_all_regs(sl, &regs);
 #endif
 #if 0
        stlink_run(sl);
@@ -176,7 +185,7 @@ int main(int argc, char *argv[]) {
        stlink_force_debug(sl);
        stlink_status(sl);
 #endif
-#if 1 /* read the system bootloader */
+#if 0 /* read the system bootloader */
        fputs("\n++++++++++ reading bootloader ++++++++++++++++\n\n", stderr);
        stlink_fread(sl, "/tmp/barfoo", sl->sys_base, sl->sys_size);
 #endif
@@ -202,6 +211,7 @@ int main(int argc, char *argv[]) {
        stlink_run_at(sl, sl->sram_base);
 #endif
 
+#if 0
        stlink_run(sl);
        stlink_status(sl);
        //----------------------------------------------------------------------
@@ -209,6 +219,7 @@ int main(int argc, char *argv[]) {
        stlink_exit_debug_mode(sl);
        stlink_current_mode(sl);
        stlink_close(sl);
+#endif
 
        //fflush(stderr); fflush(stdout);
        return EXIT_SUCCESS;
diff --git a/src/uglylogging.c b/src/uglylogging.c
new file mode 100644 (file)
index 0000000..c6d07f8
--- /dev/null
@@ -0,0 +1,58 @@
+/* 
+ * UglyLogging.  Slow, yet another wheel reinvented, but enough to make the 
+ * rest of our code pretty enough.
+ * 
+ */
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <time.h>
+
+#include "uglylogging.h"
+
+static int max_level;
+
+int ugly_init(int maximum_threshold) {
+    max_level = maximum_threshold;
+    return 0;
+}
+
+int ugly_log(int level, const char *tag, const char *format, ...) {
+    if (level > max_level) {
+        return 0;
+    }
+    va_list args;
+    va_start(args, format);
+    time_t mytt = time(NULL);
+    struct tm *tt;
+    tt = localtime(&mytt);
+    fprintf(stderr, "%d-%02d-%02dT%02d:%02d:%02d ", tt->tm_year + 1900, tt->tm_mon + 1, tt->tm_mday, tt->tm_hour, tt->tm_min, tt->tm_sec);
+    switch (level) {
+        case UDEBUG:
+            fprintf(stderr, "DEBUG %s: ", tag);
+            break;
+        case UINFO:
+            fprintf(stderr, "INFO %s: ", tag);
+            break;
+        case UWARN:
+            fprintf(stderr, "WARN %s: ", tag);
+            break;
+        case UERROR:
+            fprintf(stderr, "ERROR %s: ", tag);
+            break;
+        case UFATAL:
+            fprintf(stderr, "FATAL %s: ", tag);
+            vfprintf(stderr, format, args); 
+            exit(EXIT_FAILURE);
+            // NEVER GETS HERE!!!
+            break;
+        default:
+            fprintf(stderr, "%d %s: ", level, tag);
+            break;
+    }
+    vfprintf(stderr, format, args); 
+    va_end(args);
+    return 1;
+}
\ No newline at end of file
diff --git a/src/uglylogging.h b/src/uglylogging.h
new file mode 100644 (file)
index 0000000..fa34d46
--- /dev/null
@@ -0,0 +1,27 @@
+/* 
+ * Ugly, low performance, configurable level, logging "framework"
+ */
+
+#ifndef UGLYLOGGING_H
+#define        UGLYLOGGING_H
+
+#ifdef __cplusplus
+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, ...);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* UGLYLOGGING_H */
+
diff --git a/stlink.modprobe.conf b/stlink.modprobe.conf
deleted file mode 100644 (file)
index e918c90..0000000
+++ /dev/null
@@ -1 +0,0 @@
-options usb-storage quirks=483:3744:l
diff --git a/stlink_v1.modprobe.conf b/stlink_v1.modprobe.conf
new file mode 100644 (file)
index 0000000..94b3786
--- /dev/null
@@ -0,0 +1 @@
+options usb-storage quirks=483:3744:i