Switch to gpio mux to disable/enable pwm signal

This commit is contained in:
Scott Shawcroft 2021-07-27 12:38:18 -07:00
parent e87e1d8175
commit 4be904fd4a
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
2 changed files with 14 additions and 8 deletions

View File

@ -35,6 +35,7 @@
#include "common-hal/pwmio/PWMOut.h" #include "common-hal/pwmio/PWMOut.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
#include "src/rp2040/hardware_structs/include/hardware/structs/pwm.h" #include "src/rp2040/hardware_structs/include/hardware/structs/pwm.h"
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
#include "src/rp2_common/hardware_pwm/include/hardware/pwm.h" #include "src/rp2_common/hardware_pwm/include/hardware/pwm.h"
#include "src/common/pico_time/include/pico/time.h" #include "src/common/pico_time/include/pico/time.h"
@ -42,13 +43,16 @@ volatile alarm_id_t cur_alarm = 0;
void pulse_finish(pulseio_pulseout_obj_t *self) { void pulse_finish(pulseio_pulseout_obj_t *self) {
self->pulse_index++; self->pulse_index++;
// Turn pwm pin off by setting duty cyle to 0. // Turn pwm pin off by switching the GPIO mux to SIO (the cpu manual
common_hal_pwmio_pwmout_set_duty_cycle(&self->carrier, 0); // control).
if (self->pulse_index >= self->pulse_length) { if (self->pulse_index >= self->pulse_length) {
gpio_set_function(self->pin, GPIO_FUNC_SIO);
return; return;
} }
if (self->pulse_index % 2 == 0) { if (self->pulse_index % 2 == 0) {
common_hal_pwmio_pwmout_set_duty_cycle(&self->carrier, self->current_duty_cycle); gpio_set_function(self->pin, GPIO_FUNC_PWM);
} else {
gpio_set_function(self->pin, GPIO_FUNC_SIO);
} }
uint64_t delay = self->pulse_buffer[self->pulse_index]; uint64_t delay = self->pulse_buffer[self->pulse_index];
if (delay < self->min_pulse) { if (delay < self->min_pulse) {
@ -77,12 +81,14 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
pwmout_result_t result = common_hal_pwmio_pwmout_construct( pwmout_result_t result = common_hal_pwmio_pwmout_construct(
&self->carrier, pin, 0, frequency, false); &self->carrier, pin, 0, frequency, false);
// This will raise an exception and not return if needed. // This will raise an exception and not return if needed.
common_hal_pwmio_pwmout_raise_error(result); common_hal_pwmio_pwmout_raise_error(result);
self->current_duty_cycle = duty_cycle; // Disable gpio output before we set the duty cycle.
self->pin = self->carrier.pin->number; gpio_set_function(pin->number, GPIO_FUNC_SIO);
common_hal_pwmio_pwmout_set_duty_cycle(&self->carrier, duty_cycle);
self->pin = pin->number;
self->slice = self->carrier.slice; self->slice = self->carrier.slice;
self->min_pulse = (1000000 / self->carrier.actual_frequency); self->min_pulse = (1000000 / self->carrier.actual_frequency);
} }
@ -104,7 +110,8 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
self->pulse_index = 0; self->pulse_index = 0;
self->pulse_length = length; self->pulse_length = length;
common_hal_pwmio_pwmout_set_duty_cycle(&self->carrier, self->current_duty_cycle); // Turn on the signal by connecting the PWM to the outside pin.
gpio_set_function(self->pin, GPIO_FUNC_PWM);
uint64_t delay = self->pulse_buffer[0]; uint64_t delay = self->pulse_buffer[0];
if (delay < self->min_pulse) { if (delay < self->min_pulse) {
delay = self->min_pulse; delay = self->min_pulse;

View File

@ -42,7 +42,6 @@ typedef struct {
pwmio_pwmout_obj_t carrier; pwmio_pwmout_obj_t carrier;
uint16_t *pulse_buffer; uint16_t *pulse_buffer;
uint16_t pulse_length; uint16_t pulse_length;
uint16_t current_duty_cycle;
uint32_t min_pulse; uint32_t min_pulse;
volatile uint16_t pulse_index; volatile uint16_t pulse_index;
} pulseio_pulseout_obj_t; } pulseio_pulseout_obj_t;