Skip to content

Commit

Permalink
tests: Add ADC peripheral tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luisan00 committed Jul 15, 2022
1 parent 15ee5ee commit 4b85edc
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 1 deletion.
2 changes: 1 addition & 1 deletion boards/meshme/include/periph_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};
Expand Down
Empty file added tests/periph_adc/Kconfig
Empty file.
9 changes: 9 additions & 0 deletions tests/periph_adc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include ../Makefile.tests_common

FEATURES_REQUIRED = periph_adc

USEMODULE += ztimer
USEMODULE += ztimer_msec
USEMODULE += embunit

include $(RIOTBASE)/Makefile.include
10 changes: 10 additions & 0 deletions tests/periph_adc/app.config.test
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/periph_adc/doc.txt
Original file line number Diff line number Diff line change
@@ -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
```

@}
*/
90 changes: 90 additions & 0 deletions tests/periph_adc/main.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
#include <string.h>
#include <errno.h>
#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;
}
13 changes: 13 additions & 0 deletions tests/periph_adc/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 4b85edc

Please sign in to comment.