Skip to content

Commit 6eae7ce

Browse files
committed
Requested changes to pulsein
1 parent 333a10d commit 6eae7ce

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

ports/atmel-samd/common-hal/pulseio/PulseIn.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ void pulsein_interrupt_handler(uint8_t channel) {
9191
uint32_t current_count = tc->COUNT16.COUNT.reg;
9292

9393
pulseio_pulsein_obj_t* self = get_eic_channel_data(channel);
94-
if (self->len == 0) {
95-
update_background_ticks();
96-
}
9794
if (self->first_edge) {
9895
self->first_edge = false;
9996
pulsein_set_config(self, false);
@@ -115,17 +112,14 @@ void pulsein_interrupt_handler(uint8_t channel) {
115112
}
116113

117114
uint16_t i = (self->start + self->len) % self->maxlen;
118-
self->buffer[i] = duration;
119-
if (self->len < self->maxlen) {
115+
if (self->len <= self->maxlen) {
120116
self->len++;
121117
} else {
122-
self->start++;
118+
self->errored_too_fast = true;
119+
common_hal_mcu_enable_interrupts();
120+
return;
123121
}
124-
}
125-
if (!supervisor_background_tasks_ok() ) {
126-
common_hal_mcu_enable_interrupts();
127-
mp_raise_RuntimeError(translate("Input taking too long"));
128-
return;
122+
self->buffer[i] = duration;
129123
}
130124
self->last_overflow = current_overflow;
131125
self->last_count = current_count;
@@ -161,6 +155,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
161155
self->start = 0;
162156
self->len = 0;
163157
self->first_edge = true;
158+
self->errored_too_fast = false;
164159

165160
if (refcount == 0) {
166161
// Find a spare timer.
@@ -303,6 +298,10 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
303298
if (self->len == 0) {
304299
mp_raise_IndexError(translate("pop from an empty PulseIn"));
305300
}
301+
if (self->errored_too_fast) {
302+
self->errored_too_fast = false;
303+
mp_raise_RuntimeError(translate("Input taking too long"));
304+
}
306305
common_hal_mcu_disable_interrupts();
307306
uint16_t value = self->buffer[self->start];
308307
self->start = (self->start + 1) % self->maxlen;

supervisor/shared/background_callback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void update_background_ticks(void) {
4848
}
4949

5050
void background_callback_add_core(background_callback_t *cb) {
51+
last_background_tick = port_get_raw_ticks(NULL);
5152
CALLBACK_CRITICAL_BEGIN;
5253
if (cb->prev || callback_head == cb) {
5354
CALLBACK_CRITICAL_END;
@@ -77,7 +78,6 @@ void background_callback_run_all() {
7778
if (!callback_head) {
7879
return;
7980
}
80-
last_background_tick = port_get_raw_ticks(NULL);
8181
CALLBACK_CRITICAL_BEGIN;
8282
if (in_background_callback) {
8383
CALLBACK_CRITICAL_END;

0 commit comments

Comments
 (0)