From 4b85edc0a4e54116824d15940a5adcf59c765551 Mon Sep 17 00:00:00 2001 From: luisan00 Date: Fri, 15 Jul 2022 12:13:47 +0200 Subject: [PATCH] tests: Add ADC peripheral tests --- boards/meshme/include/periph_conf.h | 2 +- tests/periph_adc/Kconfig | 0 tests/periph_adc/Makefile | 9 +++ tests/periph_adc/app.config.test | 10 ++++ tests/periph_adc/doc.txt | 15 +++++ tests/periph_adc/main.c | 90 +++++++++++++++++++++++++++++ tests/periph_adc/tests/01-run.py | 13 +++++ 7 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 tests/periph_adc/Kconfig create mode 100644 tests/periph_adc/Makefile create mode 100644 tests/periph_adc/app.config.test create mode 100644 tests/periph_adc/doc.txt create mode 100644 tests/periph_adc/main.c create mode 100755 tests/periph_adc/tests/01-run.py diff --git a/boards/meshme/include/periph_conf.h b/boards/meshme/include/periph_conf.h index 619a12537..94fae18d7 100644 --- a/boards/meshme/include/periph_conf.h +++ b/boards/meshme/include/periph_conf.h @@ -70,7 +70,7 @@ extern "C" { /** * @brief Available ADC lines */ -static const adc_conf_t adc_config[] = { +static const adc_conf_t adc_config[] = { GPIO_PIN(PORT_A, 6), /*!< GPIO_PA6 as ADC input */ GPIO_PIN(PORT_A, 7), /*!< GPIO_PA7 as ADC input */ }; diff --git a/tests/periph_adc/Kconfig b/tests/periph_adc/Kconfig new file mode 100644 index 000000000..e69de29bb diff --git a/tests/periph_adc/Makefile b/tests/periph_adc/Makefile new file mode 100644 index 000000000..adee8f230 --- /dev/null +++ b/tests/periph_adc/Makefile @@ -0,0 +1,9 @@ +include ../Makefile.tests_common + +FEATURES_REQUIRED = periph_adc + +USEMODULE += ztimer +USEMODULE += ztimer_msec +USEMODULE += embunit + +include $(RIOTBASE)/Makefile.include diff --git a/tests/periph_adc/app.config.test b/tests/periph_adc/app.config.test new file mode 100644 index 000000000..fcc90bfd8 --- /dev/null +++ b/tests/periph_adc/app.config.test @@ -0,0 +1,10 @@ +# this file enables modules defined in Kconfig. Do not use this file for +# application configuration. This is only needed during migration. +# enable ADC peripheral driver +CONFIG_MODULE_PERIPH_ADC=y + +# enable xtimer to wake up periodically +CONFIG_MODULE_ZTIMER=y +CONFIG_MODULE_ZTIMER_MSEC=y + +CONFIG_MODULE_EMBUNIT=y \ No newline at end of file diff --git a/tests/periph_adc/doc.txt b/tests/periph_adc/doc.txt new file mode 100644 index 000000000..c254aecd4 --- /dev/null +++ b/tests/periph_adc/doc.txt @@ -0,0 +1,15 @@ +/** +@defgroup periph_adc +@ingroup test_group +@{ + +## Test the [A]nalog to [D]igital [C}onverter peripheral + +To test the code in automatic mode: + +```c +make flash test +``` + +@} + */ \ No newline at end of file diff --git a/tests/periph_adc/main.c b/tests/periph_adc/main.c new file mode 100644 index 000000000..a1f905a2b --- /dev/null +++ b/tests/periph_adc/main.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2022 Mesh4all mesh4all.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @brief ADC test + * + * @author Luis A. Ruiz + */ +#include +#include +#include "periph/adc.h" +#include "ztimer.h" +#include "embUnit.h" + +#define RES ADC_RES_10BIT +#define DELAY_MS 100U +#define LAPS 5 + +/* initialize all available ADC lines */ +void test_adc_init(void) { + int err; + for (unsigned i = 0; i < ADC_NUMOF; i++) { + if (adc_init(ADC_LINE(i)) < 0) { + printf("\nInitialization of ADC_LINE(%u) failed\n", i); + err = 1; + } else { + printf("\nSuccessfully initialized ADC_LINE(%u)\n", i); + printf("Channel %u of %u\n", i + 1, ADC_NUMOF); + err = 0; + } + ztimer_sleep(ZTIMER_MSEC, DELAY_MS); + } + + TEST_ASSERT_EQUAL_INT(0, err); +} + +void test_adc_read(void) { + int err; + int sample; + for (int lap = 0; lap < LAPS; lap++) { + + printf("\nRound %d of %d\n", lap + 1, LAPS); + + for (unsigned i = 0; i < ADC_NUMOF; i++) { + sample = adc_sample(ADC_LINE(i), RES); + if (sample < 0) { + printf("ADC_LINE(%u): selected resolution not applicable\n", i); + err = 1; + } else { + printf("ADC_LINE(%u): %i\n", i, sample); + err = 0; + } + } + ztimer_sleep(ZTIMER_MSEC, DELAY_MS); + } + + TEST_ASSERT_EQUAL_INT(0, err); +} + +Test *test_periph_adc(void) { + + EMB_UNIT_TESTFIXTURES(fixtures){ + new_TestFixture(test_adc_init), // init. ADC lines available + new_TestFixture(test_adc_read), // read value for each line + }; + + EMB_UNIT_TESTCALLER(test_periph_adc, NULL, NULL, fixtures); + + return (Test *)&test_periph_adc; +} +int main(void) { + printf("Test ADC peripherals\n"); + TESTS_START(); + TESTS_RUN(test_periph_adc()); + TESTS_END(); + return 0; +} diff --git a/tests/periph_adc/tests/01-run.py b/tests/periph_adc/tests/01-run.py new file mode 100755 index 000000000..bccd6a160 --- /dev/null +++ b/tests/periph_adc/tests/01-run.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2017 Freie Universität Berlin +# +# 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. + +import sys +from testrunner import run_check_unittests + +if __name__ == "__main__": + sys.exit(run_check_unittests())