Skip to main content
CHech.1
Associate III
Nov 7, 2021 7:35 AM UTC
Solved

How to flash a board using a bash/python script

Hi,

I'm using an STM32L496G board, currently flashing it through CubeMX/Touchgfx Designer.

In the future, i'd like to be able to flashthe board by running a bash/python/other script from a linux machine.

The board will be connected to the linux machine via uart.

I'm not sure how to do that, and I wasn't able to find helpful instructions online.

I'd appreciate your help.

Thank you!

This topic has been closed for replies.
CHech.1
CHech.1Best answer
Associate III
Dec 1, 2021 7:28 AM UTC

My solution:

(script is obviously very basic and needs error checking etc.)

import os
 
CUBE_PROGRAMMER_CLI_PATH = 'C:\\Program Files\\STMicroelectronics\\STM32Cube\\STM32CubeProgrammer\\bin'
FILE_TO_DOWNLOAD_NAME = 'MyFlashes\\MyFile.elf'
CONNECTION_PORT = 'swd'
EXTERNAL_LOADER_PATH = 'MX25R6435F_STM32L496G-DISCO.stldr'
 
 
def main():
 os.chdir(CUBE_PROGRAMMER_CLI_PATH)
 os.system(f"STM32_Programmer_CLI.exe -c port={CONNECTION_PORT} -w {FILE_TO_DOWNLOAD_NAME} -v -el {EXTERNAL_LOADER_PATH}")
 os.system(f"STM32_Programmer_CLI.exe -c port={CONNECTION_PORT} -hardRst")
 
if __name__ == '__main__':
 main()

4 replies

KnarfB
Super User
Nov 7, 2021 7:45 AM UTC

There are command line tools available: openOCD and stlink-tools.

hth

KnarfB

TDK
Nov 7, 2021 2:58 PM UTC

STM32CubeProgrammer has a command line interface (CLI) available as well. That's probably the simplest.

https://www.st.com/resource/en/user_manual/dm00403500-stm32cubeprogrammer-software-description-stmicroelectronics.pdf

"If you feel a post has answered your question, please click ""Accept as Solution""."
ST Technical Moderator
Nov 26, 2021 7:32 AM UTC

Hello @CHech.1​ ,

Were you able to resolve the issue you described in this post ?

If yes, please share the final solution you found and mark the reply as Best answer.

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
CHech.1
CHech.1AuthorBest answer
Associate III
Dec 1, 2021 7:28 AM UTC

My solution:

(script is obviously very basic and needs error checking etc.)

import os
 
CUBE_PROGRAMMER_CLI_PATH = 'C:\\Program Files\\STMicroelectronics\\STM32Cube\\STM32CubeProgrammer\\bin'
FILE_TO_DOWNLOAD_NAME = 'MyFlashes\\MyFile.elf'
CONNECTION_PORT = 'swd'
EXTERNAL_LOADER_PATH = 'MX25R6435F_STM32L496G-DISCO.stldr'
 
 
def main():
 os.chdir(CUBE_PROGRAMMER_CLI_PATH)
 os.system(f"STM32_Programmer_CLI.exe -c port={CONNECTION_PORT} -w {FILE_TO_DOWNLOAD_NAME} -v -el {EXTERNAL_LOADER_PATH}")
 os.system(f"STM32_Programmer_CLI.exe -c port={CONNECTION_PORT} -hardRst")
 
if __name__ == '__main__':
 main()