forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
I2SOut.c
310 lines (275 loc) · 9.85 KB
/
I2SOut.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <stdint.h>
#include <string.h>
#include "mpconfigport.h"
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "common-hal/audiobusio/I2SOut.h"
#include "shared-bindings/audiobusio/I2SOut.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-module/audiocore/__init__.h"
#include "bindings/rp2pio/StateMachine.h"
const uint16_t i2s_program[] = {
// ; Load the next set of samples
// ; /--- LRCLK
// ; |/-- BCLK
// ; ||
// pull noblock side 0b01 ; Loads OSR with the next FIFO value or X
0x8880,
// mov x osr side 0b01 ; Save the new value in case we need it again
0xa827,
// set y 14 side 0b01
0xe84e,
// bitloop1:
// out pins 1 side 0b00 [2]
0x6201,
// jmp y-- bitloop1 side 0b01 [2]
0x0a83,
// out pins 1 side 0b10 [2]
0x7201,
// set y 14 side 0b11 [2]
0xfa4e,
// bitloop0:
// out pins 1 side 0b10 [2]
0x7201,
// jmp y-- bitloop0 side 0b11 [2]
0x1a87,
// out pins 1 side 0b00 [2]
0x6201
};
const uint16_t i2s_program_left_justified[] = {
// ; Load the next set of samples
// ; /--- LRCLK
// ; |/-- BCLK
// ; ||
// pull noblock side 0b11 ; Loads OSR with the next FIFO value or X
0x9880,
// mov x osr side 0b11 ; Save the new value in case we need it again
0xb827,
// set y 14 side 0b11
0xf84e,
// bitloop1:
// out pins 1 side 0b00 [2]
0x6201,
// jmp y-- bitloop1 side 0b01 [2]
0x0a83,
// out pins 1 side 0b00 [2]
0x6201,
// set y 14 side 0b01 [2]
0xea4e,
// bitloop0:
// out pins 1 side 0b10 [2]
0x7201,
// jmp y-- bitloop0 side 0b11 [2]
0x1a87,
// out pins 1 side 0b10 [2]
0x7201
};
// Another version of i2s_program with the LRCLC and BCLK pin swapped
const uint16_t i2s_program_swap[] = {
// ; Load the next set of samples
// ; /--- BCLK
// ; |/-- LRCLK
// ; ||
// pull noblock side 0b11 ; Loads OSR with the next FIFO value or X
0x9880,
// mov x osr side 0b11 ; Save the new value in case we need it again
0xb827,
// set y 14 side 0b11
0xf84e,
// bitloop1:
// out pins 1 side 0b01 [2]
0x6a01,
// jmp y-- bitloop1 side 0b11 [2]
0x1a83,
// out pins 1 side 0b00 [2]
0x6201,
// set y 14 side 0b10 [2]
0xf24e,
// bitloop0:
// out pins 1 side 0b00 [2]
0x6201,
// jmp y-- bitloop0 side 0b10 [2]
0x1287,
// out pins 1 side 0b01 [2]
0x6a01
};
// Another version of i2s_program_left_justified with the LRCLC and BCLK pin
// swapped.
const uint16_t i2s_program_left_justified_swap[] = {
// ; Load the next set of samples
// ; /--- BCLK
// ; |/-- LRCLK
// ; ||
// pull noblock side 0b11 ; Loads OSR with the next FIFO value or X
0x9880,
// mov x osr side 0b11 ; Save the new value in case we need it again
0xb827,
// set y 14 side 0b11
0xf84e,
// bitloop1:
// out pins 1 side 0b00 [2]
0x6201,
// jmp y-- bitloop1 side 0b10 [2]
0x1283,
// out pins 1 side 0b00 [2]
0x6201,
// set y 14 side 0b10 [2]
0xf24e,
// bitloop0:
// out pins 1 side 0b01 [2]
0x6a01,
// jmp y-- bitloop0 side 0b11 [2]
0x1a87,
// out pins 1 side 0b01 [2]
0x6a01
};
void i2sout_reset(void) {
}
// Caller validates that pins are free.
void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t *self,
const mcu_pin_obj_t *bit_clock, const mcu_pin_obj_t *word_select,
const mcu_pin_obj_t *data, const mcu_pin_obj_t *main_clock, bool left_justified) {
if (main_clock != NULL) {
mp_raise_NotImplementedError_varg(MP_ERROR_TEXT("%q"), MP_QSTR_main_clock);
}
const mcu_pin_obj_t *sideset_pin = NULL;
const uint16_t *program = NULL;
size_t program_len = 0;
if (bit_clock->number == word_select->number - 1) {
sideset_pin = bit_clock;
if (left_justified) {
program_len = MP_ARRAY_SIZE(i2s_program_left_justified);
program = i2s_program_left_justified;
} else {
program_len = MP_ARRAY_SIZE(i2s_program);
program = i2s_program;
}
} else if (bit_clock->number == word_select->number + 1) {
sideset_pin = word_select;
if (left_justified) {
program_len = MP_ARRAY_SIZE(i2s_program_left_justified_swap);
program = i2s_program_left_justified_swap;
} else {
program_len = MP_ARRAY_SIZE(i2s_program_swap);
program = i2s_program_swap;
}
} else {
mp_raise_ValueError(MP_ERROR_TEXT("Bit clock and word select must be sequential GPIO pins"));
}
// Use the state machine to manage pins.
common_hal_rp2pio_statemachine_construct(
&self->state_machine,
program, program_len,
44100 * 32 * 6, // Clock at 44.1 khz to warm the DAC up.
NULL, 0, // init
NULL, 0, // may_exec
data, 1, 0, 0xffffffff, // out pin
NULL, 0, // in pins
0, 0, // in pulls
NULL, 0, 0, 0x1f, // set pins
sideset_pin, 2, 0, 0x1f, // sideset pins
false, // No sideset enable
NULL, PULL_NONE, // jump pin
0, // wait gpio pins
true, // exclusive pin use
false, 32, false, // shift out left to start with MSB
false, // Wait for txstall
false, 32, false, // in settings
false, // Not user-interruptible.
0, -1, // wrap settings
PIO_ANY_OFFSET,
PIO_FIFO_TYPE_DEFAULT,
PIO_MOV_STATUS_DEFAULT,
PIO_MOV_N_DEFAULT
);
self->playing = false;
audio_dma_init(&self->dma);
}
bool common_hal_audiobusio_i2sout_deinited(audiobusio_i2sout_obj_t *self) {
return common_hal_rp2pio_statemachine_deinited(&self->state_machine);
}
void common_hal_audiobusio_i2sout_deinit(audiobusio_i2sout_obj_t *self) {
if (common_hal_audiobusio_i2sout_deinited(self)) {
return;
}
if (common_hal_audiobusio_i2sout_get_playing(self)) {
common_hal_audiobusio_i2sout_stop(self);
}
common_hal_rp2pio_statemachine_deinit(&self->state_machine);
audio_dma_deinit(&self->dma);
}
void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
mp_obj_t sample, bool loop) {
if (common_hal_audiobusio_i2sout_get_playing(self)) {
common_hal_audiobusio_i2sout_stop(self);
}
uint8_t bits_per_sample = audiosample_bits_per_sample(sample);
// Make sure we transmit a minimum of 16 bits.
// TODO: Maybe we need an intermediate object to upsample instead. This is
// only needed for some I2S devices that expect at least 8.
if (bits_per_sample < 16) {
bits_per_sample = 16;
}
// We always output stereo so output twice as many bits.
uint16_t bits_per_sample_output = bits_per_sample * 2;
size_t clocks_per_bit = 6;
uint32_t frequency = bits_per_sample_output * audiosample_sample_rate(sample);
uint8_t channel_count = audiosample_channel_count(sample);
if (channel_count > 2) {
mp_raise_ValueError(MP_ERROR_TEXT("Too many channels in sample."));
}
common_hal_rp2pio_statemachine_set_frequency(&self->state_machine, clocks_per_bit * frequency);
common_hal_rp2pio_statemachine_restart(&self->state_machine);
// On the RP2040, output registers are always written with a 32-bit write.
// If the write is 8 or 16 bits wide, the data will be replicated in upper bytes.
// See section 2.1.4 Narrow IO Register Writes in the RP2040 datasheet.
// This means that identical 16-bit audio data will be written in both halves of the incoming PIO
// FIFO register. Thus we get mono-to-stereo conversion for the I2S output for free.
audio_dma_result result = audio_dma_setup_playback(
&self->dma,
sample,
loop,
false, // single channel
0, // audio channel
true, // output signed
bits_per_sample,
(uint32_t)&self->state_machine.pio->txf[self->state_machine.state_machine], // output register
self->state_machine.tx_dreq, // data request line
false); // swap channel
if (result == AUDIO_DMA_DMA_BUSY) {
common_hal_audiobusio_i2sout_stop(self);
mp_raise_RuntimeError(MP_ERROR_TEXT("No DMA channel found"));
} else if (result == AUDIO_DMA_MEMORY_ERROR) {
common_hal_audiobusio_i2sout_stop(self);
mp_raise_RuntimeError(MP_ERROR_TEXT("Unable to allocate buffers for signed conversion"));
}
self->playing = true;
}
void common_hal_audiobusio_i2sout_pause(audiobusio_i2sout_obj_t *self) {
audio_dma_pause(&self->dma);
}
void common_hal_audiobusio_i2sout_resume(audiobusio_i2sout_obj_t *self) {
// Maybe: Clear any overrun/underrun errors
audio_dma_resume(&self->dma);
}
bool common_hal_audiobusio_i2sout_get_paused(audiobusio_i2sout_obj_t *self) {
return audio_dma_get_paused(&self->dma);
}
void common_hal_audiobusio_i2sout_stop(audiobusio_i2sout_obj_t *self) {
audio_dma_stop(&self->dma);
common_hal_rp2pio_statemachine_stop(&self->state_machine);
self->playing = false;
}
bool common_hal_audiobusio_i2sout_get_playing(audiobusio_i2sout_obj_t *self) {
bool playing = audio_dma_get_playing(&self->dma);
if (!playing && self->playing) {
common_hal_audiobusio_i2sout_stop(self);
}
return playing;
}