35
35
#include "common-hal/pwmio/PWMOut.h"
36
36
#include "supervisor/shared/translate.h"
37
37
#include "src/rp2040/hardware_structs/include/hardware/structs/pwm.h"
38
+ #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
38
39
#include "src/rp2_common/hardware_pwm/include/hardware/pwm.h"
39
40
#include "src/common/pico_time/include/pico/time.h"
40
41
41
42
volatile alarm_id_t cur_alarm = 0 ;
42
43
43
44
void pulse_finish (pulseio_pulseout_obj_t * self ) {
44
45
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).
47
48
if (self -> pulse_index >= self -> pulse_length ) {
49
+ gpio_set_function (self -> pin , GPIO_FUNC_SIO );
48
50
return ;
49
51
}
50
52
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 );
52
56
}
53
57
uint64_t delay = self -> pulse_buffer [self -> pulse_index ];
54
58
if (delay < self -> min_pulse ) {
@@ -77,12 +81,14 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
77
81
78
82
pwmout_result_t result = common_hal_pwmio_pwmout_construct (
79
83
& self -> carrier , pin , 0 , frequency , false);
80
-
81
84
// This will raise an exception and not return if needed.
82
85
common_hal_pwmio_pwmout_raise_error (result );
83
86
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 ;
86
92
self -> slice = self -> carrier .slice ;
87
93
self -> min_pulse = (1000000 / self -> carrier .actual_frequency );
88
94
}
@@ -104,7 +110,8 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
104
110
self -> pulse_index = 0 ;
105
111
self -> pulse_length = length ;
106
112
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 );
108
115
uint64_t delay = self -> pulse_buffer [0 ];
109
116
if (delay < self -> min_pulse ) {
110
117
delay = self -> min_pulse ;
0 commit comments