Skip to content

STM: Incorrect us ticker timestamp after deep sleep #8117

Closed
@fkjagodzinski

Description

@fkjagodzinski

Description

After waking up from deep sleep mode the ticker_read_us(us_ticker) returns incorrect value. In fact that value is 2**32-1 or 2*33-1 bigger than expected.

Targets

NUCLEO_F746ZG,
NUCLEO_L476RG,
NUCLEO_F401RE,
NUCLEO_F429ZI,
DISCO_L475VG_IOT01A

Toolchains

GCC_ARM
I don't suspect this issue to be toolchain related. I'll update this list whenever I check other toolchain builds.

Mbed OS SHA

8d9e2e5 -- master

Code

#include "mbed.h"

#define NO_OS_INTERFERENCE 0

#if NO_OS_INTERFERENCE
#include "mbed_lp_ticker_wrapper.h"
#endif

#define SLEEP_DURATION_US 100000ULL
#define SERIAL_FLUSH_TIME_MS 20

void wakeup_callback(volatile int *wakeup_flag)
{
    (*wakeup_flag)++;
}

int main()
{
#if NO_OS_INTERFERENCE
    // Suspend the RTOS kernel scheduler to prevent interference with duration of sleep.
    osKernelSuspend();
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
    // Suspend the low power ticker wrapper to prevent interference with deep sleep lock.
    lp_ticker_wrapper_suspend();
#endif
#endif

    volatile int wakeup_flag;
    LowPowerTimeout lp_timeout;
    const ticker_data_t *const us_ticker = get_us_ticker_data();
    const ticker_data_t *const lp_ticker = get_lp_ticker_data();

    uint32_t us_ticks1, us_ticks2, lp_ticks1, lp_ticks2;
    us_timestamp_t us_ts1, lp_ts1, us_ts2, lp_ts2;

    MBED_ASSERT(sleep_manager_can_deep_sleep());

    // Wait for hardware serial buffers to flush.
    wait_ms(SERIAL_FLUSH_TIME_MS);

    wakeup_flag = 0;
    lp_timeout.attach_us(mbed::callback(wakeup_callback, &wakeup_flag), SLEEP_DURATION_US);

    us_ticks1 = us_ticker_read();
    lp_ticks1 = lp_ticker_read();
    us_ts1 = ticker_read_us(us_ticker);
    lp_ts1 = ticker_read_us(lp_ticker);

    // Deep sleep unlocked -- deep sleep mode used:
    // * us_ticker powered OFF,
    // * lp_ticker powered ON.
    while (wakeup_flag == 0) {
        sleep_manager_sleep_auto();
    }

    us_ticks2 = us_ticker_read();
    lp_ticks2 = lp_ticker_read();
    us_ts2 = ticker_read_us(us_ticker);
    lp_ts2 = ticker_read_us(lp_ticker);

    printf("------------------------------------------------------------\r\n");
    printf("%24s%12s%12s%12s\r\n", "us_ticks", "us_ts", "lp_ticks", "lp_ts");
    printf("%12s%12lu%12llu%12lu%12llu\r\n", "before", us_ticks1, us_ts1, lp_ticks1, lp_ts1);
    printf("%12s%12lu%12llu%12lu%12llu\r\n", "after", us_ticks2, us_ts2, lp_ticks2, lp_ts2);
    printf("%12s%12lu%12llu%12lu%12llu\r\n", "diff", us_ticks2 - us_ticks1, us_ts2 - us_ts1, lp_ticks2 - lp_ticks1,
           lp_ts2 - lp_ts1);
    printf("\r\n");
    wait_ms(SERIAL_FLUSH_TIME_MS);
}

Output

NUCLEO_F746ZG:

------------------------------------------------------------
                us_ticks       us_ts    lp_ticks       lp_ts
      before       20288       20258         655       19989
       after       20304  4294987570        3964      121002
        diff          16  4294967312        3309      101013

NUCLEO_L476RG:

------------------------------------------------------------
                us_ticks       us_ts    lp_ticks       lp_ts
      before       20387       20313         656       20050
       after       20435  8589954954        4031      123016
        diff          48  8589934641        3375      102966

NUCLEO_F429ZI:

------------------------------------------------------------
                us_ticks       us_ts    lp_ticks       lp_ts
      before       20208       20176       98467       20019
       after       20277  4294987541       99297      121337
        diff          69  4294967365         830      101318

Issue request type

[ ] Question
[ ] Enhancement
[X] Bug

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions