Giter Club home page Giter Club logo

stm8gal's Introduction

drawing

Overview

stm8gal is an application for programming or reading out STM8 memory using the built-in ROM bootloader (BSL) via UART or SPI interface. It is compatible with Windows, MacOS X and Linux, including Raspbian. For a list of tested configurations see section Test Overview.

Note: stm8gal replaces the STM8_serial_flasher. The latter was renamed after a proposal by Philipp Krause

Tools with similar functionality via the UART interface are:

For bug reports or feature requests please send me a note.

Have fun! Georg


License / Disclaimer


Building the Software

The majority of stm8gal is written in ISO C 99 with some OS-specific routines required e.g. for serial communication. However the required serial libs are part of all standard compiler suites and shouldn't pose a compatibility issue. In case of portability issues please contact the author for support.

Non ANSI-C extensions:

  • wiringPi library, which is Raspberry Pi specific, and allows automatic reset of the STM8 via GPIO header pin. Is pre-installed for Raspbian Stretch and above. To activate remove comment in Makefile

  • spidev kernel library for interfacing to the SPI. To activate remove comment in Makefile. The Raspberry Pi and other "embedded PCs" provide direct SPI pin access, so no extra hardware is required. For "normal" PCs, an extra hardware and likely an adaptation of the SPI send/receive routines is required (volunteers?)

A code reference can be generated by running Doxygen with input file 'Doxyfile'. Then open file './doxygen/html/index.html' with a webbrowser. For other output formats, e.g. PDF, modify 'Doxyfile' accordingly.


Linux

Use one of the following options to build stm8gal:

  • Open a terminal and change to the project directory. Then type make
  • Alternatively double-click file "_compile.sh". May require execute permissions via chmod u+x *.sh
  • in Geany open project "stm8gal.geany"and make (Shift+F9)
  • for SPI communication via supported SPI adapter (untested!):
    • requires installed spidev and user access to SPI hardware
    • specify CFLAGS += -DUSE\_SPIDEV and SOURCES += spi\_spidev\_comm.c in file "Makefile"

Note: Under Linux access to serial ports may be prohibited. To grant access rights see here


Raspberry Pi

Same as Linux above, but with additional option:

  • for reset STM8 via GPIO header:
    • specify -DUSE\_WIRING and -lwiringPi in file "Makefile"
    • if required install wiringPi. To check type gpio readall in commandline
    • enable GPIO access via raspi-config
    • add user to group gpio (sudo usermod -a -G gpio $(whoami))
  • for UART communication via GPIO header:
    • enable serial interface access via raspi-config
    • add user to group dialout (sudo usermod -a -G dialout $(whoami))
  • for SPI communication via GPIO header:
    • install spidev if required (is pre-installed for Raspbian Stretch and above)
    • specify CFLAGS += -DUSE\_SPIDEV and SOURCES += spi\_spidev\_comm.c in file "Makefile"
    • enable SPI access via raspi-config
    • add user to group spi (sudo usermod -a -G spi $(whoami))

Windows

Use one of the following options to build stm8gal:

  • in DevC++ open project "stm8gal.dev" and build all (F12)
  • in Microsoft Visual C++ open project "MSVC" and build all
  • in File Explorer double-click file "_compile.bat". Requires gcc and mingw32-make to be installed and in PATH. May require some modifications to the batchfile and file Makefile

Note: Windows commandline (cmd.exe) requires a path on a mounted drive, i.e. path to the tool has to start with 'C:', 'D:' or similar. So either copy the tool to a mounted drive or use 'connect network drive' to assign a drive letter. This limitation does not apply to POSIX operating systems


MacOSX

Same as for Linux above. However, for double-click rename files ".sh" to ".command"

Note: Due to lack of a Macintosh, compatibility with MacOSX is no longer tested. Therefore, please provide feedback if you have experience with stm8gal on a Mac. Also a Mac binary for distribution is highly appreciated. Thanks!


Using the Software

stm8gal is a commandline tool without graphical interface (volunteers...?). The application is called from the command line or via shell script using the below syntax.

usage: stm8gal with following options/commands:

