# This script configures OpenOCD for use with an ST-Link V3 programmer/debugger
# and an STM32H7 target microcontroller with 2MB RAM (dual bank).
#
# To flash your firmware:
#
#    $ openocd -f boards/openocd_stm32h7_dual_bank.cfg \
#        -c "stm_flash build-BOARD/firmware.bin 0x08000000
#
# For a gdb server on port 3333:
#
#    $ openocd -f boards/openocd_stm32h7_dual_bank.cfg


source [find interface/stlink-dap.cfg]
transport select dapdirect_swd
source [find target/stm32h7x_dual_bank.cfg]
reset_config srst_only connect_assert_srst
init

proc stm_flash { BIN0 ADDR0 {BIN1 ""} {ADDR1 ""} } {
    reset halt
    sleep 100
    wait_halt 2
    flash write_image erase $BIN0 $ADDR0
    sleep 100
    verify_image $BIN0 $ADDR0
    sleep 100
    if {$BIN1 ne ""} {
        flash write_image erase $BIN1 $ADDR1
        sleep 100
        verify_image $BIN1 $ADDR1
        sleep 100
    }
    reset run
    shutdown
}

proc stm_erase {} {
    reset halt
    sleep 100
    stm32h7x mass_erase 0
    sleep 100
    stm32h7x mass_erase 1
    sleep 100
    shutdown
}
