@@ -34,14 +34,12 @@ struct qdec_mcux_config {
34
34
struct qdec_mcux_data {
35
35
enc_config_t qdec_config ;
36
36
int32_t position ;
37
- int16_t difference ;
38
- int16_t revolution ;
37
+ uint16_t counts_per_revolution ;
39
38
};
40
39
41
40
static int qdec_mcux_attr_set (const struct device * dev , enum sensor_channel ch ,
42
41
enum sensor_attribute attr , const struct sensor_value * val )
43
42
{
44
- const struct qdec_mcux_config * config = dev -> config ;
45
43
struct qdec_mcux_data * data = dev -> data ;
46
44
47
45
if (ch != SENSOR_CHAN_ROTATION ) {
@@ -50,8 +48,11 @@ static int qdec_mcux_attr_set(const struct device *dev, enum sensor_channel ch,
50
48
51
49
switch (attr ) {
52
50
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 ;
55
56
return 0 ;
56
57
default :
57
58
return - ENOTSUP ;
@@ -69,10 +70,7 @@ static int qdec_mcux_attr_get(const struct device *dev, enum sensor_channel ch,
69
70
70
71
switch (attr ) {
71
72
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 ;
76
74
return 0 ;
77
75
default :
78
76
return - ENOTSUP ;
@@ -90,12 +88,8 @@ static int qdec_mcux_fetch(const struct device *dev, enum sensor_channel ch)
90
88
91
89
/* Read position */
92
90
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 );
96
91
97
- LOG_DBG ("pos %d, dif %d, rev %d" ,
98
- data -> position , data -> difference , data -> revolution );
92
+ LOG_DBG ("pos %d" , data -> position );
99
93
100
94
return 0 ;
101
95
}
@@ -107,12 +101,8 @@ static int qdec_mcux_ch_get(const struct device *dev, enum sensor_channel ch,
107
101
108
102
switch (ch ) {
109
103
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 ;
116
106
val -> val2 = 0 ;
117
107
break ;
118
108
default :
@@ -159,10 +149,15 @@ static void init_inputs(const struct device *dev)
159
149
160
150
#define QDEC_MCUX_INIT (n ) \
161
151
\
162
- static struct qdec_mcux_data qdec_mcux_##n##_data; \
163
- \
164
152
BUILD_ASSERT((DT_PROP_LEN(XBAR_PHANDLE(n), xbar_maps) % 2) == 0, \
165
153
"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
+ }; \
166
161
\
167
162
QDEC_MCUX_PINCTRL_DEFINE(n) \
168
163
\
@@ -184,12 +179,6 @@ static void init_inputs(const struct device *dev)
184
179
init_inputs(dev); \
185
180
\
186
181
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
- \
193
182
ENC_Init(config->base, &data->qdec_config); \
194
183
\
195
184
/* Update the position counter with initial value. */ \
0 commit comments