Skip to content

Commit 0ea750b

Browse files
committed
Fix handling of short pulses
1 parent f98ec0c commit 0ea750b

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,30 @@ volatile uint16_t current_duty_cycle;
4646
static uint32_t min_pulse = 0;
4747
static alarm_id_t cur_alarm;
4848

49-
void pulse_finish(void) {
49+
void pulse_finish(pwmio_pwmout_obj_t *carrier) {
5050
pulse_index++;
51-
// Turn off the current alarm
52-
cancel_alarm(cur_alarm);
5351
// Turn pwm pin off by setting duty cyle to 1.
54-
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,1);
52+
common_hal_pwmio_pwmout_set_duty_cycle(carrier,1);
5553
if (pulse_index >= pulse_length) {
5654
return;
5755
}
56+
if (pulse_index % 2 == 0) {
57+
common_hal_pwmio_pwmout_set_duty_cycle(carrier,current_duty_cycle);
58+
}
5859
uint64_t delay = pulse_buffer[pulse_index];
5960
if (delay < min_pulse) {
6061
delay = min_pulse;
6162
}
6263
cur_alarm = 0;
6364
// if the alarm cannot be set, try again with a longer delay
6465
while (cur_alarm == 0) {
65-
cur_alarm = add_alarm_in_us(delay, pulseout_interrupt_handler, NULL, false);
66+
cur_alarm = add_alarm_in_us(delay, pulseout_interrupt_handler, carrier, false);
6667
delay = delay + 1;
6768
}
68-
if (pulse_index % 2 == 0) {
69-
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,current_duty_cycle);
70-
}
7169
}
7270

7371
int64_t pulseout_interrupt_handler(alarm_id_t id, void *user_data) {
74-
pulse_finish();
72+
pulse_finish(user_data);
7573
return 0;
7674
}
7775

@@ -88,10 +86,12 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
8886
refcount++;
8987
pwmout_obj = (pwmio_pwmout_obj_t *)carrier;
9088
current_duty_cycle = common_hal_pwmio_pwmout_get_duty_cycle(pwmout_obj);
89+
pwm_set_enabled(carrier->slice,false);
90+
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,1);
9191
self->pin = carrier->pin->number;
9292
self->slice = carrier->slice;
93-
pwm_set_enabled(pwmout_obj->slice,false);
94-
min_pulse = (1000000 / pwmout_obj->actual_frequency) / 2;
93+
self->carrier = pwmout_obj;
94+
min_pulse = (1000000 / carrier->actual_frequency) / 2;
9595
}
9696

9797
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) {
@@ -111,9 +111,19 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
111111
pulse_index = 0;
112112
pulse_length = length;
113113

114-
add_alarm_in_us(pulses[0], pulseout_interrupt_handler, NULL, false);
115-
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,current_duty_cycle);
116-
pwm_set_enabled(pwmout_obj->slice,true);
114+
common_hal_pwmio_pwmout_set_duty_cycle(self->carrier,current_duty_cycle);
115+
pwm_set_enabled(self->slice,true);
116+
uint64_t delay = pulse_buffer[0];
117+
if (delay < min_pulse) {
118+
delay = min_pulse;
119+
}
120+
alarm_id_t init_alarm = 0;
121+
// if the alarm cannot be set, try again with a longer delay
122+
while (init_alarm == 0) {
123+
init_alarm = add_alarm_in_us(delay, pulseout_interrupt_handler, self->carrier, false);
124+
delay = delay + 1;
125+
}
126+
cur_alarm = init_alarm;
117127

118128
while (pulse_index < length) {
119129
// Do other things while we wait. The interrupts will handle sending the
@@ -122,5 +132,5 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
122132
}
123133
// Short delay to give pin time to settle before disabling PWM
124134
common_hal_mcu_delay_us(min_pulse);
125-
pwm_set_enabled(pwmout_obj->slice,false);
135+
pwm_set_enabled(self->slice,false);
126136
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H
2929

3030
#include "common-hal/microcontroller/Pin.h"
31+
#include "common-hal/pwmio/PWMOut.h"
3132
#include "src/common/pico_time/include/pico/time.h"
3233

3334
#include "py/obj.h"
@@ -38,9 +39,10 @@ typedef struct {
3839
mp_obj_base_t base;
3940
uint8_t pin;
4041
uint8_t slice;
42+
pwmio_pwmout_obj_t *carrier;
4143
} pulseio_pulseout_obj_t;
4244

4345
void pulseout_reset(void);
4446
int64_t pulseout_interrupt_handler(alarm_id_t id, void *user_data);
4547

46-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H
48+
#endif // MICROPY_INCLUDED_ATMEL SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H

0 commit comments

Comments
 (0)