Commit 2e6c83dd authored by Michał Stasiak's avatar Michał Stasiak Committed by Anas Nashif
Browse files

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: default avatarMichał Stasiak <michal.stasiak@nordicsemi.no>
parent 79222c9c
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -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;
};
@@ -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);
@@ -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;
}

@@ -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;
@@ -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;