cancel
Showing results for 
Search instead for 
Did you mean: 

How do I track down a bus fault exception

marcusrydh
Associate II
Posted on April 07, 2012 at 22:52

Hi,

I've been trying to write some code, but I'm running into a bus fault. I'm new to programming, especially with regards to microcontrollers/stm32, so I don't really know how to debug efficiently. I'm using Keil uVision4 with my STM32VL Discovery. I'm trying to communicate with some inertial sensors over I2C, and I've written some functions using the I2C optimized examples firmware available from ST. If I use these functions I've written, I can communicate fine with the sensors. However, I recently came across the example code for STM's LSM303DLHC sensor, which has some rather more involved functions for altering the sensor's register values. When I use these functions, and just run my code, I get a bus fault. When I step through the program, the bus fault disappears. I haven't understood, and have had no luck in figuring out how to use the debug information (core registers and disassembly) to help me track down the problem. Can someone help explain what information I need to look for and how to use it to track down a problem like this? Would including my source code help?
1 REPLY 1
Posted on April 08, 2012 at 03:22

I'd start by checking that the stack is adequate, Keil defaults to 1024 bytes, which is probably not enough if there are a lot of local variables, or printf of scanf type function are utilized. The stack is typically set up in the startup.s or stm32f10x_startup.s type files where the vector tables reside.

Try to confine the problem. Presuming it occurs predictably, try to bisect the code either using breakpoints, or serial output, to determine how far the code executes before in crashes.

For a Bus Fault, you probably want to try decoding the BFSR

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/Cihcfefj.html

You also want to look on the stack to see if the PC is at a consistent address when you see the fault.

For Hard Faults there are some good register dump/decoders following the methods proposed/demonstrated by Joseph Yiu. His Cortex-M3 book is highly recommended.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..