This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Code works fine in optimzation while go to hardfault in levl 0 optimzation

HI,

My code works fine in optimzation for time level . But in level - 0 optimzation it goes to hardfault before entering main.

I am using LPC1317, here is reset handler for this.

Reset_Handler   PROC
                EXPORT  Reset_Handler             [WEAK]
                IMPORT  SystemInit
                IMPORT  __main
                LDR     R0, =SystemInit
                BLX     R0
                LDR     R0, =__main
                BX      R0                                -- STAT whre problem
                ENDP

After "BX R0" code goes into hardfault with level0 however works fine in level3+cross+time optimztion.

In both optimzation value of R0 is 0x000000C1.

I guess its due to R/W data 248 bytes. However watchdog is off by deafult.
I don't initialize any var during declaration. However even due to level 0 optimzation ZI data goes to R/W data.

Parents Reply Children
  • There is some weird stuff going on in my code.
    My another code with some modifications in code only nothing else in settings works fine in zero otpimztion while go to hardfault in level 3 optimzation.

    I tried following code in this case where code works fine in zero otpimztion while go to hardfault in level 3 optimzation.:

    void HardFault_Handler (void)  {
      if (CoreDebug->DHCSR & 1)  {     // check C_DEBUGEN == 1 -> Debugger Connected
        __breakpoint (0);              // halt program execution here        Code stuck here 
      }
    while (1);                       // enter endless loop otherwise
    }
    
    

    Also I tried code this, but I think I am using it wrong as code will jump it into when hardfault occur. So it contain address 0xc4 in PC. I don't know whether its right way to debug code.
    Like now what to do: check value of PC & go to that location in diassembly wiondow.
    If yes,I tried this but PC = 0xc4. I think I am doing somethinmg wrong here.

    
    volatile unsigned long stacked_r0 ;
    volatile unsigned long stacked_r1 ;
            volatile unsigned long stacked_r2 ;
            volatile unsigned long stacked_r3 ;
            volatile unsigned long stacked_r12 ;
            volatile unsigned long stacked_lr ;
            volatile unsigned long stacked_pc ;
            volatile unsigned long stacked_psr ;
            volatile unsigned long _CFSR ;
            volatile unsigned long _HFSR ;
            volatile unsigned long _DFSR ;
            volatile unsigned long _AFSR ;
            volatile unsigned long _BFAR ;
            volatile unsigned long _MMAR ;
    
    void HardFault_Handler(unsigned long *hardfault_args)
    {
            register uint32_t __r0     __ASM("r0");
            register uint32_t __r1     __ASM("r1");
            register uint32_t __r2     __ASM("r2");
            register uint32_t __r3     __ASM("r3");
            register uint32_t __r12     __ASM("r12");
            register uint32_t __lr     __ASM("lr");
            register uint32_t __pc     __ASM("pc");
            register uint32_t msp     __ASM("msp");
    
    
            stacked_r0 = __r0;
            stacked_r1 = __r1;
            stacked_r2 = __r2;
            stacked_r3 = __r3 ;
            stacked_r12 = __r12 ;
            stacked_lr = __lr ;
            stacked_pc = __pc ;
            stacked_psr = msp ;
    
            // Configurable Fault Status Register
            // Consists of MMSR, BFSR and UFSR
            _CFSR = (*((volatile unsigned long *)(0xE000ED28))) ;
    
            // Hard Fault Status Register
            _HFSR = (*((volatile unsigned long *)(0xE000ED2C))) ;
    
            // Debug Fault Status Register
            _DFSR = (*((volatile unsigned long *)(0xE000ED30))) ;
    
            // Auxiliary Fault Status Register
            _AFSR = (*((volatile unsigned long *)(0xE000ED3C))) ;
    
            // Read the Fault Address Registers. These may not contain valid values.
            // Check BFARVALID/MMARVALID to see if they are valid values
            // MemManage Fault Address Register
            _MMAR = (*((volatile unsigned long *)(0xE000ED34))) ;
            // Bus Fault Address Register
            _BFAR = (*((volatile unsigned long *)(0xE000ED38))) ;
    
    __breakpoint(0);
    
    }
    
    

  • You will need to switch to the disassembly window.

    LDR R0, =__main BX R0 <<< single step into this code.

    What does that mean. code already in assembly.

  • What does that mean. code already in assembly.

    Select the debugger disassembly window. If you don't the single step will step at the source code level and you won't see the real detail.

  • Ok I wil check it.
    Also someone tell me that code posted by me above is right for debugging hardfault ot not.& how to find reason for hardfault now.

  • If it's repeatable, then I tend to simply see where the hardfault is generated and backtrack from there.

  • HI, I checked it & strange thing happening:

    I have part of code code:

    if (x < 2)
    {
       temp = 2 ;
    }
    
    if (!temp)
    {
                  ........do_something
    }
    
    
    

    I have checkecd it at first as x=0, in my case. Debugger also accepts thius condition & go inside the variable temp=2 line. But it ius not assigning value to temp. Don't know why.

    I have checked it many times.