SLEEP Mode with turned off RAM

Thread Starter

Xenon02

Joined Feb 24, 2021
504
I've got a problem understanding how "SLEEP Mode with turned off RAM" works ...

There is an STM32 that my friend used in his project (still don't have it physically the board), and it had this option to use SLEEP mode without RAM.
I know how Sleep mode works. When wake up MCU from SLEEP Mode it starts from the place it entered the SLEEP mode.

But with turned of RAM will it not be cleared ? Hence The whole program is restarted ? And the program will start from including first "main.h" till the main function ? As if it restarted the MCU. These are my thoughts. Because with cleared RAM all variables, context of a program etc are cleared.

Used STM : STM32L031G6.
This is what is says in Datasheet :

1730586375293.png
1730586389061.png

STANDBY Mode also when it is woken up it restarts the whole program. So I am confused to be honest when I see similarities in both modes that I can't see the difference.

Can somebody help me explain how it works ?
I would like to test it but I don't have the board + I don't know how to turn this function in HAL functions (if it's possible).

Also the website says something about STOP mode also with Disabled RAM :

1730586546302.png

Stop mode can be waken up with EXTI, or any other that causes Callback. But how can it does a Callback when RAM is cleared ? It will see a variable that has some trash data, and what it will do when it executes the callback ? Where it will go back if the context if wiped because RAM is cleared ???

I am so confused ...
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
Program code is in Flash memory, not RAM. Data is lost if RAM power is turned off.
It didn't answer all my questions though. It's to short for an answer for my big question :(
The context when there is any interrupt is in RAM (in stack).

For example simple code :


Sleep code:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    str = "WakeUP from SLEEP by EXTI\r\n";
    HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen (str), HAL_MAX_DELAY);
    HAL_PWR_DisableSleepOnExit ();
}


int main ()
{
......
......
while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

      int x = 5;
      int y = 10;

      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1);
      HAL_Delay(5000);

      HAL_SuspendTick();

      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0);  // Just to indicate that the sleep mode is activated

      HAL_PWR_EnableSleepOnExit ();

//      Enter Sleep Mode , wake up is done once User push-button is pressed
      HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);

//    What happens here, after we wake up SLEEP ? X and Y are cleared. And the context ?
       int s = x + y;

      HAL_ResumeTick();

      for (int i=0; i<20; i++)
      {
          HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
          HAL_Delay(100);
      }

  }
}
So when RAM is disabled while in SLEEP mode then what happens when we wake up using EXTI ?
Also I want to know what RAM holds. ROM/FLASH hold the instructions what to do as I understand. But what happens when it wakes up and RAM is cleared ? It encounters an variable that was cleared then what ?

That's why I am mixed. If it's to complex what I've said because of my choice of words I'll try using points

- What happens when we wake up the MCU from SLEEP Mode with RAM disabled (it was cleared).
- What happens when we wake up the MCU from STOP Mode with RAM disabled (it was cleared).
- What happens in the Callback if RAM is cleared and in the Callback is variable, or when it exits the callback. Context is stored in RAM.
- Then if STANDBY clears the RAM and it still can do the function from where it stopped then why it Restarts ?
- How RAM and ROM works when it comes to executing next lines of code ?
- Why the code can still go on even though the RAM was cleared ? It still holds the variables value, stack (which was the code context when it interrupts, maybe even when it goes sleep mode).

ROM holds the instructions, MCU takes the next instructions and executes it, while RAM holds are data (variables, stack).
But I still don't get it.

Thank you and sorry for complicating it if I am a nuisance.
 
Last edited:

MrChips

Joined Oct 2, 2009
34,790
Don't go into SLEEP mode while in ISR.
Enter in SLEEP mode only from main function.
Also, why do you need to disable RAM ever?
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
Don't go into SLEEP mode while in ISR.
Enter in SLEEP mode only from main function.
Also, why do you need to disable RAM ever?
Disabling RAM is just a curiosity, if there is an option then I want to know how it works.
I also appreciate your answers but they are to short and do not answer all my questions ;(

It doesn't help me understand what happens. I am reading how what happens for like 8h and I am more and more puzzled. How RAM and ROM works to be honest and how it works with the actual code.

As far as I understood :
-RAM : contains data like variables, function call, stack
-ROM: Functions code, overall code

-Resetting RAM will cause lost information about all Variables, stack etc.

-The program can still go on (when RAM was restarted) because it has register that reads next ROM instructions ?

- But all variables are cleared, there are no defined variables. (explain my code example what happens).

-Why then STANDBY must restart even though it also turns off RAM.

When RAM is cleared then I don't know what happens.
I feel like I don't know why I write that much text to explain what I don't understand with those answers ;(
 
Last edited:

MrChips

Joined Oct 2, 2009
34,790
I know this doesn’t answer your question but normally my STM32 systems have RTC and RAM supplied by a coin cell when in power down mode.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
I know this doesn’t answer your question but normally my STM32 systems have RTC and RAM supplied by a coin cell when in power down mode.
Okey, so that's why I asked what happens ... I give as much info as I can and now I don't know if other people will read what I wrote and somehow help me. I don't want to be mean I just am exhausted of finding answer because there is none. And I want to know it.

I've provided the picture that this option exist and I want to understand what happens.
It saves more current consumption.
1730598245462.png
0.38uA with RAM and RTC turned off.

And If RAM is cleared then starting from where SLEEP mode was called doesn't make sense ...
Because all Variables are cleared, stack is cleared ! So how it will function like here :

Code:
      int x = 5;
      int y = 10;

      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1);
      HAL_Delay(5000);

      HAL_SuspendTick();

      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0);  // Just to indicate that the sleep mode is activated

      HAL_PWR_EnableSleepOnExit ();

//      Enter Sleep Mode , wake up is done once User push-button is pressed
      HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);

//    What happens here, after we wake up SLEEP ? X and Y are cleared. And the context ?
       int s = x + y;
int s is called after we exit SLEEP Mode but x and y are cleared ... and all other variables as well. What is the sense of not resetting it then ?

PS. I've edited my last post I will add it again here I want confirmation if I am correct because ROM and RAM stuff are puzzeling:

As far as I understood :
-RAM : contains data like variables, function call, stack
-ROM: Functions code, overall code

-Resetting RAM will cause lost information about all Variables, stack etc.

-The program can still go on (when RAM was restarted) because it has register that reads next ROM instructions ?

- But all variables are cleared, there are no defined variables. (explain my code example what happens).

-Why then STANDBY must restart even though it also turns off RAM.

- What if we Wake up SLEEP Mode with EXTI that has callback ... RAM is cleared and Sleep wakes up and must execute the Callback. First of all how it will go back when RAM is cleared ? How it will use variables or structs that are cleared ...
 
Top