Skip to content

Commit 9998680

Browse files
committed
Fix samd21 and rp2
1 parent 777d79a commit 9998680

File tree

6 files changed

+27
-34
lines changed

6 files changed

+27
-34
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "shared-bindings/microcontroller/__init__.h"
4444
#include "shared-bindings/microcontroller/Pin.h"
4545
#include "shared-bindings/pulseio/PulseIn.h"
46+
#include "supervisor/samd_prevent_sleep.h"
4647
#include "supervisor/shared/tick.h"
4748
#include "supervisor/port.h"
4849

@@ -232,7 +233,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
232233
// Set config will enable the EIC.
233234
pulsein_set_config(self, true);
234235
#ifdef SAMD21
235-
rtc_start_pulse();
236+
samd_prevent_sleep();
236237
#endif
237238
}
238239

@@ -245,7 +246,7 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
245246
return;
246247
}
247248
#ifdef SAMD21
248-
rtc_end_pulse();
249+
samd_allow_sleep();
249250
#endif
250251
set_eic_handler(self->channel, EIC_HANDLER_NO_INTERRUPT);
251252
turn_off_eic_channel(self->channel);
@@ -263,7 +264,7 @@ void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) {
263264
uint32_t mask = 1 << self->channel;
264265
EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos;
265266
#ifdef SAMD21
266-
rtc_end_pulse();
267+
samd_allow_sleep();
267268
#endif
268269
}
269270

@@ -293,7 +294,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
293294
EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos;
294295

295296
#ifdef SAMD21
296-
rtc_start_pulse();
297+
samd_prevent_sleep();
297298
#endif
298299
pulsein_set_config(self, true);
299300
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H
27+
#pragma once
2928

3029
#include "common-hal/microcontroller/Pin.h"
3130

@@ -48,10 +47,3 @@ typedef struct {
4847

4948
void pulsein_interrupt_handler(uint8_t channel);
5049
void pulsein_timer_interrupt_handler(uint8_t index);
51-
#ifdef SAMD21
52-
void rtc_start_pulse(void);
53-
void rtc_end_pulse(void);
54-
#endif
55-
56-
57-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "py/gc.h"
3737
#include "py/runtime.h"
3838
#include "shared-bindings/pulseio/PulseOut.h"
39+
#include "supervisor/samd_prevent_sleep.h"
3940
#include "timer_handler.h"
4041

4142
// This timer is shared amongst all PulseOut objects under the assumption that
@@ -159,9 +160,8 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
159160
// Turn off the pinmux which should connect the port output.
160161
turn_off(self->pincfg);
161162
#ifdef SAMD21
162-
rtc_start_pulse();
163+
samd_prevent_sleep();
163164
#endif
164-
165165
}
166166

167167
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) {
@@ -185,7 +185,7 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
185185
self->pin = NO_PIN;
186186
common_hal_pwmio_pwmout_deinit(&self->pwmout);
187187
#ifdef SAMD21
188-
rtc_end_pulse();
188+
samd_allow_sleep();
189189
#endif
190190
}
191191

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H
27+
#pragma once
2928

3029
#include "common-hal/microcontroller/Pin.h"
3130

@@ -41,9 +40,3 @@ typedef struct {
4140

4241
void pulseout_reset(void);
4342
void pulseout_interrupt_handler(uint8_t index);
44-
#ifdef SAMD21
45-
void rtc_start_pulse(void);
46-
void rtc_end_pulse(void);
47-
#endif
48-
49-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H

ports/atmel-samd/supervisor/port.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "supervisor/board.h"
3131
#include "supervisor/port.h"
3232

33+
#include "supervisor/samd_prevent_sleep.h"
34+
3335
// ASF 4
3436
#include "atmel_start_pins.h"
3537
#include "peripheral_clk_config.h"
@@ -114,7 +116,7 @@
114116
#if CIRCUITPY_PEW
115117
#include "common-hal/_pew/PewPew.h"
116118
#endif
117-
static volatile bool sleep_ok = true;
119+
static volatile size_t sleep_disable_count = 0;
118120

119121
#ifdef SAMD21
120122
static uint8_t _tick_event_channel = EVSYS_SYNCH_NUM;
@@ -126,12 +128,16 @@ static bool tick_enabled(void) {
126128
// Sleeping requires a register write that can stall interrupt handling. Turning
127129
// off sleeps allows for more accurate interrupt timing. (Python still thinks
128130
// it is sleeping though.)
129-
void rtc_start_pulse(void) {
130-
sleep_ok = false;
131+
void samd_prevent_sleep(void) {
132+
sleep_disable_count++;
131133
}
132134

133-
void rtc_end_pulse(void) {
134-
sleep_ok = true;
135+
void samd_allow_sleep(void) {
136+
if (sleep_disable_count == 0) {
137+
// We should never reach this!
138+
return;
139+
}
140+
sleep_disable_count--;
135141
}
136142
#endif // SAMD21
137143

@@ -651,7 +657,11 @@ void port_interrupt_after_ticks(uint32_t ticks) {
651657
return;
652658
}
653659
#ifdef SAMD21
654-
if (!sleep_ok) {
660+
if (sleep_disable_count > 0) {
661+
// "wake" immediately even if sleep_disable_count is set to 0 between
662+
// now and when port_idle_until_interrupt is called. Otherwise we may
663+
// sleep too long.
664+
_woken_up = true;
655665
return;
656666
}
657667
#endif
@@ -687,7 +697,7 @@ void port_idle_until_interrupt(void) {
687697
}
688698
#endif
689699
common_hal_mcu_disable_interrupts();
690-
if (!background_callback_pending() && sleep_ok && !_woken_up) {
700+
if (!background_callback_pending() && sleep_disable_count == 0 && !_woken_up) {
691701
__DSB();
692702
__WFI();
693703
}

ports/raspberrypi/common-hal/picodvi/Framebuffer.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,6 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self,
254254
}
255255
self->pwm_slice = slice;
256256

257-
pwmout_never_reset(self->pwm_slice, 0);
258-
pwmout_never_reset(self->pwm_slice, 1);
259-
260257
for (size_t i = 0; i < 4; i++) {
261258
never_reset_pin_number(self->pin_pair[i]);
262259
never_reset_pin_number(self->pin_pair[i] + 1);

0 commit comments

Comments
 (0)