From ffd14277332cd139ce1f118264f44c1d81da22fe Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 8 May 2024 19:09:49 +0200 Subject: [PATCH] tests/periph/adc_multi: add test for reading two ADC lines to buffer --- tests/periph/adc_multi/Makefile | 8 +++++ tests/periph/adc_multi/main.c | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tests/periph/adc_multi/Makefile create mode 100644 tests/periph/adc_multi/main.c diff --git a/tests/periph/adc_multi/Makefile b/tests/periph/adc_multi/Makefile new file mode 100644 index 0000000000000..84c9fcd93f610 --- /dev/null +++ b/tests/periph/adc_multi/Makefile @@ -0,0 +1,8 @@ +BOARD ?= same54-xpro +include ../Makefile.periph_common + +FEATURES_REQUIRED = periph_adc_multi +USEMODULE += ztimer +USEMODULE += ztimer_msec + +include $(RIOTBASE)/Makefile.include diff --git a/tests/periph/adc_multi/main.c b/tests/periph/adc_multi/main.c new file mode 100644 index 0000000000000..c751abebd61d3 --- /dev/null +++ b/tests/periph/adc_multi/main.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2024 ML!PA Consulting GmbH + * + * 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. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test application for taking multiple samples from two ADCs in parallel + * + * @author Benjamin Valentin + * + * @} + */ + +#include + +#include "macros/units.h" +#include "periph/adc.h" +#include "ztimer.h" + +#define RES ADC_RES_10BIT +#define DELAY_MS 100U +#define NUM_SAMPLES 128U +#define SAMPLE_FREQ KHZ(100) + +int main(void) +{ + if (ADC_NUMOF < 2) { + printf("This test needs at leasst 2 ADC lines\n"); + return -1; + } + + /* initialize two ADC lines */ + for (unsigned i = 0; i < 2; i++) { + if (adc_init(ADC_LINE(i)) < 0) { + printf("Initialization of ADC_LINE(%u) failed\n", i); + return 1; + } else { + printf("Successfully initialized ADC_LINE(%u)\n", i); + } + } + + while (1) { + uint16_t samples[2][NUM_SAMPLES]; + + const adc_t lines[] = { ADC_LINE(0), ADC_LINE(1) }; + + adc_sample_multi(2, lines, NUM_SAMPLES, samples, RES, SAMPLE_FREQ); + + for (unsigned i = 0; i < NUM_SAMPLES; ++i) { + printf("%u\t%u\n", samples[0][i], samples[1][i]); + } + + ztimer_sleep(ZTIMER_MSEC, DELAY_MS); + } + + return 0; +}