circuitpython/ports/espressif/common-hal/audiobusio/I2SOut.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
4.4 KiB
C
Raw Normal View History

2020-10-30 11:05:16 -04:00
/*
* 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 "bindings/espidf/__init__.h"
// Some boards don't implement I2SOut, so suppress any routines from here.
#if CIRCUITPY_AUDIOBUSIO_I2SOUT
#include "extmod/vfs_fat.h"
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "common-hal/audiobusio/I2SOut.h"
#include "shared-bindings/audiobusio/I2SOut.h"
#include "shared-bindings/audiocore/RawSample.h"
#include "shared-bindings/microcontroller/Pin.h"
2022-05-27 15:59:54 -04:00
#include "supervisor/shared/translate/translate.h"
2020-10-30 11:05:16 -04:00
#include "driver/i2s_std.h"
2020-10-30 11:05:16 -04:00
// Caller validates that pins are free.
2021-03-15 09:57:36 -04:00
void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t *self,
const mcu_pin_obj_t *bit_clock, const mcu_pin_obj_t *word_select,
2023-08-17 14:39:14 -04:00
const mcu_pin_obj_t *data, const mcu_pin_obj_t *main_clock, bool left_justified) {
if (main_clock != NULL) {
mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_main_clock);
}
port_i2s_allocate_init(&self->i2s, left_justified);
i2s_std_config_t i2s_config = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(48000),
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = bit_clock->number,
.ws = word_select->number,
.dout = data->number,
.din = I2S_GPIO_UNUSED,
}
2020-10-30 11:05:16 -04:00
};
CHECK_ESP_RESULT(i2s_channel_init_std_mode(self->i2s.handle, &i2s_config));
2020-10-30 11:05:16 -04:00
self->bit_clock = bit_clock;
self->word_select = word_select;
self->data = data;
2021-02-04 16:30:05 -05:00
claim_pin(bit_clock);
claim_pin(word_select);
claim_pin(data);
2020-10-30 11:05:16 -04:00
}
2021-03-15 09:57:36 -04:00
bool common_hal_audiobusio_i2sout_deinited(audiobusio_i2sout_obj_t *self) {
return self->data == NULL;
2020-10-30 11:05:16 -04:00
}
2021-03-15 09:57:36 -04:00
void common_hal_audiobusio_i2sout_deinit(audiobusio_i2sout_obj_t *self) {
2020-10-30 11:05:16 -04:00
if (common_hal_audiobusio_i2sout_deinited(self)) {
return;
}
common_hal_audiobusio_i2sout_stop(self);
port_i2s_deinit(&self->i2s);
2020-10-30 11:05:16 -04:00
if (self->bit_clock) {
reset_pin_number(self->bit_clock->number);
}
self->bit_clock = NULL;
if (self->word_select) {
reset_pin_number(self->word_select->number);
}
self->word_select = NULL;
if (self->data) {
reset_pin_number(self->data->number);
}
self->data = NULL;
}
2021-03-15 09:57:36 -04:00
void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
mp_obj_t sample, bool loop) {
2020-10-30 11:05:16 -04:00
if (common_hal_audiobusio_i2sout_get_playing(self)) {
common_hal_audiobusio_i2sout_stop(self);
}
port_i2s_play(&self->i2s, sample, loop);
2020-10-30 11:05:16 -04:00
}
2021-03-15 09:57:36 -04:00
void common_hal_audiobusio_i2sout_pause(audiobusio_i2sout_obj_t *self) {
port_i2s_pause(&self->i2s);
2020-10-30 11:05:16 -04:00
}
2021-03-15 09:57:36 -04:00
void common_hal_audiobusio_i2sout_resume(audiobusio_i2sout_obj_t *self) {
port_i2s_resume(&self->i2s);
2020-10-30 11:05:16 -04:00
}
2021-03-15 09:57:36 -04:00
bool common_hal_audiobusio_i2sout_get_paused(audiobusio_i2sout_obj_t *self) {
return port_i2s_paused(&self->i2s);
2020-10-30 11:05:16 -04:00
}
2021-03-15 09:57:36 -04:00
void common_hal_audiobusio_i2sout_stop(audiobusio_i2sout_obj_t *self) {
port_i2s_stop(&self->i2s);
2020-10-30 11:05:16 -04:00
}
2021-03-15 09:57:36 -04:00
bool common_hal_audiobusio_i2sout_get_playing(audiobusio_i2sout_obj_t *self) {
return port_i2s_playing(&self->i2s);
2020-10-30 11:05:16 -04:00
}
#endif // CIRCUITPY_AUDIOBUSIO_I2SOUT