We’re using an NXP LPC1768 and have a problem with the RTC. For som reason it seems the RTC is never advancing. It is either 0 (default) or whatever I set it to using set_time(time_t). I was under the assumption that it should just work if the target has RTC in targets.json?
What I do now is pretty much this:
rtc_init();
// set time fetched via NTP, last test set it to 1590658064
set_time(timestamp);
while(1) {
log("RTC: enabled: %d, time: %d", rtc_isenabled(), rtc_read());
}
The board has a 32kHz crystal connected to pins RTCX1 and RTCX2 and it seems to work just fine according to people who can view the signal.
Are we forced to use Kernel::get_ms_count() and manually add to our NTP timestamp? It doesn’t sound too accurate, but perhaps if we fetch NTP time pretty often it should work? We need the RTC so that we can enable things scheduled with one minute of accuracy, so a few seconds is neither here nor there. But a working RTC would be preferable of course.
Which Mbed version do you use? You can check it in the mbed (or mbed-os)/platform/mbed-version.h file. As far as I remember in some versions of Mbed OS-2 the RTC did not work on LPC1768.
Unfortunately, I was not able to compile your example code:
Error: Use of undeclared identifier ‘rtc_isenabled’ in “main.cpp”, Line: 12, Col: 46
Error: Use of undeclared identifier ‘rtc_read’ in “main.cpp”, Line: 12, Col: 63
But the following code compiled with the online compiler using the Mbed OS 5.15.3 library:
#include "mbed.h"
int main()
{
DigitalOut led(LED1);
set_time(1590658064);
while (true) {
led = !led;
time_t rawTime = time(NULL);
printf("Seconds since January 1, 1970 = %d\r\n", rawTime);
printf("Time as a basic string = %s\r\n", ctime(&rawTime));
thread_sleep_for(1000);
}
}
produced the following output when running on an Mbed LPC1768 board:
Seconds since January 1, 1970 = 1590658064
Time as a basic string = Thu May 28 09:27:44 2020
Seconds since January 1, 1970 = 1590658065
Time as a basic string = Thu May 28 09:27:45 2020
Seconds since January 1, 1970 = 1590658066
Time as a basic string = Thu May 28 09:27:46 2020
...