Skip to content

Commit 2bcc6f9

Browse files
valeriosetticarlescufi
authored andcommitted
samples: sensor: adding a sample application for the STM32 qdec driver
Sample application for the STM32 quadrature encoder driver Signed-off-by: Valerio Setti <[email protected]>
1 parent cda7d05 commit 2bcc6f9

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

samples/sensor/qdec/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(qdec_sensor)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

samples/sensor/qdec/README.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _qdec_sensor:
2+
3+
Quadrature Decoder Sensor
4+
#########################
5+
6+
Overview
7+
********
8+
9+
This sample reads the value of the counter which has been configured in
10+
quadrature decoder mode.
11+
12+
It requires:
13+
* an external mechanical encoder
14+
* pin to be properly configured in the device tree
15+
16+
Building and Running
17+
********************
18+
19+
In order to run this sample you need to:
20+
21+
* enable the quadrature decoder device in your board's DT file or board overlay
22+
* add a new alias property named ``qdec0`` and make it point to the decoder
23+
device you just enabled
24+
25+
For example, here's how the overlay file of an STM32F401 board looks like when
26+
using decoder from TIM3 through pins PA6 and PA7:
27+
28+
.. code-block:: dts
29+
30+
/ {
31+
aliases {
32+
qdec0 = &qdec;
33+
};
34+
};
35+
36+
&timers3 {
37+
status = "okay";
38+
39+
qdec: qdec {
40+
status = "okay";
41+
pinctrl-0 = <&tim3_ch1_pa6 &tim3_ch2_pa7>;
42+
pinctrl-names = "default";
43+
st,input-polarity-inverted;
44+
st,input-filter-level = <FDIV32_N8>;
45+
st,counts-per-revolution = <16>;
46+
};
47+
};
48+
49+
Sample Output
50+
=============
51+
52+
Once the MCU is started it prints the counter value every second on the
53+
console
54+
55+
.. code-block:: console
56+
57+
Quadrature decoder sensor test
58+
Position = 0 degrees
59+
Position = 15 degrees
60+
Position = 30 degrees
61+
...
62+
63+
64+
Of course the read value changes once the user manually rotates the mechanical
65+
encoder.
66+
67+
.. note::
68+
69+
The reported increment/decrement can be larger/smaller than the one shown
70+
in the above example. This depends on the mechanical encoder being used and
71+
``st,counts-per-revolution`` value.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2022 STMicroelectronics
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Application overlay for creating quadrature decoder device instance
7+
*/
8+
9+
/ {
10+
aliases {
11+
qdec0 = &qdec;
12+
};
13+
};
14+
15+
&timers3 {
16+
status = "okay";
17+
18+
qdec: qdec {
19+
status = "okay";
20+
pinctrl-0 = <&tim3_ch1_pa6 &tim3_ch2_pa7>;
21+
pinctrl-names = "default";
22+
st,input-polarity-inverted;
23+
st,input-filter-level = <FDIV32_N8>;
24+
st,counts-per-revolution = <16>;
25+
};
26+
};

samples/sensor/qdec/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_SENSOR=y
2+
CONFIG_PRINTK=y

samples/sensor/qdec/sample.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sample:
2+
description: Usage quadrature decoder sensor
3+
name: qdec_sensor
4+
tests:
5+
sample.sensor.qdec_sensor:
6+
tags: sensors
7+
platform_allow: nucleo_f401re
8+
timeout: 5
9+
harness: console
10+
harness_config:
11+
type: multi_line
12+
ordered: true
13+
regex:
14+
- "Quadrature decoder sensor test"
15+
- "Position = (.*) degrees"
16+
fixture: fixture_mech_encoder

samples/sensor/qdec/src/main.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2022 Valerio Setti <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/sensor.h>
10+
#include <zephyr/sys/printk.h>
11+
12+
void main(void)
13+
{
14+
struct sensor_value val;
15+
int rc;
16+
const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(qdec0));
17+
18+
if (!device_is_ready(dev)) {
19+
printk("Qdec device is not ready\n");
20+
return;
21+
}
22+
23+
printk("Quadrature decoder sensor test\n");
24+
25+
while (true) {
26+
rc = sensor_sample_fetch(dev);
27+
if (rc != 0) {
28+
printk("Failed to fetch sample (%d)\n", rc);
29+
return;
30+
}
31+
32+
rc = sensor_channel_get(dev, SENSOR_CHAN_ROTATION, &val);
33+
if (rc != 0) {
34+
printk("Failed to get data (%d)\n", rc);
35+
return;
36+
}
37+
38+
printk("Position = %d degrees", val.val1);
39+
40+
k_msleep(1000);
41+
}
42+
}

0 commit comments

Comments
 (0)