cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 : can't read global variables

David PICARD
Associate II
Posted on December 23, 2016 at 15:23

I'm trying to blink a LED on a Nucleo-L476 with CubeMX and ST's HAL. It worked perfectly on Windows (System Workbench). Now, I'm trying to do the same on Linux with CubeMX and Qt Createor (my favorite IDE) and OpenOCD. I am now able to compile and debug the target.

However, the program crashes during HAL initialization. More precisely, when it tries to access the

SystemCoreClock

variable.

The problem, in general, is that a global function can't be accessed from a function :

uint32_t dummyVar = 123;

void dummyFunc()

{

uint32_t loc = 123;

uint32_t *p = &loc;

p = &dummyVar; // debugger says &dummyVar = 0x20000004 and p = 0x011a3b01 (outside RAM)

__asm('nop');

__asm('nop');

__asm('nop');

loc = dummyVar; // *** crash here (WWDG IRQ) or random value

}

The variable

p

here points outside the RAM, which starts at 0x20000000. The nop instructions make sure

p = &dummyVar;

is really executed and the debugger doesn't fool me. #hal #linux #memory #gcc
13 REPLIES 13
Posted on December 28, 2016 at 14:39

So, Clive was right - you compile this as

https://en.wikipedia.org/wiki/Position-independent_code

, as witnessed by 'build_output.txt':

/usr/local/gcc-arm-none-eabi-6_2-2016q4/bin/arm-none-eabi-gcc -g -O0 -Wall -Wextra -pipe -fvisibility=default -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -std=c99 -ffunction-sections -g3 -fmessage-length=0 -Wall -c -v

-fPIC

-DUSE_HAL_DRIVER -DSTM32L476xx '-D__weak=__attribute__((weak))' '-D__packed=__attribute__((__packed__))' -DDEBUG=1 -I/home/picard/prog/stm32/L4_blinky/Inc -I/home/picard/prog/stm32/L4_blinky/Drivers/CMSIS/Include -I/home/picard/prog/stm32/L4_blinky/Drivers/CMSIS/Device/ST/STM32L4xx/Include -I/home/picard/prog/stm32/L4_blinky/Drivers/STM32L4xx_HAL_Driver/Inc -c /home/picard/prog/stm32/L4_blinky/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c -o /home/picard/prog/stm32/build-L4_blinky-STM32_gcc_6_2-Debug/qtc_STM32_gc_6615075b-debug/L4-blinky.qtc-STM32-gc-6615075b.dd71dff9/.obj/b730380c1c8c37a8/stm32l4xx_hal_pwr.c.o

I'm in no way expert in this but this is some interpretation of what we see:

  • in .map, we see a .got section (for Global Offset Table holding addresses of the global variables)

    .got 0x0000000020000010 0x14 load address 0x0000000008001aa0

    though it is probably not loaded upon startup, i.e. as it is in RAM it contains some garbage
  • lines 19-21 you mention above which are supposed to execute the

     p = &stupidVar

    ; C statement do this:
    • r3 already holds pointer to .got section (calculated relatively to pc at the beginning of the routine - this again is a PIC feature, and in STM32 may/will fail if FLASH is run from the relocated position at 0x00000000)
    • load stupidVar's offset (0x10) in .got into r2
    • add r2 and r3 and use the result as address from which load value into r2 (i.e. load the address from .got, but that's garbage as said above)
    • store the result into stack frame offset 4 (i.e. into local variable p )
  • the problem upon startup you mention is that as .got contains garbage, that garbage is used as address of SystemCoreClock global variable, and accessing that address resulted in some of the faults (as you have no explicit fault handler, it ended up in the default fault handler)

As you are not aware of the -fPIC nor is it in files which appear to hold the user settings, it probably had been kindly supplied by the IDE you are using.

JW

Steven Valsesia
Associate II
Posted on December 29, 2016 at 10:34

Where do you disable the watchdog ?

Posted on December 30, 2016 at 23:35

Bravo Jan !

You pointed at the right thing ! Indeed, the IDE was doing things in my back. Anyways, once you know where to look, it suddenly gets much easier to find the

https://forum.qt.io/topic/64276/qbs-how-to-remove-fpic-option

. Now, I can see that bloody LED blink !

Thank you so much,

David.

Posted on December 30, 2016 at 23:40

I have no idea. Your question is off topic.