Global variables are not initialized after program start

1.2k Views Asked by At

I am programming STM32L152 that connected to my PC via UART.

I use GNU Tools ARM Embedded version 6.2 2016q4.

Compiler control string:

-mcpu=cortex-m3; -mthumb; -Wall; -ffunction-sections; -g; -O0; -DSTM32L152RB; -DSTM32L1XX_MD; -I.; 

Linker control string

-mcpu=cortex-m3; -mthumb; -g; -Map=mufs_stm32l152.map; -O0; --gc-sections; -L${linkdir}; -T${linkdir}/arm-gcc-link.ld;  

In fact, it default value for Coocox IDE, I don't change it.

My code:

int main(void)
{
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //PD6  -> RX UART.
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8| GPIO_Pin_7| GPIO_Pin_6;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    //enable pin
    GPIO_SetBits(GPIOB, GPIO_Pin_9);

    RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
    USART1->CR1 |= USART_CR1_UE;
    USART1->CR1 &= ~USART_CR1_M;
    USART1->CR2 &= ~USART_CR2_STOP; 
    USART1->BRR = 139; //baud rate 115200 HSI = 16 МГц
    USART1->CR1 |= USART_CR1_TE;
    USART1->CR1 |= USART_CR1_RE;

    USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);
    NVIC_EnableIRQ(USART1_IRQn);

    while(1)
    {}

}

char hh = 'G';
void USART1_IRQHandler(void)
{
    if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE))
    {
        char ch = USART_ReceiveData(USART1);
        USART_SendData(USART1, hh);
        if(ch == 'A')
            GPIO_ToggleBits(GPIOB, GPIO_Pin_8);

    }
}

When I pressing keys on my PC I expect to see in console something like this

GGGGGGGGGGGG

But instead I getting just some garbage. Sometimes it some letter sometimes it is unprintable character. And it not changing until I edit my code and recompile it. I can assign value and it work. It is look like global variable hh has no her initial value. Furthermore, if I mark it as constant it start working as expected.

What am I doing wrong?

1

There are 1 best solutions below

4
Konstantin T. On

In CooCox IDE (v1.7.6) initialization of global variables (data segment in fact) perform in startup_stmxxxxxx_md.c file in Default_Reset_Handler function. And I don't know what the point of this, but in my case code which perform data segment initialization as well as bss zero-filling, was commented out by default. For properly variables initialization you should uncomment this code.