|
| 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 "bindings/espidf/__init__.h" |
| 8 | + |
| 9 | +#include "common-hal/audiobusio/I2SIn.h" |
| 10 | +#include "py/runtime.h" |
| 11 | +#include "shared-bindings/audiobusio/I2SIn.h" |
| 12 | +#include "shared-bindings/microcontroller/Pin.h" |
| 13 | + |
| 14 | +#include "driver/i2s_std.h" |
| 15 | + |
| 16 | +#if CIRCUITPY_AUDIOBUSIO_I2SIN |
| 17 | + |
| 18 | +void common_hal_audiobusio_i2sin_construct(audiobusio_i2sin_obj_t *self, |
| 19 | + const mcu_pin_obj_t *bit_clock, const mcu_pin_obj_t *word_select, |
| 20 | + const mcu_pin_obj_t *data, const mcu_pin_obj_t *main_clock, |
| 21 | + uint32_t sample_rate, uint8_t bit_depth, bool mono, bool left_justified) { |
| 22 | + |
| 23 | + if (bit_depth != 8 && bit_depth != 16 && bit_depth != 24 && bit_depth != 32) { |
| 24 | + mp_raise_ValueError(MP_ERROR_TEXT("bit_depth must be 8, 16, 24, or 32.")); |
| 25 | + } |
| 26 | + |
| 27 | + i2s_data_bit_width_t bit_width = (i2s_data_bit_width_t)bit_depth; |
| 28 | + i2s_slot_mode_t slot_mode = mono ? I2S_SLOT_MODE_MONO : I2S_SLOT_MODE_STEREO; |
| 29 | + |
| 30 | + i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER); |
| 31 | + esp_err_t err = i2s_new_channel(&chan_cfg, NULL, &self->rx_chan); |
| 32 | + if (err == ESP_ERR_NOT_FOUND) { |
| 33 | + mp_raise_RuntimeError(MP_ERROR_TEXT("Peripheral in use")); |
| 34 | + } |
| 35 | + CHECK_ESP_RESULT(err); |
| 36 | + |
| 37 | + i2s_std_slot_config_t slot_cfg = left_justified |
| 38 | + ? (i2s_std_slot_config_t)I2S_STD_MSB_SLOT_DEFAULT_CONFIG(bit_width, slot_mode) |
| 39 | + : (i2s_std_slot_config_t)I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(bit_width, slot_mode); |
| 40 | + |
| 41 | + i2s_std_config_t std_cfg = { |
| 42 | + .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(sample_rate), |
| 43 | + .slot_cfg = slot_cfg, |
| 44 | + .gpio_cfg = { |
| 45 | + .mclk = main_clock != NULL ? main_clock->number : I2S_GPIO_UNUSED, |
| 46 | + .bclk = bit_clock->number, |
| 47 | + .ws = word_select->number, |
| 48 | + .dout = I2S_GPIO_UNUSED, |
| 49 | + .din = data->number, |
| 50 | + }, |
| 51 | + }; |
| 52 | + CHECK_ESP_RESULT(i2s_channel_init_std_mode(self->rx_chan, &std_cfg)); |
| 53 | + CHECK_ESP_RESULT(i2s_channel_enable(self->rx_chan)); |
| 54 | + |
| 55 | + self->bit_clock = bit_clock; |
| 56 | + self->word_select = word_select; |
| 57 | + self->data = data; |
| 58 | + self->mclk = main_clock; |
| 59 | + self->sample_rate = sample_rate; |
| 60 | + self->bit_depth = bit_depth; |
| 61 | + |
| 62 | + claim_pin(bit_clock); |
| 63 | + claim_pin(word_select); |
| 64 | + claim_pin(data); |
| 65 | + if (main_clock) { |
| 66 | + claim_pin(main_clock); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +bool common_hal_audiobusio_i2sin_deinited(audiobusio_i2sin_obj_t *self) { |
| 71 | + return self->data == NULL; |
| 72 | +} |
| 73 | + |
| 74 | +void common_hal_audiobusio_i2sin_deinit(audiobusio_i2sin_obj_t *self) { |
| 75 | + if (common_hal_audiobusio_i2sin_deinited(self)) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (self->rx_chan) { |
| 80 | + i2s_channel_disable(self->rx_chan); |
| 81 | + i2s_del_channel(self->rx_chan); |
| 82 | + self->rx_chan = NULL; |
| 83 | + } |
| 84 | + |
| 85 | + if (self->bit_clock) { |
| 86 | + reset_pin_number(self->bit_clock->number); |
| 87 | + } |
| 88 | + self->bit_clock = NULL; |
| 89 | + |
| 90 | + if (self->word_select) { |
| 91 | + reset_pin_number(self->word_select->number); |
| 92 | + } |
| 93 | + self->word_select = NULL; |
| 94 | + |
| 95 | + if (self->data) { |
| 96 | + reset_pin_number(self->data->number); |
| 97 | + } |
| 98 | + self->data = NULL; |
| 99 | + |
| 100 | + if (self->mclk) { |
| 101 | + reset_pin_number(self->mclk->number); |
| 102 | + } |
| 103 | + self->mclk = NULL; |
| 104 | +} |
| 105 | + |
| 106 | +uint32_t common_hal_audiobusio_i2sin_record_to_buffer(audiobusio_i2sin_obj_t *self, |
| 107 | + void *buffer, uint32_t length) { |
| 108 | + size_t result = 0; |
| 109 | + size_t element_size = self->bit_depth / 8; |
| 110 | + // 24-bit samples occupy a 32-bit slot on the I2S bus. |
| 111 | + if (self->bit_depth == 24) { |
| 112 | + element_size = 4; |
| 113 | + } |
| 114 | + esp_err_t err = i2s_channel_read(self->rx_chan, buffer, length * element_size, &result, portMAX_DELAY); |
| 115 | + CHECK_ESP_RESULT(err); |
| 116 | + return result / element_size; |
| 117 | +} |
| 118 | + |
| 119 | +uint8_t common_hal_audiobusio_i2sin_get_bit_depth(audiobusio_i2sin_obj_t *self) { |
| 120 | + return self->bit_depth; |
| 121 | +} |
| 122 | + |
| 123 | +uint32_t common_hal_audiobusio_i2sin_get_sample_rate(audiobusio_i2sin_obj_t *self) { |
| 124 | + return self->sample_rate; |
| 125 | +} |
| 126 | + |
| 127 | +#endif // CIRCUITPY_AUDIOBUSIO_I2SIN |
0 commit comments