From c40b1c4654a2782aab7b4135d077020c27c2e6a5 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 19 Aug 2021 18:40:44 +0200 Subject: [PATCH] tests/periph_rtc: test for periph_rtc_mem --- tests/periph_rtc/Makefile | 1 + tests/periph_rtc/main.c | 73 ++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/tests/periph_rtc/Makefile b/tests/periph_rtc/Makefile index 3d33287f19695..c717733c395cc 100644 --- a/tests/periph_rtc/Makefile +++ b/tests/periph_rtc/Makefile @@ -2,6 +2,7 @@ include ../Makefile.tests_common FEATURES_REQUIRED += periph_rtc FEATURES_OPTIONAL += periph_rtc_ms +FEATURES_OPTIONAL += periph_rtc_mem DISABLE_MODULE += periph_init_rtc diff --git a/tests/periph_rtc/main.c b/tests/periph_rtc/main.c index db798a6fd0e9e..1812a8a059d81 100644 --- a/tests/periph_rtc/main.c +++ b/tests/periph_rtc/main.c @@ -23,10 +23,12 @@ #include #include +#include #include "mutex.h" #include "periph_conf.h" #include "periph/rtc.h" +#include "periph/rtc_mem.h" #include "xtimer.h" #define PERIOD (2U) @@ -69,6 +71,58 @@ static void cb(void *arg) mutex_unlock(arg); } +#ifdef MODULE_PERIPH_RTC_MEM +static const uint8_t riot_msg_offset = 1; +static const char riot_msg[] = "RIOT"; +static void _set_rtc_mem(void) +{ + /* first fill the whole memory */ + uint8_t size = rtc_mem_size(); + while (size--) { + rtc_mem_write(size, &size, sizeof(size)); + } + + /* write test data */ + rtc_mem_write(riot_msg_offset, riot_msg, sizeof(riot_msg) - 1); +} + +static void _get_rtc_mem(void) +{ + char buf[4]; + rtc_mem_read(riot_msg_offset, buf, sizeof(buf)); + + if (memcmp(buf, riot_msg, sizeof(buf))) { + puts("RTC mem content does not match"); + for (unsigned i = 0; i < sizeof(buf); ++i) { + printf("%02x - %02x\n", riot_msg[i], buf[i]); + } + return; + } + + uint8_t size = rtc_mem_size(); + while (size--) { + uint8_t data; + + if (size >= riot_msg_offset && + size < riot_msg_offset + sizeof(riot_msg)) { + continue; + } + + rtc_mem_read(size, &data, 1); + if (data != size) { + puts("RTC mem content does not match"); + printf("%0x2: %0x2\n", size, data); + } + } + + + puts("RTC mem OK"); +} +#else +static inline void _set_rtc_mem(void) {} +static inline void _get_rtc_mem(void) {} +#endif + int main(void) { struct tm time = { @@ -88,6 +142,9 @@ int main(void) rtc_init(); + _set_rtc_mem(); + _get_rtc_mem(); + /* set RTC */ print_time(" Setting clock to ", &time); rtc_set_time(&time); @@ -149,17 +206,17 @@ int main(void) puts(""); /* loop over a few alarm cycles */ - while (1) { + do { mutex_lock(&rtc_mtx); puts("Alarm!"); - if (++cnt < REPEAT) { - struct tm time; - rtc_get_alarm(&time); - inc_secs(&time, PERIOD); - rtc_set_alarm(&time, cb, &rtc_mtx); - } - } + struct tm time; + rtc_get_alarm(&time); + inc_secs(&time, PERIOD); + rtc_set_alarm(&time, cb, &rtc_mtx); + } while (++cnt < REPEAT); + + _get_rtc_mem(); return 0; }