ARMmbed / mbed-os

Arm Mbed OS is a platform operating system designed for the internet of things
https://mbed.com
Other
4.65k stars 2.97k forks source link

time(NULL) not returning real RTC value after reboot #15487

Closed lefebvresam closed 7 months ago

lefebvresam commented 7 months ago

Description of defect

The following code starts counting at 12:00:00 AM after each MCU reset and doesn't reflect the real RTC value:

int main()
{
    while(1) {
        time_t seconds = time(NULL);
        char buffer[32];
        strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds));
        printf("%s             \r", buffer);
        int i = 100000;
        while(i--); // keep busy (sleep gives programming issues with STLinkV2 clone and printf cannot flush)
    }
}

After investigation the issue was solved when putting this code snippet in mbed_sdk_init() in mbed_overrides.c of STM target between the #if DEVICE_RTC/#endif lines or calling rtc_init() before the main function:

#if defined __HAL_RCC_RTCAPB_CLK_ENABLE
    __HAL_RCC_RTCAPB_CLK_ENABLE();
#endif /* __HAL_RCC_RTCAPB_CLK_ENABLE */

A test in CubeIDE shows this HAL_RTC_MspInit function:

    /* RTC clock enable */
    __HAL_RCC_RTC_ENABLE();
    __HAL_RCC_RTCAPB_CLK_ENABLE();
    __HAL_RCC_RTCAPB_CLKAM_ENABLE();

Target(s) affected by this defect ?

MCU_STM32U575xG inherited from MCU_STM32U5

custom_targets.json

{
    "MCU_STM32U575xG": {
        "inherits": [
            "MCU_STM32U5"
        ],
        "public": false,
        "mbed_rom_size": "0x100000",
        "mbed_ram_size": "0xc0000",        
        "extra_labels_add": [
            "STM32U575xG"
        ],
        "macros_add": [
            "STM32U575xx"
        ]
    },
    "HMC20": {
        "inherits": [
            "MCU_STM32U575xG"
        ],
        "overrides": {
            "clock_source" : "USE_PLL_HSE_XTAL",
            "lse_available" : "1",
            "lse_drive_load_level" : "RCC_LSEDRIVE_HIGH"
        },
        "macros_add": [
            "HSE_VALUE=10000000UL"
        ],
        "device_name": "HMC20"
    }
}

Toolchain(s) (name and version) displaying this defect ?

NA

What version of Mbed-os are you using (tag or sha) ?

Mbed 7.59.0

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

CLI2

How is this defect reproduced ?

Create a test project

lefebvresam commented 7 months ago

Handled in https://github.com/mbed-ce/mbed-os/pull/210