Skip to content

Commit

Permalink
tests/periph_rtc: test for periph_rtc_mem
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Sep 2, 2021
1 parent bf88d09 commit c40b1c4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions tests/periph_rtc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
73 changes: 65 additions & 8 deletions tests/periph_rtc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@

#include <stdio.h>
#include <time.h>
#include <string.h>

#include "mutex.h"
#include "periph_conf.h"
#include "periph/rtc.h"
#include "periph/rtc_mem.h"
#include "xtimer.h"

#define PERIOD (2U)
Expand Down Expand Up @@ -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 = {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

0 comments on commit c40b1c4

Please sign in to comment.