Merge pull request #7785 from jepler/mimxrt10xx-mqs

mimxrt10xx: Add PWMAudioOut
This commit is contained in:
Dan Halbert 2023-03-27 20:46:11 -04:00 committed by GitHub
commit 77cd20af8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 250 additions and 12 deletions

View File

@ -26,16 +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.
#if CIRCUITPY_AUDIOBUSIO_I2SOUT
#include "sdk/drivers/sai/fsl_sai.h"
typedef struct {
mp_obj_base_t base;
i2s_t i2s;

View File

@ -0,0 +1,158 @@
/*
* 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 <stdint.h>
#include <string.h>
#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);
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);
}

View File

@ -0,0 +1,41 @@
/*
* 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"
typedef struct {
mp_obj_base_t base;
i2s_t i2s;
const mcu_pin_obj_t *left_channel, *right_channel;
} audiopwmio_pwmaudioout_obj_t;
#endif

View File

@ -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

View File

@ -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),
};

View File

@ -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

View File

@ -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),
};

View File

@ -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];

View File

@ -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),
};

View File

@ -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

View File

@ -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) },