Skip to content

Commit cf4597c

Browse files
authored
Merge pull request #5484 from dhalbert/samd21-pdmin-fix
fix SAMD21 PDMIn DMA event use
2 parents a7a6b34 + fc440e7 commit cf4597c

File tree

11 files changed

+161
-67
lines changed

11 files changed

+161
-67
lines changed

ports/atmel-samd/audio_dma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ STATIC void dma_callback_fun(void *arg) {
396396
}
397397
}
398398

399-
void audio_evsys_handler(void) {
399+
void audio_dma_evsys_handler(void) {
400400
for (uint8_t i = 0; i < AUDIO_DMA_CHANNEL_COUNT; i++) {
401401
audio_dma_t *dma = audio_dma_state[i];
402402
if (dma == NULL) {

ports/atmel-samd/audio_dma.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ void audio_dma_background(void);
101101

102102
uint8_t find_sync_event_channel_raise(void);
103103

104-
void audio_evsys_handler(void);
104+
void audio_dma_evsys_handler(void);
105105

106106
#endif // MICROPY_INCLUDED_ATMEL_SAMD_AUDIO_DMA_H

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "common-hal/audiobusio/PDMIn.h"
3636
#include "shared-bindings/analogio/AnalogOut.h"
3737
#include "shared-bindings/audiobusio/PDMIn.h"
38+
#include "shared-bindings/microcontroller/__init__.h"
3839
#include "shared-bindings/microcontroller/Pin.h"
3940
#include "supervisor/shared/translate.h"
4041

@@ -64,7 +65,20 @@
6465
#define SERCTRL(name) I2S_RXCTRL_ ## name
6566
#endif
6667

68+
// Set by interrupt handler when DMA block has finished transferring.
69+
static bool pdmin_dma_block_done;
70+
// Event channel used to trigger interrupt. Set to invalid value EVSYS_SYNCH_NUM when not in use.
71+
static uint8_t pdmin_event_channel;
72+
73+
void pdmin_evsys_handler(void) {
74+
if (pdmin_event_channel < EVSYS_SYNCH_NUM && event_interrupt_active(pdmin_event_channel)) {
75+
pdmin_dma_block_done = true;
76+
}
77+
}
78+
6779
void pdmin_reset(void) {
80+
pdmin_event_channel = EVSYS_SYNCH_NUM;
81+
6882
while (I2S->SYNCBUSY.reg & I2S_SYNCBUSY_ENABLE) {}
6983
I2S->INTENCLR.reg = I2S_INTENCLR_MASK;
7084
I2S->INTFLAG.reg = I2S_INTFLAG_MASK;
@@ -368,7 +382,8 @@ static uint16_t filter_sample(uint32_t pdm_samples[4]) {
368382
uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self,
369383
uint16_t* output_buffer, uint32_t output_buffer_length) {
370384
uint8_t dma_channel = dma_allocate_channel();
371-
uint8_t event_channel = find_sync_event_channel_raise();
385+
pdmin_event_channel = find_sync_event_channel_raise();
386+
pdmin_dma_block_done = false;
372387

373388
// We allocate two buffers on the stack to use for double buffering.
374389
const uint8_t samples_per_buffer = SAMPLES_PER_BUFFER;
@@ -391,7 +406,7 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
391406
#endif
392407

393408
dma_configure(dma_channel, trigger_source, true);
394-
init_event_channel_interrupt(event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel);
409+
init_event_channel_interrupt(pdmin_event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel);
395410
// Turn on serializer now to get it in sync with DMA.
396411
i2s_set_serializer_enable(self->serializer, true);
397412
audio_dma_enable_channel(dma_channel);
@@ -402,23 +417,12 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
402417

403418
uint32_t remaining_samples_needed = output_buffer_length;
404419
while (values_output < output_buffer_length) {
405-
if (event_interrupt_overflow(event_channel)) {
406-
// Looks like we aren't keeping up. We shouldn't skip a buffer so stop early.
407-
break;
408-
}
409-
// Wait for the next buffer to fill
410-
uint32_t wait_counts = 0;
411-
#ifdef SAMD21
412-
#define MAX_WAIT_COUNTS 1000
413-
#endif
414-
#ifdef SAM_D5X_E5X
415-
#define MAX_WAIT_COUNTS 6000
416-
#endif
417-
// If wait_counts exceeds the max count, buffer has probably stopped filling;
418-
// DMA may have missed an I2S trigger event.
419-
while (!event_interrupt_active(event_channel) && ++wait_counts < MAX_WAIT_COUNTS) {
420+
while (!pdmin_dma_block_done) {
420421
RUN_BACKGROUND_TASKS;
421422
}
423+
common_hal_mcu_disable_interrupts();
424+
pdmin_dma_block_done = false;
425+
common_hal_mcu_enable_interrupts();
422426

423427
// The mic is running all the time, so we don't need to wait the usual 10msec or 100msec
424428
// for it to start up.
@@ -430,6 +434,7 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
430434
buffer = second_buffer;
431435
descriptor = &second_descriptor;
432436
}
437+
433438
// Decimate and filter the buffer that was just filled.
434439
uint32_t samples_gathered = descriptor->BTCNT.reg / words_per_sample;
435440
// Don't run off the end of output buffer. Process only as many as needed.
@@ -472,7 +477,8 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
472477
}
473478
}
474479

475-
disable_event_channel(event_channel);
480+
disable_event_channel(pdmin_event_channel);
481+
pdmin_event_channel = EVSYS_SYNCH_NUM; // Invalid event_channel.
476482
dma_free_channel(dma_channel);
477483
// Turn off serializer, but leave clock on, to avoid mic startup delay.
478484
i2s_set_serializer_enable(self->serializer, false);
@@ -481,5 +487,4 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
481487
}
482488

483489
void common_hal_audiobusio_pdmin_record_to_file(audiobusio_pdmin_obj_t* self, uint8_t* buffer, uint32_t length) {
484-
485490
}

ports/atmel-samd/common-hal/audiobusio/PDMIn.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ typedef struct {
4646

4747
void pdmin_reset(void);
4848

49+
void pdmin_evsys_handler(void);
50+
4951
void pdmin_background(void);
5052

5153
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_AUDIOBUSIO_AUDIOOUT_H

ports/atmel-samd/common-hal/busio/I2C.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "shared-bindings/microcontroller/__init__.h"
3737
#include "supervisor/shared/translate.h"
3838

39-
#include "common-hal/busio/SPI.h" // for never_reset_sercom
39+
#include "common-hal/busio/__init__.h"
4040

4141
// Number of times to try to send packet if failed.
4242
#define ATTEMPTS 2

ports/atmel-samd/common-hal/busio/SPI.c

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,51 +32,16 @@
3232
#include "peripheral_clk_config.h"
3333

3434
#include "supervisor/board.h"
35+
#include "common-hal/busio/__init__.h"
3536
#include "common-hal/microcontroller/Pin.h"
37+
3638
#include "hal/include/hal_gpio.h"
3739
#include "hal/include/hal_spi_m_sync.h"
3840
#include "hal/include/hpl_spi_m_sync.h"
3941

4042
#include "samd/dma.h"
4143
#include "samd/sercom.h"
4244

43-
bool never_reset_sercoms[SERCOM_INST_NUM];
44-
45-
void never_reset_sercom(Sercom *sercom) {
46-
// Reset all SERCOMs except the ones being used by on-board devices.
47-
Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
48-
for (int i = 0; i < SERCOM_INST_NUM; i++) {
49-
if (sercom_instances[i] == sercom) {
50-
never_reset_sercoms[i] = true;
51-
break;
52-
}
53-
}
54-
}
55-
56-
void allow_reset_sercom(Sercom *sercom) {
57-
// Reset all SERCOMs except the ones being used by on-board devices.
58-
Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
59-
for (int i = 0; i < SERCOM_INST_NUM; i++) {
60-
if (sercom_instances[i] == sercom) {
61-
never_reset_sercoms[i] = false;
62-
break;
63-
}
64-
}
65-
}
66-
67-
void reset_sercoms(void) {
68-
// Reset all SERCOMs except the ones being used by on-board devices.
69-
Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
70-
for (int i = 0; i < SERCOM_INST_NUM; i++) {
71-
if (never_reset_sercoms[i]) {
72-
continue;
73-
}
74-
// SWRST is same for all modes of SERCOMs.
75-
sercom_instances[i]->SPI.CTRLA.bit.SWRST = 1;
76-
}
77-
}
78-
79-
8045
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
8146
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
8247
const mcu_pin_obj_t *miso) {

ports/atmel-samd/common-hal/busio/SPI.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,4 @@ typedef struct {
4242
uint8_t MISO_pin;
4343
} busio_spi_obj_t;
4444

45-
void reset_sercoms(void);
46-
void never_reset_sercom(Sercom *sercom);
47-
48-
4945
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_SPI_H

ports/atmel-samd/common-hal/busio/UART.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
#include "samd/sercom.h"
4747

48-
#include "common-hal/busio/SPI.h" // for never_reset_sercom
48+
#include "common-hal/busio/__init__.h"
4949

5050
#define UART_DEBUG(...) (void)0
5151
// #define UART_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,63 @@
1-
// No busio module functions.
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "samd/sercom.h"
28+
29+
static bool never_reset_sercoms[SERCOM_INST_NUM];
30+
31+
void never_reset_sercom(Sercom *sercom) {
32+
// Reset all SERCOMs except the ones being used by on-board devices.
33+
Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
34+
for (int i = 0; i < SERCOM_INST_NUM; i++) {
35+
if (sercom_instances[i] == sercom) {
36+
never_reset_sercoms[i] = true;
37+
break;
38+
}
39+
}
40+
}
41+
42+
void allow_reset_sercom(Sercom *sercom) {
43+
// Reset all SERCOMs except the ones being used by on-board devices.
44+
Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
45+
for (int i = 0; i < SERCOM_INST_NUM; i++) {
46+
if (sercom_instances[i] == sercom) {
47+
never_reset_sercoms[i] = false;
48+
break;
49+
}
50+
}
51+
}
52+
53+
void reset_sercoms(void) {
54+
// Reset all SERCOMs except the ones being used by on-board devices.
55+
Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
56+
for (int i = 0; i < SERCOM_INST_NUM; i++) {
57+
if (never_reset_sercoms[i]) {
58+
continue;
59+
}
60+
// SWRST is same for all modes of SERCOMs.
61+
sercom_instances[i]->SPI.CTRLA.bit.SWRST = 1;
62+
}
63+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_INIT_H
28+
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_INIT_H
29+
30+
void reset_sercoms(void);
31+
void allow_reset_sercom(Sercom *sercom);
32+
void never_reset_sercom(Sercom *sercom);
33+
34+
35+
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_INIT_H

ports/atmel-samd/supervisor/port.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,42 @@
5353
#error Unknown chip family
5454
#endif
5555

56+
#if CIRCUITPY_ANALOGIO
5657
#include "common-hal/analogio/AnalogIn.h"
5758
#include "common-hal/analogio/AnalogOut.h"
59+
#endif
60+
61+
#if CIRCUITPY_AUDIOBUSIO
5862
#include "common-hal/audiobusio/PDMIn.h"
5963
#include "common-hal/audiobusio/I2SOut.h"
64+
#endif
65+
66+
#if CIRCUITPY_AUDIOIO
6067
#include "common-hal/audioio/AudioOut.h"
61-
#include "common-hal/busio/SPI.h"
68+
#endif
69+
70+
#if CIRCUITPY_BUSIO
71+
#include "common-hal/busio/__init__.h"
72+
#endif
73+
6274
#include "common-hal/microcontroller/Pin.h"
75+
76+
#if CIRCUITPY_PULSEIO
6377
#include "common-hal/pulseio/PulseIn.h"
6478
#include "common-hal/pulseio/PulseOut.h"
79+
#endif
80+
81+
#if CIRCUITPY_PWMIO
6582
#include "common-hal/pwmio/PWMOut.h"
83+
#endif
84+
85+
#if CIRCUITPY_PS2IO
6686
#include "common-hal/ps2io/Ps2.h"
87+
#endif
88+
89+
#if CIRCUITPY_RTC
6790
#include "common-hal/rtc/RTC.h"
91+
#endif
6892

6993
#if CIRCUITPY_TOUCHIO_USE_NATIVE
7094
#include "common-hal/touchio/TouchIn.h"
@@ -516,8 +540,13 @@ void evsyshandler_common(void) {
516540
supervisor_tick();
517541
}
518542
#endif
543+
519544
#if CIRCUITPY_AUDIOIO || CIRCUITPY_AUDIOBUSIO
520-
audio_evsys_handler();
545+
audio_dma_evsys_handler();
546+
#endif
547+
548+
#if CIRCUITPY_AUDIOBUSIO
549+
pdmin_evsys_handler();
521550
#endif
522551
}
523552

0 commit comments

Comments
 (0)