Skip to main content
CHech.1
Associate III
November 7, 2021
Solved

How to flash a board using a bash/python script

  • November 7, 2021
  • 4 replies
  • 3920 views

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.
Best answer by CHech.1

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
November 7, 2021

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

hth

KnarfB

TDK
Super User
November 7, 2021

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""."
Technical Moderator
November 26, 2021

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
December 1, 2021

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()