Skip to content

Commit 4be904f

Browse files
committed
Switch to gpio mux to disable/enable pwm signal
1 parent e87e1d8 commit 4be904f

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,24 @@
3535
#include "common-hal/pwmio/PWMOut.h"
3636
#include "supervisor/shared/translate.h"
3737
#include "src/rp2040/hardware_structs/include/hardware/structs/pwm.h"
38+
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
3839
#include "src/rp2_common/hardware_pwm/include/hardware/pwm.h"
3940
#include "src/common/pico_time/include/pico/time.h"
4041

4142
volatile alarm_id_t cur_alarm = 0;
4243

4344
void pulse_finish(pulseio_pulseout_obj_t *self) {
4445
self->pulse_index++;
45-
// Turn pwm pin off by setting duty cyle to 0.
46-
common_hal_pwmio_pwmout_set_duty_cycle(&self->carrier, 0);
46+
// Turn pwm pin off by switching the GPIO mux to SIO (the cpu manual
47+
// control).
4748
if (self->pulse_index >= self->pulse_length) {
49+
gpio_set_function(self->pin, GPIO_FUNC_SIO);
4850
return;
4951
}
5052
if (self->pulse_index % 2 == 0) {
51-
common_hal_pwmio_pwmout_set_duty_cycle(&self->carrier, self->current_duty_cycle);
53+
gpio_set_function(self->pin, GPIO_FUNC_PWM);
54+
} else {
55+
gpio_set_function(self->pin, GPIO_FUNC_SIO);
5256
}
5357
uint64_t delay = self->pulse_buffer[self->pulse_index];
5458
if (delay < self->min_pulse) {
@@ -77,12 +81,14 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
7781

7882
pwmout_result_t result = common_hal_pwmio_pwmout_construct(
7983
&self->carrier, pin, 0, frequency, false);
80-
8184
// This will raise an exception and not return if needed.
8285
common_hal_pwmio_pwmout_raise_error(result);
8386

84-
self->current_duty_cycle = duty_cycle;
85-
self->pin = self->carrier.pin->number;
87+
// Disable gpio output before we set the duty cycle.
88+
gpio_set_function(pin->number, GPIO_FUNC_SIO);
89+
common_hal_pwmio_pwmout_set_duty_cycle(&self->carrier, duty_cycle);
90+
91+
self->pin = pin->number;
8692
self->slice = self->carrier.slice;
8793
self->min_pulse = (1000000 / self->carrier.actual_frequency);
8894
}
@@ -104,7 +110,8 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
104110
self->pulse_index = 0;
105111
self->pulse_length = length;
106112

107-
common_hal_pwmio_pwmout_set_duty_cycle(&self->carrier, self->current_duty_cycle);
113+
// Turn on the signal by connecting the PWM to the outside pin.
114+
gpio_set_function(self->pin, GPIO_FUNC_PWM);
108115
uint64_t delay = self->pulse_buffer[0];
109116
if (delay < self->min_pulse) {
110117
delay = self->min_pulse;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ typedef struct {
4242
pwmio_pwmout_obj_t carrier;
4343
uint16_t *pulse_buffer;
4444
uint16_t pulse_length;
45-
uint16_t current_duty_cycle;
4645
uint32_t min_pulse;
4746
volatile uint16_t pulse_index;
4847
} pulseio_pulseout_obj_t;

0 commit comments

Comments
 (0)