-h/-help                        print this help
-v/-verbose [level]             set verbosity level 0..3 (default: 2)
-B/-background                  skip prompts and colors for background operation (default: foreground)
-q/-exit-prompt                 prompt for <return> prior to exit (default: no prompt)
-R/-reset [rst]                 reset for STM8: 0=skip, 1=manual, 2=DTR line (RS232), 3=send 'Re5eT!' @ 115.2kBaud, 4=Arduino pin pin 8, 5=Raspi pin 12, 6=RTS line (RS232) (default: manual)
-i/-interface [line]            communication interface: 0=UART, 1=SPI via Arduino, 2=SPI via spidev (default: UART)
-u/-uart-mode [mode]            UART mode: 0=duplex, 1=1-wire, 2=2-wire reply, other=auto-detect (default: auto-detect)
-p/-port [name]                 communication port (default: list available ports)
-b/-baudrate [speed]            communication baudrate in Baud (default: 115200)
-V/-verify [method]             verify flash content after upload: 0=skip, 1=CRC32 checksum, 2=read-back (default: read-back)
-j/-jump-addr [address]         jump to address (as dec or hex) before exit of stm8gal, or -1 for skip (default: flash)
-w/-write-file [file [addr]]    upload file from PC to uController. For binary file (*.bin) with address offset (as dec or hex)
-W/-write-byte [addr value]     change value at given address (both as dec or hex)
-r/-read [start stop output]    read memory range (as dec or hex) and save to file or print (output=console)
-e/-erase-sector [addr]         erase flash sector containing given address (as dec or hex). Use carefully!
-E/-erase-full                  mass erase complete flash. Use carefully!

Notes:

  • reset via RasPi GPIO (-R 5) is only available on a Raspberry Pi and if stm8gal was built with wiringPi support (see Building the Software)
  • interface spidev (-i 2) is only available if stm8gal was built with spidev support (see Building the Software)
  • SPI via Arduino (-i 1) and reset via Arduino GPIO (-R 4) requires an additional Arduino programmed as SPI bridge
  • to avoid address conflicts with Boot-ROM or CRC32-verify RAM routines, optional user RAM code should start at address 0x400

Supported File Formats

Supported import formats (option '-w'):

  • Motorola S19 (*.s19), for a description see here
  • Intel Hex (*.hex, *.ihx), for a description see here
  • ASCII table (*.txt) consisting of lines with 'addr value' (dec or hex). Lines starting with '#' are ignored. For example see here
  • Binary (*.bin) with an additional starting address

Supported export formats (option '-r'):

  • print to stdout ('console')
  • Motorola S19 (*.s19)
  • Intel Hex (*.hex, *.ihx)
  • ASCII table (*.txt) with 'hexAddr hexValue'
  • Binary (*.bin) without starting address

Data is uploaded and exported in the specified order, i.e. later uploads may overwrite previous uploads. Also exports only contain the previous uploads, i.e. intermediate exports only contain the memory content up to that point in time.


Examples

Program custom muBoard via USB with manual reset

  1. supply the muBoard via USB from the PC, here a RasPi. This also establishes the USB<->UART connection via an on-board FT232 adapter

  2. note name of serial port, e.g. COM10 (Win) or /dev/ttyUSB0 (Linux). Hint: launching stm8gal without argument lists the available ports

  1. software usage:

    -stm8gal -p /dev/ttyUSB0 -w main.ihx (Linux)

    -stm8gal -p COM10 -w main.ihx (Windows)


Program STM8S Discovery Board via UART pins from Raspberry Pi with reset via GPIO

  1. connect the STM8S Discovery Board to the Raspberry Pi in the same sequence as shown below. Notes:

  1. software usage:

    -stm8gal -p /dev/ttyAMA0 -w main.ihx -R 5 (RasPi 1+2)

    -stm8gal -p /dev/serial0 -w main.ihx -R 5 (RasPi 3)


Program STM8S Discovery Board via SPI pins from Raspberry Pi using spidev with reset via GPIO

  1. connect the STM8S Discovery Board to the Raspberry Pi in the same sequence as shown below. Notes:

  1. software usage:

    -stm8gal -i 2 -p /dev/spidev0.0 -w main.ihx -R 5


Program STM8S Discovery Board via Arduino SPI bridge and reset via GPIO

  1. Ensure voltage level compatibility between Arduino and STM8. Note that ATMega-based (8-bit) Arduinos generally have 5V GPIOs, while ARM-based (32-bit) boards only support 3.3V. Exposing a 3.3V device to 5V signals may damage the 3.3V device.

  2. Supply the Arduino to the PC via USB. This also establishes an USB connection to the on-board microcontroller

  3. Program the Arduino to act an an USB<->SPI bridge.

  4. Note name of serial port, e.g. COM10 (Win) or /dev/ttyUSB0 (Linux). Hint: launching stm8gal without argument lists the available ports

  5. connect the STM8S Discovery Board to the Arduino in the same sequence as shown below.

  1. software usage:

    -stm8gal -i 1 -p /dev/ttyUSB0 -w main.ihx -R 4


