From 2bcc6f9194ff844fd7fae26b32013b97b73b5b98 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 30 Aug 2022 18:27:26 +0200 Subject: [PATCH] samples: sensor: adding a sample application for the STM32 qdec driver Sample application for the STM32 quadrature encoder driver Signed-off-by: Valerio Setti --- samples/sensor/qdec/CMakeLists.txt | 8 +++ samples/sensor/qdec/README.rst | 71 +++++++++++++++++++ .../sensor/qdec/boards/nucleo_f401re.overlay | 26 +++++++ samples/sensor/qdec/prj.conf | 2 + samples/sensor/qdec/sample.yaml | 16 +++++ samples/sensor/qdec/src/main.c | 42 +++++++++++ 6 files changed, 165 insertions(+) create mode 100644 samples/sensor/qdec/CMakeLists.txt create mode 100644 samples/sensor/qdec/README.rst create mode 100644 samples/sensor/qdec/boards/nucleo_f401re.overlay create mode 100644 samples/sensor/qdec/prj.conf create mode 100644 samples/sensor/qdec/sample.yaml create mode 100644 samples/sensor/qdec/src/main.c diff --git a/samples/sensor/qdec/CMakeLists.txt b/samples/sensor/qdec/CMakeLists.txt new file mode 100644 index 00000000000..23c45c0b6c4 --- /dev/null +++ b/samples/sensor/qdec/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(qdec_sensor) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/sensor/qdec/README.rst b/samples/sensor/qdec/README.rst new file mode 100644 index 00000000000..eaeb8f9f499 --- /dev/null +++ b/samples/sensor/qdec/README.rst @@ -0,0 +1,71 @@ +.. _qdec_sensor: + +Quadrature Decoder Sensor +######################### + +Overview +******** + +This sample reads the value of the counter which has been configured in +quadrature decoder mode. + +It requires: +* an external mechanical encoder +* pin to be properly configured in the device tree + +Building and Running +******************** + +In order to run this sample you need to: + +* enable the quadrature decoder device in your board's DT file or board overlay +* add a new alias property named ``qdec0`` and make it point to the decoder + device you just enabled + +For example, here's how the overlay file of an STM32F401 board looks like when +using decoder from TIM3 through pins PA6 and PA7: + +.. code-block:: dts + + / { + aliases { + qdec0 = &qdec; + }; + }; + + &timers3 { + status = "okay"; + + qdec: qdec { + status = "okay"; + pinctrl-0 = <&tim3_ch1_pa6 &tim3_ch2_pa7>; + pinctrl-names = "default"; + st,input-polarity-inverted; + st,input-filter-level = ; + st,counts-per-revolution = <16>; + }; + }; + +Sample Output +============= + +Once the MCU is started it prints the counter value every second on the +console + +.. code-block:: console + + Quadrature decoder sensor test + Position = 0 degrees + Position = 15 degrees + Position = 30 degrees + ... + + +Of course the read value changes once the user manually rotates the mechanical +encoder. + +.. note:: + + The reported increment/decrement can be larger/smaller than the one shown + in the above example. This depends on the mechanical encoder being used and + ``st,counts-per-revolution`` value. diff --git a/samples/sensor/qdec/boards/nucleo_f401re.overlay b/samples/sensor/qdec/boards/nucleo_f401re.overlay new file mode 100644 index 00000000000..2a1f4cc1024 --- /dev/null +++ b/samples/sensor/qdec/boards/nucleo_f401re.overlay @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 STMicroelectronics + * + * SPDX-License-Identifier: Apache-2.0 + * + * Application overlay for creating quadrature decoder device instance + */ + +/ { + aliases { + qdec0 = &qdec; + }; +}; + +&timers3 { + status = "okay"; + + qdec: qdec { + status = "okay"; + pinctrl-0 = <&tim3_ch1_pa6 &tim3_ch2_pa7>; + pinctrl-names = "default"; + st,input-polarity-inverted; + st,input-filter-level = ; + st,counts-per-revolution = <16>; + }; +}; diff --git a/samples/sensor/qdec/prj.conf b/samples/sensor/qdec/prj.conf new file mode 100644 index 00000000000..9fd06347851 --- /dev/null +++ b/samples/sensor/qdec/prj.conf @@ -0,0 +1,2 @@ +CONFIG_SENSOR=y +CONFIG_PRINTK=y diff --git a/samples/sensor/qdec/sample.yaml b/samples/sensor/qdec/sample.yaml new file mode 100644 index 00000000000..a35167c1c25 --- /dev/null +++ b/samples/sensor/qdec/sample.yaml @@ -0,0 +1,16 @@ +sample: + description: Usage quadrature decoder sensor + name: qdec_sensor +tests: + sample.sensor.qdec_sensor: + tags: sensors + platform_allow: nucleo_f401re + timeout: 5 + harness: console + harness_config: + type: multi_line + ordered: true + regex: + - "Quadrature decoder sensor test" + - "Position = (.*) degrees" + fixture: fixture_mech_encoder diff --git a/samples/sensor/qdec/src/main.c b/samples/sensor/qdec/src/main.c new file mode 100644 index 00000000000..dfb0c138c8e --- /dev/null +++ b/samples/sensor/qdec/src/main.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Valerio Setti + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +void main(void) +{ + struct sensor_value val; + int rc; + const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(qdec0)); + + if (!device_is_ready(dev)) { + printk("Qdec device is not ready\n"); + return; + } + + printk("Quadrature decoder sensor test\n"); + + while (true) { + rc = sensor_sample_fetch(dev); + if (rc != 0) { + printk("Failed to fetch sample (%d)\n", rc); + return; + } + + rc = sensor_channel_get(dev, SENSOR_CHAN_ROTATION, &val); + if (rc != 0) { + printk("Failed to get data (%d)\n", rc); + return; + } + + printk("Position = %d degrees", val.val1); + + k_msleep(1000); + } +}