8000 Merge pull request #5032 from DavePutz/multi_pulseout · domdfcoding/circuitpython@8723a03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8723a03

Browse files
authored
Merge pull request adafruit#5032 from DavePutz/multi_pulseout
Moved global variables to support multiple RP2040 PulseOuts
2 parents 626b06f + b2d6203 commit 8723a03

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

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

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,7 @@
3939
#include "src/common/pico_time/include/pico/time.h"
4040

4141
static uint8_t refcount = 0;
42-
static uint16_t *pulse_buffer = NULL;
43-
static volatile uint16_t pulse_index = 0;
44-
static uint16_t pulse_length;
45-
pwmio_pwmout_obj_t *pwmout_obj;
46-
volatile uint16_t current_duty_cycle;
47-
static uint32_t min_pulse = 0;
48-
static alarm_id_t cur_alarm;
42+
volatile alarm_id_t cur_alarm = 0;
4943

5044
void turn_off(uint8_t slice) {
5145
pwm_hw->slice[slice].ctr = 0;
@@ -56,24 +50,24 @@ void turn_off(uint8_t slice) {
5650
pwm_hw->slice[slice].csr = 0;
5751
}
5852

59-
void pulse_finish(pwmio_pwmout_obj_t *carrier) {
60-
pulse_index++;
53+
void pulse_finish(pulseio_pulseout_obj_t *self) {
54+
self->pulse_index++;
6155
// Turn pwm pin off by setting duty cyle to 1.
62-
common_hal_pwmio_pwmout_set_duty_cycle(carrier,1);
63-
if (pulse_index >= pulse_length) {
56+
common_hal_pwmio_pwmout_set_duty_cycle(self->carrier,1);
57+
if (self->pulse_index >= self->pulse_length) {
6458
return;
6559
}
66-
if (pulse_index % 2 == 0) {
67-
common_hal_pwmio_pwmout_set_duty_cycle(carrier,current_duty_cycle);
60+
if (self->pulse_index % 2 == 0) {
61+
common_hal_pwmio_pwmout_set_duty_cycle(self->carrier,self->current_duty_cycle);
6862
}
69-
uint64_t delay = pulse_buffer[pulse_index];
70-
if (delay < min_pulse) {
71-
delay = min_pulse;
63+
uint64_t delay = self->pulse_buffer[self->pulse_index];
64+
if (delay < self->min_pulse) {
65+
delay = self->min_pulse;
7266
}
7367
cur_alarm = 0;
7468
// if the alarm cannot be set, try again with a longer delay
7569
while (cur_alarm == 0) {
76-
cur_alarm = add_alarm_in_us(delay, pulseout_interrupt_handler, carrier, false);
70+
cur_alarm = add_alarm_in_us(delay, pulseout_interrupt_handler, self, false);
7771
delay = delay + 1;
7872
}
7973
}
@@ -94,15 +88,14 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
9488
uint16_t duty_cycle) {
9589

9690
refcount++;
97-
pwmout_obj = (pwmio_pwmout_obj_t *)carrier;
98-
current_duty_cycle = common_hal_pwmio_pwmout_get_duty_cycle(pwmout_obj);
99-
pwm_set_enabled(carrier->slice,false);
100-
turn_off(carrier->slice);
101-
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,1);
102-
self->pin = carrier->pin->number;
103-
self->slice = carrier->slice;
104-
self->carrier = pwmout_obj;
105-
min_pulse = (1000000 / carrier->actual_frequency);
91+
self->carrier = (pwmio_pwmout_obj_t *)carrier;
92+
self->current_duty_cycle = common_hal_pwmio_pwmout_get_duty_cycle(self->carrier);
93+
pwm_set_enabled(self->carrier->slice,false);
94+
turn_off(self->carrier->slice);
95+
common_hal_pwmio_pwmout_set_duty_cycle(self->carrier,1);
96+
self->pin = self->carrier->pin->number;
97+
self->slice = self->carrier->slice;
98+
self->min_pulse = (1000000 / self->carrier->actual_frequency);
10699
}
107100

108101
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) {
@@ -118,25 +111,24 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
118111
}
119112

120113
void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pulses, uint16_t length) {
121-
pulse_buffer = pulses;
122-
pulse_index = 0;
123-
pulse_length = length;
114+
self->pulse_buffer = pulses;
115+
self->pulse_index = 0;
116+
self->pulse_length = length;
124117

125-
common_hal_pwmio_pwmout_set_duty_cycle(self->carrier,current_duty_cycle);
118+
common_hal_pwmio_pwmout_set_duty_cycle(self->carrier,self->current_duty_cycle);
126119
pwm_set_enabled(self->slice,true);
127-
uint64_t delay = pulse_buffer[0];
128-
if (delay < min_pulse) {
129-
delay = min_pulse;
120+
uint64_t delay = self->pulse_buffer[0];
121+
if (delay < self->min_pulse) {
122+
delay = self->min_pulse;
130123
}
131-
alarm_id_t init_alarm = 0;
124+
cur_alarm = 0;
132125
// if the alarm cannot be set, try again with a longer delay
133-
while (init_alarm == 0) {
134-
init_alarm = add_alarm_in_us(delay, pulseout_interrupt_handler, self->carrier, false);
126+
while (cur_alarm == 0) {
127+
cur_alarm = add_alarm_in_us(delay, pulseout_interrupt_handler, self, false);
135128
delay = delay + 1;
136129
}
137-
cur_alarm = init_alarm;
138130

139-
while (pulse_index < length) {
131+
while (self->pulse_index < length) {
140132
// Do other things while we wait. The interrupts will handle sending the
141133
// signal.
142134
RUN_BACKGROUND_TASKS;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ typedef struct {
4040
uint8_t pin;
4141
uint8_t slice;
4242
pwmio_pwmout_obj_t *carrier;
43+
uint16_t *pulse_buffer;
44+
uint16_t pulse_length;
45+
uint16_t current_duty_cycle;
46+
uint32_t min_pulse;
47+
volatile uint16_t pulse_index;
4348
} pulseio_pulseout_obj_t;
4449

4550
void pulseout_reset(void);

0 commit comments

Comments
 (0)
0