|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Jeff Epler for Adafruit Industries |
| 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 <stdint.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "mpconfigport.h" |
| 31 | + |
| 32 | +#include "py/gc.h" |
| 33 | +#include "py/mperrno.h" |
| 34 | +#include "py/runtime.h" |
| 35 | +#include "common-hal/audiobusio/__init__.h" |
| 36 | +#include "common-hal/audiopwmio/PWMAudioOut.h" |
| 37 | +#include "shared-bindings/audiopwmio/PWMAudioOut.h" |
| 38 | +#include "shared-bindings/microcontroller/Pin.h" |
| 39 | +#include "supervisor/shared/translate/translate.h" |
| 40 | +#include "supervisor/shared/tick.h" |
| 41 | + |
| 42 | +// Where required we use identifier names that are required by NXP's |
| 43 | +// API, even though they do not conform to the naming standards that Adafruit |
| 44 | +// strives to adhere to. https://www.adafruit.com/blacklivesmatter |
| 45 | +#include "sdk/drivers/sai/fsl_sai.h" |
| 46 | + |
| 47 | +STATIC void config_periph_pin(const mcu_periph_obj_t *periph) { |
| 48 | + if (!periph) { |
| 49 | + return; |
| 50 | + } |
| 51 | + if (periph->pin->mux_reg) { |
| 52 | + IOMUXC_SetPinMux( |
| 53 | + periph->pin->mux_reg, periph->mux_mode, |
| 54 | + periph->input_reg, periph->input_idx, |
| 55 | + 0, |
| 56 | + 1); |
| 57 | + } |
| 58 | + |
| 59 | + IOMUXC_SetPinConfig(0, 0, 0, 0, |
| 60 | + periph->pin->cfg_reg, |
| 61 | + IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
| 62 | + | IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
| 63 | + | IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
| 64 | + | IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
| 65 | + | IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
| 66 | + | IOMUXC_SW_PAD_CTL_PAD_SPEED(2) |
| 67 | + | IOMUXC_SW_PAD_CTL_PAD_DSE(4) |
| 68 | + | IOMUXC_SW_PAD_CTL_PAD_SRE(0)); |
| 69 | +} |
| 70 | + |
| 71 | +static void config_mqs(void) { |
| 72 | + CCM->CCGR0 = (CCM->CCGR0 & (~CCM_CCGR0_CG2_MASK)) | CCM_CCGR0_CG2(3); /* Enable MQS hmclk. */ |
| 73 | + |
| 74 | + IOMUXC_MQSEnterSoftwareReset(IOMUXC_GPR, true); /* Reset MQS. */ |
| 75 | + IOMUXC_MQSEnterSoftwareReset(IOMUXC_GPR, false); /* Release reset MQS. */ |
| 76 | + IOMUXC_MQSEnable(IOMUXC_GPR, true); /* Enable MQS. */ |
| 77 | + IOMUXC_MQSConfig(IOMUXC_GPR, kIOMUXC_MqsPwmOverSampleRate64, 0u); /* 98.304MHz/64/(0+1) = 1.536MHz |
| 78 | + Higher frequency PWM involves less low frequen cy harmonic.*/ |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +// Caller validates that pins are free. |
| 83 | +void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *self, |
| 84 | + const mcu_pin_obj_t *left_channel, const mcu_pin_obj_t *right_channel, uint16_t default_value) { |
| 85 | + |
| 86 | + int instance = -1; |
| 87 | + const mcu_periph_obj_t *left_periph = find_pin_function(mcu_mqs_left_list, left_channel, &instance, MP_QSTR_left_channel); |
| 88 | + const mcu_periph_obj_t *right_periph = find_pin_function(mcu_mqs_right_list, right_channel, &instance, MP_QSTR_right_channel); |
| 89 | + |
| 90 | + sai_transceiver_t config; |
| 91 | + SAI_GetClassicI2SConfig(&config, kSAI_WordWidth16bits, kSAI_Stereo, 1U << 0u); |
| 92 | + config.frameSync.frameSyncEarly = false; |
| 93 | + config.frameSync.frameSyncPolarity = kSAI_PolarityActiveHigh; |
| 94 | + // config.syncMode = kSAI_ModeAsync; |
| 95 | + config.fifo.fifoPacking = kSAI_FifoPackingDisabled; |
| 96 | + // These identifier names are required by NXP's API, even though they do |
| 97 | + // not conform to the naming standards that Adafruit strives to adhere to. |
| 98 | + // https://www.adafruit.com/blacklivesmatter |
| 99 | + // config.masterSlave = kSAI_Master; |
| 100 | + port_i2s_initialize(&self->i2s, instance, &config); |
| 101 | + |
| 102 | + self->left_channel = left_channel; |
| 103 | + self->right_channel = right_channel; |
| 104 | + claim_pin(left_channel); |
| 105 | + claim_pin(right_channel); |
| 106 | + config_periph_pin(left_periph); |
| 107 | + config_periph_pin(right_periph); |
| 108 | + config_mqs(); |
| 109 | +} |
| 110 | + |
| 111 | +bool common_hal_audiopwmio_pwmaudioout_deinited(audiopwmio_pwmaudioout_obj_t *self) { |
| 112 | + return port_i2s_deinited(&self->i2s); |
| 113 | +} |
| 114 | + |
| 115 | +void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t *self) { |
| 116 | + if (common_hal_audiopwmio_pwmaudioout_deinited(self)) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + port_i2s_deinit(&self->i2s); |
| 121 | + |
| 122 | + common_hal_reset_pin(self->left_channel); |
| 123 | + self->left_channel = NULL; |
| 124 | + |
| 125 | + common_hal_reset_pin(self->right_channel); |
| 126 | + self->right_channel = NULL; |
| 127 | + |
| 128 | + IOMUXC_MQSEnterSoftwareReset(IOMUXC_GPR, true); /* Reset MQS. */ |
| 129 | + CCM->CCGR0 = CCM->CCGR0 & (~CCM_CCGR0_CG2_MASK); /* Disable MQS hmclk. */ |
| 130 | +} |
| 131 | + |
| 132 | +void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self, |
| 133 | + mp_obj_t sample, bool loop) { |
| 134 | + if (common_hal_audiopwmio_pwmaudioout_get_playing(self)) { |
| 135 | + common_hal_audiopwmio_pwmaudioout_stop(self); |
| 136 | + } |
| 137 | + port_i2s_play(&self->i2s, sample, loop); |
| 138 | +} |
| 139 | + |
| 140 | +void common_hal_audiopwmio_pwmaudioout_pause(audiopwmio_pwmaudioout_obj_t *self) { |
| 141 | + port_i2s_pause(&self->i2s); |
| 142 | +} |
| 143 | + |
| 144 | +void common_hal_audiopwmio_pwmaudioout_resume(audiopwmio_pwmaudioout_obj_t *self) { |
| 145 | + port_i2s_resume(&self->i2s); |
| 146 | +} |
| 147 | + |
| 148 | +bool common_hal_audiopwmio_pwmaudioout_get_paused(audiopwmio_pwmaudioout_obj_t *self) { |
| 149 | + return port_i2s_get_paused(&self->i2s); |
| 150 | +} |
| 151 | + |
| 152 | +void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t *self) { |
| 153 | + port_i2s_stop(&self->i2s); |
| 154 | +} |
| 155 | + |
| 156 | +bool common_hal_audiopwmio_pwmaudioout_get_playing(audiopwmio_pwmaudioout_obj_t *self) { |
| 157 | + return port_i2s_get_playing(&self->i2s); |
| 158 | +} |
0 commit comments