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

Problem on debugger with extern variable

Hello everybody,
I have a problem with a global variable. This variable is declared as extern.
It is reset to 0 automatically inside the Keil µVisionb 4 debugger when used in a function written in different C file.
I make deeper reference.

I declared variable in a header like this

#ifdef __VAR_G
        #define __DEF
#else
        #define __DEF extern
#endif

__DEF uint32_t SystemCoreClock;
__DEF   uint32_t Init_OK;

I use this variable twice :
Firstly in a.c file

#define __VAR_G
#include "stm32f4xxiut.h"
void SystemInit()
{
    Init_OK = Init_OK | HSE_FAIL;
    SystemCoreClock=16000000;
}

Secondly in b.c file

#include "stm32f4xxiut.h"
void main()
{
    int b=0;
    if (Init_OK==1)
        b=2;
}

My problem is the following.
In the keil debugger, with STM32F4XX, first, system init is executed. The both variable are declared as global and a value is assigned to them. Then, the core branch to main and the both variable are reset to zero in spite of the extern keyword used inside the definition.

Do you know why there is this strange behaviour.

Thank you.

Parents
  • I found a solution by making these variables in noinit zone with zero_init option in declaration.
    This avoid resetting selected variable at the entry inside main function.

    This is absurd!

    Here is reset handler of my LPC1788 bootloader:

    Reset_Handler      PROC
                                    EXPORT  Reset_Handler             [WEAK]
                                    IMPORT  SystemInit
                                    IMPORT  __main
    
                    IF      :DEF:MBOOT
                    IMPORT  sdram_init
                    ENDIF
    
                                    LDR     R0, =SystemInit
                                    BLX     R0
    
                    IF      :DEF:MBOOT
                    ; init SDRAM before scatter loading starts...
                    LDR     R0, =sdram_init
                    BLX     R0
                    ENDIF
    
                                    LDR     R0, =__main
                                    BX      R0
                                    ENDP
    

    SystemInit call call _BEFORE_ scatter loading (__main...)!
    There is no justification for making that variable non-zero init.
    I suggest to you that you actually listen to us, instead of going into a tail spin...

Reply
  • I found a solution by making these variables in noinit zone with zero_init option in declaration.
    This avoid resetting selected variable at the entry inside main function.

    This is absurd!

    Here is reset handler of my LPC1788 bootloader:

    Reset_Handler      PROC
                                    EXPORT  Reset_Handler             [WEAK]
                                    IMPORT  SystemInit
                                    IMPORT  __main
    
                    IF      :DEF:MBOOT
                    IMPORT  sdram_init
                    ENDIF
    
                                    LDR     R0, =SystemInit
                                    BLX     R0
    
                    IF      :DEF:MBOOT
                    ; init SDRAM before scatter loading starts...
                    LDR     R0, =sdram_init
                    BLX     R0
                    ENDIF
    
                                    LDR     R0, =__main
                                    BX      R0
                                    ENDP
    

    SystemInit call call _BEFORE_ scatter loading (__main...)!
    There is no justification for making that variable non-zero init.
    I suggest to you that you actually listen to us, instead of going into a tail spin...

Children
No data