Skip to content

Commit bfa777c

Browse files
committed
Add check for timer success
1 parent b2e72b2 commit bfa777c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

ports/raspberrypi/common-hal/pulseio/PulseOut.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ static uint16_t pulse_length;
4444
pwmio_pwmout_obj_t *pwmout_obj;
4545
volatile uint16_t current_duty_cycle;
4646
static uint32_t min_pulse = 0;
47+
static alarm_id_t cur_alarm;
4748

4849
void pulse_finish(void) {
4950
pulse_index++;
51+
// Turn off the current alarm
52+
cancel_alarm(cur_alarm);
5053
// Turn pwm pin off by setting duty cyle to 1.
5154
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,1);
5255
if (pulse_index >= pulse_length) {
@@ -56,8 +59,12 @@ void pulse_finish(void) {
5659
if (delay < min_pulse) {
5760
delay = min_pulse;
5861
}
59-
60-
add_alarm_in_us(delay, pulseout_interrupt_handler, NULL, false);
62+
cur_alarm = 0;
63+
// if the alarm cannot be set, try again with a longer delay
64+
while (cur_alarm == 0) {
65+
cur_alarm = add_alarm_in_us(delay, pulseout_interrupt_handler, NULL, false);
66+
delay = delay + 1;
67+
}
6168
if (pulse_index % 2 == 0) {
6269
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,current_duty_cycle);
6370
}

0 commit comments

Comments
 (0)