为什么RTC不能使用Mbed OS 2在STM32L0上运行?

问题描述 投票:1回答:1

我正在开发一个集成了STM32L051R8T6芯片组的项目,我需要一些功能的RTC功能,比如慢速定时器和睡眠唤醒。但是,如果我调用Mbed的set_time()来设置RTC,程序将挂起或不按预期执行。

在实现任何东西之前,我正在尝试运行Mbed的RTC示例代码:https://os.mbed.com/docs/mbed-os/v5.8/reference/rtc.html,但我没有运气。 RTC似乎是用set_time()设置的,但是,当我调用time(NULL)时,我总是得到初始设置时间。看起来RTC不算数。

我正在使用Mbed的在线编译器编译STM32L053R8的代码,不确定该目标是否与我的非常不同,这就是导致问题的原因。

这是我正在尝试执行的代码:

#include "mbed.h"

int main() {
    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37

    while (true) {
        time_t seconds = time(NULL);

        printf("Time as seconds since January 1, 1970 = %d\n", seconds);

        printf("Time as a basic string = %s", ctime(&seconds));

        char buffer[32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        printf("Time as a custom formatted string = %s", buffer);

        wait(1);
    }
}

当它没有挂起时,RTC时间不会改变:

Terminal output

欢迎任何帮助!

stm32 mbed real-time-clock
1个回答
2
投票

包括rtc_api.h文件的完整路径并在代码开头添加rtc_init()解决了这个问题。 rtc_init()函数负责选择可用的时钟源。工作代码如下:

#include "mbed.h"
#include "mbed/hal/rtc_api.h"

int main() {
    rtc_init();
    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37

    while (true) {
        time_t seconds = time(NULL);

        printf("Time as seconds since January 1, 1970 = %d\n", seconds);

        printf("Time as a basic string = %s", ctime(&seconds));

        char buffer[32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        printf("Time as a custom formatted string = %s", buffer);

        wait(1);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.