Skip to content

Instantly share code, notes, and snippets.

@mvirkkunen
Created September 3, 2020 21:41
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 mvirkkunen/5c5850c640d0c7a5628ec7cd4cb46884 to your computer and use it in GitHub Desktop.
Save mvirkkunen/5c5850c640d0c7a5628ec7cd4cb46884 to your computer and use it in GitHub Desktop.
function context()
ctx = probe_context()
ctx.DbgMCU_CR = 0x7
ctx.DbgMCU_APB1_Fz = 0x0
ctx.DbgMCU_APB2_Fz = 0x0
ctx.TraceClk_Pin = 0x40002
ctx.TraceD0_Pin = 0x40003
ctx.TraceD1_Pin = 0x40004
ctx.TraceD2_Pin = 0x40005
ctx.TraceD3_Pin = 0x40006
return ctx
end
function s_DebugDeviceUnlock(ctx)
__Result = 0
s_CheckID(context())
return __Result
end
function s_DebugCoreStart(ctx)
__Result = 0
cmsis.Write32(ctx, 0xe000edf0, 0xa05f0001)
cmsis.Write32(ctx, 0xe0042004, ctx.DbgMCU_CR)
cmsis.Write32(ctx, 0xe0042008, ctx.DbgMCU_APB1_Fz)
cmsis.Write32(ctx, 0xe004200c, ctx.DbgMCU_APB2_Fz)
return __Result
end
function s_TraceStart(ctx)
__Result = 0
l_traceSWO = bool((ctx.__traceout & 0x1) ~= 0)
l_traceTPIU = bool((ctx.__traceout & 0x2) ~= 0)
if bool(l_traceSWO) then
s_EnableTraceSWO(context())
end
if bool(l_traceTPIU) then
s_EnableTraceTPIU(context())
end
return __Result
end
function s_TraceStop(ctx)
__Result = 0
l_traceSWO = bool((ctx.__traceout & 0x1) ~= 0)
l_traceTPIU = bool((ctx.__traceout & 0x2) ~= 0)
if bool(l_traceSWO) then
s_DisableTraceSWO(context())
end
if bool(l_traceTPIU) then
s_DisableTraceTPIU(context())
end
return __Result
end
function s_CheckID(ctx)
__Result = 0
l_pidr1 = 0
l_pidr2 = 0
l_jep106id = 0
l_ROMTableBase = 0
ctx.__ap = 0
l_ROMTableBase = cmsis.ReadAP(ctx, 0xf8) & ~0x3
l_pidr1 = cmsis.Read32(ctx, l_ROMTableBase + 0xfe4)
l_pidr2 = cmsis.Read32(ctx, l_ROMTableBase + 0xfe8)
l_jep106id = ((l_pidr2 & 0x7) << 4) | ((l_pidr1 >> 4) & 0xf)
if bool(bool(l_jep106id ~= 0x20)) then
cmsis.Query(ctx, 0, "Not a genuine ST Device! Abort connection", 1)
cmsis.Message(ctx, 2, "Not a genuine ST Device! Abort connection.")
end
return __Result
end
function s_EnableTraceSWO(ctx)
__Result = 0
l_dbgmcu_val = 0
l_dbgmcu_trace_val = 0
l_dbgmcu_val = cmsis.Read32(ctx, 0xe0042004) & ~0xe0
l_dbgmcu_trace_val = 1 << 5
s_ConfigureTraceSWOPin(context())
info("configure Trace I/O Enable + Trace Mode Asynchronous")
cmsis.Write32(ctx, 0xe0042004, l_dbgmcu_val | l_dbgmcu_trace_val)
return __Result
end
function s_DisableTraceSWO(ctx)
__Result = 0
l_dbgmcu_val = 0
info("unconfigure Trace I/O Enable + Trace Mode Asynchronous")
l_dbgmcu_val = cmsis.Read32(ctx, 0xe0042004) & ~0xe0
cmsis.Write32(ctx, 0xe0042004, l_dbgmcu_val)
return __Result
end
function s_EnableTraceTPIU(ctx)
__Result = 0
l_width = (ctx.__traceout & 0x3f0000) >> 16
l_dbgmcu_val = 0
l_dbgmcu_trace_val = 0
l_dbgmcu_val = cmsis.Read32(ctx, 0xe0042004) & ~0xe0
s_ConfigureTraceTPIUPins(context())
info("TPIU port width 1")
if bool(u64_ge(l_width, 1)) then
info("configure Trace I/O Enable + Trace Mode Synchronous 1 bit")
l_dbgmcu_trace_val = 3 << 5
end
info("TPIU port width 2")
if bool(u64_ge(l_width, 2)) then
info("configure Trace I/O Enable + Trace Mode Synchronous 2 bit")
l_dbgmcu_trace_val = 5 << 5
end
info("TPIU port width 4")
if bool(u64_ge(l_width, 4)) then
info("configure Trace I/O Enable + Trace Mode Synchronous 4 bit")
l_dbgmcu_trace_val = 7 << 5
end
info("configure Trace I/O Enable + Trace Mode Asynchronous")
cmsis.Write32(ctx, 0xe0042004, l_dbgmcu_val | l_dbgmcu_trace_val)
return __Result
end
function s_DisableTraceTPIU(ctx)
__Result = 0
l_dbgmcu_val = 0
info("unconfigure Trace I/O Enable + Trace Mode Synchronous")
l_dbgmcu_val = cmsis.Read32(ctx, 0xe0042004) & ~0xe0
cmsis.Write32(ctx, 0xe0042004, l_dbgmcu_val)
return __Result
end
function s_ConfigureTraceSWOPin(ctx)
__Result = 0
l_pin = 0
l_port = 0
l_portAdr = 0
l_pos = 0
l_SWO_Pin = 0x10003
info("configure SWO")
l_pin = l_SWO_Pin & 0xffff
l_port = (l_SWO_Pin >> 16) & 0xffff
l_portAdr = 0x40020000 + (((l_SWO_Pin >> 16) & 0xffff) * 0x400)
l_pos = l_pin * 2
cmsis.Write32(ctx, 0x40023830, cmsis.Read32(ctx, 0x40023830) | (1 << l_port))
cmsis.Write32(ctx, l_portAdr + 0x0, (cmsis.Read32(ctx, l_portAdr + 0x0) & ~(3 << l_pos)) | (2 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0x8, cmsis.Read32(ctx, l_portAdr + 0x8) | (3 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0xc, cmsis.Read32(ctx, l_portAdr + 0xc) & ~(3 << l_pos))
if bool(u64_lt(l_pin, 8)) then
l_pos = (l_pin & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x20, cmsis.Read32(ctx, l_portAdr + 0x20) & ~(15 << l_pos))
end
if bool(u64_ge(l_pin, 8)) then
l_pos = ((l_pin - 8) & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x24, cmsis.Read32(ctx, l_portAdr + 0x24) & ~(15 << l_pos))
end
return __Result
end
function s_ConfigureTraceTPIUPins(ctx)
__Result = 0
l_pin = 8
l_port = 0
l_portAdr = 0
l_pos = 0
l_width = (ctx.__traceout & 0x3f0000) >> 16
info("configure TRACECLK")
l_pin = ctx.TraceClk_Pin & 0xffff
l_port = (ctx.TraceClk_Pin >> 16) & 0xffff
l_portAdr = 0x40020000 + (((ctx.TraceClk_Pin >> 16) & 0xffff) * 0x400)
l_pos = l_pin * 2
cmsis.Write32(ctx, 0x40023830, cmsis.Read32(ctx, 0x40023830) | (1 << l_port))
cmsis.Write32(ctx, l_portAdr + 0x0, (cmsis.Read32(ctx, l_portAdr + 0x0) & ~(3 << l_pos)) | (2 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0x8, cmsis.Read32(ctx, l_portAdr + 0x8) | (3 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0xc, cmsis.Read32(ctx, l_portAdr + 0xc) & ~(3 << l_pos))
if bool(u64_lt(l_pin, 8)) then
l_pos = (l_pin & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x20, cmsis.Read32(ctx, l_portAdr + 0x20) & ~(15 << l_pos))
end
if bool(u64_ge(l_pin, 8)) then
l_pos = ((l_pin - 8) & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x24, cmsis.Read32(ctx, l_portAdr + 0x24) & ~(15 << l_pos))
end
info("TPIU port width 1")
if bool(u64_ge(l_width, 1)) then
info("configure TRACED0")
l_pin = ctx.TraceD0_Pin & 0xffff
l_port = (ctx.TraceD0_Pin >> 16) & 0xffff
l_portAdr = 0x40020000 + (((ctx.TraceD0_Pin >> 16) & 0xffff) * 0x400)
l_pos = l_pin * 2
cmsis.Write32(ctx, 0x40023830, cmsis.Read32(ctx, 0x40023830) | (1 << l_port))
cmsis.Write32(ctx, l_portAdr + 0x0, (cmsis.Read32(ctx, l_portAdr + 0x0) & ~(3 << l_pos)) | (2 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0x8, cmsis.Read32(ctx, l_portAdr + 0x8) | (3 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0xc, cmsis.Read32(ctx, l_portAdr + 0xc) & ~(3 << l_pos))
if bool(u64_lt(l_pin, 8)) then
l_pos = (l_pin & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x20, cmsis.Read32(ctx, l_portAdr + 0x20) & ~(15 << l_pos))
end
if bool(u64_ge(l_pin, 8)) then
l_pos = ((l_pin - 8) & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x24, cmsis.Read32(ctx, l_portAdr + 0x24) & ~(15 << l_pos))
end
end
info("TPIU port width 2")
if bool(u64_ge(l_width, 2)) then
info("configure TRACED1")
l_pin = ctx.TraceD1_Pin & 0xffff
l_port = (ctx.TraceD1_Pin >> 16) & 0xffff
l_portAdr = 0x40020000 + (((ctx.TraceD1_Pin >> 16) & 0xffff) * 0x400)
l_pos = l_pin * 2
cmsis.Write32(ctx, 0x40023830, cmsis.Read32(ctx, 0x40023830) | (1 << l_port))
cmsis.Write32(ctx, l_portAdr + 0x0, (cmsis.Read32(ctx, l_portAdr + 0x0) & ~(3 << l_pos)) | (2 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0x8, cmsis.Read32(ctx, l_portAdr + 0x8) | (3 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0xc, cmsis.Read32(ctx, l_portAdr + 0xc) & ~(3 << l_pos))
if bool(u64_lt(l_pin, 8)) then
l_pos = (l_pin & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x20, cmsis.Read32(ctx, l_portAdr + 0x20) & ~(15 << l_pos))
end
if bool(u64_ge(l_pin, 8)) then
l_pos = ((l_pin - 8) & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x24, cmsis.Read32(ctx, l_portAdr + 0x24) & ~(15 << l_pos))
end
end
info("TPIU port width 4")
if bool(u64_ge(l_width, 4)) then
info("configure TRACED2")
l_pin = ctx.TraceD2_Pin & 0xffff
l_port = (ctx.TraceD2_Pin >> 16) & 0xffff
l_portAdr = 0x40020000 + (((ctx.TraceD2_Pin >> 16) & 0xffff) * 0x400)
l_pos = l_pin * 2
cmsis.Write32(ctx, 0x40023830, cmsis.Read32(ctx, 0x40023830) | (1 << l_port))
cmsis.Write32(ctx, l_portAdr + 0x0, (cmsis.Read32(ctx, l_portAdr + 0x0) & ~(3 << l_pos)) | (2 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0x8, cmsis.Read32(ctx, l_portAdr + 0x8) | (3 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0xc, cmsis.Read32(ctx, l_portAdr + 0xc) & ~(3 << l_pos))
if bool(u64_lt(l_pin, 8)) then
l_pos = (l_pin & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x20, cmsis.Read32(ctx, l_portAdr + 0x20) & ~(15 << l_pos))
end
if bool(u64_ge(l_pin, 8)) then
l_pos = ((l_pin - 8) & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x24, cmsis.Read32(ctx, l_portAdr + 0x24) & ~(15 << l_pos))
end
info("configure TRACED3")
l_pin = ctx.TraceD3_Pin & 0xffff
l_port = (ctx.TraceD3_Pin >> 16) & 0xffff
l_portAdr = 0x40020000 + (((ctx.TraceD3_Pin >> 16) & 0xffff) * 0x400)
l_pos = l_pin * 2
cmsis.Write32(ctx, 0x40023830, cmsis.Read32(ctx, 0x40023830) | (1 << l_port))
cmsis.Write32(ctx, l_portAdr + 0x0, (cmsis.Read32(ctx, l_portAdr + 0x0) & ~(3 << l_pos)) | (2 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0x8, cmsis.Read32(ctx, l_portAdr + 0x8) | (3 << l_pos))
cmsis.Write32(ctx, l_portAdr + 0xc, cmsis.Read32(ctx, l_portAdr + 0xc) & ~(3 << l_pos))
if bool(u64_lt(l_pin, 8)) then
l_pos = (l_pin & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x20, cmsis.Read32(ctx, l_portAdr + 0x20) & ~(15 << l_pos))
end
if bool(u64_ge(l_pin, 8)) then
l_pos = ((l_pin - 8) & 7) * 4
cmsis.Write32(ctx, l_portAdr + 0x24, cmsis.Read32(ctx, l_portAdr + 0x24) & ~(15 << l_pos))
end
end
return __Result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment