Skip to content

Commit 412eb87

Browse files
committed
Switch to pin, frequency and duty_cycle PulseOut
Passing in a PWMOut still works but is deprecated. It will be removed in CircuitPython 8.0.0 This also switches STM32 timer indices and channel indices to 0-based in our pin data rather than `- 1` everywhere. The latter is more bug prone. Most of the way for micropython#3264 Tested on Metro M0, Metro M4, Feather S2, Feather nRF52840, Feather STM32F4 and Arduino RP2040.
1 parent 5fbe6d4 commit 412eb87

File tree

35 files changed

+272
-848
lines changed

35 files changed

+272
-848
lines changed

locale/circuitpython.pot

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,21 +1877,6 @@ msgstr ""
18771877
msgid "Polygon needs at least 3 points"
18781878
msgstr ""
18791879

1880-
#: ports/esp32s2/common-hal/pulseio/PulseOut.c
1881-
msgid ""
1882-
"Port does not accept PWM carrier. Pass a pin, frequency and duty cycle "
1883-
"instead"
1884-
msgstr ""
1885-
1886-
#: ports/atmel-samd/common-hal/pulseio/PulseOut.c
1887-
#: ports/cxd56/common-hal/pulseio/PulseOut.c
1888-
#: ports/nrf/common-hal/pulseio/PulseOut.c
1889-
#: ports/stm/common-hal/pulseio/PulseOut.c
1890-
msgid ""
1891-
"Port does not accept pins or frequency. Construct and pass a PWMOut Carrier "
1892-
"instead"
1893-
msgstr ""
1894-
18951880
#: shared-bindings/_bleio/Adapter.c
18961881
msgid "Prefix buffer must be on the heap"
18971882
msgstr ""

ports/atmel-samd/common-hal/pulseio/PulseOut.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ void pulseout_reset() {
103103
}
104104

105105
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
106-
const pwmio_pwmout_obj_t *carrier,
107106
const mcu_pin_obj_t *pin,
108107
uint32_t frequency,
109108
uint16_t duty_cycle) {
110-
if (!carrier || pin || frequency) {
111-
mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. Construct and pass a PWMOut Carrier instead"));
112-
}
109+
110+
pwmout_result_t result = common_hal_pwmio_pwmout_construct(
111+
&self->pwmout, pin, duty_cycle, frequency, false);
112+
113+
// This will raise an exception and not return if needed.
114+
common_hal_pwmio_pwmout_raise_error(result);
113115

114116
if (refcount == 0) {
115117
// Find a spare timer.
@@ -155,7 +157,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
155157
}
156158
refcount++;
157159

158-
self->pin = carrier->pin->number;
160+
self->pin = pin->number;
159161

160162
PortGroup *const port_base = &PORT->Group[GPIO_PORT(self->pin)];
161163
self->pincfg = &port_base->PINCFG[self->pin % 32];
@@ -173,7 +175,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
173175
}
174176

175177
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) {
176-
return self->pin == NO_PIN;
178+
return common_hal_pwmio_pwmout_deinited(&self->pwmout);
177179
}
178180

179181
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
@@ -191,6 +193,7 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
191193
pulseout_tc_index = 0xff;
192194
}
193195
self->pin = NO_PIN;
196+
common_hal_pwmio_pwmout_deinit(&self->pwmout);
194197
#ifdef SAMD21
195198
rtc_end_pulse();
196199
#endif

ports/atmel-samd/common-hal/pulseio/PulseOut.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
#include "common-hal/microcontroller/Pin.h"
3131

3232
#include "py/obj.h"
33+
#include "shared-bindings/pwmio/PWMOut.h"
3334

3435
typedef struct {
3536
mp_obj_base_t base;
3637
__IO PORT_PINCFG_Type *pincfg;
38+
pwmio_pwmout_obj_t pwmout;
3739
uint8_t pin;
3840
} pulseio_pulseout_obj_t;
3941

ports/atmel-samd/common-hal/pwmio/PWMOut.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,7 @@ uint32_t common_hal_pwmio_pwmout_get_frequency(pwmio_pwmout_obj_t *self) {
493493
bool common_hal_pwmio_pwmout_get_variable_frequency(pwmio_pwmout_obj_t *self) {
494494
return self->variable_frequency;
495495
}
496+
497+
const mcu_pin_obj_t *common_hal_pwmio_pwmout_get_pin(pwmio_pwmout_obj_t *self) {
498+
return self->pin;
499+
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ static bool pulseout_timer_handler(unsigned int *next_interval_us, void *arg) {
5858
}
5959

6060
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
61-
const pwmio_pwmout_obj_t *carrier,
6261
const mcu_pin_obj_t *pin,
6362
uint32_t frequency,
6463
uint16_t duty_cycle) {
65-
if (!carrier || pin || frequency) {
66-
mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. Construct and pass a PWMOut Carrier instead"));
67-
}
64+
65+
pwmout_result_t result = common_hal_pwmio_pwmout_construct(
66+
&self->pwmout, pin, duty_cycle, frequency, false);
67+
68+
// This will raise an exception and not return if needed.
69+
common_hal_pwmio_pwmout_raise_error(result);
6870

6971
if (pulse_fd < 0) {
7072
pulse_fd = open("/dev/timer0", O_RDONLY);
@@ -74,7 +76,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
7476
mp_raise_RuntimeError(translate("All timers in use"));
7577
}
7678

77-
self->pwm_num = carrier->number;
79+
self->pwm_num = self->pwmout.number;
7880
}
7981

8082
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
@@ -86,6 +88,8 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
8688
close(pulse_fd);
8789
pulse_fd = -1;
8890

91+
common_hal_pwmio_pwmout_deinit(&self->pwmout);
92+
8993
pulse_buffer = NULL;
9094
}
9195

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929

3030
#include "py/obj.h"
3131

32+
#include "shared-bindings/pwmio/PWMOut.h"
33+
3234
typedef struct {
3335
mp_obj_base_t base;
3436
uint8_t pwm_num;
37+
pwmio_pwmout_obj_t pwmout;
3538
} pulseio_pulseout_obj_t;
3639

3740
void pulseout_reset(void);

ports/cxd56/common-hal/pwmio/PWMOut.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,7 @@ void pwmout_start(uint8_t pwm_num) {
157157
void pwmout_stop(uint8_t pwm_num) {
158158
ioctl(pwmout_dev[pwm_num].fd, PWMIOC_STOP, 0);
159159
}
160+
161+
const mcu_pin_obj_t *common_hal_pwmio_pwmout_get_pin(pwmio_pwmout_obj_t *self) {
162+
return self->pin;
163+
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,9 @@
3232
// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset
3333

3434
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
35-
const pwmio_pwmout_obj_t *carrier,
3635
const mcu_pin_obj_t *pin,
3736
uint32_t frequency,
3837
uint16_t duty_cycle) {
39-
if (carrier || !pin || !frequency) {
40-
mp_raise_NotImplementedError(translate("Port does not accept PWM carrier. Pass a pin, frequency and duty cycle instead"));
41-
}
4238

4339
rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt();
4440
if (channel == RMT_CHANNEL_MAX) {

ports/esp32s2/common-hal/pwmio/PWMOut.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self,
152152
varfreq_timers[timer_index] = true;
153153
}
154154
self->variable_frequency = variable_frequency;
155-
self->pin_number = pin->number;
155+
self->pin = pin;
156156
self->deinited = false;
157157
self->duty_resolution = duty_bits;
158158
claim_pin(pin);
@@ -167,7 +167,7 @@ void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) {
167167
never_reset_tim[self->tim_handle.timer_num] = true;
168168
never_reset_chan[self->chan_handle.channel] = true;
169169

170-
never_reset_pin_number(self->pin_number);
170+
never_reset_pin_number(self->pin->number);
171171
}
172172

173173
void common_hal_pwmio_pwmout_reset_ok(pwmio_pwmout_obj_t *self) {
@@ -202,7 +202,7 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) {
202202
// if timer isn't varfreq this will be off aleady
203203
varfreq_timers[self->tim_handle.timer_num] = false;
204204
}
205-
reset_pin_number(self->pin_number);
205+
common_hal_reset_pin(self->pin);
206206
self->deinited = true;
207207
}
208208

@@ -232,3 +232,7 @@ uint32_t common_hal_pwmio_pwmout_get_frequency(pwmio_pwmout_obj_t *self) {
232232
bool common_hal_pwmio_pwmout_get_variable_frequency(pwmio_pwmout_obj_t *self) {
233233
return self->variable_frequency;
234234
}
235+
236+
const mcu_pin_obj_t *common_hal_pwmio_pwmout_get_pin(pwmio_pwmout_obj_t *self) {
237+
return self->pin;
238+
}

ports/esp32s2/common-hal/pwmio/PWMOut.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typedef struct {
3434
mp_obj_base_t base;
3535
ledc_timer_config_t tim_handle;
3636
ledc_channel_config_t chan_handle;
37-
uint16_t pin_number;
37+
const mcu_pin_obj_t *pin;
3838
uint8_t duty_resolution;
3939
bool variable_frequency : 1;
4040
bool deinited : 1;

0 commit comments

Comments
 (0)