Failed to reconnect serial monitor after uploading on macOS

hanyazou
Fri May 05, 2017 3:16 am
Hello there.

This is my first post at this forum. I read some posts related to USB and USB serial on Mac OS. But I can’t find the solution of my problem.

I addressed and solve the problem by myself fotunately. But I’m not sure this is proper fix. So I post here to share my patch and hear your thoughts.

Environment:
OS: Mac OS X 10.12.4
Arduino IDE: 1.8.2
Board: SushiBits One STM32F103CB (https://www.tindie.com/products/maxtch/ … pment-kits)

Error:
I’ve had a error after uploading. The IDE failed to re-connect serial monitor.

/Applications/Arduino-1.8.2.app/Contents/Java/arduino-builder -dump-prefs -logger=machine -hardware /Applications/Arduino-1.8.2.app/Contents/Java/hardware -hardware /Users/hanyazou/Library/Arduino15/packages -hardware /Users/hanyazou/Documents/Arduino/hardware -tools /Applications/Arduino-1.8.2.app/Contents/Java/tools-builder -tools /Applications/Arduino-1.8.2.app/Contents/Java/hardware/tools/avr -tools /Users/hanyazou/Library/Arduino15/packages -built-in-libraries /Applications/Arduino-1.8.2.app/Contents/Java/libraries -libraries /Users/hanyazou/Documents/Arduino/libraries -fqbn=Arduino_STM32-hanyazou:STM32F1:sushibitsone_f103c:upload_method=DFUUploadMethod -vid-pid=0X1EAF_0X0004 -ide-version=10802 -build-path /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264 -warnings=none -build-cache /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_cache_770802 -prefs=build.warn_data_percentage=75 -verbose /Users/hanyazou/github/MyArduinoScratchPad/Blink/Blink.ino
/Applications/Arduino-1.8.2.app/Contents/Java/arduino-builder -compile -logger=machine -hardware /Applications/Arduino-1.8.2.app/Contents/Java/hardware -hardware /Users/hanyazou/Library/Arduino15/packages -hardware /Users/hanyazou/Documents/Arduino/hardware -tools /Applications/Arduino-1.8.2.app/Contents/Java/tools-builder -tools /Applications/Arduino-1.8.2.app/Contents/Java/hardware/tools/avr -tools /Users/hanyazou/Library/Arduino15/packages -built-in-libraries /Applications/Arduino-1.8.2.app/Contents/Java/libraries -libraries /Users/hanyazou/Documents/Arduino/libraries -fqbn=Arduino_STM32-hanyazou:STM32F1:sushibitsone_f103c:upload_method=DFUUploadMethod -vid-pid=0X1EAF_0X0004 -ide-version=10802 -build-path /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264 -warnings=none -build-cache /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_cache_770802 -prefs=build.warn_data_percentage=75 -verbose /Users/hanyazou/github/MyArduinoScratchPad/Blink/Blink.ino
Using board 'sushibitsone_f103c' from platform in folder: /Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1
Using core 'maple' from platform in folder: /Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1
Detecting libraries used...
"/Users/hanyazou/Library/Arduino15/packages/STM32/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -w -DDEBUG_LEVEL=DEBUG_NONE -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DBOARD_sushibitsone_f103c -DVECT_TAB_ADDR=0x8002000 -DERROR_LED_PORT=GPIOB -DERROR_LED_PIN=1 -w -x c++ -E -CC -mcpu=cortex-m3 -DF_CPU=72000000L -DARDUINO=10802 -DARDUINO_SUSHIBITSONE_F103C -DARDUINO_ARCH_STM32F1 -DSERIAL_USB -DGENERIC_BOOTLOADER -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/include" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/stm32f1/include" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/usb/stm32f1" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/usb/usb_lib" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/cores/maple" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/variants/sushibitsone_f103c" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/sketch/Blink.ino.cpp" -o "/dev/null"
Generating function prototypes...
"/Users/hanyazou/Library/Arduino15/packages/STM32/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -w -DDEBUG_LEVEL=DEBUG_NONE -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DBOARD_sushibitsone_f103c -DVECT_TAB_ADDR=0x8002000 -DERROR_LED_PORT=GPIOB -DERROR_LED_PIN=1 -w -x c++ -E -CC -mcpu=cortex-m3 -DF_CPU=72000000L -DARDUINO=10802 -DARDUINO_SUSHIBITSONE_F103C -DARDUINO_ARCH_STM32F1 -DSERIAL_USB -DGENERIC_BOOTLOADER -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/include" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/stm32f1/include" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/usb/stm32f1" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/usb/usb_lib" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/cores/maple" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/variants/sushibitsone_f103c" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/sketch/Blink.ino.cpp" -o "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/preproc/ctags_target_for_gcc_minus_e.cpp"
"/Applications/Arduino-1.8.2.app/Contents/Java/tools-builder/ctags/5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/preproc/ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"/Users/hanyazou/Library/Arduino15/packages/STM32/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -w -DDEBUG_LEVEL=DEBUG_NONE -MMD -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DBOARD_sushibitsone_f103c -DVECT_TAB_ADDR=0x8002000 -DERROR_LED_PORT=GPIOB -DERROR_LED_PIN=1 -mcpu=cortex-m3 -DF_CPU=72000000L -DARDUINO=10802 -DARDUINO_SUSHIBITSONE_F103C -DARDUINO_ARCH_STM32F1 -DSERIAL_USB -DGENERIC_BOOTLOADER -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/include" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/stm32f1/include" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/usb/stm32f1" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/system/libmaple/usb/usb_lib" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/cores/maple" "-I/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/variants/sushibitsone_f103c" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/sketch/Blink.ino.cpp" -o "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/sketch/Blink.ino.cpp.o"
Compiling libraries...
Compiling core...
Using previously compiled file: /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/start.S.o
Using previously compiled file: /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/start_c.c.o
Using previously compiled file: /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/syscalls.c.o
Using previously compiled file: /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/board.cpp.o
Using previously compiled file: /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/boards.cpp.o
Using previously compiled file: /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/boards_setup.cpp.o
Using precompiled core
Linking everything together...
"/Users/hanyazou/Library/Arduino15/packages/STM32/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++" -Os -Wl,--gc-sections -mcpu=cortex-m3 "-T/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/variants/sushibitsone_f103c/ld/bootloader_20.ld" "-Wl,-Map,/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/Blink.ino.map" "-L/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/STM32F1/variants/sushibitsone_f103c/ld" -o "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/Blink.ino.elf" "-L/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264" -lm -lgcc -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols -Wl,--start-group "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/sketch/Blink.ino.cpp.o" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/start.S.o" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/start_c.c.o" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/syscalls.c.o" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/board.cpp.o" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/boards.cpp.o" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/core/wirish/boards_setup.cpp.o" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/../arduino_cache_770802/core/core_Arduino_STM32-hanyazou_STM32F1_sushibitsone_f103c_upload_method_DFUUploadMethod_b8b0acbbc794d5ed9ab449fd87ac250d.a" -Wl,--end-group
"/Users/hanyazou/Library/Arduino15/packages/STM32/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-objcopy" -O binary "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/Blink.ino.elf" "/var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/Blink.ino.bin"
Sketch uses 13052 bytes (9%) of program storage space. Maximum is 131072 bytes.
Global variables use 2816 bytes of dynamic memory.
/Users/hanyazou/Documents/Arduino/hardware/Arduino_STM32-hanyazou/tools/macosx/maple_upload cu.usbmodem143411 2 1EAF:0003 /var/folders/b1/159gqtss0vn9gjsspxhl_6840000gn/T/arduino_build_207264/Blink.ino.bin
dfu-util 0.8
dfu-util: Invalid DFU suffix signature

dfu-util: A valid DFU suffix will be required in a future dfu-util release!!!
Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2014 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Deducing device DFU version from functional descriptor length
Opening DFU capable USB device...
ID 1eaf:0003
Run-time device DFU version 0110
Claiming USB DFU Interface...
Setting Alternate Setting #2 ...
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 0110
Device returned transfer size 1024
Copying data from PC to DFU device

Download [ ] 0% 0 bytes
Download [= ] 7% 1024 bytes
Download [=== ] 14% 2048 bytes
Download [===== ] 21% 3072 bytes
Download [======= ] 29% 4096 bytes
Download [========= ] 36% 5120 bytes
Download [========== ] 43% 6144 bytes
Download [============ ] 50% 7168 bytes
Download [============== ] 58% 8192 bytes
Download [================ ] 65% 9216 bytes
Download [================== ] 72% 10240 bytes
Download [==================== ] 80% 11264 bytesdfu-util: can't detach
processing.app.SerialException: Error opening serial port '/dev/cu.usbmodem143411'.
at processing.app.Serial.<init>(Serial.java:141)
at processing.app.Serial.<init>(Serial.java:78)
at processing.app.SerialMonitor$3.<init>(SerialMonitor.java:95)
at processing.app.SerialMonitor.open(SerialMonitor.java:95)
at processing.app.AbstractMonitor.resume(AbstractMonitor.java:110)
at processing.app.Editor.resumeOrCloseSerialMonitor(Editor.java:2207)
at processing.app.Editor.access$2200(Editor.java:78)
at processing.app.Editor$DefaultExportHandler.run(Editor.java:2173)
at java.lang.Thread.run(Thread.java:745)
Caused by: jssc.SerialPortException: Port name - /dev/cu.usbmodem143411; Method name - openPort(); Exception type - Port not found.
at jssc.SerialPort.openPort(SerialPort.java:167)
at processing.app.Serial.<init>(Serial.java:130)
... 8 more
Error opening serial port '/dev/cu.usbmodem143411'.

Download [===================== ] 87% 12288 bytes
Download [======================= ] 94% 13052 bytes
Download [=========================] 100% 13052 bytes
Download done.
state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present
Done!
Resetting USB to switch back to runtime mode


ag123
Fri May 05, 2017 9:05 pm
if you happen to have an st-link v2 or jtag/openocd dongle or usb-uart dongle
you may like to try the stm32duino bootloader
https://github.com/rogerclarkmelbourne/ … bootloader
there have been some enhancements which lets the sketch restart quickly after the reset following a dfu sketch upload
https://github.com/rogerclarkmelbourne/ … its/master

the needed tools are like an st-link v2 or jtag/openocd or usb-uart dongle
https://github.com/rogerclarkmelbourne/ … from-Linux
do backup the existing bootloader by extracting the image from the flash memory at 0x8000000 – length 20 kbytes just in case u’d need to restore the bootloader.

if you aren’t too familar with these, perhaps stay with the stock boot loader and try installing the bootloader another time
—–
as for myself as i’m using linux, i use a separate serial monitor utility e.g. minicom, screen or putty instead of using the serial monitor bundled in the ide
you may like to find a similar separate serial monitor utility if that helps

in my sketches i like to make the sketch wait for a key press before running, e.g.

setup() {
...

while (1) {
uint8 c = 0;
Serial.println("press g to start");
if (Serial.available()) c = Serial.read();
if(c == 'g') break;
delay(1000);
}


hanyazou
Fri May 05, 2017 11:26 pm
Thank you for your reply.

I don’t know about ‘the existing bootloader’ because I coundn’t find any software information about the board provided by the board supplier. The supplier provide board design (net list and layout) instead. So I’ve modified the STM32duino bootloader for the net list and flash it by ST-Link at first. Here is my modification for the board.

diff --git a/STM32F1/Makefile b/STM32F1/Makefile
index 3aa6252..154b9f5 100644
--- a/STM32F1/Makefile
+++ b/STM32F1/Makefile
@@ -129,6 +129,7 @@ generic-pb7: begin clean gccversion build_generic-pb7 sizeafter finished copy_g
generic-pb0: begin clean gccversion build_generic-pb0 sizeafter finished copy_generic-pb0 end
stbee : begin clean gccversion build_stbee sizeafter finished copy_stbee end
naze32: begin clean gccversion build_naze32 sizeafter finished copy_naze32 end
+sushibits-f103: begin clean gccversion build_sushibits-f103 sizeafter finished copy_sushibits-f103 end
generic-pb12: begin clean gccversion build_generic-pb12 sizeafter finished copy_generic-pb12 end
hytiny-stm32f103t: begin clean gccversion build_hytiny-stm32f103t sizeafter finished copy_hytiny-stm32f103t end
build: elf bin lss sym
@@ -330,6 +331,17 @@ copy_naze32:
cp $(TARGET).bin binaries/naze32_boot20.bin
@echo

+build_sushibits-f103: TARGETFLAGS= -DTARGET_SUSHIBITS_F103
+# Set the linker script
+build_sushibits-f103: LDFLAGS +=-T$(ST_LIB)/c_only_md_high_density.ld
+build_sushibits-f103: elf bin lss sym
+copy_sushibits-f103:
+ @echo
+ @echo "Copying to binaries folder"
+ @echo
+ cp $(TARGET).bin binaries/sushibits_boot20_f103.bin
+ @echo
+
build_generic-pb12: TARGETFLAGS= -DTARGET_GENERIC_F103_PB12
# Set the linker script
build_generic-pb12: LDFLAGS +=-T$(ST_LIB)/c_only_md_high_density.ld
diff --git a/STM32F1/config.h b/STM32F1/config.h
index 2ede089..7fae279 100644
--- a/STM32F1/config.h
+++ b/STM32F1/config.h
@@ -284,6 +284,25 @@
#define LED_PIN 3
#define LED_ON_STATE 0

+#elif defined TARGET_SUSHIBITS_F103
+ // PB2
+ #define LED_BANK GPIOB
+ #define LED_PIN 2
+ #define LED_ON_STATE 1
+ #define BOOTLOADER_WAIT 10
+
+ // PA0
+ #define BUTTON_BANK GPIOE
+ #define BUTTON_PIN 5
+ #define BUTTON_PRESSED_STATE 1
+
+ // Flag that this type of board has ENABLE like SushiBits
+ #define HAS_SUSHIBITS_HARDWARE 1
+ // PC13
+ #define USB_ENABLE_BANK GPIOC
+ #define USB_ENABLE_PIN 13
+ #define USB_ON_STATE 1
+
#elif defined TARGET_GENERIC_F103_PB12

#define LED_BANK GPIOB
diff --git a/STM32F1/usb.c b/STM32F1/usb.c
index 54f1643..719c19e 100644
--- a/STM32F1/usb.c
+++ b/STM32F1/usb.c
@@ -50,6 +50,12 @@ void setupUSB (void) {
gpio_write_bit(USB_DISC_BANK,USB_DISC_PIN,0); /* present ourselves to the host */
#else

+#ifdef HAS_SUSHIBITS_HARDWARE
+ /* Setup USB ENABLE pin as output push/pull */
+ SET_REG(GPIO_CR(USB_ENABLE_BANK,USB_ENABLE_PIN),(GET_REG(GPIO_CR(USB_ENABLE_BANK,USB_ENABLE_PIN)) & crMask(USB_ENABLE_PIN)) | CR_OUTPUT_PP << CR_SHITF(USB_ENABLE_PIN));
+ gpio_write_bit(USB_ENABLE_BANK, USB_ENABLE_PIN, USB_ON_STATE);
+#endif
+
/* Generic boards don't have disconnect hardware, so we drive PA12 which is connected to the usb D+ line*/
#define USB_DISC_BANK GPIOA
#define USB_DISC_PIN 12


hanyazou
Sun May 07, 2017 5:51 am
I’ve needed one more modification for the bootloader.This is a bad dirty hack though it works well.

Because newer macOS USB implementation avoids libusb_reset_device() from resetting the STM32 bootloader target, you must reset the target in other way. You should ‘consider timing out and self-resetting’ for proper handling this situation as the comment says.

diff --git a/STM32F1/dfu.c b/STM32F1/dfu.c
index 6091fe6..d47cfa9 100644
--- a/STM32F1/dfu.c
+++ b/STM32F1/dfu.c
@@ -278,6 +278,11 @@ bool dfuUpdateByRequest(void) {
/* consider timing out and self-resetting */
dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET;

+ /*
+ * XXX FORCE RESET!!!
+ */
+ systemHardReset();
+
} else if (startState == dfuUPLOAD_IDLE) {
/* device expecting further dfu_upload requests */


RogerClark
Thu May 11, 2017 10:17 am
Which board are you using ?

I can see you change when I look at the last commit to your repo, but it looks like you added a new board variant, rather than some feature change


hanyazou
Thu May 11, 2017 11:02 am
Which board are you using ?

I’m using SushiBits One board.

Environment:
OS: Mac OS X 10.12.4
Arduino IDE: 1.8.2
Board: SushiBits One STM32F103CB (https://www.tindie.com/products/maxtch/ … pment-kits)

I think these two modification bellow are essential for Mac USB implementation because libusb_reset_device() can not reset target devices.

diff --git a/STM32F1/dfu.c b/STM32F1/dfu.c
index 6091fe6..d47cfa9 100644
--- a/STM32F1/dfu.c
+++ b/STM32F1/dfu.c
@@ -278,6 +278,11 @@ bool dfuUpdateByRequest(void) {
/* consider timing out and self-resetting */
dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET;

+ /*
+ * XXX FORCE RESET!!!
+ */
+ systemHardReset();
+
} else if (startState == dfuUPLOAD_IDLE) {
/* device expecting further dfu_upload requests */


RogerClark
Thu May 11, 2017 10:38 pm
Ok, let me know if the same issue applies to the Blue Pill

There are a number of other Mac users on the forum, and I think they may have their own personal work-around for these issues.

It seems to vary depending on the age / model of Mac and the version of OSX and also whether the board is plugged directly into the Mac or via the keyboard ( acting as a USB hub)

There is a partially implemented feature in the bootloader code, which can hold it in DFU mode, using a command sent via a non volatile ram address.
However the command / flag is not currently being set by the Libmaple code prior to resetting the board.
But I think it may just be a matter of uncommenting it.

If I get chance I will see if I can find what you need to uncomment, or possibly what code you need to add.

Of course, this may not fix it for you, but it wont do any harm to try it.


hanyazou
Sat May 13, 2017 6:50 am
I’ve received my blue-pill earlier than expected.

First, I wrote generic_boot20_pc13.bin with ST-Link V2.
I have two Mac:
iMac:~$ sysctl hw.model
hw.model: iMac13,2 // late 2012


RogerClark
Sat May 13, 2017 6:57 am
Arduino_STM32 can’t reset blue-pill after uploading sketch because dfu-util’s -R option does not work on MacOS. I’d like to fix this.

Yes. DFU util needs to support reset for it to work :-(

I’m not sure why DFU needs this, as it would make more sense if perhaps the DFU protocol sent the file size and then rebooted at the end

But the way Leaflabs orginally wrote the bootloader it needs the reset to reboot it – and I assume there must have been a good reason for this, as the Leaflabs guys really knew what they were doing.


hanyazou
Sat May 13, 2017 8:25 am
I’m not sure why DFU needs this, as it would make more sense if perhaps the DFU protocol sent the file size and then rebooted at the end

According to USB DFU specification, this seems to be proper behavior of the bootloader.

http://www.usb.org/developers/docs/devc … FU_1.1.pdf

7.Manifestation Phase

After the zero length DFU_DNLOAD request terminates the Transfer phase, the device is ready to manifest the new firmware. As described previously, some devices may accumulate the firmware image and perform the entire reprogramming operation at one time. Others may have only a small amount remaining to be reprogrammed, and still others may have none. Regardless, the device enters the dfuMANIFEST-SYNC state and awaits the solicitation of the status report by the host. Upon receipt of the anticipated DFU_GETSTATUS, the device enters the dfuMANIFEST state, where it completes its reprogramming operations.Following a successful reprogramming, the device enters one of two states: dfuMANIFEST-SYNC or dfuMANIFEST-WAIT-RESET, depending on whether or not it is still capable of communicating via USB. The host is aware of which state the device will enter by virtue of the bmAttributes bit bitManifestationTolerant. If the device enters dfuMANIFEST-SYNC (bitMainfestationTolerant = 1), then the host issues the DFU_GETSTATUS request, and the device enters the dfuIDLE state. At that point, the host can perform another download, solicit an upload, or issue a USB reset to return the device to application run-time mode. If, however, the device enters the dfuMANIFEST-WAIT-RESET state (bitManifestationTolerant = 0), then if bitWillDetach = 1 the device generates a detach-attach sequence on the bus, otherwise (bitWillDetach = 0) the host must issue a USB reset to the device. After the bus reset the device will evaluate the firmware status and enter the appropriate mode.

The STM32duino-bootloader’s bmAttributes is 0x3 (bitWillDetach=0, bitWillDetach=0). The host (MacOS) must issue a USB reset to the device in this case.


ag123
Sat May 13, 2017 3:56 pm
there is this post recently about including newlib nano
http://www.stm32duino.com/viewtopic.php … =10#p27885
Dears, I was playing with the compiler/linker options (latest Maple F1 core) and it happened to me that appending “–specs=nano.specs” to the linker, of course reduced a bit the sketch size but also broke the automatic board reset on a blue pill: since that addition I had to manually reset the board in order to complete the upload..
Removing the option restored the automatic board reset feature.
Couldn’t test on other boards..

Best, E.

ps: compiler version: arm-none-eabi-gcc\4.8.3-2014q1

hence you may like to update platforms.txt and exclude that –specs=nano.specs to see if it’d help

i’ve not reviewed the bootloader code, but i’d think dfu-util -R would set the bits in the bootloader and trigger that usb reset, usb reset is basically just pulling the D+, D- lines low for 10ms – 20ms


RogerClark
Sat May 13, 2017 9:21 pm
Thanks

I dont know why Leaflabs chose to require DFU to send reset.

You could try changing those values reported by the bootloader, and add some code to check for a empty transmission, and seem that works on the mac


hanyazou
Sat May 20, 2017 5:33 am
I can’t change the bitWillDetach (= 0) because bitWillDetach = 1 means the device (the bootloader in this case) have the detach-attach capability and the bootloader does not have the capability. I think it will be waste of flash memory to add the detach-attach capability. No one does like bloated bootloader.

And I’ve checked that libusb_reset_device() on Mac does NOT issue reset signal (Both D+ and D- lines are pulled down for 10ms). I’m sure because I’ve used my logic analyzer to probe USB signal on USB cable. The bootloader has done nothing wrong.

I’ve made new patch which adds some timeout to exit the MANIFEST_WAIT_RESET status. This works well on Linux and Mac. This mod consume 32 bytes in the binary size. (The size of generic_boot20_pc13.bin increased from 7188 to 7220 bytes.)

I’d like to send PR to the STM32-bootloader repo.

diff --git a/STM32F1/binaries/generic_boot20_pc13.bin b/STM32F1/binaries/generic_boot20_pc13.bin
index 71bce95..ed49094 100644
Binary files a/STM32F1/binaries/generic_boot20_pc13.bin and b/STM32F1/binaries/generic_boot20_pc13.bin differ
diff --git a/STM32F1/dfu.c b/STM32F1/dfu.c
index 6091fe6..d4fa062 100644
--- a/STM32F1/dfu.c
+++ b/STM32F1/dfu.c
@@ -60,6 +60,7 @@ void dfuInit(void) {
dfuAppStatus.bwPollTimeout2 = 0x00;
dfuAppStatus.bState = dfuIDLE;
dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */
+ dfuAppStatus.bwWaitResetTimeout = 0;
userFirmwareLen = 0;
thisBlockLen = 0;;
userAppAddr = USER_CODE_RAM; /* default RAM user code location */
@@ -275,9 +276,11 @@ bool dfuUpdateByRequest(void) {
/* device has programmed new firmware but needs external
usb reset or power on reset to run the new code */

- /* consider timing out and self-resetting */
dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET;

+ /* set timeout */
+ dfuAppStatus.bwWaitResetTimeout = 500;
+
} else if (startState == dfuUPLOAD_IDLE) {
/* device expecting further dfu_upload requests */

@@ -369,6 +372,9 @@ void dfuUpdateByReset(void) {
}

void dfuUpdateByTimeout(void) {
+ if (dfuAppStatus.bState == dfuMANIFEST_WAIT_RESET && --dfuAppStatus.bwWaitResetTimeout == 0) {
+ systemHardReset();
+ }
}

u8 *dfuCopyState(u16 length) {
@@ -464,7 +470,12 @@ bool dfuUploadStarted() {
void dfuFinishUpload() {
while (1)
{
+ u32 c;
+ for (c = 0; c < 9000; c++)
+ {
__asm("nop");
+ }
+ dfuUpdateByTimeout();

/* Roger Clark.
Commented out code associated with upload to RAM
diff --git a/STM32F1/dfu.h b/STM32F1/dfu.h
index d6ad1bc..70d2dc4 100644
--- a/STM32F1/dfu.h
+++ b/STM32F1/dfu.h
@@ -45,6 +45,7 @@ typedef struct _DFUStatus {
u8 bwPollTimeout2;
u8 bState; /* state of device at the time the host receives the message! */
u8 iString;
+ u32 bwWaitResetTimeout;
} DFUStatus;

typedef enum _PLOT {


ag123
Sat May 20, 2017 7:49 pm
it is an interesting if somewhat surprising result as pulling D+/D- pins low for 10ms (usb reset) is intended to tell the usb host controllers to start enumerating again. i’d guess i’m not too familar with these aspects but it may seem that the mac’s usb controller could be buggy? well, i’m not sure too :?

RogerClark
Sat May 20, 2017 10:43 pm
ag123 wrote:it is an interesting if somewhat surprising result as pulling D+/D- pins low for 10ms (usb reset) is intended to tell the usb host controllers to start enumerating again. i’d guess i’m not too familar with these aspects but it may seem that the mac’s usb controller could be buggy? well, i’m not sure too :?

hanyazou
Sun May 21, 2017 1:39 am
ag123 wrote:it is an interesting if somewhat surprising result as pulling D+/D- pins low for 10ms (usb reset) is intended to tell the usb host controllers to start enumerating again.

RogerClark
Sun May 21, 2017 6:59 am
I thought the bootloader was waiting for the DFU reset command, rather than some generic USB reset caused by pulling D+ and D- low

hanyazou
Sun May 21, 2017 7:59 am
RogerClark wrote:I thought the bootloader was waiting for the DFU reset command, rather than some generic USB reset caused by pulling D+ and D- low

kamilo
Wed May 24, 2017 9:43 pm
I’ve tried your patch hanyazou, but unfortunately it doesn’t seem to work for me. The board won’t reset after sketch upload.

hanyazou wrote:I can’t change the bitWillDetach (= 0) because bitWillDetach = 1 means the device (the bootloader in this case) have the detach-attach capability and the bootloader does not have the capability. I think it will be waste of flash memory to add the detach-attach capability. No one does like bloated bootloader.

And I’ve checked that libusb_reset_device() on Mac does NOT issue reset signal (Both D+ and D- lines are pulled down for 10ms). I’m sure because I’ve used my logic analyzer to probe USB signal on USB cable. The bootloader has done nothing wrong.

I’ve made new patch which adds some timeout to exit the MANIFEST_WAIT_RESET status. This works well on Linux and Mac. This mod consume 32 bytes in the binary size. (The size of generic_boot20_pc13.bin increased from 7188 to 7220 bytes.)

I’d like to send PR to the STM32-bootloader repo.

diff --git a/STM32F1/binaries/generic_boot20_pc13.bin b/STM32F1/binaries/generic_boot20_pc13.bin
index 71bce95..ed49094 100644
Binary files a/STM32F1/binaries/generic_boot20_pc13.bin and b/STM32F1/binaries/generic_boot20_pc13.bin differ
diff --git a/STM32F1/dfu.c b/STM32F1/dfu.c
index 6091fe6..d4fa062 100644
--- a/STM32F1/dfu.c
+++ b/STM32F1/dfu.c
@@ -60,6 +60,7 @@ void dfuInit(void) {
dfuAppStatus.bwPollTimeout2 = 0x00;
dfuAppStatus.bState = dfuIDLE;
dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */
+ dfuAppStatus.bwWaitResetTimeout = 0;
userFirmwareLen = 0;
thisBlockLen = 0;;
userAppAddr = USER_CODE_RAM; /* default RAM user code location */
@@ -275,9 +276,11 @@ bool dfuUpdateByRequest(void) {
/* device has programmed new firmware but needs external
usb reset or power on reset to run the new code */

- /* consider timing out and self-resetting */
dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET;

+ /* set timeout */
+ dfuAppStatus.bwWaitResetTimeout = 500;
+
} else if (startState == dfuUPLOAD_IDLE) {
/* device expecting further dfu_upload requests */

@@ -369,6 +372,9 @@ void dfuUpdateByReset(void) {
}

void dfuUpdateByTimeout(void) {
+ if (dfuAppStatus.bState == dfuMANIFEST_WAIT_RESET && --dfuAppStatus.bwWaitResetTimeout == 0) {
+ systemHardReset();
+ }
}

u8 *dfuCopyState(u16 length) {
@@ -464,7 +470,12 @@ bool dfuUploadStarted() {
void dfuFinishUpload() {
while (1)
{
+ u32 c;
+ for (c = 0; c < 9000; c++)
+ {
__asm("nop");
+ }
+ dfuUpdateByTimeout();

/* Roger Clark.
Commented out code associated with upload to RAM
diff --git a/STM32F1/dfu.h b/STM32F1/dfu.h
index d6ad1bc..70d2dc4 100644
--- a/STM32F1/dfu.h
+++ b/STM32F1/dfu.h
@@ -45,6 +45,7 @@ typedef struct _DFUStatus {
u8 bwPollTimeout2;
u8 bState; /* state of device at the time the host receives the message! */
u8 iString;
+ u32 bwWaitResetTimeout;
} DFUStatus;

typedef enum _PLOT {


RogerClark
Wed May 24, 2017 9:47 pm
Have you tried a variation of @danieleff’s suggestion, as posted to the PR by @ag123 ( on github ) ?

hanyazou
Wed May 24, 2017 11:00 pm
RogerClark wrote:Have you tried a variation of @danieleff’s suggestion, as posted to the PR by @ag123 ( on github ) ?

RogerClark
Wed May 24, 2017 11:18 pm
https://github.com/rogerclarkmelbourne/ … 2/pull/292

RogerClark
Thu May 25, 2017 10:42 am
I’ve updated all the Maple upload scripts with @danieleff’s suggested fix

I’m not sure if it will cause problems for Rick if he is not using the Arduino Serial Terminal, but the worst case should just be a short delay (timeout)


hanyazou
Thu May 25, 2017 12:18 pm
Sorry for late reply. I get back home now.
But my mac and blue-pill requires this change.

echo -n Waiting for ${dummy_port_fullpath} serial...

COUNTER=0
while [ ! -c ${dummy_port_fullpath} ] && ((COUNTER++ < 40)); do
sleep 0.1
done

+ sleep 0.3
echo Done


danieleff
Thu May 25, 2017 1:52 pm
Can you check changing -c to -r?

hanyazou
Thu May 25, 2017 2:09 pm
No. It fails also if it lacks ‘sleep 0.3’

COUNTER=0
while [ ! -r ${dummy_port_fullpath} ] && ((COUNTER++ < 40)); do
sleep 0.1
done
#sleep 0.3


RogerClark
Thu May 25, 2017 9:28 pm
I wonder if this is all Macs or just one specific model.

If its all Macs the additional sleep could be added to the Mac script, but 0.3 may not be enough for slow machines.

What CPU speed etc is your Mac, and what version of OSX are you using.

I have an old machine running Yosemite that I can test with, but I dont have time at the moment


hanyazou
Fri May 26, 2017 12:14 pm
My mac is iMac late 2012 3.4GHz Intel Core i7 and OS version is Mac OSX 10.12.4.

This works well on my Mac.

echo -n wait for ${dummy_port_fullpath}...
COUNTER=0
while ! dd if=${dummy_port_fullpath} of=/dev/null count=0 > /dev/null 2>&1 && ((COUNTER++ < 40)); do
sleep 0.1
done
echo done.


ag123
Fri May 26, 2017 8:20 pm
i’m not too sure if /usr/bin/test or /bin/test exists on macos, but i’d guess
[ ! -r ${dummy_port_fullpath} ]

RogerClark
Fri May 26, 2017 9:35 pm
hanyazou wrote:My mac is iMac late 2012 3.4GHz Intel Core i7 and OS version is Mac OSX 10.12.4.

This works well on my Mac.

echo -n wait for ${dummy_port_fullpath}...
COUNTER=0
while ! dd if=${dummy_port_fullpath} of=/dev/null count=0 > /dev/null 2>&1 && ((COUNTER++ < 40)); do
sleep 0.1
done
echo done.


hanyazou
Fri Jun 02, 2017 11:02 pm
I’ve sent a PR.
https://github.com/rogerclarkmelbourne/ … 2/pull/297

Leave a Reply

Your email address will not be published. Required fields are marked *