Skip to content

Commit c7b6e66

Browse files
committed
Fixes for pulsein tick handling
1 parent d7eedc5 commit c7b6e66

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "shared-bindings/pulseio/PulseIn.h"
4545
#include "supervisor/shared/tick.h"
4646
#include "supervisor/shared/translate.h"
47+
#include "supervisor/port.h"
4748

4849
// This timer is shared amongst all PulseIn objects as a higher resolution clock.
4950
static uint8_t refcount = 0;
@@ -77,7 +78,7 @@ static void pulsein_set_config(pulseio_pulsein_obj_t* self, bool first_edge) {
7778
}
7879

7980
void pulsein_interrupt_handler(uint8_t channel) {
80-
// turn off interrupts while in the handler
81+
// Turn off interrupts while in handler
8182
common_hal_mcu_disable_interrupts();
8283
// Grab the current time first.
8384
uint32_t current_overflow = overflow_count;
@@ -90,10 +91,8 @@ void pulsein_interrupt_handler(uint8_t channel) {
9091
uint32_t current_count = tc->COUNT16.COUNT.reg;
9192

9293
pulseio_pulsein_obj_t* self = get_eic_channel_data(channel);
93-
if (!supervisor_background_tasks_ok() ) {
94-
common_hal_mcu_enable_interrupts();
95-
mp_raise_RuntimeError(translate("Input taking too long"));
96-
return;
94+
if (self->len == 0) {
95+
update_background_ticks();
9796
}
9897
if (self->first_edge) {
9998
self->first_edge = false;
@@ -123,6 +122,11 @@ void pulsein_interrupt_handler(uint8_t channel) {
123122
self->start++;
124123
}
125124
}
125+
if (!supervisor_background_tasks_ok() ) {
126+
common_hal_mcu_enable_interrupts();
127+
mp_raise_RuntimeError(translate("Input taking too long"));
128+
return;
129+
}
126130
self->last_overflow = current_overflow;
127131
self->last_count = current_count;
128132
common_hal_mcu_enable_interrupts();
@@ -304,7 +308,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
304308
self->start = (self->start + 1) % self->maxlen;
305309
self->len--;
306310
common_hal_mcu_enable_interrupts();
307-
308311
return value;
309312
}
310313

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void pulsein_reset(void);
5050

5151
void pulsein_interrupt_handler(uint8_t channel);
5252
void pulsein_timer_interrupt_handler(uint8_t index);
53+
void update_background_ticks(void);
5354
#ifdef SAMD21
5455
void rtc_set_continuous(void);
5556
void rtc_start_pulsein(void);

supervisor/shared/background_callback.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ STATIC volatile background_callback_t *callback_head, *callback_tail;
3737
#define CALLBACK_CRITICAL_BEGIN (common_hal_mcu_disable_interrupts())
3838
#define CALLBACK_CRITICAL_END (common_hal_mcu_enable_interrupts())
3939

40-
volatile uint64_t last_background_tick = 0;
40+
uint64_t last_background_tick = 0;
4141

4242
uint64_t get_background_ticks(void) {
4343
return last_background_tick;
4444
}
4545

46+
void update_background_ticks(void) {
47+
last_background_tick = port_get_raw_ticks(NULL);
48+
}
49+
4650
void background_callback_add_core(background_callback_t *cb) {
4751
CALLBACK_CRITICAL_BEGIN;
4852
if (cb->prev || callback_head == cb) {

0 commit comments

Comments
 (0)