Memory dump muBoard via USB with manual reset

  1. supply the muBoard via USB from the PC, here a RasPi. This also establishes the USB<->UART connection via an on-board FT232 adapter

  2. note name of serial port, e.g. COM10 (Win) or /dev/ttyUSB0 (Linux). Hint: launching stm8gal without argument lists the available ports

  1. software usage:

    -stm8gal -p /dev/ttyUSB0 -r 0x8000 0x8FFF dump.s19 (Linux, Motorola S19 format)

    -stm8gal -p /dev/ttyUSB0 -r 0x8000 0x8FFF dump.txt (Linux, table format)

    -stm8gal -p COM10 -r 0x8000 0x8FFF dump.s19 (Windows, Motorola S19 format)


Notes

  • bootloader programming via UART, SPI or CAN is supported by most STM8 devices. However, not all devices support each interface. A full description of the bootloaders can be found in UM0560, including an overview of STM8 devices with respective bootloader mode. For stm8gal >=v1.2.0 the UART mode can optionally be auto-detected:

  • To program via stm8gal, communication between PC and STM8 must be possible. On the STM8 side this is via UART or SPI, on the PC side this is generally via USB or RS232.

    • On some boards, e.g. the muBoard, an USB<->UART bridge is already present. In this case, no additional hardware is required

    • However, e.g. the popular STM8S Discovery and STM8L Discovery boards connect to a standard PC via SWIM (=debug) interface. In this case a separate adapter is required to connect to the respective STM8 UART or SPI pins, e.g. UM232R. When connecting, make sure that the voltage levels of STM8 and the adapter are compatible. Exposing a 3.3V device to 5V signals may damage the 3.3V device.

    • Exception is the Raspberry Pi or similar “embedded PCs“ with direct access to UART and SPI pins via the GPIO header. In this case make sure that the voltage levels of STM8 and embedded PC are compatible, e.g. for RasPi with 3.3V pins also supply the STM8 with Vdd=3.3V. As noted above, never expose a 3.3V device to 5V signals.

    • Alternatively an Arduino can be used to act as a USB<->SPI bridge using a dedicated "gateway software". For the Arduino project and a technical documentation see the Arduino SPI_bridge repository. Again, note compatible voltage levels of Arduino and STM8.

  • Prior to uploading code via stm8gal the STM8 ROM bootloader needs to be activated via option bytes. For details see the respective STM8 device datasheet. If required, change the option bytes via SWIM debug interface using

    • ST Visual Programmer application by STM (proprietary, Windows only)

    • stm8flash by Valentin Dudouyt to upload and run the STM8 program in subfolder './BSL_activate'

    • Notes

      • Virgin devices (i.e. flash completely erased) automatically have the bootloader enabled
  • The BSL can be entered only within 1s after reset or power-on. Exception are virgin devices, which remain in bootloader mode indefinitely.

  • The UART "reply" mode (see above) supports single-wire interfaces like LIN or ISO9141. It requires a "Rx echo" for each sent byte. Using the reply mode with dual wires therefore requires stm8gal to echo each received byte individually, which results in extremely low reading speed due to USB latency.

  • SPI communication via spidev or Arduino SPI bridge works reliably only up to 250kBaud in my test setup (see above)

  • The STM32 uses a very similar bootloader protocol, so adapting the flasher tool for STM32 should be straightforward. However, I have no board available, but please feel free to go ahead...


Test Overview

stm8gal has recently been tested only for the below STM8 devices and operating systems. Theoretically it should work for all STM8 devices with bootloader, especially since STM8AF, STLUX, STNRG and STM8SPLNB seem to be test variants of the STM8S, and STM8AL and STM8T seem to be test variants of the STM8L. However, this has not been tested, so if you use stm8gal in another setup, any feedback is highly appreciated!

green: test passed; yellow: not yet tested


Known Bugs

  • currently no known bugs

If you are aware of bugs, please drop me a note or start an issue on the project homepage.


Revision History

