lujji / stm8-bootloader

Serial bootloader for STM8 microcontrollers
MIT License
93 stars 27 forks source link

Would it be possible to adapt to serial via software? #8

Open rtek1000 opened 1 year ago

rtek1000 commented 1 year ago

Hi,

Unfortunately the W1209 board does not have UART pin usability. But I'm managing to emulate USART using timers.

Would it be possible to emulate the serial port to use in the bootloader?

Ref.: https://github.com/rtek1000/W1209-firmware-modified/blob/master/W1209-firmware-Remote/w1209-firmware-modified-eclipse-remote/Core/Src/remote.c

https://github.com/rtek1000/W1209-firmware-modified/blob/master/W1209-firmware-Remote/w1209-firmware-modified-eclipse-remote/Core/Src/timer.c

rtek1000 commented 1 year ago

I modified it to use timers, but I haven't tested it yet, follow the link (935 bytes):

https://github.com/rtek1000/stm8-bootloader

rtek1000 commented 1 year ago

I managed to test it, there was a bug, because I was using interrupt, it took me a while to understand that you cannot use interrupt in the bootloader. Apparently working, I haven't tested it with very large application code.

Ideally, I wanted to implement my own interrupt table, however, it seems that it’s hard-coded inside SDCC. After spending some time with the documentation and browsing through the mailing lists, I just ended up looking at the compiler’s source code. There is a function createInterruptVect() inside SDCCglue.c which is responsible for generating the interrupt vectors. As it turns out, it checks whether or not main() is implemented and then proceeds with the interrupt table generation. So the solution was pretty simple: rename main into bootloader_main and no interrupt vectors will be generated.

So the solution was pretty simple: rename main into bootloader_main and no interrupt vectors will be generated.

https://lujji.github.io/blog/serial-bootloader-for-stm8/

lujji commented 1 year ago

The interrupts are not working in bootloader because the interrupt table points to application IVT (see init.s). You could implement the ISR you're interested in and check if bootloader is active by testing some magic value in RAM for example. From there you could either continue executing the bootloader ISR code or jump to application ISR.

rtek1000 commented 1 year ago

I had tried to implement the interrupt in this file (init.s), but I couldn't. It would be interesting to have an example, perhaps with EXTI.