10000 Merge pull request #789 from tannewt/pdmin3 · larsks/micropython@9d484c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d484c4

Browse files
authored
Merge pull request micropython#789 from tannewt/pdmin3
Re-enable PDMIn without ASF
2 parents 449385b + 04f75b8 commit 9d484c4

File tree

14 files changed

+388
-171
lines changed

14 files changed

+388
-171
lines changed

ports/atmel-samd/Makefile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ SRC_COMMON_HAL = \
299299
usb_hid/Device.c \
300300
audioio/__init__.c \
301301
audioio/AudioOut.c \
302-
# audiobusio/PDMIn.c \
303-
touchio/__init__.c \
302+
# touchio/__init__.c \
304303
touchio/TouchIn.c \
305304
306305
ifeq ($(INTERNAL_LIBM),1)
307306
SRC_LIBM = $(addprefix lib/,\
308307
libm/math.c \
308+
libm/roundf.c \
309309
libm/fmodf.c \
310310
libm/nearbyintf.c \
311311
libm/ef_sqrt.c \
@@ -363,16 +363,14 @@ SRC_SHARED_MODULE = \
363363
uheap/__init__.c \
364364
ustack/__init__.c
365365

366-
ifeq ($(CHIP_FAMILY),samd21)
367-
SRC_COMMON_HAL += \
368-
audiobusio/__init__.c \
369-
audiobusio/I2SOut.c
370-
endif
366+
# The smallest SAMD51 packages don't have I2S. Everything else does.
371367
ifneq ($(CHIP_VARIANT),SAMD51G18A)
372368
ifneq ($(CHIP_VARIANT),SAMD51G19A)
373369
SRC_COMMON_HAL += \
374370
audiobusio/__init__.c \
375-
audiobusio/I2SOut.c
371+
audiobusio/I2SOut.c \
372+
audiobusio/PDMIn.c
373+
SRC_C += i2s.c
376374
endif
377375
endif
378376

ports/atmel-samd/audio_dma.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,19 +267,23 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t* dma,
267267
turn_on_event_system();
268268
dma->event_channel = find_sync_event_channel();
269269
init_event_channel_interrupt(dma->event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel);
270-
find_sync_event_channel();
271270

272271
// We keep the audio_dma_t for internal use and the sample as a root pointer because it
273272
// contains the audiodma structure.
274273
audio_dma_state[dma->dma_channel] = dma;
275274
MP_STATE_PORT(playing_audio)[dma->dma_channel] = dma->sample;
276275
}
277276

278-
dma->beat_size = 1;
279-
dma->bytes_per_sample = 1;
277+
280278
if (audiosample_bits_per_sample(sample) == 16) {
281279
dma->beat_size = 2;
282280
dma->bytes_per_sample = 2;
281+
} else {
282+
dma->beat_size = 1;
283+
dma->bytes_per_sample = 1;
284+
if (single_channel) {
285+
output_register_address += 1;
286+
}
283287
}
284288
// Transfer both channels at once.
285289
if (!single_channel && audiosample_channel_count(sample) == 2) {

ports/atmel-samd/audio_dma.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ uint8_t audiosample_channel_count(mp_obj_t sample_obj);
6464
void audio_dma_init(audio_dma_t* dma);
6565
void audio_dma_reset(void);
6666

67+
uint8_t find_free_audio_dma_channel(void);
68+
6769
// This sets everything up but doesn't start the timer.
6870
// Sample is the python object for the sample to play.
6971
// loop is true if we should loop the sample.

ports/atmel-samd/common-hal/audiobusio/I2SOut.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
#include "audio_dma.h"
4949
#include "clocks.h"
5050
#include "events.h"
51-
#include "samd21_pins.h"
51+
#include "i2s.h"
52+
#include "pins.h"
5253
#include "shared_dma.h"
5354
#include "timers.h"
5455

@@ -155,18 +156,7 @@ void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t* self,
155156
self->clock_unit = ws_clock_unit;
156157
self->serializer = serializer;
157158

158-
// Make sure the I2S peripheral is running so we can see if the resources we need are free.
159-
#ifdef SAMD51
160-
hri_mclk_set_APBDMASK_I2S_bit(MCLK);
161-
162-
// Connect the clock units to the 2mhz clock by default. They can't reset without it.
163-
connect_gclk_to_peripheral(5, I2S_GCLK_ID_0);
164-
connect_gclk_to_peripheral(5, I2S_GCLK_ID_1);
165-
#endif
166-
167-
#ifdef SAMD21
168-
_pm_enable_bus_clock(PM_BUS_APBC, I2S);
169-
#endif
159+
turn_on_i2s();
170160

171161
if (I2S->CTRLA.bit.ENABLE == 0) {
172162
I2S->CTRLA.bit.SWRST = 1;
@@ -281,8 +271,7 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t* self,
281271
}
282272

283273
// Configure the I2S peripheral
284-
I2S->CTRLA.bit.ENABLE = 0;
285-
while (I2S->SYNCBUSY.bit.ENABLE == 1) {}
274+
i2s_set_enable(false);
286275

287276
I2S->CLKCTRL[self->clock_unit].reg = clkctrl;
288277
#ifdef SAMD21
@@ -296,8 +285,7 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t* self,
296285
enable_clock_generator(self->gclk, CLOCK_48MHZ, divisor);
297286
connect_gclk_to_peripheral(self->gclk, I2S_GCLK_ID_0 + self->clock_unit);
298287

299-
I2S->CTRLA.bit.ENABLE = 1;
300-
while (I2S->SYNCBUSY.bit.ENABLE == 1) {}
288+
i2s_set_enable(true);
301289

302290
#ifdef SAMD21
303291
uint32_t tx_register = (uint32_t) &I2S->DATA[self->serializer].reg;

0 commit comments

Comments
 (0)
0