Skip to content

Instantly share code, notes, and snippets.

@fabiovila
Last active June 12, 2020 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiovila/cbcf073928c0eb8036d2d2da023629d0 to your computer and use it in GitHub Desktop.
Save fabiovila/cbcf073928c0eb8036d2d2da023629d0 to your computer and use it in GitHub Desktop.
OpenOCD as standalone stm8 flash programming

Add this to stm8s.cfg

proc program_device {filename flashstart} {
halt
wait_halt
load_image $filename $flashstart
sleep 10
reset halt
resume
sleep 10
shutdown
};

Usage

Invoke openocd with option ...

-c "program_device bin/main.ihx 0"

bin/main.ihx is your hex file

Example (xPack OpenOCD):

 openocd  -f scripts/interface/stlink.cfg -f scripts/target/stm8s.cfg -c "init"  -c "program_device bin/main.ihx 0"
# script for stm8s family
#
# stm8 devices support SWIM transports only.
#
transport select stlink_swim
if { [info exists CHIPNAME] } {
set _CHIPNAME $CHIPNAME
} else {
set _CHIPNAME stm8s
}
# Work-area is a space in RAM used for flash programming
# By default use 1kB
if { [info exists WORKAREASIZE] } {
set _WORKAREASIZE $WORKAREASIZE
} else {
set _WORKAREASIZE 0x400
}
if { [info exists FLASHSTART] } {
set _FLASHSTART $FLASHSTART
} else {
set _FLASHSTART 0x8000
}
if { [info exists FLASHEND] } {
set _FLASHEND $FLASHEND
} else {
set _FLASHEND 0xffff
}
if { [info exists EEPROMSTART] } {
set _EEPROMSTART $EEPROMSTART
} else {
set _EEPROMSTART 0x4000
}
if { [info exists EEPROMEND] } {
set _EEPROMEND $EEPROMEND
} else {
set _EEPROMEND 0x43ff
}
if { [info exists OPTIONSTART] } {
set _OPTIONSTART $OPTIONSTART
} else {
set _OPTIONSTART 0x4800
}
if { [info exists OPTIONEND] } {
set _OPTIONEND $OPTIONEND
} else {
set _OPTIONEND 0x487f
}
if { [info exists BLOCKSIZE] } {
set _BLOCKSIZE $BLOCKSIZE
} else {
set _BLOCKSIZE 0x80
}
hla newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id 0
set _TARGETNAME $_CHIPNAME.cpu
target create $_TARGETNAME stm8 -chain-position $_CHIPNAME.cpu
$_TARGETNAME configure -work-area-phys 0x0 -work-area-size $_WORKAREASIZE -work-area-backup 1
$_TARGETNAME configure -flashstart $_FLASHSTART -flashend $_FLASHEND -eepromstart $_EEPROMSTART -eepromend $_EEPROMEND
$_TARGETNAME configure -optionstart $_OPTIONSTART -optionend $_OPTIONEND -blocksize $_BLOCKSIZE
# Uncomment this line to enable interrupts while instruction step
#$_TARGETNAME configure -enable_step_irq
# The khz rate does not apply here, only slow <0> and fast <1>
adapter_khz 1
reset_config srst_only
# uncomment this line to connect under reset
#reset_config srst_nogate connect_assert_srst
proc program_device {filename flashstart} {
halt
wait_halt
load_image $filename $flashstart
sleep 10
reset halt
resume
sleep 10
shutdown
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment