Timer not work after wake up from stop mode Topic is solved

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

leon_titan
Posts: 24
Joined: Sun Jan 12, 2020 2:11 pm
Has thanked: 4 times

Timer not work after wake up from stop mode

Postby leon_titan » Sun Nov 13, 2022 9:09 am

Hi,

I am working on STM32L433 board with stop2 mode. And when the board wake up from stop2 mode, the timer not work at all.

My code:

Code: Select all

static virtual_timer_t timer_vt;

static uint32_t RCC_AHB1ENR_bk;
static uint32_t RCC_AHB2ENR_bk;
static uint32_t RCC_AHB3ENR_bk;

static uint32_t RCC_APB1ENR1_bk;
static uint32_t RCC_APB1ENR2_bk;
static uint32_t RCC_APB2ENR_bk;

static uint32_t RCC_AHB1SMENR_bk;
static uint32_t RCC_AHB2SMENR_bk;
static uint32_t RCC_AHB3SMENR_bk;

static uint32_t RCC_APB1SMENR1_bk;
static uint32_t RCC_APB1SMENR2_bk;
static uint32_t RCC_APB2SMENR_bk;

static uint32_t ADC1_CR_bk;
static uint32_t USART1_CR1_bk;
static uint32_t USART2_CR1_bk;
static uint32_t USART3_CR1_bk;
static uint32_t OPAMP_CSR_bk;

static void lp_backup(void) {
      /* backup clock & periph settings */
  RCC_AHB1ENR_bk = RCC->AHB1ENR;
  RCC_AHB2ENR_bk = RCC->AHB2ENR;
  RCC_AHB3ENR_bk = RCC->AHB3ENR;

  RCC_APB1ENR1_bk = RCC->APB1ENR1;
  RCC_APB1ENR2_bk = RCC->APB1ENR2;
  RCC_APB2ENR_bk = RCC->APB2ENR;

  RCC_AHB1SMENR_bk = RCC->AHB1SMENR;
  RCC_AHB2SMENR_bk = RCC->AHB2SMENR;
  RCC_AHB3SMENR_bk = RCC->AHB3SMENR;

  RCC_APB1SMENR1_bk = RCC->APB1SMENR1;
  RCC_APB1SMENR2_bk = RCC->APB1SMENR2;
  RCC_APB2SMENR_bk = RCC->APB2SMENR;

  ADC1_CR_bk = ADC1->CR;
  USART1_CR1_bk = USART1->CR1;
  USART2_CR1_bk = USART2->CR1;
  USART3_CR1_bk = USART3->CR1;
  OPAMP_CSR_bk = OPAMP->CSR;
}

static void lp_restore(void) {
    RCC->AHB1ENR = RCC_AHB1ENR_bk;
    RCC->AHB2ENR = RCC_AHB2ENR_bk;
    RCC->AHB3ENR = RCC_AHB3ENR_bk;

    RCC->APB1ENR1 = RCC_APB1ENR1_bk;
    RCC->APB1ENR2 = RCC_APB1ENR2_bk;
    RCC->APB2ENR = RCC_APB2ENR_bk;

    RCC->AHB1SMENR = RCC_AHB1SMENR_bk;
    RCC->AHB2SMENR = RCC_AHB2SMENR_bk;
    RCC->AHB3SMENR = RCC_AHB3SMENR_bk;

    RCC->APB1SMENR1 = RCC_APB1SMENR1_bk;
    RCC->APB1SMENR2 = RCC_APB1SMENR2_bk;
    RCC->APB2SMENR = RCC_APB2SMENR_bk;

    ADC1->CR = ADC1_CR_bk;
    USART1->CR1 = USART1_CR1_bk;
    USART2->CR1 = USART2_CR1_bk;
    USART3->CR1 = USART3_CR1_bk;
    OPAMP->CSR = OPAMP_CSR_bk;
}

void enter_stop(void) {
    lp_backup();

    init_stop_gpio();

    DBGMCU->CR = 0;

    SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;

  /* Set Stop mode 2 */
  MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STOP2);

  /* Set SLEEPDEEP bit of Cortex System Control Register */
  SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));

    __WFI();

  /* Reset SLEEPDEEP bit of Cortex System Control Register */
  CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));

    __disable_irq();
    stm32_clock_init();
    SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
    __enable_irq();

    lp_restore();
    palInit();
   
    //reset the timer
    sleep_timer_reset();
}

static void sleep_timer_cb(virtual_timer_t *vtp, void *arg) {
    (void)vtp;
    (void)arg;

    enter_stop();
}

void sleep_timer_reset(void) {
    chVTSet(&timer_vt, TIME_MS2I(SLEEP_TIME), sleep_timer_cb, NULL);
}



The sleep_timer_reset() is called when startup and after wake up. After first stop and wake up, the timer not work any more.
How to solve this please? I have read this post(viewtopic.php?t=3381) and have no idea.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Timer not work after wake up from stop mode

Postby Giovanni » Sun Nov 13, 2022 9:17 am

Hi,

You mean the virtual timer? it is possible that VT system time has loss sync with the HW timer counter. Are you using tickless mode?

Giovanni

leon_titan
Posts: 24
Joined: Sun Jan 12, 2020 2:11 pm
Has thanked: 4 times

Re: Timer not work after wake up from stop mode

Postby leon_titan » Sun Nov 13, 2022 9:27 am

Giovanni wrote:Hi,

You mean the virtual timer? it is possible that VT system time has loss sync with the HW timer counter. Are you using tickless mode?

Giovanni

Yes the virtual timer. Dont know is tickless mode or not, how to check that? :)

Edit:
It should be the default mode since I haven't change the mode in my code.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Timer not work after wake up from stop mode  Topic is solved

Postby Giovanni » Sun Nov 13, 2022 9:50 am

Hi,

It is the CH_CFG_ST_TIMEDELTA parameter in chconf.h, put it to zero to disable tickless mode, also put CH_CFG_ST_FREQUENCY to 1000.

See if this makes any difference.

Giovanni

leon_titan
Posts: 24
Joined: Sun Jan 12, 2020 2:11 pm
Has thanked: 4 times

Re: Timer not work after wake up from stop mode

Postby leon_titan » Sun Nov 13, 2022 9:58 am

Giovanni wrote:Hi,

It is the CH_CFG_ST_TIMEDELTA parameter in chconf.h, put it to zero to disable tickless mode, also put CH_CFG_ST_FREQUENCY to 1000.

See if this makes any difference.

Giovanni

Great, it works after change the config you said. And what should I do now? Leave it this way or?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Timer not work after wake up from stop mode

Postby Giovanni » Sun Nov 13, 2022 10:03 am

Hi,

There are 2 possible causes for tickless mode to not work:

1) TIM2 is not restarted after exiting STOP2, this timer is used by virtual timers in tickless mode.
2) VTs are out of sync with TIM2 for some reason, this could be hard to address.

Said that, there is nothing wrong with classic mode, you just get a periodic interrupt from systick.

Giovanni

leon_titan
Posts: 24
Joined: Sun Jan 12, 2020 2:11 pm
Has thanked: 4 times

Re: Timer not work after wake up from stop mode

Postby leon_titan » Sun Nov 13, 2022 10:08 am

Giovanni wrote:Hi,

There are 2 possible causes for tickless mode to not work:

1) TIM2 is not restarted after exiting STOP2, this timer is used by virtual timers in tickless mode.
2) VTs are out of sync with TIM2 for some reason, this could be hard to address.

Said that, there is nothing wrong with classic mode, you just get a periodic interrupt from systick.

Giovanni

Alright, I will check the TIM2 status first.

BTW, whats the Cons of classic mode compare to the tickless mode?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Timer not work after wake up from stop mode

Postby Giovanni » Sun Nov 13, 2022 10:53 am

leon_titan wrote:
Giovanni wrote:Hi,

There are 2 possible causes for tickless mode to not work:

1) TIM2 is not restarted after exiting STOP2, this timer is used by virtual timers in tickless mode.
2) VTs are out of sync with TIM2 for some reason, this could be hard to address.

Said that, there is nothing wrong with classic mode, you just get a periodic interrupt from systick.

Giovanni

Alright, I will check the TIM2 status first.

BTW, whats the Cons of classic mode compare to the tickless mode?


Getting tick interrupts.

Giovanni

leon_titan
Posts: 24
Joined: Sun Jan 12, 2020 2:11 pm
Has thanked: 4 times

Re: Timer not work after wake up from stop mode

Postby leon_titan » Sun Nov 13, 2022 11:52 am

Alright, thanks man, you saved my day.


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 31 guests