Time functions are broken on bluepill_f103c8 (mbed)

The Time functions does not work on bluepill_f103c8, seems to always return -1. Functions are tested with the example code on their website https://os.mbed.com/docs/latest/reference/time.html

Version: Home 0.9.3 Core 3.5.3a8
IDE: Atom
OS: Windows10


#include "mbed.h"

Serial pc(B6,B7,57600);//TX, RX,BAUD RATE

int main()
    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
        while (true)
            time_t seconds = time(NULL);

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

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

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


platform = ststm32
board = bluepill_f103c8
framework = mbed

Yep this doesn’t work. When you debug it you’ll see that when you go the time function, mbedos tries to use the _rtc_read read function but that is NULL.


These RTC functions are usually set by calling attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)), but somehow this not done for this board / platform.

I’ll see where I can find the needed functions. The STM32F103C8 does have an RTC, you just need to call into the STM32HAL functions to use it.

Well, that was easy. The RTC API is implemented in targets/TARGET_STM/rtc_api.c. It is guarded by the macro

//entire code..

And that macro wasn’t set. Defining this macro activates the RTC functionality and auto-initializes everything (no need to call attach_rtc yourself) Go to your platformio.ini and add as a build flag:

build_flags = -DDEVICE_RTC=1

Your program will then output

Time as seconds since January 1, 1970 = 1256729737
Time as a basic string = Wed Oct 28 11:35:37 2009
Time as a custom formatted string = 11:35 AM

Tested this on my own Bluepill board.


Thank you, i just verified this on my blue pill and it indeed works.
I raised an issue in the STM32 github to see what they think about it but no response so far

Hi @OscarGarciaF! How did you enable bluepill board in the mbed online compiler? I can’t find this board in platform list to reproduce the issue.

it’s not oficially supported. check here it provides a workaround to use it

Hm, I don’t know the reason, but the RTC module is disabled in the configuration for the bluepill board in the official mbed repository. So there are 2 options: add build_flags = -DDEVICE_RTC=1 to your platformio.ini or use nucleo_f103rb board.

Hi build_flags = -DDEVICE_RTC=1 do works as @maxgerhardt indicated above