From e2565e2305df87c12f115514d66634ba9bfdfd60 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 23 Mar 2023 14:00:14 -0500 Subject: [PATCH 1/6] mimxrt10xx: Add PWMAudioOut .. via a peripheral known as the "MQS" (medium quality sound). It uses an ~192kHz PWM signal to generate audio. It sounds OK on a small speaker with no amplifier. There's a small pop when starting/stopping audio, as is typical. --- .../mimxrt10xx/common-hal/audiobusio/I2SOut.h | 4 +- .../common-hal/audiobusio/__init__.c | 1 + .../common-hal/audiopwmio/PWMAudioOut.c | 159 ++++++++++++++++++ .../common-hal/audiopwmio/PWMAudioOut.h | 43 +++++ .../common-hal/audiopwmio/__init__.c | 0 .../common-hal/audiopwmio/__init__.h | 0 ports/mimxrt10xx/mpconfigport.mk | 1 + .../mimxrt10xx/MIMXRT1011/periph.c | 18 +- .../mimxrt10xx/MIMXRT1011/periph.h | 3 + .../mimxrt10xx/MIMXRT1021/periph.c | 10 ++ .../mimxrt10xx/MIMXRT1021/periph.h | 3 + .../mimxrt10xx/MIMXRT1062/periph.c | 10 ++ .../mimxrt10xx/MIMXRT1062/periph.h | 3 + 13 files changed, 246 insertions(+), 9 deletions(-) create mode 100644 ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c create mode 100644 ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h create mode 100644 ports/mimxrt10xx/common-hal/audiopwmio/__init__.c create mode 100644 ports/mimxrt10xx/common-hal/audiopwmio/__init__.h diff --git a/ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h b/ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h index c4194402be30e..2473eb4cab277 100644 --- a/ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h +++ b/ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h @@ -26,15 +26,13 @@ #pragma once +#if CIRCUITPY_AUDIOBUSIO_I2SOUT #include "supervisor/background_callback.h" #include "common-hal/microcontroller/Pin.h" #include "common-hal/audiobusio/__init__.h" // Some boards don't implement I2SOut, so suppress any routines from here. -#if CIRCUITPY_AUDIOBUSIO_I2SOUT - -#include "sdk/drivers/sai/fsl_sai.h" typedef struct { mp_obj_base_t base; diff --git a/ports/mimxrt10xx/common-hal/audiobusio/__init__.c b/ports/mimxrt10xx/common-hal/audiobusio/__init__.c index 515c39a268a28..f2de99f0040f9 100644 --- a/ports/mimxrt10xx/common-hal/audiobusio/__init__.c +++ b/ports/mimxrt10xx/common-hal/audiobusio/__init__.c @@ -284,6 +284,7 @@ static void i2s_fill_buffer(i2s_t *self) { static void i2s_callback_fun(void *self_in) { i2s_t *self = self_in; + mp_printf(&mp_plat_print, "."); i2s_fill_buffer(self); } diff --git a/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c new file mode 100644 index 0000000000000..dd50c7950a3b4 --- /dev/null +++ b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c @@ -0,0 +1,159 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +#include "mpconfigport.h" + +#include "py/gc.h" +#include "py/mperrno.h" +#include "py/runtime.h" +#include "common-hal/audiobusio/__init__.h" +#include "common-hal/audiopwmio/PWMAudioOut.h" +#include "shared-bindings/audiopwmio/PWMAudioOut.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "supervisor/shared/translate/translate.h" +#include "supervisor/shared/tick.h" + +// Where required we use identifier names that are required by NXP's +// API, even though they do not conform to the naming standards that Adafruit +// strives to adhere to. https://www.adafruit.com/blacklivesmatter +#include "sdk/drivers/sai/fsl_sai.h" + +STATIC void config_periph_pin(const mcu_periph_obj_t *periph) { + if (!periph) { + return; + } + if (periph->pin->mux_reg) { + IOMUXC_SetPinMux( + periph->pin->mux_reg, periph->mux_mode, + periph->input_reg, periph->input_idx, + 0, + 1); + } + + IOMUXC_SetPinConfig(0, 0, 0, 0, + periph->pin->cfg_reg, + IOMUXC_SW_PAD_CTL_PAD_HYS(0) + | IOMUXC_SW_PAD_CTL_PAD_PUS(0) + | IOMUXC_SW_PAD_CTL_PAD_PUE(0) + | IOMUXC_SW_PAD_CTL_PAD_PKE(1) + | IOMUXC_SW_PAD_CTL_PAD_ODE(0) + | IOMUXC_SW_PAD_CTL_PAD_SPEED(2) + | IOMUXC_SW_PAD_CTL_PAD_DSE(4) + | IOMUXC_SW_PAD_CTL_PAD_SRE(0)); +} + +static void config_mqs(void) { + CCM->CCGR0 = (CCM->CCGR0 & (~CCM_CCGR0_CG2_MASK)) | CCM_CCGR0_CG2(3); /* Enable MQS hmclk. */ + + IOMUXC_MQSEnterSoftwareReset(IOMUXC_GPR, true); /* Reset MQS. */ + IOMUXC_MQSEnterSoftwareReset(IOMUXC_GPR, false); /* Release reset MQS. */ + IOMUXC_MQSEnable(IOMUXC_GPR, true); /* Enable MQS. */ + IOMUXC_MQSConfig(IOMUXC_GPR, kIOMUXC_MqsPwmOverSampleRate64, 0u); /* 98.304MHz/64/(0+1) = 1.536MHz + Higher frequency PWM involves less low frequen cy harmonic.*/ + +} + +// Caller validates that pins are free. +void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *self, + const mcu_pin_obj_t *left_channel, const mcu_pin_obj_t *right_channel, uint16_t default_value) { + + int instance = -1; + const mcu_periph_obj_t *left_periph = find_pin_function(mcu_mqs_left_list, left_channel, &instance, MP_QSTR_left_channel); + const mcu_periph_obj_t *right_periph = find_pin_function(mcu_mqs_right_list, right_channel, &instance, MP_QSTR_right_channel); + + mp_printf(&mp_plat_print, "note: peripheral %d\n", instance); + sai_transceiver_t config; + SAI_GetClassicI2SConfig(&config, kSAI_WordWidth16bits, kSAI_Stereo, 1U << 0u); + config.frameSync.frameSyncEarly = false; + config.frameSync.frameSyncPolarity = kSAI_PolarityActiveHigh; + // config.syncMode = kSAI_ModeAsync; + config.fifo.fifoPacking = kSAI_FifoPackingDisabled; + // These identifier names are required by NXP's API, even though they do + // not conform to the naming standards that Adafruit strives to adhere to. + // https://www.adafruit.com/blacklivesmatter + // config.masterSlave = kSAI_Master; + port_i2s_initialize(&self->i2s, instance, &config); + + self->left_channel = left_channel; + self->right_channel = right_channel; + claim_pin(left_channel); + claim_pin(right_channel); + config_periph_pin(left_periph); + config_periph_pin(right_periph); + config_mqs(); +} + +bool common_hal_audiopwmio_pwmaudioout_deinited(audiopwmio_pwmaudioout_obj_t *self) { + return port_i2s_deinited(&self->i2s); +} + +void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t *self) { + if (common_hal_audiopwmio_pwmaudioout_deinited(self)) { + return; + } + + port_i2s_deinit(&self->i2s); + + common_hal_reset_pin(self->left_channel); + self->left_channel = NULL; + + common_hal_reset_pin(self->right_channel); + self->right_channel = NULL; + + IOMUXC_MQSEnterSoftwareReset(IOMUXC_GPR, true); /* Reset MQS. */ + CCM->CCGR0 = CCM->CCGR0 & (~CCM_CCGR0_CG2_MASK); /* Disable MQS hmclk. */ +} + +void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self, + mp_obj_t sample, bool loop) { + if (common_hal_audiopwmio_pwmaudioout_get_playing(self)) { + common_hal_audiopwmio_pwmaudioout_stop(self); + } + port_i2s_play(&self->i2s, sample, loop); +} + +void common_hal_audiopwmio_pwmaudioout_pause(audiopwmio_pwmaudioout_obj_t *self) { + port_i2s_pause(&self->i2s); +} + +void common_hal_audiopwmio_pwmaudioout_resume(audiopwmio_pwmaudioout_obj_t *self) { + port_i2s_resume(&self->i2s); +} + +bool common_hal_audiopwmio_pwmaudioout_get_paused(audiopwmio_pwmaudioout_obj_t *self) { + return port_i2s_get_paused(&self->i2s); +} + +void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t *self) { + port_i2s_stop(&self->i2s); +} + +bool common_hal_audiopwmio_pwmaudioout_get_playing(audiopwmio_pwmaudioout_obj_t *self) { + return port_i2s_get_playing(&self->i2s); +} diff --git a/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h new file mode 100644 index 0000000000000..27cebc649bd73 --- /dev/null +++ b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#if CIRCUITPY_AUDIOBUSIO_I2SOUT +#include "supervisor/background_callback.h" +#include "common-hal/microcontroller/Pin.h" + +#include "common-hal/audiobusio/__init__.h" + +// Some boards don't implement I2SOut, so suppress any routines from here. + +typedef struct { + mp_obj_base_t base; + i2s_t i2s; + const mcu_pin_obj_t *left_channel, *right_channel; +} audiopwmio_pwmaudioout_obj_t; + +#endif diff --git a/ports/mimxrt10xx/common-hal/audiopwmio/__init__.c b/ports/mimxrt10xx/common-hal/audiopwmio/__init__.c new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/ports/mimxrt10xx/common-hal/audiopwmio/__init__.h b/ports/mimxrt10xx/common-hal/audiopwmio/__init__.h new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/ports/mimxrt10xx/mpconfigport.mk b/ports/mimxrt10xx/mpconfigport.mk index a952460259e6b..f7ab3d8130c94 100644 --- a/ports/mimxrt10xx/mpconfigport.mk +++ b/ports/mimxrt10xx/mpconfigport.mk @@ -17,6 +17,7 @@ CIRCUITPY_AUDIOCORE = 1 CIRCUITPY_AUDIOIO = 0 CIRCUITPY_AUDIOMIXER = 1 CIRCUITPY_AUDIOMP3 = 1 +CIRCUITPY_AUDIOPWMIO = 1 CIRCUITPY_BUSDEVICE = 1 CIRCUITPY_COUNTIO = 0 CIRCUITPY_FREQUENCYIO = 0 diff --git a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1011/periph.c b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1011/periph.c index fbbf65c8a5950..b67f7b117969d 100644 --- a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1011/periph.c +++ b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1011/periph.c @@ -169,25 +169,31 @@ const mcu_pwm_obj_t mcu_pwm_list[20] = { const mcu_periph_obj_t mcu_sai_rx_bclk_list[] = { PERIPH_PIN(1, 0, 0, 0, &pin_GPIO_08), - PERIPH_PIN(2, 1, 0, 0, &pin_GPIO_00), + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_00), }; const mcu_periph_obj_t mcu_sai_rx_data0_list[] = { PERIPH_PIN(1, 0, 0, 0, &pin_GPIO_03), - PERIPH_PIN(2, 1, 0, 0, &pin_GPIO_SD_03), + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_SD_03), }; const mcu_periph_obj_t mcu_sai_rx_sync_list[] = { PERIPH_PIN(1, 0, 0, 0, &pin_GPIO_02), - PERIPH_PIN(2, 1, 0, 0, &pin_GPIO_SD_04), + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_SD_04), }; const mcu_periph_obj_t mcu_sai_tx_bclk_list[] = { PERIPH_PIN(1, 0, 0, 0, &pin_GPIO_06), - PERIPH_PIN(2, 1, 0, 0, &pin_GPIO_SD_01), + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_SD_01), }; const mcu_periph_obj_t mcu_sai_tx_data0_list[] = { PERIPH_PIN(1, 0, 0, 0, &pin_GPIO_04), - PERIPH_PIN(2, 1, 0, 0, &pin_GPIO_SD_02), + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_SD_02), }; const mcu_periph_obj_t mcu_sai_tx_sync_list[] = { PERIPH_PIN(1, 0, 0, 0, &pin_GPIO_07), - PERIPH_PIN(2, 1, 0, 0, &pin_GPIO_SD_00), + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_SD_00), +}; +const mcu_periph_obj_t mcu_mqs_left_list[] = { + PERIPH_PIN(3, 4, 0, 0, &pin_GPIO_AD_01), +}; +const mcu_periph_obj_t mcu_mqs_right_list[] = { + PERIPH_PIN(3, 4, 0, 0, &pin_GPIO_AD_02), }; diff --git a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1011/periph.h b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1011/periph.h index 109717528f530..045e33ca0e5f5 100644 --- a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1011/periph.h +++ b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1011/periph.h @@ -54,4 +54,7 @@ extern const mcu_periph_obj_t mcu_sai_tx_bclk_list[2]; extern const mcu_periph_obj_t mcu_sai_tx_data0_list[2]; extern const mcu_periph_obj_t mcu_sai_tx_sync_list[2]; +extern const mcu_periph_obj_t mcu_mqs_left_list[1]; +extern const mcu_periph_obj_t mcu_mqs_right_list[1]; + #endif // MICROPY_INCLUDED_MIMXRT10XX_MIMXRT1011_PERIPHERALS_MIMXRT1011_PERIPH_H diff --git a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1021/periph.c b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1021/periph.c index b80fcf5065be4..59bd3cc1054c8 100644 --- a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1021/periph.c +++ b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1021/periph.c @@ -311,3 +311,13 @@ const mcu_periph_obj_t mcu_sai_tx_sync_list[] = { PERIPH_PIN(3, 3, 0, 0, &pin_GPIO_EMC_34), PERIPH_PIN(3, 3, 0, 0, &pin_GPIO_SD_B1_07), }; +const mcu_periph_obj_t mcu_mqs_left_list[] = { + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_AD_B0_07), + PERIPH_PIN(3, 2, 0, 0, &pin_GPIO_EMC_17), + PERIPH_PIN(3, 3, 0, 0, &pin_GPIO_EMC_38), +}; +const mcu_periph_obj_t mcu_mqs_right_list[] = { + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_AD_B0_06), + PERIPH_PIN(3, 2, 0, 0, &pin_GPIO_EMC_16), + PERIPH_PIN(3, 3, 0, 0, &pin_GPIO_EMC_37), +}; diff --git a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1021/periph.h b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1021/periph.h index 31ac10e651d48..51c97b3eef2c6 100644 --- a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1021/periph.h +++ b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1021/periph.h @@ -53,3 +53,6 @@ extern const mcu_periph_obj_t mcu_sai_rx_sync_list[7]; extern const mcu_periph_obj_t mcu_sai_tx_bclk_list[7]; extern const mcu_periph_obj_t mcu_sai_tx_data0_list[7]; extern const mcu_periph_obj_t mcu_sai_tx_sync_list[7]; + +extern const mcu_periph_obj_t mcu_mqs_left_list[3]; +extern const mcu_periph_obj_t mcu_mqs_right_list[3]; diff --git a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.c b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.c index 8c038bdb7189d..c43f4eb96b6ff 100644 --- a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.c +++ b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.c @@ -362,3 +362,13 @@ const mcu_periph_obj_t mcu_sai_tx_sync_list[] = { PERIPH_PIN(3, 3, 0, 0, &pin_GPIO_EMC_39), PERIPH_PIN(3, 8, 0, 0, &pin_GPIO_SD_B1_02), }; +const mcu_periph_obj_t mcu_mqs_left_list[] = { + PERIPH_PIN(3, 3, 0, 0, &pin_GPIO_EMC_14), + PERIPH_PIN(3, 2, 0, 0, &pin_GPIO_B0_01), + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_AD_B0_05), +}; +const mcu_periph_obj_t mcu_mqs_right_list[] = { + PERIPH_PIN(3, 3, 0, 0, &pin_GPIO_EMC_13), + PERIPH_PIN(3, 2, 0, 0, &pin_GPIO_B0_00), + PERIPH_PIN(3, 1, 0, 0, &pin_GPIO_AD_B0_04), +}; diff --git a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.h b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.h index ab8eb226780c9..28ea8e37429bd 100644 --- a/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.h +++ b/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.h @@ -54,4 +54,7 @@ extern const mcu_periph_obj_t mcu_sai_tx_bclk_list[7]; extern const mcu_periph_obj_t mcu_sai_tx_data0_list[7]; extern const mcu_periph_obj_t mcu_sai_tx_sync_list[7]; +extern const mcu_periph_obj_t mcu_mqs_left_list[3]; +extern const mcu_periph_obj_t mcu_mqs_right_list[3]; + #endif // MICROPY_INCLUDED_MIMXRT10XX_MIMXRT1062_PERIPHERALS_MIMXRT1011_PERIPH_H From e1c8a3062a0c5000162ff8730dcb28dc2637a78c Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 23 Mar 2023 21:06:53 -0500 Subject: [PATCH 2/6] remove debugging print --- ports/mimxrt10xx/common-hal/audiobusio/__init__.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/mimxrt10xx/common-hal/audiobusio/__init__.c b/ports/mimxrt10xx/common-hal/audiobusio/__init__.c index f2de99f0040f9..515c39a268a28 100644 --- a/ports/mimxrt10xx/common-hal/audiobusio/__init__.c +++ b/ports/mimxrt10xx/common-hal/audiobusio/__init__.c @@ -284,7 +284,6 @@ static void i2s_fill_buffer(i2s_t *self) { static void i2s_callback_fun(void *self_in) { i2s_t *self = self_in; - mp_printf(&mp_plat_print, "."); i2s_fill_buffer(self); } From 0c0e06c940848e9bf26642272e5bb2eb0fa1352a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 23 Mar 2023 21:10:04 -0500 Subject: [PATCH 3/6] remove comment that was copypasted --- ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h index 27cebc649bd73..b9e4279927af0 100644 --- a/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h +++ b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h @@ -32,8 +32,6 @@ #include "common-hal/audiobusio/__init__.h" -// Some boards don't implement I2SOut, so suppress any routines from here. - typedef struct { mp_obj_base_t base; i2s_t i2s; From b1d9331367b4717996cf4c353a18212491dcfa5b Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 23 Mar 2023 21:11:05 -0500 Subject: [PATCH 4/6] move comment next to its associated #if --- ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h b/ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h index 2473eb4cab277..319e52c1592fc 100644 --- a/ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h +++ b/ports/mimxrt10xx/common-hal/audiobusio/I2SOut.h @@ -26,14 +26,13 @@ #pragma once +// Some boards don't implement I2SOut, so suppress any routines from here. #if CIRCUITPY_AUDIOBUSIO_I2SOUT #include "supervisor/background_callback.h" #include "common-hal/microcontroller/Pin.h" #include "common-hal/audiobusio/__init__.h" -// Some boards don't implement I2SOut, so suppress any routines from here. - typedef struct { mp_obj_base_t base; i2s_t i2s; From d6a067b0e564cc9d40d7782541393c5fe63f99e2 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 27 Mar 2023 10:52:23 -0500 Subject: [PATCH 5/6] Make PWMAudioOut object ll, have finaliser .. for reasons stated in the comment --- shared-bindings/audiopwmio/PWMAudioOut.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/shared-bindings/audiopwmio/PWMAudioOut.c b/shared-bindings/audiopwmio/PWMAudioOut.c index b4cea751840a4..56c6412bc6302 100644 --- a/shared-bindings/audiopwmio/PWMAudioOut.c +++ b/shared-bindings/audiopwmio/PWMAudioOut.c @@ -114,7 +114,12 @@ STATIC mp_obj_t audiopwmio_pwmaudioout_make_new(const mp_obj_type_t *type, size_ validate_obj_is_free_pin_or_none(args[ARG_right_channel].u_obj, MP_QSTR_right_channel); // create AudioOut object from the given pin - audiopwmio_pwmaudioout_obj_t *self = m_new_obj(audiopwmio_pwmaudioout_obj_t); + // The object is made long-lived because many implementations keep + // a pointer to the object (e.g., for the interrupt handler), which + // will not work properly if the object is moved. It is created + // with a finaliser as some ports use these (rather than 'reset' functions) + // to ensure resources are collected at interpreter shutdown. + audiopwmio_pwmaudioout_obj_t *self = m_new_ll_obj_with_finaliser(audiopwmio_pwmaudioout_obj_t); self->base.type = &audiopwmio_pwmaudioout_type; common_hal_audiopwmio_pwmaudioout_construct(self, left_channel_pin, right_channel_pin, args[ARG_quiescent_value].u_int); @@ -249,6 +254,7 @@ MP_PROPERTY_GETTER(audiopwmio_pwmaudioout_paused_obj, STATIC const mp_rom_map_elem_t audiopwmio_pwmaudioout_locals_dict_table[] = { // Methods { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiopwmio_pwmaudioout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiopwmio_pwmaudioout_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audiopwmio_pwmaudioout___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audiopwmio_pwmaudioout_play_obj) }, From 9d090ee73eabe5a84eb7f2aeacad431b4994cd29 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 27 Mar 2023 10:52:52 -0500 Subject: [PATCH 6/6] get rid of another debug print --- ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c index dd50c7950a3b4..8650522b4141b 100644 --- a/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c +++ b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.c @@ -87,7 +87,6 @@ void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *s const mcu_periph_obj_t *left_periph = find_pin_function(mcu_mqs_left_list, left_channel, &instance, MP_QSTR_left_channel); const mcu_periph_obj_t *right_periph = find_pin_function(mcu_mqs_right_list, right_channel, &instance, MP_QSTR_right_channel); - mp_printf(&mp_plat_print, "note: peripheral %d\n", instance); sai_transceiver_t config; SAI_GetClassicI2SConfig(&config, kSAI_WordWidth16bits, kSAI_Stereo, 1U << 0u); config.frameSync.frameSyncEarly = false;