I'm developing an embedded application on stm8s
microcontroller using STVD
IDE and COSMIC
compiler.
I'm trying to define a static bool
variable to use it to execute a snippet of code only once. Unexpectedly, the variable once
is not initialized to FALSE
or 0
. Although I explicitly initialized it. Here is the snippet of my code:
uint32_t crc32_buffer(void)
{
static bool once = FALSE;
uint32_t crc = 0;
if(!once)
{
calcTable();
crc = 10;
once = TRUE;
}
return crc;
}
When I tried to check the physical memory location, I found that every time after entering a new debugging session (even after hardware restarting the controller) and before running the application itself, the memory address 0x80
has the same value 0x14
.
If I modified the code to assign FALSE
to once
after the variable's initialization:
once = FALSE;
The memory location is changed to contain 0x00
. Then if I exit this debugging session and then re-modified the code to delete this line of code and start a new debugging session, I find the memory location 0x80
has again 0x14
before running the application.
I don't understand what can prevent the compiler to initialize the variable to 0
. I don't understand what can write 0x14
to the memory location even before running the application.
I tried setting a breakpoint if the memory location 0x80
was accessed (read/write) but the application didn't stop until it reached the if
statement in the code snippet.
UPDATE-2
As many pointed out the startup procedure, I don’t use the default startup code. However, I'm using a custom one.
When I used the standard startup code instead of the custom one I was using, the memory locations were set to 0
before main()
function start execution. This is not the case with the custom startup code.
So, when I define a new static
variable and explicitly initialize it to FALSE
, this initialization will only take place in the startup code before main()
, right?