Bootloader damaged when using streaming.cpp

JohnO
Sun May 07, 2017 4:19 pm
I tried to run some example code from [here](https://github.com/JChristensen/extEEPR … eepromTest) . The example includes some Serial code from [here](http://arduiniana.org/libraries/streaming/). Loading the example into a STM32F103C8T6 went fine but the bootloader didn’t enumerate after the USB reset. Power cycle and still no joy. Attempting to reload the bootloader failed as the device wasn’t recognised using STLink and BMP. Boot0 was the route to get the bootloader reloaded and a repeat of above when the sketch was loaded. After removing the *streaming <<* references the example loaded and operated fine.

I haven’t seen stream before and it appears quite useful except for its impact on the bootloader. Does anyone have any pointers I might look into to get to the bottom of the issue?


stevestrong
Sun May 07, 2017 4:25 pm
You can generate a map file and examine it.

JohnO
Sun May 07, 2017 5:37 pm
https://pastebin.com/r0JaupQi

I’ll examine it now.


ag123
Sun May 07, 2017 6:01 pm
there is (only) 20k of ram on the stm32f103, hence when trying out perhaps some fancy c++ stuff, it’s possibly good to keep it in mind, i.e. ram is scarce, but nevertheless, it could be something else that cause it to fail rather than simply running out of memory :lol:

stevestrong
Sun May 07, 2017 6:08 pm
To analyze map file you can use this link from Daniel: http://danieleff.com/stm32/map_analizer/
or download AMap.

JohnO
Sun May 07, 2017 8:10 pm
2937 used / 20480 total RAM
16137 used / 122880 total FLASH

JohnO
Sat May 20, 2017 3:26 pm
I wonder if I came make the bootloader area read only.

ag123
Sat May 20, 2017 6:57 pm
just some thoughts, check your ld scripts, look inside the file e.g. did you use bootloader_20.ld? in arduino ide, this probably relates to selecting the ‘bootloader version’
and for uart / jtag / swd (st-link) installs, check the *install address* or perhaps the correct ld script used. i think some installers probably use the elf addresses which basically originate in the ld scripts itself. i.e. the ld scripts is used to build the elf file (hence would have the ld script addresses), then when you install the file specifying the elf file, that address (which is the same as that in the ld script used) is where the sketch / binary gets installed.

/*
* libmaple linker script for "Flash" builds.
*
* A Flash build puts .text (and .rodata) in Flash, and
* .data/.bss/heap (of course) in SRAM, but offsets the sections by
* enough space to store the Maple bootloader, which lives in low
* Flash and uses low memory.
*/

/*
* This pulls in the appropriate MEMORY declaration from the right
* subdirectory of stm32/mem/ (the environment must call ld with the
* right include directory flags to make this happen). Boards can also
* use this file to use any of libmaple's memory-related hooks (like
* where the heap should live).
*/
MEMORY
{
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
rom (rx) : ORIGIN = 0x08002000, LENGTH = 120K
}

/* Provide memory region aliases for common.inc */
REGION_ALIAS("REGION_TEXT", rom);
REGION_ALIAS("REGION_DATA", ram);
REGION_ALIAS("REGION_BSS", ram);
REGION_ALIAS("REGION_RODATA", rom);

/* Let common.inc handle the real work. */
INCLUDE common.inc


Rick Kimball
Sat May 20, 2017 9:11 pm
ag123 wrote:there is (only) 20k of ram on the stm32f103, hence when trying out perhaps some fancy c++ stuff, it’s possibly good to keep it in mind, i.e. ram is scarce, but nevertheless, it could be something else that cause it to fail rather than simply running out of memory :lol:

zmemw16
Sat May 20, 2017 9:22 pm
if this is programming from the ide with a st-link ?
could you try this ?
connect another serial device, serial-usb block, another micro, even a nano :)
select it in the port monitor
program the maple mini via st-link, does it now re-enumerate?
stephen

ag123
Sat May 20, 2017 9:49 pm
thanks rick, i’d need to try out streaming.h, i’ve been mindlessly using serial.print() or serial.println() as i’ve mistakenly assumed that using those templates would take up more memory :D

RogerClark
Sat May 20, 2017 11:14 pm
The sketch address is hard coded into the bootloader, ( there are 2 possible start addresses 0x8002000 and 0x8005000), neither of which would overwrite the bootloader its self on the Blue Pill)

It is possible for the sketch code to overwrite the bootloader, or erase the whole device. The bootloader updater sketch does it.

AFIK, Its not possible to protect against the code erasing its self.

