|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +#include "bindings/espidf/__init__.h" |
| 10 | + |
| 11 | +#include "common-hal/audio_i2sin/I2SIn.h" |
| 12 | +#include "py/runtime.h" |
| 13 | +#include "shared-bindings/audio_i2sin/I2SIn.h" |
| 14 | +#include "shared-bindings/microcontroller/Pin.h" |
| 15 | + |
| 16 | +#include "driver/i2s_std.h" |
| 17 | + |
| 18 | +#if CIRCUITPY_AUDIO_I2SIN |
| 19 | + |
| 20 | +void common_hal_audio_i2sin_i2sin_construct(audio_i2sin_i2sin_obj_t *self, |
| 21 | + const mcu_pin_obj_t *bit_clock, const mcu_pin_obj_t *word_select, |
| 22 | + const mcu_pin_obj_t *data, const mcu_pin_obj_t *main_clock, |
| 23 | + uint32_t sample_rate, uint8_t bit_depth, bool mono, bool left_justified) { |
| 24 | + |
| 25 | + if (bit_depth != 8 && bit_depth != 16 && bit_depth != 24 && bit_depth != 32) { |
| 26 | + mp_raise_ValueError(MP_ERROR_TEXT("bit_depth must be 8, 16, 24, or 32.")); |
| 27 | + } |
| 28 | + |
| 29 | + i2s_data_bit_width_t bit_width = (i2s_data_bit_width_t)bit_depth; |
| 30 | + |
| 31 | + i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER); |
| 32 | + esp_err_t err = i2s_new_channel(&chan_cfg, NULL, &self->rx_chan); |
| 33 | + if (err == ESP_ERR_NOT_FOUND) { |
| 34 | + mp_raise_RuntimeError(MP_ERROR_TEXT("Peripheral in use")); |
| 35 | + } |
| 36 | + CHECK_ESP_RESULT(err); |
| 37 | + |
| 38 | + // Always configure the bus as stereo. The newer-family I2S peripherals |
| 39 | + // (S2/S3/C-series) ignore I2S_SLOT_MODE_MONO on RX and write both slots |
| 40 | + // into the DMA buffer regardless, which yields buffers that fill at 2x |
| 41 | + // the WS rate and produces half-speed audio. By configuring stereo and |
| 42 | + // dropping one slot ourselves in record_to_buffer, behavior is uniform |
| 43 | + // across chips. |
| 44 | + i2s_std_slot_config_t slot_cfg = left_justified |
| 45 | + ? (i2s_std_slot_config_t)I2S_STD_MSB_SLOT_DEFAULT_CONFIG(bit_width, I2S_SLOT_MODE_STEREO) |
| 46 | + : (i2s_std_slot_config_t)I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(bit_width, I2S_SLOT_MODE_STEREO); |
| 47 | + |
| 48 | + i2s_std_config_t std_cfg = { |
| 49 | + .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(sample_rate), |
| 50 | + .slot_cfg = slot_cfg, |
| 51 | + .gpio_cfg = { |
| 52 | + .mclk = main_clock != NULL ? main_clock->number : I2S_GPIO_UNUSED, |
| 53 | + .bclk = bit_clock->number, |
| 54 | + .ws = word_select->number, |
| 55 | + .dout = I2S_GPIO_UNUSED, |
| 56 | + .din = data->number, |
| 57 | + }, |
| 58 | + }; |
| 59 | + CHECK_ESP_RESULT(i2s_channel_init_std_mode(self->rx_chan, &std_cfg)); |
| 60 | + CHECK_ESP_RESULT(i2s_channel_enable(self->rx_chan)); |
| 61 | + |
| 62 | + self->bit_clock = bit_clock; |
| 63 | + self->word_select = word_select; |
| 64 | + self->data = data; |
| 65 | + self->mclk = main_clock; |
| 66 | + self->sample_rate = sample_rate; |
| 67 | + self->bit_depth = bit_depth; |
| 68 | + self->mono = mono; |
| 69 | + |
| 70 | + claim_pin(bit_clock); |
| 71 | + claim_pin(word_select); |
| 72 | + claim_pin(data); |
| 73 | + if (main_clock) { |
| 74 | + claim_pin(main_clock); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +bool common_hal_audio_i2sin_i2sin_deinited(audio_i2sin_i2sin_obj_t *self) { |
| 79 | + return self->data == NULL; |
| 80 | +} |
| 81 | + |
| 82 | +void common_hal_audio_i2sin_i2sin_deinit(audio_i2sin_i2sin_obj_t *self) { |
| 83 | + if (common_hal_audio_i2sin_i2sin_deinited(self)) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (self->rx_chan) { |
| 88 | + i2s_channel_disable(self->rx_chan); |
| 89 | + i2s_del_channel(self->rx_chan); |
| 90 | + self->rx_chan = NULL; |
| 91 | + } |
| 92 | + |
| 93 | + if (self->bit_clock) { |
| 94 | + reset_pin_number(self->bit_clock->number); |
| 95 | + } |
| 96 | + self->bit_clock = NULL; |
| 97 | + |
| 98 | + if (self->word_select) { |
| 99 | + reset_pin_number(self->word_select->number); |
| 100 | + } |
| 101 | + self->word_select = NULL; |
| 102 | + |
| 103 | + if (self->data) { |
| 104 | + reset_pin_number(self->data->number); |
| 105 | + } |
| 106 | + self->data = NULL; |
| 107 | + |
| 108 | + if (self->mclk) { |
| 109 | + reset_pin_number(self->mclk->number); |
| 110 | + } |
| 111 | + self->mclk = NULL; |
| 112 | +} |
| 113 | + |
| 114 | +uint32_t common_hal_audio_i2sin_i2sin_record_to_buffer(audio_i2sin_i2sin_obj_t *self, |
| 115 | + void *buffer, uint32_t length) { |
| 116 | + size_t element_size = self->bit_depth / 8; |
| 117 | + // 24-bit samples occupy a 32-bit slot on the I2S bus. |
| 118 | + if (self->bit_depth == 24) { |
| 119 | + element_size = 4; |
| 120 | + } |
| 121 | + |
| 122 | + if (!self->mono) { |
| 123 | + size_t result = 0; |
| 124 | + esp_err_t err = i2s_channel_read(self->rx_chan, buffer, length * element_size, |
| 125 | + &result, portMAX_DELAY); |
| 126 | + CHECK_ESP_RESULT(err); |
| 127 | + return result / element_size; |
| 128 | + } |
| 129 | + |
| 130 | + // Mono: bus is configured stereo, so each WS frame yields two slots in |
| 131 | + // the DMA buffer. Read in chunks into a scratch and keep only the left |
| 132 | + // slot of each frame. |
| 133 | + uint8_t scratch[256]; |
| 134 | + const size_t frame_bytes = 2 * element_size; |
| 135 | + const size_t scratch_frames = sizeof(scratch) / frame_bytes; |
| 136 | + uint8_t *out = (uint8_t *)buffer; |
| 137 | + uint32_t produced = 0; |
| 138 | + while (produced < length) { |
| 139 | + size_t want_frames = length - produced; |
| 140 | + if (want_frames > scratch_frames) { |
| 141 | + want_frames = scratch_frames; |
| 142 | + } |
| 143 | + size_t got_bytes = 0; |
| 144 | + esp_err_t err = i2s_channel_read(self->rx_chan, scratch, |
| 145 | + want_frames * frame_bytes, &got_bytes, portMAX_DELAY); |
| 146 | + CHECK_ESP_RESULT(err); |
| 147 | + size_t got_frames = got_bytes / frame_bytes; |
| 148 | + for (size_t i = 0; i < got_frames; i++) { |
| 149 | + memcpy(out + produced * element_size, |
| 150 | + scratch + i * frame_bytes, |
| 151 | + element_size); |
| 152 | + produced++; |
| 153 | + } |
| 154 | + if (got_frames < want_frames) { |
| 155 | + break; |
| 156 | + } |
| 157 | + } |
| 158 | + return produced; |
| 159 | +} |
| 160 | + |
| 161 | +uint8_t common_hal_audio_i2sin_i2sin_get_bit_depth(audio_i2sin_i2sin_obj_t *self) { |
| 162 | + return self->bit_depth; |
| 163 | +} |
| 164 | + |
| 165 | +uint32_t common_hal_audio_i2sin_i2sin_get_sample_rate(audio_i2sin_i2sin_obj_t *self) { |
| 166 | + return self->sample_rate; |
| 167 | +} |
| 168 | + |
| 169 | +#endif // CIRCUITPY_AUDIO_I2SIN |
0 commit comments