|
31 | 31 | #include "atmel_start_pins.h" |
32 | 32 | #include "hal/include/hal_gpio.h" |
33 | 33 |
|
| 34 | +#include "background.h" |
34 | 35 | #include "mpconfigport.h" |
35 | 36 | #include "py/gc.h" |
36 | 37 | #include "py/runtime.h" |
@@ -60,10 +61,16 @@ void pulsein_interrupt_handler(uint8_t channel) { |
60 | 61 | uint32_t current_us; |
61 | 62 | uint64_t current_ms; |
62 | 63 | current_tick(¤t_ms, ¤t_us); |
| 64 | + |
63 | 65 | // current_tick gives us the remaining us until the next tick but we want the number since the |
64 | 66 | // last ms. |
65 | 67 | current_us = 1000 - current_us; |
66 | 68 | pulseio_pulsein_obj_t* self = get_eic_channel_data(channel); |
| 69 | + if (!background_tasks_ok() || self->errored_too_fast) { |
| 70 | + self->errored_too_fast = true; |
| 71 | + common_hal_pulseio_pulsein_pause(self); |
| 72 | + return; |
| 73 | + } |
67 | 74 | if (self->first_edge) { |
68 | 75 | self->first_edge = false; |
69 | 76 | pulsein_set_config(self, false); |
@@ -118,6 +125,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, |
118 | 125 | self->first_edge = true; |
119 | 126 | self->last_us = 0; |
120 | 127 | self->last_ms = 0; |
| 128 | + self->errored_too_fast = 0; |
121 | 129 |
|
122 | 130 | set_eic_channel_data(pin->extint_channel, (void*) self); |
123 | 131 |
|
@@ -157,6 +165,9 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, |
157 | 165 | // Make sure we're paused. |
158 | 166 | common_hal_pulseio_pulsein_pause(self); |
159 | 167 |
|
| 168 | + // Reset erroring |
| 169 | + self->errored_too_fast = false; |
| 170 | + |
160 | 171 | // Send the trigger pulse. |
161 | 172 | if (trigger_duration > 0) { |
162 | 173 | gpio_set_pin_pull_mode(self->pin, GPIO_PULL_OFF); |
@@ -207,6 +218,11 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) { |
207 | 218 | return self->len; |
208 | 219 | } |
209 | 220 |
|
| 221 | +bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) { |
| 222 | + uint32_t mask = 1 << self->channel; |
| 223 | + return (EIC->INTENSET.reg & (mask << EIC_INTENSET_EXTINT_Pos)) == 0; |
| 224 | +} |
| 225 | + |
210 | 226 | uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, |
211 | 227 | int16_t index) { |
212 | 228 | common_hal_mcu_disable_interrupts(); |
|
0 commit comments