vdudouyt / stm8flash

program your stm8 devices with SWIM/stlinkv(1,2)
GNU General Public License v2.0
401 stars 182 forks source link

Fix flashing controllers with no NRST pin #157

Closed not-fl3 closed 8 months ago

not-fl3 commented 1 year ago

Note that I am not exactly sure what I am doing and I can't be sure that this change does not break things.

The problem I encountered - stm8flash always failed with error 0x05 when trying to flash a controller without NRST pin(specifically STM8L050J3) or with NRST pin disconnected(tested with Stm8s103f3 with NRST disconnected from ST-LINK).

Before this fix writing to FLASH_NCR2 never succeeded, resulting in flashing error 0x05:

SWIM error 0x05

One possible workaround was - skip writing to FLASH_CR2/FLASH_NCR2 and flash memory in "bytes" mode - withblock_size1 instm8.c`. It worked on STM8L050 perfectly, but super slow.

According to https://www.st.com/resource/en/programming_manual/pm0051-how-to-program-stm8s-and-stm8a-flash-program-memory-and-data-eeprom-stmicroelectronics.pdf

The programming can also be performed directly through the SWIM interface. In this case, it
is recommended to stall the device in order to prevent the core from accessing the Flash
program memory during the block program or erase operation. This can be done by setting
the STALL bit in the DM_CSR2 debug module register. Refer to the STM8 SWIM
communication protocol and debug module (UM0470) for more information

They say "recommended", but somehow it is actually necessary?

spth commented 1 year ago

The STM8L050J3 i s marked "ok" in the README.md, so it apparently is working for some users. So is the STM8S001J3. If I remember correctly, I used stm8flash for the latter myself a few years ago.

not-fl3 commented 1 year ago

The STM8L050J3 i s marked "ok" in the README.md, so it apparently is working for some users. So is the STM8S001J3. If I remember correctly, I used stm8flash for the latter myself a few years ago.

yeah, it is marked OK, so highly likely I am doing something wrong

But I figured I'll better leave this as a PR, I spent way too much time trying to google this cursed error 0x05 :D

Just in case, I'll leave the steps to reproduce the problem:

Also note that stm8flash's espstlink has this stalling step (that's where I gleaned the code)

schneidersoft commented 1 year ago

I have an stm8L050J3 and cannot reproduce this behavior. I am able to read and write the flash of this chip even with reset tied to 3.3v. (stm8L050J3 doesn't have reset) Don't have the STM8S001J3 so i can't test it on that...

not-fl3 commented 1 year ago

I have an stm8L050J3 and cannot reproduce this behavior. I am able to read and write the flash of this chip even with reset tied to 3.3v. Don't have the STM8S001J3 so i can't test it on that...

somehow with my stm8L050J3 I can flash it only once. If its the same firmware again - it will successfully "flash" (cause nothing will be actually written to the flash due to diffs), but if its a different firmware - it will give an error...

schneidersoft commented 1 year ago

Ok. I've seen it now as well. It seems to really depend on what you attempt to write. section 3.6 of the datasheet gives some good hints as to why. in any case its not a good idea to have the core running while writing to flash.

Your changes seem to fix the issue. But I'm not sure if it's ok to do that for all the chips... It also probably doesn't need to be done on every page but only once

JDaance commented 1 year ago

I had problems flashing a TSDZ2 motor from linux, got the SWIM 0x05 error every time. This patched fixed it!

I have no idea what I'm doing really so cant provide much information, using a ready made cable with a stlinkv2 built into it. The part number passed to stm8flash is "stm8s105?6"

ehsanecc commented 10 months ago

Tested on stm8s005?6 and fixed the issue! Thanks a lot