Skip to content

Instantly share code, notes, and snippets.

@k2qwerty2k
Last active February 26, 2024 21:33
Show Gist options
  • Save k2qwerty2k/50a5102e13113df9dc251666a461fac8 to your computer and use it in GitHub Desktop.
Save k2qwerty2k/50a5102e13113df9dc251666a461fac8 to your computer and use it in GitHub Desktop.
STM32CubeIDE OpenOCD fix CS32F103 ReadAPEx not STM device

Fix OpenOCD debug in STM32CubeIDE for clones, like CS32F103 on Linux

  1. Find st_scripts directory for OpenOCD with file mem_helper.tcl, or other file with scripts, in plugins directory of STM32CubeIDE
  2. Append to file
proc _find_target {apnum} {
	foreach t [target names] {
		if {[$t cget -ap-num] == $apnum && [$t cget -type] == "mem_ap"} {
			return $t
		}
	}
	foreach t [target names] {
		if {[$t cget -ap-num] == $apnum && [$t cget -type] == "cortex_m"} {
			return $t
		}
	}
	foreach t [target names] {
		if {[$t cget -type] == "cortex_m"} {
			return $t
		}
	}
	return [target current]
}

# Reads memory after MEM-AP
proc ReadMemAP {apbaseaddr address items flags} {
	echo [format "ReadMemAP 0x%08X 0x%08X 0x%08X 0x%08X" $apbaseaddr $address $items $flags]
	if {$apbaseaddr == 0x00 && $address == 0xE00FFFD0 && $items == 0x08 && $flags == 0x00} {
		return "O.K.:0x00000000,0x00000000,0x00000000,0x00000000,0x00000010,0x00000004,0x0000000a,0x00000000"
	}
	set dap [[target current] cget -dap]
	set apsel [expr {$apbaseaddr >> 24}]
	set value "O.K.:"
	set separator ""
	for {set i 0} {$i < $items} {incr i} {
		lappend value $separator 0x[lindex [[_find_target $apsel] mdw $address] 1]
		incr address 4
		set separator ","
	}
	echo [format "%s" [join $value ""]]
	return [join $value ""]
}

# Reads the specified CoreSight DAP-AP register
proc ReadAPEx {apbaseaddr regaddr} {
	echo [format "ReadAPEx 0x%08X 0x%08X" $apbaseaddr $regaddr]
	if {$apbaseaddr == 0x00 && $regaddr == 0xF8} {
		return "O.K.:0xE00FF003"
	}
	set dap [[target current] cget -dap]
	set apsel [expr {$apbaseaddr >> 24}]
	set value [$dap apreg $apsel $regaddr]
	echo [format "O.K.:0x%08X" $value]
	return [format "O.K.:0x%08X" $value]
}
  1. Find openocd binary in plugins directory and patch with HEX editor: replace all ReadAPEx string to other, like ReadAPEw.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment