Skip to content

Commit

Permalink
[nrf fromtree] drivers: sensor: qdec: fix QDEC overflow handling
Browse files Browse the repository at this point in the history
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]>
(cherry picked from commit 2e6c83d)
  • Loading branch information
mstasiaknordic committed Oct 21, 2024
1 parent 8d6fc59 commit 16bfb18
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions drivers/sensor/nordic/qdec_nrfx/qdec_nrfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ LOG_MODULE_REGISTER(qdec_nrfx, CONFIG_SENSOR_LOG_LEVEL);


struct qdec_nrfx_data {
int32_t fetched_acc;
int32_t acc;
bool overflow;
sensor_trigger_handler_t data_ready_handler;
const struct sensor_trigger *data_ready_trigger;
};
Expand All @@ -49,6 +51,8 @@ static void accumulate(struct qdec_nrfx_data *data, int32_t acc)

if (!overflow) {
data->acc += acc;
} else {
data->overflow = true;
}

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

accumulate(data, acc);

unsigned int key = irq_lock();

data->fetched_acc = data->acc;
data->acc = 0;

irq_unlock(key);

if (data->overflow) {
data->overflow = false;
return -EOVERFLOW;
}

return 0;
}

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

key = irq_lock();
acc = data->acc;
data->acc = 0;
acc = data->fetched_acc;
irq_unlock(key);

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

case NRF_QDEC_EVENT_ACCOF:
dev_data->overflow = true;
break;

default:
LOG_ERR("unhandled event (0x%x)", event.type);
break;
Expand Down

0 comments on commit 16bfb18

Please sign in to comment.