I would recommend you reflash the bootloader, and comfirm it works by loading a blink sketch.

Then read back the bootloader section, and save to file ( using stlink or serial)
Then load the offending code, and run it.

Then readback the bootloader section and do a binary compare, to see if you are corrupting the bootloader and if so, is the whole section now empty.


ag123
Sun May 21, 2017 1:35 am
it would seemed to me that either st-link(jtag/swd) or uart is involved in installing the sketch, the mistakes e.g. in selecting the ld script could result in it overwriting the boot loader.
short of writing codes that interacts with the flash controller which includes using special registers, erasing blocks and writing new blocks, it’d seem unlikely that a ‘normal’ sketch does that :?
just 2 cents

RogerClark
Sun May 21, 2017 6:58 am
ag123 wrote:it would seemed to me that either st-link(jtag/swd) or uart is involved in installing the sketch, the mistakes e.g. in selecting the ld script could result in it overwriting the boot loader.
short of writing codes that interacts with the flash controller which includes using special registers, erasing blocks and writing new blocks, it’d seem unlikely that a ‘normal’ sketch does that :?
just 2 cents

JohnO
Sun May 21, 2017 2:25 pm
Thank you all for your responses, I plan to work through them today.

@RickKimball could you post the exact command you used to burn the bootloader, I have access to st-link and (your) BMP. I did use generic_boot20_pc13.bin from Roger’s repo. I run STM32duino on Mac and *think* I originally burned the bootloader using Win7. I did try it a few times before posting my initial problem.


Rick Kimball
Sun May 21, 2017 2:48 pm
@JohnO

I had used an stlink to load the bootloader. The location of your stm32duino directory is different adjust for your configuration:

$ /home/kimballr/Arduino/hardware/stm32duino_maple/tools/linux/stlink/st-flash write generic_boot20_pc13.bin 0x08000000


JohnO
Sun May 21, 2017 3:06 pm
Thanks Rik, I have used the same command on Mac, with a different path.

JohnO
Sun May 21, 2017 4:16 pm
I tried the streaming test code provided by Rik and all works fine. However, when I use the original example code:

//Test extEEPROM library.
//Writes the EEPROM full of 32-bit integers and reads them back to verify.
//Wire a button from digital pin 6 to ground, this is used as a start button
//so the sketch doesn't do unnecessary EEPROM writes every time it's reset.
//Jack Christensen 09Jul2014

#include <extEEPROM.h> //http://github.com/JChristensen/extEEPROM/tree/dev
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
#include <Wire.h> //http://arduino.cc/en/Reference/Wire

//Two 24LC256 EEPROMs on the bus
const uint32_t totalKBytes = 64; //for read and write test functions
extEEPROM eep(kbits_256, 2, 64); //device size, number of devices, page size

const uint8_t btnStart = 6; //start button

void setup(void)
{
pinMode(btnStart, INPUT_PULLUP);
Serial.begin(115200);
uint8_t eepStatus = eep.begin(extEEPROM::twiClock400kHz); //go fast!
if (eepStatus) {
Serial << endl << F("extEEPROM.begin() failed, status = ") << eepStatus << endl;
while (1);
}

Serial << endl << F("Press button to start...") << endl;
while (digitalRead(btnStart) == HIGH) delay(10); //wait for button push

uint8_t chunkSize = 64; //this can be changed, but must be a multiple of 4 since we're writing 32-bit integers
// eeErase(chunkSize, 0, totalKBytes * 1024 - 1);
eeWrite(chunkSize);
eeRead(chunkSize);

dump(0, 16); //the first 16 bytes
dump(32752, 32); //across the device boundary
dump(65520, 16); //the last 16 bytes
}

void loop(void)
{
}


JohnO
Sun May 21, 2017 4:18 pm
I wonder if pin 6 relates to the SWD port.

Rick Kimball
Sun May 21, 2017 4:28 pm
JohnO wrote:The BP fails to enumerate after the above code is loaded. A further concern is that SWD mode is no longer available unless BOOT0 is high.
st-link reports:

Rick Kimball
Sun May 21, 2017 4:38 pm
JohnO wrote:I tried the streaming test code provided by Rik and all works fine. However, when I use the original example code:

JohnO
Sun May 21, 2017 4:51 pm
Although it is possible to reflash the BootLoader with BOOT0 high, SWD without BOOT0 does not work until an erase with BOOT0 high is done.
/usr/local/bin/st-flash erase

