tests: drivers: counter: Add test suite for nrf rtc fixed top value feature

Added test suite which performs sanity check on instances with
fixed-top feature enabled.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2019-10-04 11:31:27 +02:00 committed by Carles Cufí
commit ab288a8e9e
6 changed files with 151 additions and 0 deletions

View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(NONE)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,2 @@
CONFIG_COUNTER_RTC0=y
CONFIG_COUNTER_RTC2=y

View file

@ -0,0 +1,6 @@
&rtc0 {
fixed-top;
};
&rtc2 {
fixed-top;
};

View file

@ -0,0 +1,3 @@
CONFIG_COUNTER=y
CONFIG_BT=n
CONFIG_ZTEST=y

View file

@ -0,0 +1,126 @@
/*
* Copyright (c) 2019, Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <drivers/counter.h>
#include <ztest.h>
#include <kernel.h>
#include <logging/log.h>
#include <hal/nrf_rtc.h>
LOG_MODULE_REGISTER(test);
static volatile u32_t top_cnt;
const char *devices[] = {
#ifdef CONFIG_COUNTER_RTC0
/* Nordic RTC0 may be reserved for Bluetooth */
DT_NORDIC_NRF_RTC_RTC_0_LABEL,
#endif
/* Nordic RTC1 is used for the system clock */
#ifdef CONFIG_COUNTER_RTC2
DT_NORDIC_NRF_RTC_RTC_2_LABEL,
#endif
};
typedef void (*counter_test_func_t)(const char *dev_name);
static void counter_setup_instance(const char *dev_name)
{
top_cnt = 0U;
}
static void counter_tear_down_instance(const char *dev_name)
{
int err;
struct device *dev;
dev = device_get_binding(dev_name);
err = counter_stop(dev);
zassert_equal(0, err, "%s: Counter failed to stop", dev_name);
}
static void test_all_instances(counter_test_func_t func)
{
for (int i = 0; i < ARRAY_SIZE(devices); i++) {
counter_setup_instance(devices[i]);
func(devices[i]);
counter_tear_down_instance(devices[i]);
/* Allow logs to be printed. */
k_sleep(100);
}
}
void test_set_custom_top_value_fails_on_instance(const char *dev_name)
{
struct device *dev;
int err;
struct counter_top_cfg top_cfg = {
.callback = NULL,
.flags = 0
};
dev = device_get_binding(dev_name);
top_cfg.ticks = counter_get_max_top_value(dev) - 1;
err = counter_set_top_value(dev, &top_cfg);
zassert_true(err != 0, "%s: Expected error code", dev_name);
}
void test_set_custom_top_value_fails(void)
{
test_all_instances(test_set_custom_top_value_fails_on_instance);
}
static void top_handler(struct device *dev, void *user_data)
{
top_cnt++;
}
void test_top_handler_on_instance(const char *dev_name)
{
struct device *dev;
u32_t tmp_top_cnt;
int err;
struct counter_top_cfg top_cfg = {
.callback = top_handler,
.flags = 0
};
dev = device_get_binding(dev_name);
top_cfg.ticks = counter_get_max_top_value(dev);
err = counter_set_top_value(dev, &top_cfg);
zassert_equal(0, err, "%s: Unexpected error code (%d)", dev_name, err);
#ifdef CONFIG_COUNTER_RTC0
nrf_rtc_task_trigger(NRF_RTC0, NRF_RTC_TASK_TRIGGER_OVERFLOW);
#endif
#ifdef CONFIG_COUNTER_RTC2
nrf_rtc_task_trigger(NRF_RTC2, NRF_RTC_TASK_TRIGGER_OVERFLOW);
#endif
counter_start(dev);
k_busy_wait(10000);
tmp_top_cnt = top_cnt;
zassert_equal(tmp_top_cnt, 1, "%s: Expected top handler", dev_name);
}
void test_top_handler(void)
{
test_all_instances(test_top_handler_on_instance);
}
void test_main(void)
{
ztest_test_suite(test_counter,
ztest_unit_test(test_set_custom_top_value_fails),
ztest_unit_test(test_top_handler)
);
ztest_run_test_suite(test_counter);
}

View file

@ -0,0 +1,5 @@
tests:
peripheral.counter:
tags: drivers
depends_on: counter
platform_whitelist: nrf52840_pca0056