Hello I'm having a problem with stm32flash returning "Failed to init device." error when trying to flash my STM32L462 microcontroller through UART on my Linux Operating system.
These are the steps I'm going through when trying to flash the MCU:
First I run the script to put the MCU into bootloader mode
# BOOT0 set to 1 echo 4 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio4/direction echo 1 > /sys/class/gpio/gpio4/value # Reset set to 1 echo 28 > /sys/class/gpio/export sleep 0.1 echo out > /sys/class/gpio/gpio28/direction echo 1 > /sys/class/gpio/gpio28/value sleep 0.1 echo 0 > /sys/class/gpio/gpio28/value sleep 0.1
then I try to flash the MCU with stm32flash
:
stm32flash /dev/ttymxc2 -w /home/root/booting_files/zephyr.hex
where I get the response:
stm32flash 0.6 http://stm32flash.sourceforge.net/ Using Parser : Intel HEX Interface serial_posix: 57600 8E1 Failed to init device.
Looking at the code of stm32flash
I can see that the "Failed to init device" can happen inside stm32_send_init_seq
function, when trying to read a byte from the port. The cause can be either:
timeout
not reading OK or NACK response
I also found the comment saying that putting -c
flag prevents sending the init sequence. This is the output when trying to do that:
root@var-som-mx6:~/booting_files# stm32flash -c /dev/ttymxc2 -w /home/root/booting_files/zephyr.hex stm32flash 0.6 http://stm32flash.sourceforge.net/ Using Parser : Intel HEX Interface serial_posix: 57600 8E1 Failed to read ACK byte Unexpected reply from device on command 0x01
I've also tried changing the baud rate to 115200 and serial port mode to 8o1 (and the combination of the two, e.g. 57600 baud rate and 8o1 serial mode), but the problem persists.
Lastly I tried if I can flash the MCU with the Stm32CubeProgrammer and I can do that. However, when looking at verbose logging during the establishing the connection to the MCU I can see that the MCU times out on the response when the Stm32CubProgrammer sends the 0x7F byte the first time (and after sending the 0x7F the second time the MCU sends the proper response). I'm attaching the logs from that program.
What do you think? Do you have any idea of what I could try, because I'm slowly running out of ideas on how to solve this...
Anonymous
Maybe same problem as decribed here:
https://sourceforge.net/p/stm32flash/tickets/114/
The issue in ticket 114 should only affect STM32L412xx/422xx devices (bootloader V13.1).
The -c option (no init sequence) is to be used if the bootloader is already initialized by a previous connection (without reset in between). "Unexpected reply from device on command 0x01" is exactly what you would get if using -c on first connection after reset.
It seems to me that Stm32CubeProgrammer would do exactly the same as stm32flash. Please try the attached patch which gives more debug information.
Last edit: Tormod Volden 2021-08-28
I also have the same error
$ sudo stm32flash /dev/ttyUSB0
stm32flash STM32duino_0.5.1
https://github.com/stm32duino/stm32flash
Interface serial_posix: 57600 8E1
written 1 bytes, starting with 0x7f
read timed out
written 1 bytes, starting with 0x7f
read timed out
Failed to init device.
I am having a similar issue. This is 3d fysetc spider board connected to orangepi pc with stm32f446.
I am currently entering boot mode manually with:
echo 0 > /sys/class/gpio/gpio20/value
sleep 0.5
echo 0 > /sys/class/gpio/gpio10/value
sleep 0.2
echo 1 > /sys/class/gpio/gpio10/value
sleep 0.1
And exit with:
echo 1 > /sys/class/gpio/gpio20/value
sleep 0.5
echo 0 > /sys/class/gpio/gpio10/value
sleep 0.2
echo 1 > /sys/class/gpio/gpio10/value
"stm32flash -b 460800 -r foo5.bin /dev/ttyS3"
sometimes works sometimes does not work. I do not think those sleeps help anything at all and it is just as unreliable with any speed. I have compared the read flash contents with different baud rates and they are identical so I might just as well use 460800 to shave off time testing.
Interestingly ever since I got this board I have had similar issues updating firmware which might indicate that the system bootloader might be buggy. I am wondering if there is a a way to update it?
stm32flash 0.7
http://stm32flash.sourceforge.net/
Interface serial_posix: 460800 8E1
written 1 bytes, starting with 0x7f
read 1 bytes, starting with 0x00
Failed to init device.
we see the same error on STM32L4476XXX
any resolution to this issue?
There can be many reasons for "Failed to init device". Often it is related to how you force the device into bootloader mode, e.g. via gpio patterns.
I suspect some bootloaders are slow to start up, so extra delays may help.
Please try debugging it like this:
strace -o /tmp/trace.log -f -tt stm32flash -w fw.bin /dev/ttyUSB0
See for example https://sourceforge.net/p/stm32flash/tickets/153/?page=1#9fb7
This is the output from the STMCubeProgrammer, after 0x7F and 0x79, there is a a gap to the next command which is 0x2 and not 0x1 in your code, Also, when I look on the scope, it goes as 0x01 Stop bit error, and I do not see 0xFD getting transmitted, not sure why the initial commands are different and also, the Stop bit error instead of sending 2 bytes is a issue in your code.
14:47:17:818 : Port configuration: parity = even, baudrate = 115200, data-bit = 8, stop-bit = 1.0, flow-control = off
14:47:17:818 : No Init bits value is : 0
14:47:17:818 : Sending init command:
14:47:17:819 : byte 0x7F sent successfully to target
14:47:17:819 : Wait ends after 1 loop, dataready = 1, delay = 1
14:47:17:819 : Received response from target: 0x79
14:47:17:819 : Activating device: OK
14:47:17:819 : Board : --
14:47:17:820 : Sending GetID command and its XOR:
14:47:17:820 : byte 0x02 sent successfully to target
14:47:17:820 : byte 0xFD sent successfully to target
14:47:17:820 : Wait ends after 1 loop, dataready = 1, delay = 2
14:47:17:821 : Received response from target: 0x79
14:47:17:821 : Received 4 data bytes from target : 0x01041579
14:47:17:821 : Chip ID: 0x415
14:47:17:821 : Sending Get command and its XOR:
14:47:17:821 : byte 0x00 sent successfully to target
14:47:17:822 : byte 0xFF sent successfully to target
14:47:17:822 : Wait ends after 1 loop, dataready = 1, delay = 3
If you are posting as Anonymous, please sign with a name so the post can be identified. There are 4 posts from Anonymous here and I don't know if it is 1, 2, 3, or 4 different issues and devices.
add extra wait for bootstrap loadup is resolved the issue for me. Thanks