JohnO
Sun May 21, 2017 4:55 pm
It really isn’t enumerating, I am familiar with Linux dmesg etc, on Mac I am using command line to list the USB devices:
Johns-MacBook-Pro:~ john$ lsusb
Bus 026 Device 001: ID 8087:0024 Intel Corporation Hub
Bus 026 Device 002: ID 05ac:8509 Apple Inc. FaceTime HD Camera (Built-in) Serial: CC2CA802BCDGFKL0
Bus 029 Device 001: ID 8087:0024 Intel Corporation Hub
Bus 029 Device 002: ID 0424:2513 SMSC Hub
Bus 029 Device 005: ID 05ac:0253 Apple Inc. Apple Internal Keyboard / Trackpad
Bus 029 Device 003: ID 05ac:8242 Apple Inc. IR Receiver
Bus 029 Device 004: ID 0a5c:4500 Broadcom Corp. BRCM20702 Hub
Bus 029 Device 008: ID 05ac:821d Apple Inc. Bluetooth USB Host Controller
Bus 020 Device 006: ID 1eaf:0003 1eaf Maple 003 Serial: LLM 003
Bus 000 Device 001: ID 1d6b:IPCI Linux Foundation USB 2.0 Bus
Bus 000 Device 001: ID 1d6b:IPCI Linux Foundation USB 2.0 Bus
Bus 000 Device 001: ID 1d6b:IPPT Linux Foundation USB 3.0 Bus
Johns-MacBook-Pro:~ john$

Rick Kimball
Sun May 21, 2017 4:59 pm
JohnO wrote:It really isn’t enumerating, I am familiar with Linux dmesg etc, on Mac I am using command line to list the USB devices:
Johns-MacBook-Pro:~ john$ lsusb
...
Bus 020 Device 006: ID 1eaf:0003 1eaf Maple 003 Serial: LLM 003
...Johns-MacBook-Pro:~ john$

JohnO
Sun May 21, 2017 5:02 pm
After loading the sketch I see:
Johns-MacBook-Pro:~ john$ lsusb
Bus 026 Device 001: ID 8087:0024 Intel Corporation Hub
Bus 026 Device 002: ID 05ac:8509 Apple Inc. FaceTime HD Camera (Built-in) Serial: CC2CA802BCDGFKL0
Bus 029 Device 001: ID 8087:0024 Intel Corporation Hub
Bus 029 Device 002: ID 0424:2513 SMSC Hub
Bus 029 Device 005: ID 05ac:0253 Apple Inc. Apple Internal Keyboard / Trackpad
Bus 029 Device 003: ID 05ac:8242 Apple Inc. IR Receiver
Bus 029 Device 004: ID 0a5c:4500 Broadcom Corp. BRCM20702 Hub
Bus 029 Device 008: ID 05ac:821d Apple Inc. Bluetooth USB Host Controller
Bus 000 Device 001: ID 1d6b:IPCI Linux Foundation USB 2.0 Bus
Bus 000 Device 001: ID 1d6b:IPCI Linux Foundation USB 2.0 Bus
Bus 000 Device 001: ID 1d6b:IPPT Linux Foundation USB 3.0 Bus
Johns-MacBook-Pro:~ john$

ag123
Sun May 21, 2017 5:04 pm
normally those flashes comes from the (dfu) bootloader, try to run dfu-util -l

JohnO
Sun May 21, 2017 5:07 pm
Johns-MacBook-Pro:~ john$ dfu-util -l
dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 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
Found Runtime: [05ac:821d] ver=0154, devnum=8, cfg=1, intf=3, path="29-3", alt=0, name="UNKNOWN", serial="UNKNOWN"


JohnO
Sun May 21, 2017 5:10 pm
Johns-MacBook-Pro:~ john$ lsusb
Bus 026 Device 001: ID 8087:0024 Intel Corporation Hub
Bus 026 Device 002: ID 05ac:8509 Apple Inc. FaceTime HD Camera (Built-in) Serial: CC2CA802BCDGFKL0
Bus 029 Device 001: ID 8087:0024 Intel Corporation Hub
Bus 029 Device 002: ID 0424:2513 SMSC Hub
Bus 029 Device 005: ID 05ac:0253 Apple Inc. Apple Internal Keyboard / Trackpad
Bus 029 Device 003: ID 05ac:8242 Apple Inc. IR Receiver
Bus 029 Device 004: ID 0a5c:4500 Broadcom Corp. BRCM20702 Hub
Bus 029 Device 008: ID 05ac:821d Apple Inc. Bluetooth USB Host Controller

