Skip to content

Commit 2e6c83d

Browse files
mstasiaknordicnashif
authored andcommitted
drivers: sensor: qdec: fix QDEC overflow handling
QDEC sensor driver fails to inform user of the overflow in the ACC register, which makes the most recently fetched data invalid. An error code return has been added to nrfx_qdec_sample_fetch(), that indicates that an overflow has occured, based on oveflow flag. Also, raw_acc field was added in the qdec_nrfx_data structure, to adjust QDEC to sensor API rules - two subsequent sensor_channel_get() calls should will yield the same values. Signed-off-by: Michał Stasiak <[email protected]>
1 parent 79222c9 commit 2e6c83d

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

drivers/sensor/nordic/qdec_nrfx/qdec_nrfx.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ LOG_MODULE_REGISTER(qdec_nrfx, CONFIG_SENSOR_LOG_LEVEL);
2626

2727

2828
struct qdec_nrfx_data {
29+
int32_t fetched_acc;
2930
int32_t acc;
31+
bool overflow;
3032
sensor_trigger_handler_t data_ready_handler;
3133
const struct sensor_trigger *data_ready_trigger;
3234
};
@@ -49,6 +51,8 @@ static void accumulate(struct qdec_nrfx_data *data, int32_t acc)
4951

5052
if (!overflow) {
5153
data->acc += acc;
54+
} else {
55+
data->overflow = true;
5256
}
5357

5458
irq_unlock(key);
@@ -70,6 +74,18 @@ static int qdec_nrfx_sample_fetch(const struct device *dev,
7074

7175
accumulate(data, acc);
7276

77+
unsigned int key = irq_lock();
78+
79+
data->fetched_acc = data->acc;
80+
data->acc = 0;
81+
82+
irq_unlock(key);
83+
84+
if (data->overflow) {
85+
data->overflow = false;
86+
return -EOVERFLOW;
87+
}
88+
7389
return 0;
7490
}
7591

@@ -87,8 +103,7 @@ static int qdec_nrfx_channel_get(const struct device *dev,
87103
}
88104

89105
key = irq_lock();
90-
acc = data->acc;
91-
data->acc = 0;
106+
acc = data->fetched_acc;
92107
irq_unlock(key);
93108

94109
val->val1 = (acc * FULL_ANGLE) / config->steps;
@@ -148,6 +163,10 @@ static void qdec_nrfx_event_handler(nrfx_qdec_event_t event, void *p_context)
148163
}
149164
break;
150165

166+
case NRF_QDEC_EVENT_ACCOF:
167+
dev_data->overflow = true;
168+
break;
169+
151170
default:
152171
LOG_ERR("unhandled event (0x%x)", event.type);
153172
break;

0 commit comments

Comments
 (0)