im-tomu / toboot

Bootloader for the EFM32HG Tomu Board
https://tomu.im/
GNU General Public License v3.0
72 stars 28 forks source link

toboot always runs on power cycle #31

Closed todbot closed 6 years ago

todbot commented 6 years ago

Hi, I have successfully put the prebuilt and recompiled versions of toboot on my tomu board. I can then use dfu-util to successfully upload and run any of the im-tomu/tomu-samples examples and the im-tomu/tomu-bootloader/tests/secure-erase/pass-1 test code.

However, on re-applying power to the board (i.e. plugging it back into USB), the toboot bootloader always runs.

Any idea why this is happening? (also, thanks for toboot, it's awesome)

todbot commented 6 years ago

Specifically, here's what I do for tomu-samples:

git clone https://github.com/im-tomu/tomu-samples && cd tomu-samples
make deps
cd efm32hg-blinky-usb
make
dfu-util -v --device 1209:70b1 --download blinky-usb.bin

The above completes apparently successfully (tho dfu-util does give some warnings) and the "EFM32 Example USB Device" appears in the USB device list.

But if the Tomu board is power-cycled, it goes immediately back to toboot.

xobs commented 6 years ago

This is by design. See point 5 of https://github.com/im-tomu/tomu-bootloader#using

This is because most people probably want this behavior. Otherwise, they would flash something like the blinky example and then not know how to get out of it.

The solution is to add a Toboot V2.0 header (see https://github.com/im-tomu/tomu-bootloader/blob/master/API.md#program-header). Such a header could be added in a macro.

Unrelatedly: You can add a DFU suffix to the binfile using the dfu-suffix command.

todbot commented 6 years ago

I could not find any definition of TOBOOT_CONFIG_FLAG_POWERON_ENTER in tomu-bootloader@master so I thought maybe it was stale documentation. Am I just missing it?

I found I was able to get the bootloader to boot the programmed application if I commented out the test_reset_cause() in main.c:238 while still keeping the ability to trigger bootloader via boot token or pin short.

I'm still testing this, but I wonder if the conditional in test_reset_cause():

    if (rstcause & RMU_RSTCAUSE_PORST)

should have its logic sense flipped to be:

    if (!(rstcause & RMU_RSTCAUSE_PORST))

because we only to consider running the bootloader if the reset cause was not power-on reset, yes?

xobs commented 6 years ago

I've added support to tomu-samples, and have updated miniblink to add toboot-v2 support: https://github.com/im-tomu/tomu-samples/blob/master/libopencm3-samples/miniblink/miniblink.c#L40

Please see if replacing TOBOOT_CONFIGURATION(0) with TOBOOT_CONFIGURATION(TOBOOT_CONFIG_FLAG_AUTORUN) does what you're looking to do.

The reason that conditional is the way it is is because we MAY consider running Toboot if it's a fresh poweron. If it's a reboot, then we'll never enter Toboot through this method, instead opting to check for the magic token, or an excess number of reboots.

An example of a reboot where you won't want to enter Toboot is when you upload a new program using DFU, after which it does a reboot.