Skip to content

Commit 7f78eb7

Browse files
JurajLieskovskynashif
authored andcommitted
drivers: sensor: st: qdec_stm32: addition of fractional part
The fractional part of the qdec sensor readout is set to zero (val2 = 0). Changed the format of the run time data to Q26.6 and adjusted the assignment of val1 and val2 accordingly. Signed-off-by: Juraj Lieskovský <lieskovsky.juraj@gmail.com>
1 parent 3f58d49 commit 7f78eb7

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/sensor/st/qdec_stm32/qdec_stm32.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct qdec_stm32_dev_cfg {
3838

3939
/* Device run time data */
4040
struct qdec_stm32_dev_data {
41-
int32_t position;
41+
uint32_t position;
4242
};
4343

4444
static int qdec_stm32_fetch(const struct device *dev, enum sensor_channel chan)
@@ -56,7 +56,9 @@ static int qdec_stm32_fetch(const struct device *dev, enum sensor_channel chan)
5656
* can be ignored
5757
*/
5858
counter_value = LL_TIM_GetCounter(dev_cfg->timer_inst) % dev_cfg->counts_per_revolution;
59-
dev_data->position = (counter_value * 360) / dev_cfg->counts_per_revolution;
59+
60+
/* The angle calculated in the fixed-point format (Q26.6 format) */
61+
dev_data->position = (counter_value * 23040) / dev_cfg->counts_per_revolution;
6062

6163
return 0;
6264
}
@@ -67,8 +69,8 @@ static int qdec_stm32_get(const struct device *dev, enum sensor_channel chan,
6769
struct qdec_stm32_dev_data *const dev_data = dev->data;
6870

6971
if (chan == SENSOR_CHAN_ROTATION) {
70-
val->val1 = dev_data->position;
71-
val->val2 = 0;
72+
val->val1 = dev_data->position >> 6;
73+
val->val2 = (dev_data->position & 0x3F) * 15625;
7274
} else {
7375
return -ENOTSUP;
7476
}

0 commit comments

Comments
 (0)