0xBAAAAAAD
address
This address is close to the GPIOE_BSRR
address we used before but this address is invalid.Invalid in the sense that there’s no register at this address.
Now, let’s try it.
$ cargo run
Breakpoint 3, main () at src/07-registers/src/main.rs:9
9 aux7::init();
(gdb) continue
Continuing.
Breakpoint 2, UserHardFault_ (ef=0x10001fc0)
at $REGISTRY/cortex-m-rt-0.6.3/src/lib.rs:535
535 loop {
We tried to do an invalid operation, reading memory that doesn’t exist, so the processor raised anexception, a hardware exception.
There are different kind of exceptions. Each kind of exception is raised by different conditions andeach one is handled by a different exception handler.
The aux7
crate depends on the cortex-m-rt
crate which defines a defaulthard fault handler, named UserHardFault
, that handles the “invalid memoryaddress” exception. openocd.gdb
placed a breakpoint on ; that’s whythe debugger halted your program while it was executing the exception handler.We can get more information about the exception from the debugger. Let’s see:
ef
is a snapshot of the program state right before the exception occurred. Let’s inspect it:
$1 = cortex_m_rt::ExceptionFrame {
r0: 0x48001800,
r1: 0x48001800,
r2: 0xb,
r3: 0xc,
r12: 0xd,
lr: 0x800019f,
pc: 0x80028d6,
xpsr: 0x1000000
}
The exception was caused by the ldr r0, [r0, #0]
instruction, a read instruction. The instructiontried to read the memory at the address indicated by the r0
register. By the way, r0
is a CPU(processor) register not a memory mapped register; it doesn’t have an associated address like, say,.
Wouldn’t it be nice if we could check what the value of the r0
register was right at the instantwhen the exception was raised? Well, we already did! The r0
field in the ef
value we printedbefore is the value of r0
register had when the exception was raised. Here it is again:
$1 = cortex_m_rt::ExceptionFrame {
r0: 0x48001800,
r1: 0x48001800,
r2: 0xb,
r3: 0xc,
r12: 0xd,
lr: 0x800019f,
pc: 0x80028d6,
xpsr: 0x1000000
}
r0
contains the value which is the invalid address we called the read_volatile
function with.