cancel
Showing results for 
Search instead for 
Did you mean: 

Wake from standby issues

Clonimus74
Senior II
Posted on March 20, 2016 at 20:28

Hi all,

I have a few problems with waking from Standby. I use STM32L152, IAR V7.5, the MCU has constant power supplied to it. I have a custom bootloader. If my MCU has only the bootloader I don't seem to have any problems. My aim is to use Standby as power off mode. Here is my power on code in the bootloader:


__root static void PowerUp(void)

{

const uint8_t kPOWER_OFF_BUTTON_PRESSING_TIME = 100;


static uint8_t power_on_sequence = 1;

static uint16_t button_timming = 0;


GpioInit();

LedsTimerInit();


__enable_interrupt();


while (power_on_sequence)

{

if (sys_tick_10ms)

{

sys_tick_10ms = 0;


if (PowerButtonPressed() || ((bootloader_reached_from_application) && (PWR_GetFlagStatus(PWR_FLAG_SB) == RESET)))

{

button_timming++;


if (button_timming >= kPOWER_OFF_BUTTON_PRESSING_TIME)

{

button_timming = 0;


Spi2Init();

InitOled();

current_state_function = &SystemInitialization;


while(PowerButtonPressed());


OledClearScreen();


power_on_sequence = 0;

}

}

else

{

button_timming = 0;

/* Enter standby */

__disable_interrupt();

RCC_LSICmd(DISABLE);

PWR_UltraLowPowerCmd(ENABLE);

PWR_WakeUpPinCmd(PWR_WakeUpPin_1, DISABLE);

PWR_WakeUpPinCmd(PWR_WakeUpPin_2, DISABLE);

PWR_WakeUpPinCmd(PWR_WakeUpPin_3, DISABLE);

PWR_ClearFlag(PWR_FLAG_WU);

PWR_ClearFlag(PWR_FLAG_SB);

PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE);

PWR_EnterSTANDBYMode();

}

}

}

}

The idea is not to turn on (stay in standby) if the power on button (PA0-WKUP) is not pressed for over 1sec. Once I have an application programmed to the MCU (the bootloader detects there's an application and jumps to it) the issues begin- Weird issue #1: bootloader_reached_from_application is located at a known area in RAM and used to indicate (if not 0) that we jumped to the bootloader from the application (no need to wait for 1 sec button press). What happened was that after waking from Standby (using a short >sec button press) the system just turned on and jumped to the application even though the variable was not set (the application didn't set it before entering Standby and it is 0 after reset), well it seems the RAM value after reset is unknown, so I had to add a check that I came from standby to overcome this. can't figure out why. here is the declaration of the variable (same in bootloader and application):

#pragma location=0x20000000
__no_init static uint8_t bootloader_reached_from_application;

It is reset at the beginning of the application, only set if erase command from PC bootloader arrives. Weird issue #2: If I power off the device in the application (go to standby), pressing the button (PA0) for 1sec will turn on the device (the MCU will reset as it exits the Standby mode and boot to the bootloader), but it works randomly, i.e. it might work (on-off cycles) a few times and suddenly gets stuck, the device is in unknown state. I tried toggling LED and I know that right after the application goes to Standby, pressing the button doesn't bring me to main() in the bootloader. Here is the power off sequence in the application (at first line #19 wasn't there, it was added in order to try entering standby from the bootloader [code above] cleanly, didn't work):


void PowerOffSystem(void)

{

/* 200ms vibration to indicate power off */

haptic_feedbak_parameters.duration_in_10ms = 0x20;

haptic_feedbak_parameters.enable_vibration = 1;


DisableAnalogVoltage();

TurnBlueLedOff();

TurnRedLedOff();

UpdateLoggerControlParametersInEeprom();

oled_parameters.screen_refresh_disabled = 1;

OledClearScreen();

ResetBle();


while(PowerButtonPressed()) //Wait for button release so we will not get to endless power on-off cycle

IWDG_ReloadCounter();


while (haptic_feedbak_parameters.busy);

NVIC_SystemReset();


DisableAllPeripherals();


/* Enter standby */

RCC_LSICmd(DISABLE);

PWR_UltraLowPowerCmd(ENABLE);

PWR_WakeUpPinCmd(PWR_WakeUpPin_1, DISABLE);

PWR_WakeUpPinCmd(PWR_WakeUpPin_2, DISABLE);

PWR_WakeUpPinCmd(PWR_WakeUpPin_3, DISABLE);

PWR_ClearFlag(PWR_FLAG_WU);

PWR_ClearFlag(PWR_FLAG_SB);

PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE);

PWR_EnterSTANDBYMode();

}

Here is the main function in the bootloader, when the system gets stuck (pressing the button after entering Standby) it doesn't seem I get to line #5:


int main(void)

{

__disable_interrupt();


Set_System();


/* Infinite loop */

while (1)

{

current_state_function = &PowerUp;


soft_reset = 1;


while (soft_reset)

current_state_function();

}

}

Thank you in advance for your help #standby #bootloader #stm32l15x
3 REPLIES 3
Clonimus74
Senior II
Posted on March 22, 2016 at 09:57

I solved the problem!

It seems it is a compiler issue. All I had to do is declare '' haptic_feedbak_parameters'' as volatile

.

I have no idea why it was a problem (I didn't bother to go through the disassembly), haptic_feedbak_parameters.busy does change in interrupt but even if its value would not have been correct I would either stay in the while loop or immediately advance to the Standby mode (prior to the haptic feedback to finish). Here is the affected code segment (taken from the OP), now it actually works without line #19 so I can enter Standby from the application and not reset to Bootloader in order to enter Standby from there.

 while (haptic_feedbak_parameters.busy);
 NVIC_SystemReset();
 
 DisableAllPeripherals();
 
 /* Enter standby */
 RCC_LSICmd(DISABLE);
 PWR_UltraLowPowerCmd(ENABLE);
 PWR_WakeUpPinCmd(PWR_WakeUpPin_1, DISABLE);
 PWR_WakeUpPinCmd(PWR_WakeUpPin_2, DISABLE);
 PWR_WakeUpPinCmd(PWR_WakeUpPin_3, DISABLE);
 PWR_ClearFlag(PWR_FLAG_WU);
 PWR_ClearFlag(PWR_FLAG_SB);
 PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE);
 PWR_EnterSTANDBYMode();

I use IAR Embedded Workbench for ARM 7.502 and I find a lot of optimization bugs using it.
Nesrine M_O
Lead II
Posted on March 22, 2016 at 10:43

Hi Clonimus74,

Thank you for posting your findings and how you fixed the issue, it is good to hear that it was solved. 

-Syrine-

Clonimus74
Senior II
Posted on March 22, 2016 at 11:42

Syrine,

Sure.

As I wrote I have many optimization problems with the newest IAR (I use the newest since I need it for the new STM32L4 family). Are you, in ST, aware of these problems? are you in contact with IAR about it? It became a pain to use it, but I don't like the other tools (Keil etc.)