1
\$\begingroup\$

I am currently facing some problems with STM32F4, the process "hangs" and I am not able to understand at what point it "locked". When this happened, I collected the following values for the following variables (I created the variable stepError to "translate" the CFSR variable):

void prvGetRegistersFromStack (uint32_t * pulFaultStackAddress)
{
volatile uint32_t CFSRValue = SCB-> CFSR;
volatile uint32_t HFSRValue = SCB-> HFSR;
char stepError [1024] = "";
if ((HFSRValue & (1 << 30)) = 0) {
CFSRValue >> = 16;
if ((CFSRValue & (1 << 9)) = 0) strcpy (stepError, "Divide by zero");
if ((CFSRValue & (1 << 8))! = 0) strcpy (stepError, "Unaligned access");
if ((CFSRValue & (1 << 3)) = 0) strcpy (stepError, "No UsageFault coprocessor");
if ((CFSRValue & (1 << 2)) = 0) strcpy (stepError, "Invalid PC load UsageFault");
if ((CFSRValue & (1 << 1))! = 0) strcpy (stepError, "Invalid state");
if ((CFSRValue & (1 << 0))! = 0) strcpy (stepError, "Undefined instruction");
}

/ * These are volatile to try and prevent the compiler / linker optimizing them
away the variables never actually get used. If the debugger will not show the
values of the variables, make them global my moving their declaration outside
of this function. * /
volatile uint32_t r0;
volatile uint32_t r1;
volatile uint32_t r2;
volatile uint32_t r3;
volatile uint32_t r12;
volatile uint32_t lr; / * Link register. * /
volatile uint32_t pc; / * Program counter. * /
volatile uint32_t psr; / * Program status register. * /

r0 = pulFaultStackAddress [0];
r1 = pulFaultStackAddress [1];
r2 = pulFaultStackAddress [2];
r3 = pulFaultStackAddress [3];

r12 = pulFaultStackAddress [4];
lr = pulFaultStackAddress [5]; // Bit (2 or 3) = 0 determines MSP (Main Stack Pointer); 1 = PSP (Process Stack Pointer)
pc = pulFaultStackAddress [6]; // Variable that contains the address where the error occurred. To check where it was, search the Disassembly on the screen Debug the address
psr = pulFaultStackAddress [7];

/ * When the following line is hit, the variables contain the register values. * /

// Joseph Yiu:
/ *
1) Look at LR value when the core enter hardfault, if bit 2 is 0, then read the value of MSP. Otherwise, read the value of PSP.
2) Based on the MSP / PSP value, you should be able to locate the start of stack frame, stacked PC is in address SP + 24.
3) Generate a disassembled listing of the program you run, and try to locate the stack PC address in the disassembled program list.
* /

GPIO_WriteLed (0,1);
for (int i = 0; i <= 10; i ++)
{
PWM_Change_DutyCycle (i, 0);
}
for (;;);
}

HFSRValue 1073741824 CFSRValue 0 StepError 0x2001fbb0 ""

r0 0 r1 0 r2 0 r3 11

r12 536890019 lr 134334773 pc 0x0801bab0 psr 3221225472

But I can not know from these values where the error occurred, whether it was caused by usb, serial, encoder or ADC converter and etc. How to implement void HardFault_Handler (void) so I can recognize where the error occurs?

\$\endgroup\$
  • \$\begingroup\$ Service announcement: R12=0x20004AA3‬, LR=0x‭801C935‬, PSR=0x‭C0000000‬, HFSR=0x‭40000000‬(forced) \$\endgroup\$ – Turbo J Jun 28 '18 at 14:36
  • \$\begingroup\$ Note that you might want to examine the lower 16 bits of the CFSR register, too. Trying to access a peripherial that is not enabled could trigger a bus fault. \$\endgroup\$ – Turbo J Jun 28 '18 at 14:38
  • \$\begingroup\$ It's not clear to me how you've determined that a HardFault has occurred. The fact that some other code somewhere has hung does not necessarily mean this was caused by a HardFault. \$\endgroup\$ – brhans Jun 28 '18 at 14:51
  • \$\begingroup\$ Caught in something that is not part of the code. When the PC gets the value of the handler method, or with getRegisters, it means that it was not the processor that crashed, but something external (interrupt, external communication, ADC converter) ... \$\endgroup\$ – Eduardo Cardoso Jun 28 '18 at 14:58
  • \$\begingroup\$ You can't assume that a hardfault is caused by something external. A hardfault can be caused by trying to write to read-only memory. What is the assembly code near 0x0801BAB0? \$\endgroup\$ – Elliot Alderson Jun 28 '18 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.