使用 Mbed-OS 5 在 STM32F4 上出现间歇但重复的 HAL_RTC_SetDate 错误

Intermittent but repeated HAL_RTC_SetDate error on STM32F4 with Mbed-OS 5

我一直在努力找出我的 PCB/embedded 代码出了什么问题。 PCB是围绕STM32F407VE设计的。 set_time() 每 5/10/20/60/120 秒调用一次。

time_t et = time(NULL);
debug("%d: NTP sync started\r\n", et);
set_time(et);

第一次调用代码后 600 秒,我将看到如下内容:

1587754882: NTP sync started
1587754902: NTP sync started
1587754922: NTP sync started
1587754942: NTP sync started
1587754962: NTP sync started
1587754982: NTP sync started
1587755002: NTP sync started
1587755022: NTP sync started
1587755042: NTP sync started
1587755062: NTP sync started
1587755082: NTP sync started
1587755102: NTP sync started
1587755122: NTP sync started
1587755142: NTP sync started
1587755162: NTP sync started
1587755182: NTP sync started
1587755202: NTP sync started
1587755222: NTP sync started
1587755242: NTP sync started
1587755262: NTP sync started
1587755282: NTP sync started
1587755302: NTP sync started
1587755322: NTP sync started
1587755342: NTP sync started
1587755362: NTP sync started
1587755382: NTP sync started
1587755402: NTP sync started
1587755422: NTP sync started
1587755442: NTP sync started
1587755462: NTP sync started
1587755482: NTP sync started


++ MbedOS Error Info ++
Error Status: 0x80FF0100 Code: 256 Module: 255
Error Message: Fatal Run-time error
Location: 0x8022591
Error Value: 0x0
Current Thread: main  Id: 0x20005B8C Entry: 0x801A273 StackSize: 0x2000 StackMem: 0x2000B4C0 SP: 0x2000D270 
For more info, visit: https://mbed.com/s/error?error=0x80FF0100&tgt=ARCH_MAX
-- MbedOS Error Info --
HAL_RTC_SetDate error

如果间隔为5秒,则第121次调用会触发系统崩溃。如果间隔2分钟,那么第6次调用会触发crash。

其他一些观察: 1) 无论使用 LSI 还是 LSE 作为 RTC 时钟源,都会发生这种情况。 2) 如果我注释掉 set_time() 那么系统就不会崩溃。 3) 以下简单测试代码不会在同一块PCB 上崩溃。但是如果启用注释掉的代码,它会重现问题。

    time_t et;
    printf("%d: system started\r\n", time(NULL));
    uint8_t toResetRTC = 0, toSaveFooIntoBackupRegister = 0;
    uint32_t foo = 12345;
    while (1)
    {
        toResetRTC++;
        if(toResetRTC > 4)
        {
            et = time(NULL);
            printf("%d: RTC reset started\r\n", et);
            set_time(et);
            toResetRTC = 0;
        }
        toSaveFooIntoBackupRegister++;
        if(toSaveFooIntoBackupRegister > 59)
        {
            RTC_HandleTypeDef RtcHandle;
            RtcHandle.Instance = RTC;
            HAL_PWR_EnableBkUpAccess();
            HAL_RTCEx_BKUPWrite(&RtcHandle, 0, foo);
            // This following line messes up HAL_RTC_SetDate() and HAL_RTC_SetTime() when called by set_time();
            // If the following line is enabled, system crashes every 1 minute
            //HAL_PWR_DisableBkUpAccess();
            toSaveFooIntoBackupRegister = 0;
        }
        ThisThread::sleep_for(1000);
    }

我认为代码库的其他部分会定期干扰 RTC。 有人可以提供一些关于如何进行故障排除的指示吗?

谢谢。

更新: 包含 set_time() 的第一个代码块是通过从 HSE 获取时钟的自动收报机调用的。当我查看控制台输出时,当 RTC 源自 LSI(见下文)时,我意识到间隔已更改为 570 第 31 运行 导致系统崩溃。对我来说很明显,崩溃不会因为 RTC 固有的某种神秘节奏而发生,而是因为 RTC 之外的东西。并且有一段代码从系统启动后执行 600 秒。此块代码最终导致找到答案。

RTC 来自 LSI 时的崩溃日志。

1587760500: NTP sync started
1587760519: NTP sync started
1587760538: NTP sync started
1587760557: NTP sync started
1587760576: NTP sync started
1587760595: NTP sync started
1587760614: NTP sync started
1587760633: NTP sync started
1587760652: NTP sync started
1587760671: NTP sync started
1587760690: NTP sync started
1587760709: NTP sync started
1587760728: NTP sync started
1587760747: NTP sync started
1587760766: NTP sync started
1587760785: NTP sync started
1587760804: NTP sync started
1587760823: NTP sync started
1587760842: NTP sync started
1587760861: NTP sync started
1587760880: NTP sync started
1587760899: NTP sync started
1587760918: NTP sync started
1587760937: NTP sync started
1587760956: NTP sync started
1587760975: NTP sync started
1587760994: NTP sync started
1587761013: NTP sync started
1587761032: NTP sync started
1587761051: NTP sync started
1587761070: NTP sync started


++ MbedOS Error Info ++
Error Status: 0x80FF0100 Code: 256 Module: 255
Error Message: Fatal Run-time error
Location: 0x8022591
Error Value: 0x0
Current Thread: main  Id: 0x20005B8C Entry: 0x801A273 StackSize: 0x2000 StackMem: 0x2000B4C0 SP: 0x2000D270 
For more info, visit: https://mbed.com/s/error?error=0x80FF0100&tgt=ARCH_MAX
-- MbedOS Error Info --
HAL_RTC_SetDate error

问题最终被追踪到代码库的另一部分,最终在 3 跳后调用执行:

HAL_PWR_DisableBkUpAccess();

我的印象是该行仅禁用对 RTC 备份数据寄存器(在代码库中使用)和备份 SRAM 的访问。但是根据用户手册,尽管它的名字,它也禁用对 RTC 寄存器的访问,并且在 mbed 上的 RTC 初始化期间调用它的对应项。

HAL_PWR_EnableBkUpAccess();