test: Add on-device unit test for STM32 RTC

BRANCH=none
BUG=b:151105339
TEST=make BOARD=bloonchipper test-stm32f_rtc -j
     Flash stm32f_rtc.bin and "runtest" in the console

Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Change-Id: I3debfd93b62cb269ad61af0e4ca7e195554b5548
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2171569
Reviewed-by: Eric Yilun Lin <yllin@chromium.org>
This commit is contained in:
Tom Hughes 2020-04-29 15:13:36 -07:00 committed by Commit Bot
parent a3bfb8b262
commit 1f692079b7
8 changed files with 67 additions and 0 deletions

View File

@ -20,3 +20,4 @@ test-list-y=\
rtc \
sha256 \
sha256_unrolled \
stm32f_rtc \

View File

@ -21,3 +21,4 @@ test-list-y=\
rtc \
sha256 \
sha256_unrolled \
stm32f_rtc \

View File

@ -19,3 +19,4 @@ test-list-y=\
rtc \
sha256 \
sha256_unrolled \
stm32f_rtc \

View File

@ -18,3 +18,4 @@ test-list-y=\
rtc \
sha256 \
sha256_unrolled \
stm32f_rtc \

View File

@ -313,6 +313,7 @@ void reset_rtc_alarm(struct rtc_time_reg *rtc)
rtc_lock_regs();
}
test_mockable_static
void __rtc_alarm_irq(void)
{
struct rtc_time_reg rtc;

View File

@ -151,6 +151,7 @@ sha256-y=sha256.o
sha256_unrolled-y=sha256.o
shmalloc-y=shmalloc.o
static_if-y=static_if.o
stm32f_rtc-y=stm32f_rtc.o
stress-y=stress.o
system-y=system.o
thermal-y=thermal.o

52
test/stm32f_rtc.c Normal file
View File

@ -0,0 +1,52 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "clock-f.h"
#include "test_util.h"
static volatile uint32_t rtc_fired;
static struct rtc_time_reg rtc_irq;
static const int rtc_delay_ms = 500;
/* Override default RTC interrupt handler */
void __rtc_alarm_irq(void)
{
atomic_add(&rtc_fired, 1);
reset_rtc_alarm(&rtc_irq);
}
test_static int test_rtc_alarm(void)
{
struct rtc_time_reg rtc;
uint32_t rtc_diff_us;
uint32_t rtc_diff_ms;
const int delay_us = rtc_delay_ms * MSEC;
set_rtc_alarm(0, delay_us, &rtc, 0);
msleep(2 * rtc_delay_ms);
/* Make sure the interrupt fired exactly once. */
TEST_EQ(1, atomic_read_clear(&rtc_fired), "%d");
rtc_diff_us = get_rtc_diff(&rtc, &rtc_irq);
ccprintf("rtc_diff_us: %d\n", rtc_diff_us);
/* Assume we'll always fire within 1 ms. May need to be adjusted if
* this doesn't hold.
*/
rtc_diff_ms = rtc_diff_us / MSEC;
TEST_EQ(rtc_diff_ms, rtc_delay_ms, "%d");
return EC_SUCCESS;
}
void run_test(void)
{
RUN_TEST(test_rtc_alarm);
test_print_result();
}

9
test/stm32f_rtc.tasklist Normal file
View File

@ -0,0 +1,9 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* See CONFIG_TASK_LIST in config.h for details.
*/
#define CONFIG_TEST_TASK_LIST /* no tasks */