Skip to content

Commit 9a2997f

Browse files
Jeppe Odgaardcarlescufi
authored andcommitted
drivers: sensor: mcux qdec rework rotation
Remove modulus feature and return degrees larger than count_per_revolution. This makes it possible to detect if a full rotation has occured. Signed-off-by: Jeppe Odgaard <[email protected]>
1 parent 21c5af7 commit 9a2997f

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

drivers/sensor/qdec_mcux/qdec_mcux.c

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ struct qdec_mcux_config {
3434
struct qdec_mcux_data {
3535
enc_config_t qdec_config;
3636
int32_t position;
37-
int16_t difference;
38-
int16_t revolution;
37+
uint16_t counts_per_revolution;
3938
};
4039

4140
static int qdec_mcux_attr_set(const struct device *dev, enum sensor_channel ch,
4241
enum sensor_attribute attr, const struct sensor_value *val)
4342
{
44-
const struct qdec_mcux_config *config = dev->config;
4543
struct qdec_mcux_data *data = dev->data;
4644

4745
if (ch != SENSOR_CHAN_ROTATION) {
@@ -50,8 +48,11 @@ static int qdec_mcux_attr_set(const struct device *dev, enum sensor_channel ch,
5048

5149
switch (attr) {
5250
case SENSOR_ATTR_QDEC_MOD_VAL:
53-
data->qdec_config.positionModulusValue = val->val1;
54-
ENC_Init(config->base, &data->qdec_config);
51+
if (val->val1 <= 0 || val->val1 > UINT16_MAX) {
52+
LOG_ERR("SENSOR_ATTR_QDEC_MOD_VAL value invalid");
53+
return -EINVAL;
54+
}
55+
data->counts_per_revolution = val->val1;
5556
return 0;
5657
default:
5758
return -ENOTSUP;
@@ -69,10 +70,7 @@ static int qdec_mcux_attr_get(const struct device *dev, enum sensor_channel ch,
6970

7071
switch (attr) {
7172
case SENSOR_ATTR_QDEC_MOD_VAL:
72-
/* NOTE: Register is an unsigned 32 bit value which is stored
73-
* in a signed 32 bit integer
74-
*/
75-
val->val1 = data->qdec_config.positionModulusValue;
73+
val->val1 = data->counts_per_revolution;
7674
return 0;
7775
default:
7876
return -ENOTSUP;
@@ -90,12 +88,8 @@ static int qdec_mcux_fetch(const struct device *dev, enum sensor_channel ch)
9088

9189
/* Read position */
9290
data->position = ENC_GetPositionValue(config->base);
93-
/* Read hold values to get the values from when position was read */
94-
data->difference = ENC_GetHoldPositionDifferenceValue(config->base);
95-
data->revolution = ENC_GetHoldRevolutionValue(config->base);
9691

97-
LOG_DBG("pos %d, dif %d, rev %d",
98-
data->position, data->difference, data->revolution);
92+
LOG_DBG("pos %d", data->position);
9993

10094
return 0;
10195
}
@@ -107,12 +101,8 @@ static int qdec_mcux_ch_get(const struct device *dev, enum sensor_channel ch,
107101

108102
switch (ch) {
109103
case SENSOR_CHAN_ROTATION:
110-
val->val1 = (int64_t)(data->position * 360) /
111-
data->qdec_config.positionModulusValue;
112-
val->val2 = 0;
113-
break;
114-
case SENSOR_CHAN_RPM:
115-
val->val1 = data->revolution;
104+
val->val1 = ((int64_t)data->position * 360) /
105+
data->counts_per_revolution;
116106
val->val2 = 0;
117107
break;
118108
default:
@@ -159,10 +149,15 @@ static void init_inputs(const struct device *dev)
159149

160150
#define QDEC_MCUX_INIT(n) \
161151
\
162-
static struct qdec_mcux_data qdec_mcux_##n##_data; \
163-
\
164152
BUILD_ASSERT((DT_PROP_LEN(XBAR_PHANDLE(n), xbar_maps) % 2) == 0, \
165153
"xbar_maps length must be an even number"); \
154+
BUILD_ASSERT(DT_INST_PROP(n, counts_per_revolution) > 0 && \
155+
DT_INST_PROP(n, counts_per_revolution) < UINT16_MAX, \
156+
"counts_per_revolution value invalid"); \
157+
\
158+
static struct qdec_mcux_data qdec_mcux_##n##_data = { \
159+
.counts_per_revolution = DT_INST_PROP(n, counts_per_revolution) \
160+
}; \
166161
\
167162
QDEC_MCUX_PINCTRL_DEFINE(n) \
168163
\
@@ -184,12 +179,6 @@ static void init_inputs(const struct device *dev)
184179
init_inputs(dev); \
185180
\
186181
ENC_GetDefaultConfig(&data->qdec_config); \
187-
data->qdec_config.positionModulusValue = \
188-
DT_INST_PROP(n, counts_per_revolution); \
189-
data->qdec_config.revolutionCountCondition = \
190-
kENC_RevolutionCountOnRollOverModulus; \
191-
data->qdec_config.enableModuloCountMode = true; \
192-
\
193182
ENC_Init(config->base, &data->qdec_config); \
194183
\
195184
/* Update the position counter with initial value. */ \

0 commit comments

Comments
 (0)