v1.6.0 (2023-08-08)

  • switch from static RAM buffer to dynamic memory image
  • support for 32-bit addresses (see memory_image.h)
  • migrate to VSCode / PlatformIO file structure for convenience

v1.5.0 (2021-01-23)

  • replaced write/erase RAM routines by STM with OSS routines (see #4)
  • removed STM8S 32kB v1.4 and 128kB v2.4. According to STM support were never released and development is stopped
  • removed STM8S 256kB v1.0. According to STM support variant was never released and development is stopped
  • fixed bug in v1.4.3 which prevented flash write/erase after CRC32-verify

v1.4.3 (2021-01-09)

  • fixed bug in SPI read-back after write
  • fixed bug in CRC32-verify (see #20)

v1.4.2 (2020-12-26)

  • support re-synchronization w/o STM8 reset
  • add option verify via CRC32 checksum (see #20)
    • add parameter to verify option (-V/-verify). Is required due to new CRC32 check

v1.4.1 (2020-12-13)

  • minor bugfix

v1.4.0 (2020-04-09)

  • improved S19 export for >16bit addresses
  • added IHX export option

v1.3.0 (2019-01-02)

  • add multiple up- and downloads in single run
  • added option to print memory map and sector erase
  • fixed S19 export bugs for >16bit addresses and small images
  • fixed IHX import bug for record type 5
  • fixed mass erase timeout bug
  • fixed bug for files with "holes" -> only write specified data
  • harmonized files with https://github.com/gicking/hexfile_merger

v1.2.0 (2018-12-02)

  • add automatic UART mode detection (duplex, 1-wire, 2-wire reply). See UART mode issue
  • changed default UART baudrate to 115.2kBaud for robustness

v1.1.8 (2018-10-07)

  • add option for background operation for IDE usage. Skip prompts and setting console color & title
  • add different verbosity levels (0..2) for IDE usage

v1.1.7 (2018-01-04)


v1.1.6 (2017-12-22)


v1.1.5 (2017-12-20)

  • added SPI support via Arduino bridge
  • replace manual reset parameter "-Q" with "-R 0"
  • fixed bug in "2-wire reply mode" (wrong echo)

v1.1.4 (2017-12-14)


v1.1.3 (2017-08-29)


v1.1.2 (2016-05-25)

  • add optional flash mass erase prior to upload

v1.1.1 (2016-02-03):

  • add support for STM8L family (skip RAM code upload)
  • add memory dump to file

v1.1.0 (2015-06-22):

  • add support for STM8 bootloader “reply mode“
  • add optional reset of STM8 via DTR (RS232/USB) or GPIO18 (Raspberry)

v1.0.0 (2014-12-21):

  • initial release by Georg Icking-Konert under the Apache License 2.0

stm8gal's People

Contributors

basilhussain avatar gicking avatar hifiphile avatar martin-kaiser avatar tenbaht avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stm8gal's Issues

confusion about the uart mode

This is not really a bug report, but a report of an odd finding. I am posting here instead of PM in the hope that this might be useful information for others as well.

I am using stm8gal (on mint19/ubuntu 18.4) with the simple breakout boards from China for the STM8S105K4T6 but the default communication fails:

$ stm8gal -p /dev/ttyUSB0

stm8gal (v1.1.8)
  reset STM8 and press <return>
  open serial port '/dev/ttyUSB0' with 230.4kBaud ... ok
  synchronize ... ok (ACK)
  determine device ... 

error in 'bsl_memCheck()': ACK1 timeout (expect 1, received 0), exit!

It turns out, that the MCU expects data transfer without parity (8N1), despite UM0560 specifically asking for even parity (8E1). This (surprisingly) works:

$ stm8gal -u 2 -p /dev/ttyUSB0

stm8gal (v1.1.8)
  reset STM8 and press <return>
  open serial port '/dev/ttyUSB0' with 230.4kBaud ... ok
  synchronize ... ok (ACK)
  determine device ... ok (STM8S; 32kB flash; BSL v1.3)
  Uploading RAM routines ... ok (304B from 0x00a0)
  jump to address 0x8000 ... ok
done with program

Do you have any idea why? I couldn't find a detailed list about the expected data format for each CPU.

Verify slow in reply mode

Reading out and/or verifying data in "UART reply mode" is very slow over 2-wire interface. This is caused by a few ms latency for each byte which must be echoed by the PC. Using a FT232R, reading back 14kB takes ~5min

open port failed under mac and ubuntu wsl

I get the error open serial port '/dev/bus/usb/001' ... Error: in 'init_port(/dev/bus/usb/001)': open port failed
under mac and ubuntu wsl
lsusb output:

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 0483:3748 STMicroelectronics ST-LINK/V2
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

command i run:
sudo stm8gal -p /dev/bus/usb/001 -w STM8L15X_MD.hex with and without sudo

stm8gal -p does not list anything

maybe its my programmeri dont know. just wanted to let you know!
stm8flash is working - if we work this out together i can provide Mac os m1 builds :)

Pre-built windows executable please :)

I hope you are still around :)

