8000 Enable Audio Channel A and B Swapping for RP2040 · tlyu/circuitpython@dd796db · GitHub
[go: up one dir, main page]

Skip to content

Commit dd796db

Browse files
committed
Enable Audio Channel A and B Swapping for RP2040
1 parent bbff8b5 commit dd796db

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

ports/raspberrypi/audio_dma.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ STATIC size_t audio_dma_convert_samples(audio_dma_t *dma, uint8_t *input, uint32
122122
// Not currently used, but might be in the future.
123123
mp_raise_RuntimeError(MP_ERROR_TEXT("Audio conversion not implemented"));
124124
}
125+
if (dma->swap_channel){
126+
// Loop for swapping left and right channels
127+
for (uint32_t i = 0; i < out_i; i += 2) {
128+
uint16_t temp = ((uint16_t *)output)[i];
129+
((uint16_t *)output)[i] = ((uint16_t *)output)[i + 1];
130+
((uint16_t *)output)[i + 1] = temp;
131+
}
132+
}
125133
#pragma GCC diagnostic pop
126134
return output_length_used;
127135
}
@@ -183,7 +191,8 @@ audio_dma_result audio_dma_setup_playback(
183191
bool output_signed,
184192
uint8_t output_resolution,
185193
uint32_t output_register_address,
186-
uint8_t dma_trigger_source) {
194+
uint8_t dma_trigger_source,
195+
bool swap_channel) {
187196

188197
// Use two DMA channels to play because the DMA can't wrap to itself without the
189198
// buffer being power of two aligned.
@@ -212,6 +221,7 @@ audio_dma_result audio_dma_setup_playback(
212221
dma->output_resolution = output_resolution;
213222
dma->sample_resolution = audiosample_bits_per_sample(sample);
214223
dma->output_register_address = output_register_address;
224+
dma->swap_channel = swap_channel;
215225

216226
audiosample_reset_buffer(sample, single_channel_output, audio_channel);
217227

ports/raspberrypi/audio_dma.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ typedef struct {
5151
bool unsigned_to_signed;
5252
bool output_signed;
5353
bool playing_in_progress;
54+
bool swap_channel;
5455
} audio_dma_t;
5556

5657
typedef enum {
@@ -81,7 +82,8 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
8182
bool output_signed,
8283
uint8_t output_resolution,
8384
uint32_t output_register_address,
84-
uint8_t dma_trigger_source);
85+
uint8_t dma_trigger_source,
86+
bool swap_channel);
8587

8688
void audio_dma_stop(audio_dma_t *dma);
8789
bool audio_dma_get_playing(audio_dma_t *dma);

ports/raspberrypi/common-hal/audiobusio/I2SOut.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
202202
true, // output signed
203203
bits_per_sample,
204204
(uint32_t)&self->state_machine.pio->txf[self->state_machine.state_machine], // output register
205-
self->state_machine.tx_dreq); // data request line
205+
self->state_machine.tx_dreq, // data request line
206+
false); // swap channel
206207

207208
if (result == AUDIO_DMA_DMA_BUSY) {
208209
common_hal_audiobusio_i2sout_stop(self);

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,14 @@ void audiopwmout_reset() {
111111
void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *self,
112112
const mcu_pin_obj_t *left_channel, const mcu_pin_obj_t *right_channel, uint16_t quiescent_value) {
113113

114-
self->stereo = right_channel != NULL;
114+
self->stereo = left_channel != NULL && right_channel != NULL;
115115

116116
if (self->stereo) {
117117
if (pwm_gpio_to_slice_num(left_channel->number) != pwm_gpio_to_slice_num(right_channel->number)) {
118118
mp_raise_ValueError(MP_ERROR_TEXT("Pins must share PWM slice"));
119119
}
120-
if (pwm_gpio_to_channel(left_channel->number) != 0) {
121-
mp_raise_ValueError(MP_ERROR_TEXT("Stereo left must be on PWM channel A"));
122-
}
123-
if (pwm_gpio_to_channel(right_channel->number) != 1) {
124-
mp_raise_ValueError(MP_ERROR_TEXT("Stereo right must be on PWM channel B"));
125-
}
120+
// Check channel swapping, by default left_channel == 0
121+
self->swap_channel = pwm_gpio_to_channel(left_channel->number) != 0;
126122
}
127123

128124
// Typically pwmout doesn't let us change frequency with two objects on the
@@ -233,7 +229,8 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
233229
false, // output signed
234230
BITS_PER_SAMPLE,
235231
(uint32_t)tx_register, // output register: PWM cc register
236-
0x3b + pacing_timer); // data request line
232+
0x3b + pacing_timer, // data request line
233+
self->swap_channel);
237234

238235
if (result == AUDIO_DMA_DMA_BUSY) {
239236
common_hal_audiopwmio_pwmaudioout_stop(self);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct {
3939
uint16_t quiescent_value;
4040
uint8_t pacing_timer;
4141
bool stereo; // if false, only using left_pwm.
42+
bool swap_channel;
4243
} audiopwmio_pwmaudioout_obj_t;
4344

4445
void audiopwmout_reset(void);

0 commit comments

Comments
 (0)
0