2012-04-07 01:52 PM
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?2012-04-07 06:22 PM
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 BFSRhttp://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.2024-09-10 02:02 PM
Here is an updated link on how to debug a bus or hard fault on STM32 MCUs :
https://community.st.com/t5/stm32-mcus/how-to-debug-a-hardfault-on-an-arm-cortex-m-stm32/ta-p/672235