Skip to content

Instantly share code, notes, and snippets.

@cpq
Last active December 4, 2023 14:00
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 cpq/5b7ab583f5d8e936e48555e76f909647 to your computer and use it in GitHub Desktop.
Save cpq/5b7ab583f5d8e936e48555e76f909647 to your computer and use it in GitHub Desktop.
Flashing rt1060-evk via vcon SWD
#!/bin/bash
# SWD commands to call rt1060 flash boot rom functions
DEVCALL="curl -su :$VCON_API_KEY https://dash.vcon.io/api/v3/devices/13"
# SWD exec: se COMMANDS. Hex addresses are without leading 0x
se() { $DEVCALL/rpc/swd.exec -d "{\"req\": \"$*\"}" | jq -cr .resp ; }
rm() { se rm,$1 | cut -d, -f2 ; }
dump() { $DEVCALL/rpc/swd.read -d "{\"addr\":$1, \"size\": $2}" | jq -r .data | xxd -r -p | hexdump -C ; }
hex() { node -p "($*).toString(16)"; }
# Initialise and halt target
se init wm,e000edf0,a05f0003 wm,e000edfc,1 wm,e000ed0c,5fa0004
# BOOTLOADER=$(rm 20001c)
# FLASH_API=$(rm $(hex 0x$BOOTLOADER + 16))
# GET_FLASH_CONFIG_FUNC=$(rm $(hex 0x$FLASH_API + 36))
# FLASH_INIT_FUNC=$(rm $(hex 0x$FLASH_API + 4))
# FLASH_RESET_FUNC=$(rm $(hex 0x$FLASH_API + 24))
# FLASH_PROGRAM_PAGE_FUNC=$(rm $(hex 0x$FLASH_API + 8))
# FLASH_ERASE_SECTOR_FUNC=$(rm $(hex 0x$FLASH_API + 16))
# MASS_ERASE_FUNC=$(rm $(hex 0x$FLASH_API + 12))
GET_FLASH_CONFIG_FUNC=2139d9
FLASH_RESET_FUNC=2114c9
FLASH_ERASE_SECTOR_FUNC=212983
FLASH_PROGRAM_PAGE_FUNC=212c85
FLASH_INIT_FUNC=212c5b
# Clear cfg placeholder. 0x1d0 = 0x10 (&cfg) + 448 (offsetof(cfg.pageSize))
se wm,1d0,0 wm,1d4,0 wm,1d8,0 wm,1c,0 wm,10,0
echo "==> Calling get_config"
se wm,0,be00be00 # Write BKPT #0 at address 0, to halt after function call
se wm,4,c0000008 wm,8,0 # Create flash opts at address 0x4
se wr,0,0 wr,1,10 wr,2,4 # Params r0..r3: get_config(0, &cfg, &opt)
# set sp=32k, lr=0 (point to BKPT), pc=GET_CONFIG
se wr,d,8000 wr,e,1 wr,f,$GET_FLASH_CONFIG_FUNC
se wm,e000edf0,a05f0001 # Resume processor
se rm,e000edf0,2,2 rr,0 # Wait for halt, read return code
echo "==> Flash page size: 0x$(rm 1d0)"
echo "==> Flash sector size: 0x$(rm 1d4)"
echo "==> Calling flash_init"
se wr,0,0 wr,1,10 # Params: instance 0, flash config at address 0x10
se wr,d,8000 wr,e,1 wr,f,$FLASH_INIT_FUNC
se wm,e000edf0,a05f0001 # Resume processor
se rm,e000edf0,2,2 rr,0 # Wait for halt, read return code
# echo "==> Calling flash_reset"
# se wr,0,0 # Params: instance 0, flash config at address 0x10
# se wr,d,8000 wr,e,0 wr,f,$FLASH_RESET_FUNC
# se wm,e000edf0,a05f0001 # Resume processor
# se rm,e000edf0,2,2 rr,0 # Wait for halt, read return code
echo "==> Erasing first sector"
se wr,0,0 wr,1,10 wr,2,0 wr,3,1000 # Params: instance 0, flash config, address, sector size
se wr,d,8000 wr,e,1 wr,f,$FLASH_ERASE_SECTOR_FUNC
se wm,e000edf0,a05f0001 # Resume processor
se rm,e000edf0,2,2 rr,0 # Wait for halt
echo "==> Show the beginning of flash"
dump $(node -p 0x60000000) 64
echo "==> Writing first sector"
se wm,400,aabbccdd wm,404,eeff7755
se wr,0,0 wr,1,10 wr,2,0 wr,3,400 # Params: instance 0, flash config, address, source RAM address
se wr,d,8000 wr,e,1 wr,f,$FLASH_PROGRAM_PAGE_FUNC
se wm,e000edf0,a05f0001 # Resume processor
se rm,e000edf0,2,2 rr,0 # Wait for halt
# echo "==> MASS ERASE"
# se wr,0,0 wr,1,10 # Params
# se wr,d,8000 wr,e,1 wr,f,$MASS_ERASE_FUNC
# se wm,e000edf0,a05f0001 # Resume processor
# se rm,e000edf0,2,2 rr,0 # Wait for halt
echo "==> Show the beginning of flash"
dump $(node -p 0x60000000) 64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment