From 1a9e19cc6c181ea0c920a1e453a33a00dd22e027 Mon Sep 17 00:00:00 2001
From: Hauke Petersen <mail@haukepetersen.de>
Date: Thu, 6 Nov 2014 13:19:06 +0100
Subject: [PATCH] tests/periph_adc: added test for adc driver

---
 tests/periph_adc/Makefile  |  7 ++++
 tests/periph_adc/README.md | 13 ++++++
 tests/periph_adc/main.c    | 84 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)
 create mode 100644 tests/periph_adc/Makefile
 create mode 100644 tests/periph_adc/README.md
 create mode 100644 tests/periph_adc/main.c

diff --git a/tests/periph_adc/Makefile b/tests/periph_adc/Makefile
new file mode 100644
index 0000000000..837041f3ac
--- /dev/null
+++ b/tests/periph_adc/Makefile
@@ -0,0 +1,7 @@
+APPLICATION = periph_adc
+include ../Makefile.tests_common
+
+FEATURES_REQUIRED = periph_adc
+USEMODULE += vtimer
+
+include $(RIOTBASE)/Makefile.include
diff --git a/tests/periph_adc/README.md b/tests/periph_adc/README.md
new file mode 100644
index 0000000000..498ec155f0
--- /dev/null
+++ b/tests/periph_adc/README.md
@@ -0,0 +1,13 @@
+Expected result
+===============
+When running this test, you should see the samples of all configured ADC channels
+continuously streamed to std-out.
+
+Background
+==========
+This test application will initialize each configured ADC device to sample with
+10-bit accuracy. Once configured the application will continuously convert each
+available channel and print the conversion results to std-out.
+
+For verification of the output connect the ADC pins to known voltage levels
+and compare the output.
diff --git a/tests/periph_adc/main.c b/tests/periph_adc/main.c
new file mode 100644
index 0000000000..9e950047ea
--- /dev/null
+++ b/tests/periph_adc/main.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+/**
+ * @ingroup tests
+ * @{
+ *
+ * @file
+ * @brief       Test case for the low-level ADC driver
+ *
+ * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
+ *
+ * @}
+ */
+
+#include <stdio.h>
+
+#include "cpu.h"
+#include "board.h"
+#include "vtimer.h"
+#include "periph/adc.h"
+
+#if ADC_NUMOF < 1
+#error "Please enable at least 1 ADC device to run this test"
+#endif
+
+#define RES             ADC_RES_10BIT
+#define DELAY           (100 * 1000U)
+
+static int values[ADC_NUMOF][ADC_MAX_CHANNELS];
+
+int main(void)
+{
+    puts("\nRIOT ADC peripheral driver test\n");
+    puts("This test simply converts each available ADC channel about every 10ms\n\n");
+
+    for (int i = 0; i < ADC_NUMOF; i++) {
+        /* initialize result vector */
+        for (int j = 0; j < ADC_MAX_CHANNELS; j++) {
+            values[i][j] = -1;
+        }
+        /* initialize ADC device */
+        printf("Initializing ADC_%i @ %i bit resolution", i, (6 + (2* RES)));
+        if (adc_init(i, RES) == 0) {
+            puts("    ...[ok]");
+        }
+        else {
+            puts("    ...[failed]");
+            return 1;
+        }
+    }
+
+    puts("\n");
+
+    while (1) {
+        /* convert each channel for this ADC device */
+        for (int i = 0; i < ADC_NUMOF; i++) {
+            for (int j = 0; j < ADC_MAX_CHANNELS; j++) {
+                values[i][j] = adc_sample(i, j);
+            }
+        }
+
+        /* print the results */
+        printf("Values: ");
+        for (int i = 0; i < ADC_NUMOF; i++) {
+            for (int j = 0; j < ADC_MAX_CHANNELS; j++) {
+                if (values[i][j] >= 0) {
+                    printf("ADC_%i-CH%i: %4i  ", i, j, values[i][j]);
+                }
+            }
+        }
+        printf("\n");
+
+        /* sleep a little while */
+        vtimer_usleep(DELAY);
+    }
+
+    return 0;
+}
-- 
GitLab