Requested changes to pulsein

This commit is contained in:
root 2020-08-20 16:57:00 -05:00
parent 333a10deda
commit 6eae7ce78f
2 changed files with 11 additions and 12 deletions

View File

@ -91,9 +91,6 @@ void pulsein_interrupt_handler(uint8_t channel) {
uint32_t current_count = tc->COUNT16.COUNT.reg; uint32_t current_count = tc->COUNT16.COUNT.reg;
pulseio_pulsein_obj_t* self = get_eic_channel_data(channel); pulseio_pulsein_obj_t* self = get_eic_channel_data(channel);
if (self->len == 0) {
update_background_ticks();
}
if (self->first_edge) { if (self->first_edge) {
self->first_edge = false; self->first_edge = false;
pulsein_set_config(self, false); pulsein_set_config(self, false);
@ -115,17 +112,14 @@ void pulsein_interrupt_handler(uint8_t channel) {
} }
uint16_t i = (self->start + self->len) % self->maxlen; uint16_t i = (self->start + self->len) % self->maxlen;
self->buffer[i] = duration; if (self->len <= self->maxlen) {
if (self->len < self->maxlen) {
self->len++; self->len++;
} else { } else {
self->start++; self->errored_too_fast = true;
common_hal_mcu_enable_interrupts();
return;
} }
} self->buffer[i] = duration;
if (!supervisor_background_tasks_ok() ) {
common_hal_mcu_enable_interrupts();
mp_raise_RuntimeError(translate("Input taking too long"));
return;
} }
self->last_overflow = current_overflow; self->last_overflow = current_overflow;
self->last_count = current_count; self->last_count = current_count;
@ -161,6 +155,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
self->start = 0; self->start = 0;
self->len = 0; self->len = 0;
self->first_edge = true; self->first_edge = true;
self->errored_too_fast = false;
if (refcount == 0) { if (refcount == 0) {
// Find a spare timer. // Find a spare timer.
@ -303,6 +298,10 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
if (self->len == 0) { if (self->len == 0) {
mp_raise_IndexError(translate("pop from an empty PulseIn")); mp_raise_IndexError(translate("pop from an empty PulseIn"));
} }
if (self->errored_too_fast) {
self->errored_too_fast = false;
mp_raise_RuntimeError(translate("Input taking too long"));
}
common_hal_mcu_disable_interrupts(); common_hal_mcu_disable_interrupts();
uint16_t value = self->buffer[self->start]; uint16_t value = self->buffer[self->start];
self->start = (self->start + 1) % self->maxlen; self->start = (self->start + 1) % self->maxlen;

View File

@ -48,6 +48,7 @@ void update_background_ticks(void) {
} }
void background_callback_add_core(background_callback_t *cb) { void background_callback_add_core(background_callback_t *cb) {
last_background_tick = port_get_raw_ticks(NULL);
CALLBACK_CRITICAL_BEGIN; CALLBACK_CRITICAL_BEGIN;
if (cb->prev || callback_head == cb) { if (cb->prev || callback_head == cb) {
CALLBACK_CRITICAL_END; CALLBACK_CRITICAL_END;
@ -77,7 +78,6 @@ void background_callback_run_all() {
if (!callback_head) { if (!callback_head) {
return; return;
} }
last_background_tick = port_get_raw_ticks(NULL);
CALLBACK_CRITICAL_BEGIN; CALLBACK_CRITICAL_BEGIN;
if (in_background_callback) { if (in_background_callback) {
CALLBACK_CRITICAL_END; CALLBACK_CRITICAL_END;