Skip to content

Commit be1d1d2

Browse files
authored
Merge pull request #5667 from dhalbert/rp2-pwmout-counter-fix
fix mistaken use of PWM channel for slice
2 parents 4de6c7c + 4a4c5d7 commit be1d1d2

File tree

6 files changed

+45
-46
lines changed

6 files changed

+45
-46
lines changed

ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
207207
uint32_t tx_register = (uint32_t)&pwm_hw->slice[self->left_pwm.slice].cc;
208208
if (self->stereo) {
209209
// Shift the destination if we are outputting to both PWM channels.
210-
tx_register += self->left_pwm.channel * sizeof(uint16_t);
210+
tx_register += self->left_pwm.ab_channel * sizeof(uint16_t);
211211
}
212212

213213
self->pacing_timer = pacing_timer;

ports/raspberrypi/common-hal/countio/Counter.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
2525
mp_raise_RuntimeError(translate("PWM slice already in use"));
2626
}
2727

28-
uint8_t channel = pwm_gpio_to_channel(self->pin_a);
29-
if (!pwmio_claim_slice_channels(self->slice_num)) {
28+
uint8_t ab_channel = pwm_gpio_to_channel(self->pin_a);
29+
if (!pwmio_claim_slice_ab_channels(self->slice_num)) {
3030
mp_raise_RuntimeError(translate("PWM slice channel A already in use"));
3131
}
3232

@@ -69,7 +69,7 @@ void common_hal_countio_counter_deinit(countio_counter_obj_t *self) {
6969
pwm_set_enabled(self->slice_num, false);
7070
pwm_set_irq_enabled(self->slice_num, false);
7171

72-
pwmio_release_slice_channels(self->slice_num);
72+
pwmio_release_slice_ab_channels(self->slice_num);
7373

7474
reset_pin_number(self->pin_a);
7575

@@ -98,13 +98,14 @@ void common_hal_countio_counter_reset(countio_counter_obj_t *self) {
9898
void counter_interrupt_handler(void) {
9999
uint32_t mask = pwm_get_irq_status_mask();
100100

101-
uint8_t i = 1, pos = 1;
101+
uint8_t i = 1;
102+
uint8_t pos = 0;
102103
while (!(i & mask)) {
103104
i = i << 1;
104105
++pos;
105106
}
106107

107-
countio_counter_obj_t *self = MP_STATE_PORT(counting)[pos - 1];
108+
countio_counter_obj_t *self = MP_STATE_PORT(counting)[pos];
108109
if (self != NULL) {
109110
pwm_clear_irq(self->slice_num);
110111
self->count += 65536;

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
uint32_t target_slice_frequencies[NUM_PWM_SLICES];
4343
uint32_t slice_variable_frequency;
4444

45-
#define CHANNELS_PER_SLICE 2
45+
#define AB_CHANNELS_PER_SLICE 2
4646
static uint32_t channel_use;
4747
static uint32_t never_reset_channel;
4848

@@ -58,11 +58,11 @@ static uint32_t never_reset_channel;
5858
// So 65534 should be the maximum top value, and we'll set CC to be TOP+1 as appropriate.
5959
#define MAX_TOP 65534
6060

61-
static uint32_t _mask(uint8_t slice, uint8_t channel) {
62-
return 1 << (slice * CHANNELS_PER_SLICE + channel);
61+
static uint32_t _mask(uint8_t slice, uint8_t ab_channel) {
62+
return 1 << (slice * AB_CHANNELS_PER_SLICE + ab_channel);
6363
}
6464

65-
bool pwmio_claim_slice_channels(uint8_t slice) {
65+
bool pwmio_claim_slice_ab_channels(uint8_t slice) {
6666
uint32_t channel_use_mask_a = _mask(slice, 0);
6767
uint32_t channel_use_mask_b = _mask(slice, 1);
6868

@@ -78,37 +78,37 @@ bool pwmio_claim_slice_channels(uint8_t slice) {
7878
return true;
7979
}
8080

81-
void pwmio_release_slice_channels(uint8_t slice) {
81+
void pwmio_release_slice_ab_channels(uint8_t slice) {
8282
uint32_t channel_mask = _mask(slice, 0);
8383
channel_use &= ~channel_mask;
8484
channel_mask = _mask(slice, 1);
8585
channel_use &= ~channel_mask;
8686
}
8787

88-
void pwmout_never_reset(uint8_t slice, uint8_t channel) {
89-
never_reset_channel |= _mask(slice, channel);
88+
void pwmout_never_reset(uint8_t slice, uint8_t ab_channel) {
89+
never_reset_channel |= _mask(slice, ab_channel);
9090
}
9191

92-
void pwmout_reset_ok(uint8_t slice, uint8_t channel) {
93-
never_reset_channel &= ~_mask(slice, channel);
92+
void pwmout_reset_ok(uint8_t slice, uint8_t ab_channel) {
93+
never_reset_channel &= ~_mask(slice, ab_channel);
9494
}
9595

9696
void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) {
97-
pwmout_never_reset(self->slice, self->channel);
97+
pwmout_never_reset(self->slice, self->ab_channel);
9898

9999
never_reset_pin_number(self->pin->number);
100100
}
101101

102102
void common_hal_pwmio_pwmout_reset_ok(pwmio_pwmout_obj_t *self) {
103-
pwmout_reset_ok(self->slice, self->channel);
103+
pwmout_reset_ok(self->slice, self->ab_channel);
104104
}
105105

106106
void pwmout_reset(void) {
107107
// Reset all slices
108108
for (size_t slice = 0; slice < NUM_PWM_SLICES; slice++) {
109109
bool reset = true;
110-
for (size_t channel = 0; channel < CHANNELS_PER_SLICE; channel++) {
111-
uint32_t channel_use_mask = _mask(slice, channel);
110+
for (size_t ab_channel = 0; ab_channel < AB_CHANNELS_PER_SLICE; ab_channel++) {
111+
uint32_t channel_use_mask = _mask(slice, ab_channel);
112112
if ((never_reset_channel & channel_use_mask) != 0) {
113113
reset = false;
114114
continue;
@@ -124,8 +124,8 @@ void pwmout_reset(void) {
124124
}
125125
}
126126

127-
pwmout_result_t pwmout_allocate(uint8_t slice, uint8_t channel, bool variable_frequency, uint32_t frequency) {
128-
uint32_t channel_use_mask = _mask(slice, channel);
127+
pwmout_result_t pwmout_allocate(uint8_t slice, uint8_t ab_channel, bool variable_frequency, uint32_t frequency) {
128+
uint32_t channel_use_mask = _mask(slice, ab_channel);
129129

130130
// Check the channel first.
131131
if ((channel_use & channel_use_mask) != 0) {
@@ -171,15 +171,15 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self,
171171
}
172172

173173
uint8_t slice = pwm_gpio_to_slice_num(pin->number);
174-
uint8_t channel = pwm_gpio_to_channel(pin->number);
174+
uint8_t ab_channel = pwm_gpio_to_channel(pin->number);
175175

176-
int r = pwmout_allocate(slice, channel, variable_frequency, frequency);
176+
int r = pwmout_allocate(slice, ab_channel, variable_frequency, frequency);
177177
if (r != PWMOUT_OK) {
178178
return r;
179179
}
180180

181181
self->slice = slice;
182-
self->channel = channel;
182+
self->ab_channel = ab_channel;
183183

184184
if (target_slice_frequencies[slice] != frequency) {
185185
// Reset the counter and compare values.
@@ -202,11 +202,11 @@ bool common_hal_pwmio_pwmout_deinited(pwmio_pwmout_obj_t *self) {
202202
return self->pin == NULL;
203203
}
204204

205-
void pwmout_free(uint8_t slice, uint8_t channel) {
206-
uint32_t channel_mask = _mask(slice, channel);
205+
void pwmout_free(uint8_t slice, uint8_t ab_channel) {
206+
uint32_t channel_mask = _mask(slice, ab_channel);
207207
channel_use &= ~channel_mask;
208208
never_reset_channel &= ~channel_mask;
209-
uint32_t slice_mask = ((1 << CHANNELS_PER_SLICE) - 1) << (slice * CHANNELS_PER_SLICE);
209+
uint32_t slice_mask = ((1 << AB_CHANNELS_PER_SLICE) - 1) << (slice * AB_CHANNELS_PER_SLICE);
210210
if ((channel_use & slice_mask) == 0) {
211211
target_slice_frequencies[slice] = 0;
212212
slice_variable_frequency &= ~(1 << slice);
@@ -218,7 +218,7 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) {
218218
if (common_hal_pwmio_pwmout_deinited(self)) {
219219
return;
220220
}
221-
pwmout_free(self->slice, self->channel);
221+
pwmout_free(self->slice, self->ab_channel);
222222
reset_pin_number(self->pin->number);
223223
self->pin = NULL;
224224
}
@@ -235,13 +235,13 @@ extern void common_hal_pwmio_pwmout_set_duty_cycle(pwmio_pwmout_obj_t *self, uin
235235
compare_count = ((uint32_t)duty * self->top + MAX_TOP / 2) / MAX_TOP;
236236
}
237237
// compare_count is the CC register value, which should be TOP+1 for 100% duty cycle.
238-
pwm_set_chan_level(self->slice, self->channel, compare_count);
238+
pwm_set_chan_level(self->slice, self->ab_channel, compare_count);
239239
// Wait for wrap so that we know our new cc value has been applied. Clear
240240
// the internal interrupt and then wait for it to be set. Worst case, we
241241
// wait a full cycle.
242-
pwm_hw->intr = 1 << self->channel;
243-
while ((pwm_hw->en & (1 << self->channel)) != 0 &&
244-
(pwm_hw->intr & (1 << self->channel)) == 0 &&
242+
pwm_hw->intr = 1 << self->slice;
243+
while ((pwm_hw->en & (1 << self->slice)) != 0 &&
244+
(pwm_hw->intr & (1 << self->slice)) == 0 &&
245245
!mp_hal_is_interrupted()) {
246246
}
247247
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PWMIO_PWMOUT_H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PWMIO_PWMOUT_H
27+
#ifndef MICROPY_INCLUDED_RASPBERRY_PI_COMMON_HAL_PWMIO_PWMOUT_H
28+
#define MICROPY_INCLUDED_RASPBERRY_PI_COMMON_HAL_PWMIO_PWMOUT_H
2929

3030
#include "common-hal/microcontroller/Pin.h"
3131

@@ -34,8 +34,8 @@
3434
typedef struct {
3535
mp_obj_base_t base;
3636
const mcu_pin_obj_t *pin;
37-
uint8_t slice;
38-
uint8_t channel;
37+
uint8_t slice; // 0-7
38+
uint8_t ab_channel; // 0-1: A or B slice channel
3939
bool variable_frequency;
4040
uint16_t duty_cycle;
4141
uint32_t actual_frequency;
@@ -46,13 +46,13 @@ void pwmout_reset(void);
4646
// Private API for AudioPWMOut.
4747
void pwmio_pwmout_set_top(pwmio_pwmout_obj_t *self, uint16_t top);
4848
// Private APIs for RGBMatrix
49-
enum pwmout_result_t pwmout_allocate(uint8_t slice, uint8_t channel, bool variable_frequency, uint32_t frequency);
50-
void pwmout_free(uint8_t slice, uint8_t channel);
51-
void pwmout_never_reset(uint8_t slice, uint8_t channel);
52-
void pwmout_reset_ok(uint8_t slice, uint8_t channel);
49+
enum pwmout_result_t pwmout_allocate(uint8_t slice, uint8_t ab_channel, bool variable_frequency, uint32_t frequency);
50+
void pwmout_free(uint8_t slice, uint8_t ab_channel);
51+
void pwmout_never_reset(uint8_t slice, uint8_t ab_channel);
52+
void pwmout_reset_ok(uint8_t slice, uint8_t ab_channel);
5353

54-
// Private API for countio to claim both channels on a slice
55-
bool pwmio_claim_slice_channels(uint8_t slice);
56-
void pwmio_release_slice_channels(uint8_t slice);
54+
// Private API for countio to claim both ab_channels on a slice
55+
bool pwmio_claim_slice_ab_channels(uint8_t slice);
56+
void pwmio_release_slice_ab_channels(uint8_t slice);
5757

58-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PWMIO_PWMOUT_H
58+
#endif // MICROPY_INCLUDED_RASPBERRY_PI_COMMON_HAL_PWMIO_PWMOUT_H

tests/extmod/utimeq1.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
def dprint(*v):
1818
print(*v)
1919

20-
2120
else:
2221

2322
def dprint(*v):

tools/pydfu.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
def get_string(dev, index):
8484
return usb.util.get_string(dev, 255, index)
8585

86-
8786
else:
8887
# PyUSB 1.0.0.b2 dropped the length argument
8988
def get_string(dev, index):

0 commit comments

Comments
 (0)