I am trying to learn how to use an STM8S with a STM8S-discovery board and I can flash using STVP, but I'd like to incorporate a command line flash into my VS Code (tasks.json calling the Makefile flash option I wrote)

I have gcc installed, but I don't want to have to start installing mingw etc.

I don't suppose you have a pre-built win10 version available for me to use do you?

Gordon

allow to program the option bytes

It would be useful to be able to access and modify the option bytes using stm8gal. Especially the ROP (read out protection) bit for unlocking a locked MCU is important.

Cannot program STM8S105K6 anymore

I cannot program my STM8S105K6-based boards anymore, which used to work.

Versions prior to 1d91c12:

> ./stm8gal -p /dev/ttyUSB0 -u 2 -R 1 -w /tmp/arduino_build_28225/Blink.ino.hex  

stm8gal (v1.2.0 beta)
  load Intel hex file 'Blink.ino.hex' ... ok (2.7kB)
  reset STM8 and press <return>
  open serial port '/dev/ttyUSB0' with 230.4kBaud ... ok
  synchronize ... ok (ACK)
  determine device ... ok (STM8S; 32kB flash; BSL v1.3)
  Uploading RAM routines ... ok (304B from 0x00a0)
  write 2.7kB (0x8000 to 0x8aa5) ... ok   
  read 2.7kB (0x8000 to 0x8aa4) ... ok
  verify memory ... ok
  activate bootloader ... ok
  jump to address 0x8000 ... ok
done with program

1d91c12 and later:

> ./stm8gal -p /dev/ttyUSB0 -u 2 -R 1 -w /tmp/arduino_build_28225/Blink.ino.hex 

stm8gal (v1.2.0 beta)
  load Intel hex file 'Blink.ino.hex' ... ok (2.7kB)
  reset STM8 and press <return>
  open serial port '/dev/ttyUSB0' with 230.4kBaud ... ok
  synchronize ... 

error in 'bsl_sync()': no response from BSL, exit!

They are connected via a CH340G USB-to-serial interface on the same board and I'm using Linux.

Build for OSX and for Windows fails due to missing SPI device

Compilation on OSX and on Windows fails for two reasons:

  • the Makefile tries to compile spi_spidev_comm.c (shouldn't be compiled at all on these platforms)
  • the flag -DUSE_SPIDEV is automatically set for OSX.

An easy workaround is to delete spi_spidev_comm.c from the list in SOURCES in the Makefile and start the compilation with this call: make OS=Win. Yes, even on OSX it needs OS=Win.

Does not handle 64K devices in bsl_getInfo()

The bsl_getInfo() function in bootloader.c attempts to determine memory size of the device by reading from the last byte of flash. I notice that it does not have a case for 64K devices. I believe this will mis-classify some 64K devices as having 32K of flash memory and being 'medium density' devices, when they should be 'high density'.

For example, the STM8AF5288 is a 64K device and is classified as 'high density'. See Table 1 (pg. 6) of UM0560. ST's website product page also says this: "They are referred to as high density STM8A devices in STM8S series and STM8AF series 8-bit microcontrollers reference manual (RM0016)".

I think this will lead to stm8gal incorrectly erroring with an "unsupported device" message, when in fact it should install the RAM routines for a 128K device (further evidenced by the ROM BL of a STM8AF5288 being byte-for-byte identical to a STM8S208).

Reset via DTR pin is broken

Resetting a board via DTR pin does not work anymore.
The guilty commit appears to be cd470a7, which kind of makes sense. I.e. you'll have to open the port before you can toggle DTR.

Write byte as decimal fails

stm8gal help: -W/-write-byte [addr value] change value at given address (as dec or hex)

However, an address or value in decimal is wrongly interpreted as hexadecimal.

Update: also true for -j, -w (bin), -r and -e

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.