From a49dada2913ed5a1cfbbc2210a63f169f33bfd60 Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Tue, 15 Oct 2019 11:47:52 +0200 Subject: [PATCH] tests: add test for qmc5883l driver --- tests/driver_qmc5883l/Makefile | 7 ++ tests/driver_qmc5883l/README.md | 26 ++++++ tests/driver_qmc5883l/main.c | 150 ++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 tests/driver_qmc5883l/Makefile create mode 100644 tests/driver_qmc5883l/README.md create mode 100644 tests/driver_qmc5883l/main.c diff --git a/tests/driver_qmc5883l/Makefile b/tests/driver_qmc5883l/Makefile new file mode 100644 index 0000000000..c84c8d96ec --- /dev/null +++ b/tests/driver_qmc5883l/Makefile @@ -0,0 +1,7 @@ +include ../Makefile.tests_common + +USEMODULE += qmc5883l_int +USEMODULE += core_thread_flags +USEMODULE += xtimer + +include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_qmc5883l/README.md b/tests/driver_qmc5883l/README.md new file mode 100644 index 0000000000..5394206d3a --- /dev/null +++ b/tests/driver_qmc5883l/README.md @@ -0,0 +1,26 @@ +# About +This test application is created for testing/demonstrating the driver for +QMC5883L magnetic sensors. + +# Configuration +This test uses the driver's default configuration as provided in +`drivers/qmc5883l/include/qmc5883l_params.h`. You can override +selected values from the command line or your application like so: +``` +$ CFLAGS="-DQMC5883L_PARAM_OSR=QMC5883L_OSR_128" make all +``` + +The data ready (DRDY) interrupt pin and handling is disabled per default. If +you which to use it, you have to specify the MCU pin that is connected to the +DRDY pin for your QMC5883L sensor using the `QMC5883L_PARAM_PIN_DRDY` +configuration option. + +Independent of the ping configuration, the according submodule `qmc5883l_int` is +always includes. This way, the test application will automatically use +interrupts instead of polling to read the sensor data whenever a specific pin +(anything other than `GPIO_UNDEF`) is configured. + +# Usage +Flash this application to any board you have connected a QMC5883L sensor to. +When starting, this application will continuously read sensor data and print +them to STDIO. diff --git a/tests/driver_qmc5883l/main.c b/tests/driver_qmc5883l/main.c new file mode 100644 index 0000000000..966c279f39 --- /dev/null +++ b/tests/driver_qmc5883l/main.c @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2019 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 application for the QMC5883L magnetic sensor + + * @author Hauke Petersen + * + * @} + */ + +#include + +#include "xtimer.h" +#include "thread.h" +#include "thread_flags.h" + +#include "qmc5883l.h" +#include "qmc5883l_params.h" + +#define PWR_OFF_DELAY (1u) +#define FLAG_DRDY (0x0400) + +/* allocate the device descriptor */ +static qmc5883l_t _dev; + +#ifdef MODULE_QMC5883L_INT +static thread_t *_tmain; + +static void _on_drdy(void *arg) +{ + (void)arg; + thread_flags_set(_tmain, FLAG_DRDY); +} +#endif + +static void _read_and_dump(void) +{ + int16_t data[3]; + int res = qmc5883l_read(&_dev, data); + if ((res == QMC5883L_OK) || (res == QMC5883L_OVERFLOW)) { + printf("Reading - X:%6i Y:%6i Z:%6i [mGauss]", + (int)data[0], (int)data[1], (int)data[2]); + if (res == QMC5883L_OVERFLOW) { + printf(" - OVERFLOWED"); + } + puts(""); + } + else if (res == QMC5883L_OVERFLOW) { + puts("Reading - overflow"); + } + else if (res == QMC5883L_NODATA) { + puts("Reading - no new data available"); + } +} + +int main(void) +{ + uint32_t delay = US_PER_MS; + + puts("QMC5883L test application"); + puts("Please refer to the README.md for more information\n"); + + /* initialize the sensor with default configuration parameters */ + if (qmc5883l_init(&_dev, &qmc5883l_params[0]) != QMC5883L_OK) { + puts("Error: unable to initialize device"); + return 1; + } + + printf("QMC5883L successfully initialized.\nData rate: "); + switch (qmc5883l_params[0].odr) { + case QMC5883L_ODR_10HZ: puts("10Hz"); delay *= 100; break; + case QMC5883L_ODR_50HZ: puts("50Hz"); delay *= 20; break; + case QMC5883L_ODR_100HZ: puts("100Hz"); delay *= 10; break; + case QMC5883L_ODR_200HZ: puts("200Hz"); delay *= 5; break; + } + printf("Data range: "); + switch (qmc5883l_params[0].rng) { + case QMC5883L_RNG_2G: puts("2G"); break; + case QMC5883L_RNG_8G: puts("8G"); break; + } + printf("Over sample rate: "); + switch (qmc5883l_params[0].osr) { + case QMC5883L_OSR_512: puts("512"); break; + case QMC5883L_OSR_256: puts("256"); break; + case QMC5883L_OSR_128: puts("128"); break; + case QMC5883L_OSR_64: puts("64"); break; + } +#ifdef MODULE_QMC5883L_INT + printf("Mode: "); + if (qmc5883l_params[0].pin_drdy != GPIO_UNDEF) { + puts("interrupt driven"); + } + else { + puts("polling"); + } +#endif + puts(""); + + /* test the driver's power cycling */ + puts("Power cycle test: powering device off now"); + if (qmc5883l_poweroff(&_dev) != QMC5883L_OK) { + puts("Error: unable to power off device"); + return 1; + } + xtimer_sleep(PWR_OFF_DELAY); + if (qmc5883l_poweron(&_dev) != QMC5883L_OK) { + puts("Error: unable to power on the device again"); + return 1; + } + puts("Power cycle test: device is powered back on now"); + +#ifdef MODULE_QMC5883L_INT + /* safe a reference to the main thread TCB so we can wait for flags */ + if (qmc5883l_params[0].pin_drdy != GPIO_UNDEF) { + _tmain = (thread_t *)thread_get(thread_getpid()); + + if (qmc5883l_init_int(&_dev, _on_drdy, NULL) != QMC5883L_OK) { + puts("Error: unable to configure interrupt callback"); + return 1; + } + + while (1) { + thread_flags_wait_any(FLAG_DRDY); + _read_and_dump(); + } + } +#endif + + while (1) { + int ready; + do { + xtimer_usleep(delay); + ready = qmc5883l_data_ready(&_dev); + } while (ready != QMC5883L_OK); + _read_and_dump(); + } + + /* should never be reached */ + return 0; +}