Skip to content

Commit 965a496

Browse files
authored
Merge pull request #3120 from DavePutz/issue#3049
Issue#3049 -PulseIn not working in CP5.4
2 parents a784420 + 0932b64 commit 965a496

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ void pulsein_interrupt_handler(uint8_t channel) {
125125
}
126126

127127
void pulsein_reset() {
128+
#ifdef SAMD21
129+
rtc_end_pulsein();
130+
#endif
128131
refcount = 0;
129132
pulsein_tc_index = 0xff;
130133
overflow_count = 0;
@@ -221,6 +224,10 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
221224

222225
// Set config will enable the EIC.
223226
pulsein_set_config(self, true);
227+
#ifdef SAMD21
228+
rtc_start_pulsein();
229+
#endif
230+
224231
}
225232

226233
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
@@ -231,6 +238,9 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
231238
if (common_hal_pulseio_pulsein_deinited(self)) {
232239
return;
233240
}
241+
#ifdef SAMD21
242+
rtc_end_pulsein();
243+
#endif
234244
set_eic_handler(self->channel, EIC_HANDLER_NO_INTERRUPT);
235245
turn_off_eic_channel(self->channel);
236246
reset_pin_number(self->pin);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,11 @@ void pulsein_reset(void);
5050

5151
void pulsein_interrupt_handler(uint8_t channel);
5252
void pulsein_timer_interrupt_handler(uint8_t index);
53+
#ifdef SAMD21
54+
void rtc_set_continuous(void);
55+
void rtc_start_pulsein(void);
56+
void rtc_end_pulsein(void);
57+
#endif
58+
5359

5460
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H

ports/atmel-samd/supervisor/port.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@
9191
#if CIRCUITPY_PEW
9292
#include "common-hal/_pew/PewPew.h"
9393
#endif
94+
volatile bool hold_interrupt = false;
95+
#ifdef SAMD21
96+
void rtc_start_pulsein(void) {
97+
rtc_set_continuous();
98+
hold_interrupt = true;
99+
}
100+
101+
void rtc_end_pulsein(void) {
102+
hold_interrupt = false;
103+
}
104+
105+
void rtc_set_continuous(void) {
106+
while (RTC->MODE0.STATUS.bit.SYNCBUSY);
107+
RTC->MODE0.READREQ.reg = RTC_READREQ_RREQ | RTC_READREQ_RCONT | 0x0010;
108+
while (RTC->MODE0.STATUS.bit.SYNCBUSY);
109+
}
110+
#endif
94111

95112
extern volatile bool mp_msc_enabled;
96113

@@ -489,6 +506,11 @@ void port_interrupt_after_ticks(uint32_t ticks) {
489506
// We'll interrupt sooner with an overflow.
490507
return;
491508
}
509+
#ifdef SAMD21
510+
if (hold_interrupt) {
511+
return;
512+
}
513+
#endif
492514
RTC->MODE0.COMP[0].reg = current_ticks + (ticks << 4);
493515
RTC->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_CMP0;
494516
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_CMP0;
@@ -503,7 +525,7 @@ void port_sleep_until_interrupt(void) {
503525
}
504526
#endif
505527
common_hal_mcu_disable_interrupts();
506-
if (!tud_task_event_ready()) {
528+
if (!tud_task_event_ready() && !hold_interrupt) {
507529
__DSB();
508530
__WFI();
509531
}

0 commit comments

Comments
 (0)