Skip to content

Commit c014ac3

Browse files
authored
Reworked check for input taking too long
1 parent 6b93f97 commit c014ac3

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static uint8_t refcount = 0;
5151
static uint8_t pulsein_tc_index = 0xff;
5252

5353
volatile static uint32_t overflow_count = 0;
54+
volatile static uint32_t start_overflow = 0;
5455

5556
void pulsein_timer_interrupt_handler(uint8_t index) {
5657
if (index != pulsein_tc_index) return;
@@ -91,6 +92,9 @@ void pulsein_interrupt_handler(uint8_t channel) {
9192
uint32_t current_count = tc->COUNT16.COUNT.reg;
9293

9394
pulseio_pulsein_obj_t* self = get_eic_channel_data(channel);
95+
if (self->len == 0 ) {
96+
start_overflow = overflow_count;
97+
}
9498
if (self->first_edge) {
9599
self->first_edge = false;
96100
pulsein_set_config(self, false);
@@ -110,16 +114,21 @@ void pulsein_interrupt_handler(uint8_t channel) {
110114
if (total_diff < duration) {
111115
duration = total_diff;
112116
}
117+
//check if the input is taking too long, 15 timer overflows is approx 1 second
118+
if (current_overflow - start_overflow > 15) {
119+
self->errored_too_fast = true;
120+
common_hal_pulseio_pulsein_pause(self);
121+
common_hal_mcu_enable_interrupts();
122+
return;
123+
}
113124

114125
uint16_t i = (self->start + self->len) % self->maxlen;
126+
self->buffer[i] = duration;
115127
if (self->len < self->maxlen) {
116128
self->len++;
117129
} else {
118-
self->errored_too_fast = true;
119-
common_hal_mcu_enable_interrupts();
120-
return;
130+
self->start++;
121131
}
122-
self->buffer[i] = duration;
123132
}
124133
self->last_overflow = current_overflow;
125134
self->last_count = current_count;
@@ -300,6 +309,7 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
300309
mp_raise_IndexError(translate("pop from an empty PulseIn"));
301310
}
302311
if (self->errored_too_fast) {
312+
self->errored_too_fast = 0;
303313
mp_raise_RuntimeError(translate("Input taking too long"));
304314
}
305315
common_hal_mcu_disable_interrupts();

0 commit comments

Comments
 (0)