1

I have a rp2040 board using Earl E. Philhower core. I would like to use the rtc functions just like the one mentioned in the sdk. Especially the alarm function which is critical in my project.

Supposedly from what i know you have access to all the functions in the sdk on the arduino IDE but when i tried out the example in the sdk it would not compile saying that rtc functions are not declared

char datetime_buf[256];
char *datetime_str = &datetime_buf[0];

    datetime_t t = {
            .year  = 2020,
            .month = 06,
            .day   = 05,
            .dotw  = 5, // 0 is Sunday, so 5 is Friday
            .hour  = 15,
            .min   = 45,
            .sec   = 00
    };
    
void setup() {
    Serial.begin(9600);
    Serial.println("Hello RTC!");

    rtc_init();
    rtc_set_datetime(&t);
}

// the loop function runs over and over again forever
void loop() {
        rtc_get_datetime(&t);
        
        Serial.println(datetime_str);
        sleep_ms(100);             // wait for a second
}

Earl E. Philhower comes with a Time sketch example but problem is i could not find a documentation if a alarm like function exist

/* Simple demonstration of setting and printing the current time */
/* The initial time will need to come from an RTC, NTP, or user */

/* Released to the public domain by Earle F. Philhower, III <earlephilhower@yahoo.com> */

#include <time.h>
#include <sys/time.h>

void setup() {
  Serial.begin(115200);
  delay(5000);
  struct timeval tv;

  tv.tv_sec = 1611198855; // Jan 21, 2021  3:14:15AM ...RPi Pico Release;
  tv.tv_usec = 0;
  settimeofday(&tv, nullptr);
}

void loop() {
  time_t now;
  char buff[80];

  time(&now);
  strftime(buff, sizeof(buff), "%c", localtime(&now));
  Serial.println(buff);
  delay(1000);
}
Jack
  • 213
  • 1
  • 9
  • Did you `#include "hardware/rtc.h"` when trying to use the RTC from the SDK? – Majenko Nov 15 '21 at 13:42
  • @Majenko Yes, it could not find the library. `fatal error: hardware/rtc.h: No such file or directoryResolveLibrary(hardware/rtc.h)` – Jack Nov 15 '21 at 13:44

1 Answers1

3

You can try to use my RP2040_RTC Library, especially this RP2040_RTC_Alarm example.

Both ArduinoCore-mbed and Earle Philhower arduino-pico cores are supported.

khoih-prog
  • 431
  • 3
  • 6
  • This looks nice, it even has example on how to get time via, ethernet which is one of the reason for the alarm, i need to update the time every 6 hours. – Jack Nov 15 '21 at 14:57
  • by the way , is this a hardware implementation of RTC? – Jack Nov 15 '21 at 15:38
  • Yes. It's the RP2040 hardware RTC, as mentioned in section `4.1.21. hardware_rtc` of [Raspberry Pi Pico-C/C++ SDK](https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf) – khoih-prog Nov 15 '21 at 18:31
  • Cool, thank you for making the library :) – Jack Nov 15 '21 at 19:02
  • I have a question about alarms, if the alarm goes off it triggers an interrupt right? and just like how standard interrupts work, it will stop whatever its doing and process that interrupt right? – Jack Nov 17 '21 at 14:51
  • Yes. Check the ISR code here [RP2040_RTC_Alarm.ino#L54-L58](https://github.com/khoih-prog/RP2040_RTC/blob/main/examples/Alarm/RP2040_RTC_Alarm/RP2040_RTC_Alarm.ino#L54-L58). The ISR just sets a flag and flag is read and processed in loop() at [RP2040_RTC_Alarm.ino#L179-L186](https://github.com/khoih-prog/RP2040_RTC/blob/main/examples/Alarm/RP2040_RTC_Alarm/RP2040_RTC_Alarm.ino#L179-L186) – khoih-prog Nov 17 '21 at 19:03
  • Another question, how many alarms can be set at a given time? – Jack Nov 18 '21 at 17:16