cancel
Showing results for 
Search instead for 
Did you mean: 

How to access *any* memory address without a bus fault?

gw
Associate II
Posted on May 08, 2014 at 11:57

How could I write some code that would access a memory address and wouldn't generate a bus fault if it was incorrect? Or failing that, is there a way to safely check if an address is valid, or maybe even a way to have the BusFault handler return and have normal execution resume?

I'm writing a language interpreter for an STM32F103, and there are some cases when this would be very handy:

  • Providing a 'peek' instruction that wouldn't crash the micro if an invalid address was given (presumably some addresses would still cause pain - but it'd be a lot less likely!)
  • Checking at run-time how much usable RAM there was, and expanding the stack+heap to use it - because some chips appear to just be re-badged higher end parts (eg. some STM32F103VC chips have 64kb instead of the marketed 48kb) - this is for use by enthusiasts - obviously it wouldn't be relied on for production.

I've tried quite hard to get something working, but haven't had much luck so far - if I return from the Bus Fault handler (even after clearing the flags) the processor immediately HardFaults.

thanks!
20 REPLIES 20
gw
Associate II
Posted on May 08, 2014 at 18:13

Just tried this - it was actually SP+24 - I can't count.

My mistake had been trying to clear the busfault active flag with:

SCB->SHCSR &= ~SCB_SHCSR_BUSFAULTACT;

I had assumed that the repeated BusFault IRQs were because the active flag needed clearing, but in reality the code above caused the HardFault when the function returned (no idea why!). 

If I remove that, and add code to set the old PC up correctly, it all works! Thanks for your help everyone!