Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Questions on C programming for STM32

hm_fa_da

Full Member level 5
Joined
Sep 16, 2003
Messages
287
Helped
10
Reputation
20
Reaction score
4
Trophy points
1,298
Activity points
3,217
Hello,

I have a question on C programming for STM32 by Keil compiler:

If I define a variable on top of my code, (before main() ) like:

/* USER CODE BEGIN PV */

uint8_t Var_x = 5;

/* USER CODE END PV */

int main(void)
{
program code ....

HAL_NVIC_SystemReset(); // **Software reset code
}

1- when the circuit is powered up and hardware reset, the Var_x will get value 5, right ?
2- How about if the code gets to the software reset function in main() ? would it set the Var_x value to 5 after software reset ?

or maybe I can ask the question in this way, is the below code functionality exactly same with the above one :

/* USER CODE BEGIN PV */

uint8_t Var_x;

/* USER CODE END PV */

int main(void)
{
Var_x = 5;

program code ....

HAL_NVIC_SystemReset(); // **Software reset code
}


In fact I am using the "HAL_NVIC_SystemReset();" in:

void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */

HAL_NVIC_SystemReset();

/* USER CODE END Error_Handler_Debug */
}

and:

void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */

HAL_NVIC_SystemReset();

/* USER CODE END HardFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */

/* USER CODE END W1_HardFault_IRQn 0 */
}
}


I'm not sure if in these cases, the Var_x gets value 5 after software reset, I can't simulate this status (Hardfault or error handler) to check it ... I guess maybe hardfault condition makes it impossible to access memory after reset to set the Var_x to value 5 ... ?!


Thank you
 
Last edited:
I'm not sure about the Keil runtime, but in general the hardware resets call some runtime initialisation code that then calls the 'main' function. The purpose of that initialisation code is to set up the stack, heap, initialise global variables etc.. (Of course local variables etc. are a different story.)
I would be very surprised if any reset somehow managed to bypass that initialisation code and call the 'main' function directly. (To be honest, I have been surprised in the past!!)
Many compilers to have a special way to allow variables to retain their values across resets but you have to actively tell the compiler to do this.
Susan
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top