STM32F10x light HID driverless bootloader supporting HD devices

What are you developing?
User avatar
Vassilis
Posts: 8
Joined: Wed Dec 18, 2019 3:04 pm
Location: Thessaloniki, Greece
Contact:

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by Vassilis »

+1 :)
Vassilis Serasidis

TheKikGen
Posts: 15
Joined: Tue Mar 03, 2020 11:13 am

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by TheKikGen »

I passed the size of the bootloader to 4K because it is not possible to restore a totally clean MCU context before jumping to the user code.
+ if your GPIOs setting are more complex than the usual Bluepill, that will consume more bytes, and I was already at the edge of the 2K....
So 4K I think is a good compromise.

I added a "double push" mode, allowing to enter easily to the bootloader mode without setting the Boot1 jumper or waiting for an hypothetic reset from DTR/DTS...
Note : this is still a beta version...

stevestrong
Posts: 268
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 5
Location: Munich, Germany
Contact:

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by stevestrong »

TheKikGen wrote:
Sat May 02, 2020 2:09 pm
I added a "double push" mode, allowing to enter easily to the bootloader mode without setting the Boot1 jumper or waiting for an hypothetic reset from DTR/DTS...
I am really curious how should this work, can you please explain?

TheKikGen
Posts: 15
Joined: Tue Mar 03, 2020 11:13 am

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by TheKikGen »

Sure Steve.
I use backup registers which are preserved after an hardware reset.

1/ First, the bck register DR10 (used by stm32duino) is checked for the magic word, because if already set, we enter directly in bootloader mode
2/ The bckp register is set to the value of the magic word before entering in a short loop (+/- 2s)
3/ If reset is pressed during that loop, the value will be preserved, and the bootloader mode will be engaged at the next start
4/ else, DR10 is set to 0.

Code: Select all

if ( ! MustEnterBooloader ) {
	// If reset occurs before the end of loop, that will activate the bootloader at the start
	BKP->BACKUP_REG = MAGIC_WORD1;
	for (uint16_t i = 1 ; i<20 ; i++) {
		LED1_ON;
		SLEEP_M(100) ;                                     
		LED1_OFF;
		SLEEP_M(50);
	}
}
BKP->BACKUP_REG = 0x0000;
(...)
One thing to retain also, is that the current version of HID_BOOTLOADER uses DR4, and consequently can't reboot the board in bootloader mode when the Arduino IDE is toggling DTR for a reset, because the magic word is written in DR10 in that case.

from usbserial.cpp Roger's core source file :

Code: Select all

#ifdef SERIAL_USB 
            // The magic reset sequence is "1EAF".
            // Got the magic sequence -> reset, presumably into the bootloader.
			bkp_init();
			bkp_enable_writes();
---->			bkp_write(10, 0x424C);
			bkp_disable_writes();
			nvic_sys_reset();			
#endif

stevestrong
Posts: 268
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 5
Location: Munich, Germany
Contact:

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by stevestrong »

The reset using a magic sequence is known, long time ago.
As you pointed out, the libmaple (Roger's) core uses the DTR negate edge as trigger the check for the magic sequence written in the backup register 10.
The bootloader has to delete this word from the backup register.
Is this what you mean under "double push mode"?
But this method was already implemented in the HID bootloader from Vassilis, right?
And I am also using it in my cdc bootloader.
I wasn't aware that this wasn't implemented from the beginning in your sw.
True that Vassilis uses reg nr 4 for this, instead of nr 10 as stm32duino bootloader does and as the libmaple core expects.

TheKikGen
Posts: 15
Joined: Tue Mar 03, 2020 11:13 am

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by TheKikGen »

The bck register was implemented for sure in my sw from the beginning (i use that even in my other midi sw), but in the classical way, like you mentioned fro the Roger's bootloader and Vassilis.
I'm talking about a trick to get into the bootloader with 2 short consecutives reset, by playing with DR10 (see the code above).

START
. if DR10 = magic word goto bootloader mode
. put magic word to DR10
. [loop start]
. ------- (if reset here, DR10 = magic word - goto START)
. [loop end]
. put 0 to DR10 -> Jump to user code.

stevestrong
Posts: 268
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 5
Location: Munich, Germany
Contact:

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by stevestrong »

I see now what you mean.
You want to assure that you go back to bootloader mode if a reset occurs during waiting for upload.
But that is easily achievable if you only clear br 10 short before jumping to user code, no need to put it again (step 4) if it is there already (step 1).

TheKikGen
Posts: 15
Joined: Tue Mar 03, 2020 11:13 am

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by TheKikGen »

I reformulate again steve.

(reset)
1. If magic word is here goto UPLOAD (another path) then Reset after flashing
2. Magic word is not here. put Magic Word in DR10
3. wait 2 seconds to let the user a chance to reset and go back to step 1
4. Set DR10 to 0
5. Jump

if the MW is here, no problem, and i follow the upload path
if the MW it is not here and i want to force bootloader mode -> this is the trick (the only way to force that is to use BOOT1 HIGH)...

stevestrong
Posts: 268
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 5
Location: Munich, Germany
Contact:

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by stevestrong »

Ok, so you want to replace the annoying setting of boot 1 jumper by a 2 sec wait period.
I think Roger's bootloader 2.0 does the same (3 secs), but he also has a fast_boot version which does not wait.
At the end, I think you will also end up with two versions, there are people who do not like waiting those extra seconds at each reset, especially when the magic sequence trick works. But I agree, it certainly makes sense when you do not use USB serial in your application.

TheKikGen
Posts: 15
Joined: Tue Mar 03, 2020 11:13 am

Re: STM32F10x light HID driverless bootloader supporting HD devices

Post by TheKikGen »

Yes !
The Roger's bootloader is very long to start (more than 3 s), especially because USB (CDC) is launched during the boot up sequence (for the DFU mode notably). My first approach was quite similar with HID, but you must first shutdown USB, wait more or less 4 seconds, then start again USB with your descriptors, then shutdown again to start the user app... and "fastboot" is a compilation flag, so it is not configurable at run time .

I'm thinking about adding some features like :
- A page eraser
- a user code eraser
- a download firmware (reading what's in flash)
- a MCU info

Post Reply

Return to “Projects”