@@ -51,6 +51,7 @@ static uint8_t refcount = 0;
51
51
static uint8_t pulsein_tc_index = 0xff ;
52
52
53
53
volatile static uint32_t overflow_count = 0 ;
54
+ volatile static uint32_t start_overflow = 0 ;
54
55
55
56
void pulsein_timer_interrupt_handler (uint8_t index ) {
56
57
if (index != pulsein_tc_index ) return ;
@@ -91,6 +92,9 @@ void pulsein_interrupt_handler(uint8_t channel) {
91
92
uint32_t current_count = tc -> COUNT16 .COUNT .reg ;
92
93
93
94
pulseio_pulsein_obj_t * self = get_eic_channel_data (channel );
95
+ if (self -> len == 0 ) {
96
+ start_overflow = overflow_count ;
97
+ }
94
98
if (self -> first_edge ) {
95
99
self -> first_edge = false;
96
100
pulsein_set_config (self , false);
@@ -110,16 +114,21 @@ void pulsein_interrupt_handler(uint8_t channel) {
110
114
if (total_diff < duration ) {
111
115
duration = total_diff ;
112
116
}
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
+ }
113
124
114
125
uint16_t i = (self -> start + self -> len ) % self -> maxlen ;
126
+ self -> buffer [i ] = duration ;
115
127
if (self -> len < self -> maxlen ) {
116
128
self -> len ++ ;
117
129
} else {
118
- self -> errored_too_fast = true;
119
- common_hal_mcu_enable_interrupts ();
120
- return ;
130
+ self -> start ++ ;
121
131
}
122
- self -> buffer [i ] = duration ;
123
132
}
124
133
self -> last_overflow = current_overflow ;
125
134
self -> last_count = current_count ;
@@ -300,6 +309,7 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
300
309
mp_raise_IndexError (translate ("pop from an empty PulseIn" ));
301
310
}
302
311
if (self -> errored_too_fast ) {
312
+ self -> errored_too_fast = 0 ;
303
313
mp_raise_RuntimeError (translate ("Input taking too long" ));
304
314
}
305
315
common_hal_mcu_disable_interrupts ();
0 commit comments