Bus 020 Device 006: ID 1eaf:0003 1eaf Maple 003 Serial: LLM 003

Bus 000 Device 001: ID 1d6b:IPCI Linux Foundation USB 2.0 Bus
Bus 000 Device 001: ID 1d6b:IPCI Linux Foundation USB 2.0 Bus
Bus 000 Device 001: ID 1d6b:IPPT Linux Foundation USB 3.0 Bus


ag123
Sun May 21, 2017 5:10 pm
be careful, that’s your
Bus 029 Device 008: ID 05ac:821d Apple Inc. Bluetooth USB Host Controller

JohnO
Sun May 21, 2017 5:13 pm
I don’t follow you, I have made other observations on USB http://www.stm32duino.com/viewtopic.php … 386#p28386.

JohnO
Sun May 21, 2017 5:16 pm
Umh, so when I use the example code you supplied it works as I expect and when I run my example code it doesn’t. It also screws the BP to prevent the bootloader and also prevents normal, non BOOT0 SWD access.

Could you load up my example code into a BP and check if you still have access to the BP while it is loaded?


ag123
Sun May 21, 2017 5:19 pm
if you need debug in your sketch, call enableDebugPorts();
http://www.stm32duino.com/viewtopic.php … 084#p27916
enableDebugPorts();

JohnO
Sun May 21, 2017 5:30 pm
With a clean erase and flash “dfu-util -l” looks like this:
Johns-MacBook-Pro:~ john$ dfu-util -l
dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 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
Found Runtime: [05ac:821d] ver=0154, devnum=8, cfg=1, intf=3, path="29-3", alt=0, name="UNKNOWN", serial="UNKNOWN"
Found DFU: [1eaf:0003] ver=0201, devnum=28, cfg=1, intf=0, path="20-1", alt=2, name="STM32duino bootloader v1.0 Upload to Flash 0x8002000", serial="LLM 003"
Found DFU: [1eaf:0003] ver=0201, devnum=28, cfg=1, intf=0, path="20-1", alt=1, name="STM32duino bootloader v1.0 Upload to Flash 0x8005000", serial="LLM 003"
Found DFU: [1eaf:0003] ver=0201, devnum=28, cfg=1, intf=0, path="20-1", alt=0, name="STM32duino bootloader v1.0 ERROR. Upload to RAM not supported.", serial="LLM 003"


ag123
Sun May 21, 2017 5:33 pm
you can install a sketch using dfu-util -a 2 -R -D sketch.bin

Rick Kimball
Sun May 21, 2017 5:35 pm
JohnO wrote:Umh, so when I use the example code you supplied it works as I expect and when I run my example code it doesn’t. It also screws the BP to prevent the bootloader and also prevent SWD access.

Could you load up my example code into a BP and check if you still have access to the BP while it is loaded?


JohnO
Sun May 21, 2017 7:54 pm
Thank you @ag123 and @RickKimball for your help and insight.

@ag123 I don’t think I know enough yet to get into the debug options you have highlighted.

@RickKimball I think you have proven the streaming.h and as you say I could bypass the problem. However, I will probably keep gnawing at the issue since I don’t like leaving issues like this to roam free.


ag123
Mon May 22, 2017 3:11 am
@JohnO
as i do not know about your setup for debug, i’m only guessing that you may be doing debug using a different IDE (e.g. eclipse) or perhaps some scripts or another program. however, do be aware that some of the debug implementations upload the program over jtag/swd just prior to starting debug.

this overwrites the sketch installed using dfu-util (by arduino ide).

hence, you need to check your debug setup if you can prevent the sketch install over jtag/swd during debug.
alternatively, you need to set your *install address* for the debug to install the sketch at the correct address e.g. 0x8002000 rather than 0x8000000, if debug install the sketch at 0x8000000 it will simply overwrite the bootloader as well.


JohnO
Mon May 22, 2017 6:59 am
Thanks for sticking with me @ag123. I am not currently using any debug/JTAG equipment. I am quite new to STM32duino and am using Arduino 1.6.12 linked via USB to a BP running the STM32duino bootloader. The bootloader is burned using a clone st-link. I have other equipment including BMP but as yet these have not been brought into play.

ag123
Mon May 22, 2017 12:04 pm
note that st-link is a fully capable jtag/swd debug device, you can setup openocd and gdb and do a full blown jtag/swd debug, including things like flashing any binaries (boot loader or any sketches) at any locations, set breakpoints, examine codes, registers etc
hence be sure u know what you are doing when using st-link ;)

Leave a Reply

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