Skip to content

Commit

Permalink
drivers/rtt64: add RTC emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed May 31, 2022
1 parent 529f01d commit 561853a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 6 deletions.
98 changes: 98 additions & 0 deletions drivers/rtt64/rtt64_rtc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (C) 2022 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/

/**
* @ingroup drivers_rtt64
* @{
*
* @file
* @brief Basic RTC implementation based on RTT64
*
* @note Unlike a real RTC, this emulated version is not guaranteed to keep
* time across reboots or deep sleep.
* If your hardware provides the means to implement a real RTC, always
* prefer that over this emulated version!
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*
* @}
*/

#include <time.h>

#include "irq.h"
#include "periph/rtc.h"
#include "rtt64.h"
#include "time_units.h"

#define ENABLE_DEBUG 0
#include "debug.h"

void rtc_init(void)
{
rtt64_init();
}

int rtc_set_time(struct tm *time)
{
rtt64_set_time(mktime(time), 0);
return 0;
}

int rtc_get_time_ms(struct tm *time, uint16_t *ms)
{
uint64_t secs;
uint32_t us;

rtt64_get_time(&secs, &us);

if (ms) {
*ms = us / US_PER_MS;
}
localtime_r((time_t *)&secs, time);

return 0;
}

int rtc_get_time(struct tm *time)
{
return rtc_get_time_ms(time, NULL);
}

int rtc_get_alarm(struct tm *time)
{
uint64_t secs;
uint32_t us;

rtt64_get_alarm_time(&secs, &us);
localtime_r((time_t *)&secs, time);

return 0;
}

int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
{
rtt64_set_alarm_time(mktime(time), 0, cb, arg);

return 0;
}

void rtc_clear_alarm(void)
{
rtt64_clear_alarm();
}

void rtc_poweron(void)
{
rtt_poweron();
}

void rtc_poweroff(void)
{
rtt_poweroff();
}
2 changes: 1 addition & 1 deletion pkg/fatfs/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ ifneq (,$(filter vfs_auto_format,$(USEMODULE)))
endif

# Use RTC for timestamps if available
ifeq (,$(filter rtt_rtc,$(USEMODULE)))
ifeq (,$(filter rtt_rtc rtt64,$(USEMODULE)))
FEATURES_OPTIONAL += periph_rtc
endif
4 changes: 2 additions & 2 deletions pkg/fatfs/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ NATIVEINCLUDES += -I$(RIOTPKG)/fatfs/vendor/include

DIRS += $(RIOTBASE)/pkg/fatfs/fatfs_diskio/mtd

ifneq (,$(filter fatfs_vfs,$(USEMODULE)))
ifneq (,$(filter fatfs_vfs rtt64,$(USEMODULE)))
DIRS += $(RIOTBASE)/pkg/fatfs/fatfs_vfs
endif

#if periph_rtc is available use it. Otherwise use static timestamps
ifeq (,$(filter rtt_rtc periph_rtc,$(USEMODULE)))
ifeq (,$(filter rtt_rtc periph_rtc rtt64,$(USEMODULE)))
CFLAGS += -DFATFS_FFCONF_OPT_FS_NORTC=1
else
CFLAGS += -DFATFS_FFCONF_OPT_FS_NORTC=0
Expand Down
2 changes: 1 addition & 1 deletion sys/shell/commands/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ ifneq (,$(filter lwip_netif,$(USEMODULE)))
SRC += sc_lwip_netif.c
endif

ifneq (,$(filter rtt_rtc periph_rtc,$(USEMODULE)))
ifneq (,$(filter rtt_rtc periph_rtc rtt64,$(USEMODULE)))
SRC += sc_rtc.c
endif

Expand Down
4 changes: 2 additions & 2 deletions sys/shell/commands/shell_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extern int _at30tse75x_handler(int argc, char **argv);
extern int _saul(int argc, char **argv);
#endif

#if defined(MODULE_PERIPH_RTC) || defined(MODULE_RTT_RTC)
#if defined(MODULE_PERIPH_RTC) || defined(MODULE_RTT_RTC) || defined(MODULE_RTT64)
extern int _rtc_handler(int argc, char **argv);
#endif

Expand Down Expand Up @@ -278,7 +278,7 @@ const shell_command_t _shell_command_list[] = {
{ "random_init", "initializes the PRNG", _random_init },
{ "random_get", "returns 32 bit of pseudo randomness", _random_get },
#endif
#if defined(MODULE_PERIPH_RTC) || defined(MODULE_RTT_RTC)
#if defined(MODULE_PERIPH_RTC) || defined(MODULE_RTT_RTC) || defined(MODULE_RTT64)
{"rtc", "control RTC peripheral interface", _rtc_handler},
#endif
#ifdef MODULE_RTT_CMD
Expand Down

0 comments on commit 561853a

Please # to comment.