Creataing the Hodor project.
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "audio_codec_ctrl_if.h"
|
||||
#include "esp_codec_dev_defaults.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_idf_version.h"
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#define TICK_PER_MS portTICK_PERIOD_MS
|
||||
#else
|
||||
#define TICK_PER_MS portTICK_RATE_MS
|
||||
#endif
|
||||
#define DEFAULT_I2C_CLOCK (100000)
|
||||
#define DEFAULT_I2C_TRANS_TIMEOUT (100)
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !CONFIG_CODEC_I2C_BACKWARD_COMPATIBLE
|
||||
#include "driver/i2c_master.h"
|
||||
#define USE_IDF_I2C_MASTER
|
||||
#else
|
||||
#include "driver/i2c.h"
|
||||
#endif
|
||||
|
||||
#define TAG "I2C_If"
|
||||
typedef struct {
|
||||
audio_codec_ctrl_if_t base;
|
||||
bool is_open;
|
||||
uint8_t port;
|
||||
uint8_t addr;
|
||||
#ifdef USE_IDF_I2C_MASTER
|
||||
i2c_master_dev_handle_t dev_handle;
|
||||
#endif
|
||||
} i2c_ctrl_t;
|
||||
|
||||
static int _i2c_ctrl_open(const audio_codec_ctrl_if_t *ctrl, void *cfg, int cfg_size)
|
||||
{
|
||||
if (ctrl == NULL || cfg == NULL || cfg_size != sizeof(audio_codec_i2c_cfg_t)) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
i2c_ctrl_t *i2c_ctrl = (i2c_ctrl_t *) ctrl;
|
||||
audio_codec_i2c_cfg_t *i2c_cfg = (audio_codec_i2c_cfg_t *) cfg;
|
||||
i2c_ctrl->port = i2c_cfg->port;
|
||||
i2c_ctrl->addr = i2c_cfg->addr;
|
||||
#ifdef USE_IDF_I2C_MASTER
|
||||
if (i2c_cfg->bus_handle == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
int clock_speed_hz = i2c_cfg->clock_speed_hz ? i2c_cfg->clock_speed_hz : DEFAULT_I2C_CLOCK;
|
||||
i2c_device_config_t dev_cfg = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = (i2c_cfg->addr >> 1),
|
||||
.scl_speed_hz = clock_speed_hz,
|
||||
};
|
||||
int ret = i2c_master_bus_add_device(i2c_cfg->bus_handle, &dev_cfg, &i2c_ctrl->dev_handle);
|
||||
return (ret == ESP_OK) ? 0 : ESP_CODEC_DEV_DRV_ERR;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool _i2c_ctrl_is_open(const audio_codec_ctrl_if_t *ctrl)
|
||||
{
|
||||
if (ctrl) {
|
||||
i2c_ctrl_t *i2c_ctrl = (i2c_ctrl_t *) ctrl;
|
||||
return i2c_ctrl->is_open;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef USE_IDF_I2C_MASTER
|
||||
static int _i2c_master_read_reg(i2c_ctrl_t *i2c_ctrl, int addr, int addr_len, void *data, int data_len)
|
||||
{
|
||||
uint8_t addr_data[2] = {0};
|
||||
if (addr_len > 1) {
|
||||
addr_data[0] = addr >> 8;
|
||||
addr_data[1] = addr & 0xff;
|
||||
} else {
|
||||
addr_data[0] = addr & 0xff;
|
||||
}
|
||||
int ret = i2c_master_transmit_receive(i2c_ctrl->dev_handle, addr_data, addr_len, data, data_len, DEFAULT_I2C_TRANS_TIMEOUT);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Fail to read from dev %x", i2c_ctrl->addr);
|
||||
}
|
||||
return ret ? ESP_CODEC_DEV_READ_FAIL : ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
static int _i2c_master_write_reg(i2c_ctrl_t *i2c_ctrl, int addr, int addr_len, void *data, int data_len)
|
||||
{
|
||||
esp_err_t ret = ESP_CODEC_DEV_NOT_SUPPORT;
|
||||
int len = addr_len + data_len;
|
||||
if (len <= 4) {
|
||||
// Not support write huge data
|
||||
uint8_t write_data[4] = {0};
|
||||
int i = 0;
|
||||
if (addr_len > 1) {
|
||||
write_data[i++] = addr >> 8;
|
||||
write_data[i++] = addr & 0xff;
|
||||
} else {
|
||||
write_data[i++] = addr & 0xff;
|
||||
}
|
||||
uint8_t *w = (uint8_t*)data;
|
||||
while (i < len) {
|
||||
write_data[i++] = *(w++);
|
||||
}
|
||||
ret = i2c_master_transmit(i2c_ctrl->dev_handle, write_data, len, DEFAULT_I2C_TRANS_TIMEOUT);
|
||||
}
|
||||
if (ret != 0) {
|
||||
ESP_LOGE(TAG, "Fail to write to dev %x", i2c_ctrl->addr);
|
||||
}
|
||||
return ret ? ESP_CODEC_DEV_WRITE_FAIL : ESP_CODEC_DEV_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int _i2c_ctrl_read_reg(const audio_codec_ctrl_if_t *ctrl, int addr, int addr_len, void *data, int data_len)
|
||||
{
|
||||
if (ctrl == NULL || data == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
i2c_ctrl_t *i2c_ctrl = (i2c_ctrl_t *) ctrl;
|
||||
if (i2c_ctrl->is_open == false) {
|
||||
return ESP_CODEC_DEV_WRONG_STATE;
|
||||
}
|
||||
#ifdef USE_IDF_I2C_MASTER
|
||||
return _i2c_master_read_reg(i2c_ctrl, addr, addr_len, data, data_len);
|
||||
#else
|
||||
esp_err_t ret = ESP_OK;
|
||||
i2c_cmd_handle_t cmd;
|
||||
cmd = i2c_cmd_link_create();
|
||||
ret |= i2c_master_start(cmd);
|
||||
ret |= i2c_master_write_byte(cmd, i2c_ctrl->addr, 1);
|
||||
ret |= i2c_master_write(cmd, (uint8_t *) &addr, addr_len, 1);
|
||||
ret |= i2c_master_stop(cmd);
|
||||
ret |= i2c_master_cmd_begin(i2c_ctrl->port, cmd, DEFAULT_I2C_TRANS_TIMEOUT / TICK_PER_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
|
||||
cmd = i2c_cmd_link_create();
|
||||
ret |= i2c_master_start(cmd);
|
||||
ret |= i2c_master_write_byte(cmd, i2c_ctrl->addr | 0x01, 1);
|
||||
|
||||
for (int i = 0; i < data_len - 1; i++) {
|
||||
ret |= i2c_master_read_byte(cmd, (uint8_t *) data + i, 0);
|
||||
}
|
||||
ret |= i2c_master_read_byte(cmd, (uint8_t *) data + (data_len - 1), 1);
|
||||
|
||||
ret |= i2c_master_stop(cmd);
|
||||
ret |= i2c_master_cmd_begin(i2c_ctrl->port, cmd, DEFAULT_I2C_TRANS_TIMEOUT / TICK_PER_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Fail to read from dev %x", i2c_ctrl->addr);
|
||||
}
|
||||
return ret ? ESP_CODEC_DEV_READ_FAIL : ESP_CODEC_DEV_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int _i2c_ctrl_write_reg(const audio_codec_ctrl_if_t *ctrl, int addr, int addr_len, void *data, int data_len)
|
||||
{
|
||||
i2c_ctrl_t *i2c_ctrl = (i2c_ctrl_t *) ctrl;
|
||||
if (ctrl == NULL || data == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
if (i2c_ctrl->is_open == false) {
|
||||
return ESP_CODEC_DEV_WRONG_STATE;
|
||||
}
|
||||
#ifdef USE_IDF_I2C_MASTER
|
||||
return _i2c_master_write_reg(i2c_ctrl, addr, addr_len, data, data_len);
|
||||
#else
|
||||
esp_err_t ret = ESP_OK;
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
ret |= i2c_master_start(cmd);
|
||||
ret |= i2c_master_write_byte(cmd, i2c_ctrl->addr, 1);
|
||||
ret |= i2c_master_write(cmd, (uint8_t *) &addr, addr_len, 1);
|
||||
if (data_len) {
|
||||
ret |= i2c_master_write(cmd, data, data_len, 1);
|
||||
}
|
||||
ret |= i2c_master_stop(cmd);
|
||||
ret |= i2c_master_cmd_begin(i2c_ctrl->port, cmd, DEFAULT_I2C_TRANS_TIMEOUT / TICK_PER_MS);
|
||||
if (ret != 0) {
|
||||
ESP_LOGE(TAG, "Fail to write to dev %x", i2c_ctrl->addr);
|
||||
}
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return ret ? ESP_CODEC_DEV_WRITE_FAIL : ESP_CODEC_DEV_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int _i2c_ctrl_get_info(const audio_codec_ctrl_if_t *ctrl, audio_codec_ctrl_info_t *info)
|
||||
{
|
||||
if (ctrl == NULL || info == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
i2c_ctrl_t *i2c_ctrl = (i2c_ctrl_t *)ctrl;
|
||||
info->type = AUDIO_CODEC_CTRL_I2C;
|
||||
info->i2c.addr = i2c_ctrl->addr;
|
||||
info->i2c.port = i2c_ctrl->port;
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
static int _i2c_ctrl_close(const audio_codec_ctrl_if_t *ctrl)
|
||||
{
|
||||
if (ctrl == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
i2c_ctrl_t *i2c_ctrl = (i2c_ctrl_t *) ctrl;
|
||||
#ifdef USE_IDF_I2C_MASTER
|
||||
if (i2c_ctrl->dev_handle) {
|
||||
i2c_master_bus_rm_device(i2c_ctrl->dev_handle);
|
||||
}
|
||||
#endif
|
||||
i2c_ctrl->is_open = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const audio_codec_ctrl_if_t *audio_codec_new_i2c_ctrl(audio_codec_i2c_cfg_t *i2c_cfg)
|
||||
{
|
||||
if (i2c_cfg == NULL) {
|
||||
ESP_LOGE(TAG, "Bad configuration");
|
||||
return NULL;
|
||||
}
|
||||
i2c_ctrl_t *ctrl = calloc(1, sizeof(i2c_ctrl_t));
|
||||
if (ctrl == NULL) {
|
||||
ESP_LOGE(TAG, "No memory for instance");
|
||||
return NULL;
|
||||
}
|
||||
ctrl->base.open = _i2c_ctrl_open;
|
||||
ctrl->base.is_open = _i2c_ctrl_is_open;
|
||||
ctrl->base.read_reg = _i2c_ctrl_read_reg;
|
||||
ctrl->base.write_reg = _i2c_ctrl_write_reg;
|
||||
ctrl->base.get_info = _i2c_ctrl_get_info;
|
||||
ctrl->base.close = _i2c_ctrl_close;
|
||||
int ret = _i2c_ctrl_open(&ctrl->base, i2c_cfg, sizeof(audio_codec_i2c_cfg_t));
|
||||
if (ret != 0) {
|
||||
free(ctrl);
|
||||
return NULL;
|
||||
}
|
||||
ctrl->is_open = true;
|
||||
return &ctrl->base;
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "esp_idf_version.h"
|
||||
#include "audio_codec_ctrl_if.h"
|
||||
#include "esp_codec_dev_defaults.h"
|
||||
#include "driver/spi_common.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define TAG "SPI_If"
|
||||
|
||||
typedef struct {
|
||||
audio_codec_ctrl_if_t base;
|
||||
bool is_open;
|
||||
uint8_t port;
|
||||
int16_t cs_pin;
|
||||
spi_device_handle_t spi_handle;
|
||||
} spi_ctrl_t;
|
||||
|
||||
int _spi_ctrl_open(const audio_codec_ctrl_if_t *ctrl, void *cfg, int cfg_size)
|
||||
{
|
||||
if (ctrl == NULL || cfg == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
if (cfg_size != sizeof(audio_codec_spi_cfg_t)) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
spi_ctrl_t *spi_ctrl = (spi_ctrl_t *)ctrl;
|
||||
audio_codec_spi_cfg_t *spi_cfg = (audio_codec_spi_cfg_t *)cfg;
|
||||
int speed = spi_cfg->clock_speed ? spi_cfg->clock_speed : 1000000;
|
||||
spi_device_interface_config_t dev_cfg = {
|
||||
.clock_speed_hz = speed, // Clock out at 10 MHz
|
||||
.mode = 0, // SPI mode 0
|
||||
.queue_size = 6, // queue 7 transactions at a time
|
||||
};
|
||||
dev_cfg.spics_io_num = spi_cfg->cs_pin;
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 4))
|
||||
spi_host_device_t host_id = SPI2_HOST;
|
||||
#elif (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0))
|
||||
spi_host_device_t host_id = SPI3_HOST;
|
||||
#else
|
||||
spi_host_device_t host_id = HSPI_HOST;
|
||||
#endif /* (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 4)) */
|
||||
int ret = spi_bus_add_device(host_id, &dev_cfg, &spi_ctrl->spi_handle);
|
||||
if (ret == 0) {
|
||||
gpio_set_pull_mode(spi_cfg->cs_pin, GPIO_FLOATING);
|
||||
spi_ctrl->cs_pin = spi_cfg->cs_pin;
|
||||
spi_ctrl->is_open = true;
|
||||
}
|
||||
return ret == 0 ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
bool _spi_ctrl_is_open(const audio_codec_ctrl_if_t *ctrl)
|
||||
{
|
||||
if (ctrl) {
|
||||
spi_ctrl_t *spi_ctrl = (spi_ctrl_t *) ctrl;
|
||||
return spi_ctrl->is_open;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int _spi_ctrl_read_reg(const audio_codec_ctrl_if_t *ctrl, int addr, int addr_len, void *data, int data_len)
|
||||
{
|
||||
if (ctrl == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
spi_ctrl_t *spi_ctrl = (spi_ctrl_t *) ctrl;
|
||||
if (spi_ctrl->is_open == false) {
|
||||
return ESP_CODEC_DEV_WRONG_STATE;
|
||||
}
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (addr_len) {
|
||||
uint16_t *v = (uint16_t *) &addr;
|
||||
while (addr_len >= 2) {
|
||||
spi_transaction_t t = {0};
|
||||
t.length = 2 * 8;
|
||||
t.tx_buffer = v;
|
||||
ret = spi_device_transmit(spi_ctrl->spi_handle, &t);
|
||||
v++;
|
||||
addr_len -= 2;
|
||||
}
|
||||
}
|
||||
if (data_len) {
|
||||
spi_transaction_t t = {0};
|
||||
t.length = data_len * 8;
|
||||
t.rxlength = data_len * 8;
|
||||
t.rx_buffer = data;
|
||||
ret = spi_device_transmit(spi_ctrl->spi_handle, &t);
|
||||
}
|
||||
return ret == ESP_OK ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
static int _spi_ctrl_write_reg(const audio_codec_ctrl_if_t *ctrl, int addr, int addr_len, void *data, int data_len)
|
||||
{
|
||||
spi_ctrl_t *spi_ctrl = (spi_ctrl_t *) ctrl;
|
||||
if (ctrl == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
if (spi_ctrl->is_open == false) {
|
||||
return ESP_CODEC_DEV_WRONG_STATE;
|
||||
}
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (addr_len) {
|
||||
uint16_t *v = (uint16_t *) &addr;
|
||||
while (addr_len >= 2) {
|
||||
spi_transaction_t t = {0};
|
||||
t.length = 2 * 8;
|
||||
t.tx_buffer = v;
|
||||
ret = spi_device_transmit(spi_ctrl->spi_handle, &t);
|
||||
v++;
|
||||
addr_len -= 2;
|
||||
}
|
||||
}
|
||||
if (data_len) {
|
||||
spi_transaction_t t = {0};
|
||||
t.length = data_len * 8;
|
||||
t.tx_buffer = data;
|
||||
ret = spi_device_transmit(spi_ctrl->spi_handle, &t);
|
||||
}
|
||||
return ret == ESP_OK ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
static int _spi_ctrl_get_info(const audio_codec_ctrl_if_t *ctrl, audio_codec_ctrl_info_t *info)
|
||||
{
|
||||
if (ctrl == NULL || info == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
spi_ctrl_t *spi_ctrl = (spi_ctrl_t *)ctrl;
|
||||
info->type = AUDIO_CODEC_CTRL_SPI;
|
||||
info->spi.cs_pin = spi_ctrl->cs_pin;
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
int _spi_ctrl_close(const audio_codec_ctrl_if_t *ctrl)
|
||||
{
|
||||
spi_ctrl_t *spi_ctrl = (spi_ctrl_t *) ctrl;
|
||||
if (ctrl == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
int ret = 0;
|
||||
if (spi_ctrl->spi_handle) {
|
||||
ret = spi_bus_remove_device(spi_ctrl->spi_handle);
|
||||
}
|
||||
spi_ctrl->is_open = false;
|
||||
return ret == ESP_OK ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
const audio_codec_ctrl_if_t *audio_codec_new_spi_ctrl(audio_codec_spi_cfg_t *spi_cfg)
|
||||
{
|
||||
spi_ctrl_t *ctrl = calloc(1, sizeof(spi_ctrl_t));
|
||||
if (ctrl == NULL) {
|
||||
ESP_LOGE(TAG, "No memory for instance");
|
||||
return NULL;
|
||||
}
|
||||
ctrl->base.open = _spi_ctrl_open;
|
||||
ctrl->base.is_open = _spi_ctrl_is_open;
|
||||
ctrl->base.read_reg = _spi_ctrl_read_reg;
|
||||
ctrl->base.write_reg = _spi_ctrl_write_reg;
|
||||
ctrl->base.get_info = _spi_ctrl_get_info;
|
||||
ctrl->base.close = _spi_ctrl_close;
|
||||
int ret = _spi_ctrl_open(&ctrl->base, spi_cfg, sizeof(audio_codec_spi_cfg_t));
|
||||
if (ret != 0) {
|
||||
ESP_LOGE(TAG, "Fail to open SPI driver");
|
||||
free(ctrl);
|
||||
return NULL;
|
||||
}
|
||||
return &ctrl->base;
|
||||
}
|
||||
@@ -0,0 +1,710 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include "esp_codec_dev_defaults.h"
|
||||
#include "esp_adc/adc_continuous.h"
|
||||
|
||||
#define TAG "CODEC_ADC_IF"
|
||||
|
||||
/**
|
||||
* Compat shim for IDF < 5.5.2:
|
||||
* - adc_continuous_parse_data() was added in IDF 5.5.2
|
||||
* Keep the parsed sample type private to this component so
|
||||
* backports of newer ADC helpers do not conflict with local
|
||||
* compatibility code on older branches.
|
||||
*/
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 5, 2)
|
||||
#include "hal/adc_ll.h"
|
||||
|
||||
#ifdef ADC_LL_UNIT2_CHANNEL_SUBSTRATION
|
||||
#define CODEC_ADC_UNIT2_CHANNEL_SUBSTRATION ADC_LL_UNIT2_CHANNEL_SUBSTRATION
|
||||
#else
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
#define CODEC_ADC_UNIT2_CHANNEL_SUBSTRATION (2)
|
||||
#else
|
||||
#define CODEC_ADC_UNIT2_CHANNEL_SUBSTRATION (0)
|
||||
#endif /* CONFIG_IDF_TARGET_ESP32P4 */
|
||||
#endif /* ADC_LL_UNIT2_CHANNEL_SUBSTRATION */
|
||||
|
||||
#elif !defined(SOC_ADC_SAMPLE_FREQ_THRES_LOW) || !defined(SOC_ADC_SAMPLE_FREQ_THRES_HIGH)
|
||||
#include "hal/adc_ll.h"
|
||||
#endif /* ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 5, 2) */
|
||||
|
||||
/**
|
||||
* Optional safety filter on read path:
|
||||
* - 0 (default): trust reconfigured pattern and output all parsed samples directly.
|
||||
* - 1: keep only samples matching current active pattern subset.
|
||||
*/
|
||||
#ifndef ADC_DATA_READ_FILTER_ENABLE
|
||||
#define ADC_DATA_READ_FILTER_ENABLE 0
|
||||
#endif /* ADC_DATA_READ_FILTER_ENABLE */
|
||||
|
||||
#ifdef SOC_ADC_MAX_CHANNEL_NUM
|
||||
#define CODEC_ADC_MAX_CHANNEL_NUM SOC_ADC_MAX_CHANNEL_NUM
|
||||
#elif SOC_ADC_PERIPH_NUM > 1
|
||||
#define CODEC_ADC_MAX_CHANNEL_NUM ((SOC_ADC_CHANNEL_NUM(0) > SOC_ADC_CHANNEL_NUM(1)) ? SOC_ADC_CHANNEL_NUM(0) : SOC_ADC_CHANNEL_NUM(1))
|
||||
#else
|
||||
#define CODEC_ADC_MAX_CHANNEL_NUM SOC_ADC_CHANNEL_NUM(0)
|
||||
#endif /* SOC_ADC_MAX_CHANNEL_NUM */
|
||||
|
||||
#ifdef SOC_ADC_SAMPLE_FREQ_THRES_LOW
|
||||
#define CODEC_ADC_SAMPLE_FREQ_THRES_LOW SOC_ADC_SAMPLE_FREQ_THRES_LOW
|
||||
#else
|
||||
#define CODEC_ADC_SAMPLE_FREQ_THRES_LOW ADC_LL_SAMPLE_FREQ_THRES_LOW
|
||||
#endif /* SOC_ADC_SAMPLE_FREQ_THRES_LOW */
|
||||
|
||||
#ifdef SOC_ADC_SAMPLE_FREQ_THRES_HIGH
|
||||
#define CODEC_ADC_SAMPLE_FREQ_THRES_HIGH SOC_ADC_SAMPLE_FREQ_THRES_HIGH
|
||||
#else
|
||||
#define CODEC_ADC_SAMPLE_FREQ_THRES_HIGH ADC_LL_SAMPLE_FREQ_THRES_HIGH
|
||||
#endif /* SOC_ADC_SAMPLE_FREQ_THRES_HIGH */
|
||||
|
||||
typedef struct {
|
||||
adc_unit_t unit;
|
||||
adc_channel_t channel;
|
||||
uint32_t raw_data;
|
||||
bool valid;
|
||||
} codec_adc_parsed_data_t;
|
||||
|
||||
typedef struct {
|
||||
audio_codec_data_if_t base;
|
||||
adc_continuous_handle_t handle;
|
||||
audio_codec_adc_continuous_cfg_t cfg; /* Immutable config snapshot from new() */
|
||||
/* base_patterns keeps the full configured channel/pattern table.
|
||||
* set_fmt() only selects a subset by index into this table. */
|
||||
adc_digi_pattern_config_t base_patterns[SOC_ADC_PATT_LEN_MAX];
|
||||
uint8_t base_pattern_num;
|
||||
/* active_pattern_idx maps current runtime channels -> base_patterns index. */
|
||||
uint8_t active_pattern_idx[SOC_ADC_PATT_LEN_MAX];
|
||||
uint8_t active_pattern_num;
|
||||
uint8_t out_bits; /* Output PCM bits (16 or 32) */
|
||||
bool use_ext_handle;
|
||||
bool is_open;
|
||||
bool enabled;
|
||||
uint8_t *raw_read_buf;
|
||||
size_t raw_read_buf_size;
|
||||
codec_adc_parsed_data_t *parsed_buf;
|
||||
uint32_t current_sample_rate_hz;
|
||||
uint8_t base_pattern_bits[SOC_ADC_PERIPH_NUM][CODEC_ADC_MAX_CHANNEL_NUM];
|
||||
uint8_t active_pattern_bits[SOC_ADC_PERIPH_NUM][CODEC_ADC_MAX_CHANNEL_NUM];
|
||||
} adc_data_t;
|
||||
|
||||
static inline bool adc_data_bit_width_valid(uint8_t bit_width)
|
||||
{
|
||||
return bit_width == ADC_BITWIDTH_DEFAULT ||
|
||||
(bit_width >= ADC_BITWIDTH_9 && bit_width <= SOC_ADC_DIGI_MAX_BITWIDTH);
|
||||
}
|
||||
|
||||
static inline bool adc_unit_supported(adc_unit_t unit)
|
||||
{
|
||||
#ifdef SOC_ADC_DIG_SUPPORTED_UNIT
|
||||
return SOC_ADC_DIG_SUPPORTED_UNIT(unit);
|
||||
#else
|
||||
return unit == ADC_UNIT_1;
|
||||
#endif /* SOC_ADC_DIG_SUPPORTED_UNIT */
|
||||
}
|
||||
|
||||
static inline uint8_t adc_get_effective_sample_bits(uint8_t bit_width)
|
||||
{
|
||||
return bit_width == ADC_BITWIDTH_DEFAULT ? SOC_ADC_DIGI_MAX_BITWIDTH : bit_width;
|
||||
}
|
||||
|
||||
static inline int32_t adc_centered_to_pcm(int32_t centered, uint8_t out_bits, uint8_t sample_bits)
|
||||
{
|
||||
return (int32_t)(centered * (int64_t)(1ULL << (out_bits - sample_bits)));
|
||||
}
|
||||
|
||||
static inline bool adc_unit_channel_in_range(adc_unit_t unit, adc_channel_t channel)
|
||||
{
|
||||
return unit < SOC_ADC_PERIPH_NUM && channel < SOC_ADC_CHANNEL_NUM(unit);
|
||||
}
|
||||
|
||||
static inline void adc_store_pattern_bits(uint8_t pattern_bits[SOC_ADC_PERIPH_NUM][CODEC_ADC_MAX_CHANNEL_NUM],
|
||||
adc_unit_t unit, adc_channel_t channel, uint8_t sample_bits)
|
||||
{
|
||||
if (adc_unit_channel_in_range(unit, channel) && pattern_bits[unit][channel] == 0) {
|
||||
pattern_bits[unit][channel] = sample_bits;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t adc_get_sample_bits_by_unit_channel(const adc_data_t *adc_data, adc_unit_t unit, adc_channel_t channel)
|
||||
{
|
||||
if (adc_unit_channel_in_range(unit, channel)) {
|
||||
if (adc_data->active_pattern_bits[unit][channel]) {
|
||||
return adc_data->active_pattern_bits[unit][channel];
|
||||
}
|
||||
if (adc_data->base_pattern_bits[unit][channel]) {
|
||||
return adc_data->base_pattern_bits[unit][channel];
|
||||
}
|
||||
}
|
||||
return SOC_ADC_DIGI_MAX_BITWIDTH;
|
||||
}
|
||||
|
||||
#if ADC_DATA_READ_FILTER_ENABLE
|
||||
static bool adc_sample_allowed(const adc_data_t *adc_data, adc_unit_t unit, adc_channel_t channel)
|
||||
{
|
||||
return adc_unit_channel_in_range(unit, channel) && adc_data->active_pattern_bits[unit][channel] != 0;
|
||||
}
|
||||
#endif /* ADC_DATA_READ_FILTER_ENABLE */
|
||||
|
||||
/**
|
||||
* Parse raw ADC DMA bytes into component-private parsed sample array.
|
||||
* On IDF >= 5.5.2 delegates to the real driver API;
|
||||
* on older IDF performs the parsing locally with full access to
|
||||
* adc_data_t (format, active patterns, etc.).
|
||||
*/
|
||||
static esp_err_t adc_parse_raw_data(adc_data_t *adc_data,
|
||||
const uint8_t *raw_data,
|
||||
uint32_t raw_data_size,
|
||||
codec_adc_parsed_data_t *parsed_data,
|
||||
uint32_t *num_parsed_samples)
|
||||
{
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 2)
|
||||
if (!raw_data || !parsed_data || !num_parsed_samples) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (raw_data_size == 0 || raw_data_size % SOC_ADC_DIGI_RESULT_BYTES != 0) {
|
||||
*num_parsed_samples = 0;
|
||||
return ESP_ERR_INVALID_SIZE;
|
||||
}
|
||||
|
||||
uint32_t sample_count = raw_data_size / SOC_ADC_DIGI_RESULT_BYTES;
|
||||
adc_continuous_data_t sdk_parsed_data[sample_count];
|
||||
esp_err_t ret = adc_continuous_parse_data(adc_data->handle, raw_data, raw_data_size,
|
||||
sdk_parsed_data, num_parsed_samples);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
for (uint32_t i = 0; i < *num_parsed_samples; i++) {
|
||||
parsed_data[i].unit = sdk_parsed_data[i].unit;
|
||||
parsed_data[i].channel = sdk_parsed_data[i].channel;
|
||||
parsed_data[i].raw_data = sdk_parsed_data[i].raw_data;
|
||||
parsed_data[i].valid = sdk_parsed_data[i].valid;
|
||||
}
|
||||
return ESP_OK;
|
||||
#else
|
||||
if (!raw_data || !parsed_data || !num_parsed_samples) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (raw_data_size == 0 || raw_data_size % SOC_ADC_DIGI_RESULT_BYTES != 0) {
|
||||
*num_parsed_samples = 0;
|
||||
return ESP_ERR_INVALID_SIZE;
|
||||
}
|
||||
|
||||
uint32_t samples = raw_data_size / SOC_ADC_DIGI_RESULT_BYTES;
|
||||
for (uint32_t i = 0; i < samples; i++) {
|
||||
adc_digi_output_data_t *p =
|
||||
(adc_digi_output_data_t *)&raw_data[i * SOC_ADC_DIGI_RESULT_BYTES];
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
parsed_data[i].unit = ADC_UNIT_1;
|
||||
parsed_data[i].channel = p->type1.channel;
|
||||
parsed_data[i].raw_data = p->type1.data;
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
if (adc_data->cfg.format == ADC_DIGI_OUTPUT_FORMAT_TYPE2) {
|
||||
parsed_data[i].unit = p->type2.unit ? ADC_UNIT_2 : ADC_UNIT_1;
|
||||
parsed_data[i].channel = p->type2.channel;
|
||||
parsed_data[i].raw_data = p->type2.data;
|
||||
} else {
|
||||
bool uses_adc1 = false;
|
||||
for (int j = 0; j < adc_data->active_pattern_num; j++) {
|
||||
if ((adc_unit_t)adc_data->base_patterns[adc_data->active_pattern_idx[j]].unit == ADC_UNIT_1) {
|
||||
uses_adc1 = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
parsed_data[i].unit = uses_adc1 ? ADC_UNIT_1 : ADC_UNIT_2;
|
||||
parsed_data[i].channel = p->type1.channel;
|
||||
parsed_data[i].raw_data = p->type1.data;
|
||||
}
|
||||
#else
|
||||
#if SOC_ADC_PERIPH_NUM == 1
|
||||
parsed_data[i].unit = ADC_UNIT_1;
|
||||
#else
|
||||
parsed_data[i].unit = p->type2.unit ? ADC_UNIT_2 : ADC_UNIT_1;
|
||||
#endif /* SOC_ADC_PERIPH_NUM == 1 */
|
||||
parsed_data[i].channel = (parsed_data[i].unit == ADC_UNIT_2)
|
||||
? p->type2.channel - CODEC_ADC_UNIT2_CHANNEL_SUBSTRATION
|
||||
: p->type2.channel;
|
||||
parsed_data[i].raw_data = p->type2.data;
|
||||
#endif /* CONFIG_IDF_TARGET_ESP32 */
|
||||
parsed_data[i].valid =
|
||||
(parsed_data[i].channel < SOC_ADC_CHANNEL_NUM(parsed_data[i].unit));
|
||||
}
|
||||
*num_parsed_samples = samples;
|
||||
return ESP_OK;
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 2) */
|
||||
}
|
||||
|
||||
static adc_digi_convert_mode_t adc_calc_conv_mode(const adc_digi_pattern_config_t *patterns, uint8_t pattern_num)
|
||||
{
|
||||
bool has_unit1 = false;
|
||||
bool has_unit2 = false;
|
||||
for (int i = 0; i < pattern_num; i++) {
|
||||
if ((adc_unit_t)patterns[i].unit == ADC_UNIT_1) {
|
||||
has_unit1 = true;
|
||||
} else if ((adc_unit_t)patterns[i].unit == ADC_UNIT_2) {
|
||||
has_unit2 = true;
|
||||
}
|
||||
}
|
||||
if (has_unit1 && has_unit2) {
|
||||
return ADC_CONV_BOTH_UNIT;
|
||||
}
|
||||
return has_unit2 ? ADC_CONV_SINGLE_UNIT_2 : ADC_CONV_SINGLE_UNIT_1;
|
||||
}
|
||||
|
||||
static esp_err_t adc_apply_patterns(adc_data_t *adc_data, const uint8_t *pattern_idx, uint8_t pattern_num, uint32_t sample_rate_hz)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(pattern_num > 0, ESP_ERR_INVALID_ARG, TAG, "Pattern number should be > 0");
|
||||
ESP_RETURN_ON_FALSE(sample_rate_hz >= (CODEC_ADC_SAMPLE_FREQ_THRES_LOW + pattern_num - 1) / pattern_num,
|
||||
ESP_ERR_INVALID_ARG, TAG,
|
||||
"Sample rate should be >= %lu", (unsigned long)((CODEC_ADC_SAMPLE_FREQ_THRES_LOW + pattern_num - 1) / pattern_num));
|
||||
ESP_RETURN_ON_FALSE(sample_rate_hz <= (CODEC_ADC_SAMPLE_FREQ_THRES_HIGH / pattern_num), ESP_ERR_INVALID_ARG, TAG,
|
||||
"Sample rate out of range: %lu * %u", (unsigned long)sample_rate_hz, pattern_num);
|
||||
|
||||
/* Build a temporary hardware pattern list from selected base indices. */
|
||||
adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
|
||||
uint8_t active_pattern_bits[SOC_ADC_PERIPH_NUM][CODEC_ADC_MAX_CHANNEL_NUM] = {0};
|
||||
for (int i = 0; i < pattern_num; i++) {
|
||||
adc_pattern[i] = adc_data->base_patterns[pattern_idx[i]];
|
||||
adc_store_pattern_bits(active_pattern_bits,
|
||||
(adc_unit_t)adc_pattern[i].unit,
|
||||
(adc_channel_t)adc_pattern[i].channel,
|
||||
adc_get_effective_sample_bits(adc_pattern[i].bit_width));
|
||||
}
|
||||
|
||||
/* IDF continuous driver expects total conversion rate, so multiply by channel count. */
|
||||
adc_continuous_config_t dig_cfg = {
|
||||
.sample_freq_hz = sample_rate_hz * pattern_num,
|
||||
.conv_mode = adc_calc_conv_mode(adc_pattern, pattern_num),
|
||||
.format = adc_data->cfg.format,
|
||||
.pattern_num = pattern_num,
|
||||
.adc_pattern = adc_pattern,
|
||||
};
|
||||
|
||||
esp_err_t ret = adc_continuous_config(adc_data->handle, &dig_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_config failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
memcpy(adc_data->active_pattern_idx, pattern_idx, pattern_num);
|
||||
adc_data->active_pattern_num = pattern_num;
|
||||
memcpy(adc_data->active_pattern_bits, active_pattern_bits, sizeof(adc_data->active_pattern_bits));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static bool _adc_data_is_open(const audio_codec_data_if_t *h)
|
||||
{
|
||||
adc_data_t *adc_data = (adc_data_t *)h;
|
||||
return adc_data ? adc_data->is_open : false;
|
||||
}
|
||||
|
||||
static int _adc_data_enable(const audio_codec_data_if_t *h, esp_codec_dev_type_t dev_type, bool enable)
|
||||
{
|
||||
adc_data_t *adc_data = (adc_data_t *)h;
|
||||
ESP_RETURN_ON_FALSE(adc_data != NULL, ESP_CODEC_DEV_INVALID_ARG, TAG, "ADC data is NULL");
|
||||
ESP_RETURN_ON_FALSE(adc_data->is_open, ESP_CODEC_DEV_WRONG_STATE, TAG, "ADC data not open");
|
||||
ESP_RETURN_ON_FALSE(dev_type == ESP_CODEC_DEV_TYPE_IN, ESP_CODEC_DEV_INVALID_ARG, TAG, "Invalid dev_type");
|
||||
|
||||
if (enable == adc_data->enabled) {
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (enable) {
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
|
||||
/* Drop stale frames before (re)start to keep first read aligned with current config. */
|
||||
ret = adc_continuous_flush_pool(adc_data->handle);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_flush_pool failed before start: %s", esp_err_to_name(ret));
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) */
|
||||
ret = adc_continuous_start(adc_data->handle);
|
||||
if (ret == ESP_OK) {
|
||||
adc_data->enabled = true;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_start failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
} else {
|
||||
ret = adc_continuous_stop(adc_data->handle);
|
||||
if (ret == ESP_OK) {
|
||||
adc_data->enabled = false;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_stop failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
return ret == ESP_OK ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
static int _adc_data_read(const audio_codec_data_if_t *h, uint8_t *data, int size)
|
||||
{
|
||||
adc_data_t *adc_data = (adc_data_t *)h;
|
||||
ESP_RETURN_ON_FALSE(adc_data != NULL && data != NULL, ESP_CODEC_DEV_INVALID_ARG, TAG, "Invalid argument");
|
||||
ESP_RETURN_ON_FALSE(adc_data->is_open, ESP_CODEC_DEV_WRONG_STATE, TAG, "ADC data not open");
|
||||
ESP_RETURN_ON_FALSE(adc_data->enabled, ESP_CODEC_DEV_WRONG_STATE, TAG, "ADC data not enabled");
|
||||
ESP_RETURN_ON_FALSE(adc_data->out_bits == 16 || adc_data->out_bits == 32,
|
||||
ESP_CODEC_DEV_WRONG_STATE, TAG, "Invalid output bits");
|
||||
ESP_RETURN_ON_FALSE(size >= 0, ESP_CODEC_DEV_INVALID_ARG, TAG, "Read size should be >= 0");
|
||||
if (size == 0) {
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
const int out_bytes_per_sample = adc_data->out_bits / 8;
|
||||
ESP_RETURN_ON_FALSE((size % out_bytes_per_sample) == 0,
|
||||
ESP_CODEC_DEV_INVALID_ARG, TAG, "Read size should be %d-byte aligned", out_bytes_per_sample);
|
||||
|
||||
uint32_t ret_num = 0;
|
||||
int target_samples = size / out_bytes_per_sample;
|
||||
int produced = 0;
|
||||
uint8_t *dst = data;
|
||||
|
||||
while (produced < target_samples) {
|
||||
int samples_left = target_samples - produced;
|
||||
size_t request = samples_left * SOC_ADC_DIGI_RESULT_BYTES;
|
||||
if (request > adc_data->raw_read_buf_size) {
|
||||
request = adc_data->raw_read_buf_size;
|
||||
}
|
||||
if (request < SOC_ADC_DIGI_RESULT_BYTES) {
|
||||
request = SOC_ADC_DIGI_RESULT_BYTES;
|
||||
}
|
||||
|
||||
/* 1) Pull raw DMA bytes from ADC continuous driver. */
|
||||
esp_err_t ret = adc_continuous_read(adc_data->handle, adc_data->raw_read_buf, request, &ret_num, 1000 / portTICK_PERIOD_MS);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_read failed: %s", esp_err_to_name(ret));
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
if (ret_num < SOC_ADC_DIGI_RESULT_BYTES) {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_read returned too few bytes: %lu", (unsigned long)ret_num);
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
/* 2) Parse raw bytes into (unit/channel/raw_data/valid) tuples. */
|
||||
uint32_t num_parsed = 0;
|
||||
ret = adc_parse_raw_data(adc_data, adc_data->raw_read_buf, ret_num,
|
||||
adc_data->parsed_buf, &num_parsed);
|
||||
if (ret != ESP_OK || num_parsed == 0) {
|
||||
ESP_LOGE(TAG, "Call to adc_parse_raw_data failed: %s, parsed:%lu",
|
||||
esp_err_to_name(ret), (unsigned long)num_parsed);
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
int valid = 0;
|
||||
for (uint32_t i = 0; i < num_parsed; i++) {
|
||||
const codec_adc_parsed_data_t *s = &adc_data->parsed_buf[i];
|
||||
if (!s->valid) {
|
||||
continue;
|
||||
}
|
||||
#if ADC_DATA_READ_FILTER_ENABLE
|
||||
if (!adc_sample_allowed(adc_data, s->unit, s->channel)) {
|
||||
continue;
|
||||
}
|
||||
#endif /* ADC_DATA_READ_FILTER_ENABLE */
|
||||
/* 3) Convert each parsed sample to requested PCM width. */
|
||||
uint8_t sample_bits = adc_get_sample_bits_by_unit_channel(adc_data, s->unit, s->channel);
|
||||
ESP_RETURN_ON_FALSE(sample_bits > 0 && sample_bits <= adc_data->out_bits && sample_bits <= 31,
|
||||
ESP_CODEC_DEV_DRV_ERR, TAG, "Invalid sample bits: %u", sample_bits);
|
||||
uint32_t sample_mask = (uint32_t)((1ULL << sample_bits) - 1U);
|
||||
int32_t raw_centered = (int32_t)(s->raw_data & sample_mask) - (int32_t)(1U << (sample_bits - 1));
|
||||
if (adc_data->out_bits == 16) {
|
||||
int16_t sample_16 = (int16_t)adc_centered_to_pcm(raw_centered, 16, sample_bits);
|
||||
memcpy(dst + (produced + valid) * out_bytes_per_sample, &sample_16, sizeof(sample_16));
|
||||
} else {
|
||||
int32_t sample_32 = adc_centered_to_pcm(raw_centered, 32, sample_bits);
|
||||
memcpy(dst + (produced + valid) * out_bytes_per_sample, &sample_32, sizeof(sample_32));
|
||||
}
|
||||
valid++;
|
||||
if (produced + valid >= target_samples) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ESP_RETURN_ON_FALSE(valid > 0, ESP_CODEC_DEV_DRV_ERR, TAG, "No valid ADC samples in frame");
|
||||
produced += valid;
|
||||
}
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
static int _adc_data_set_fmt(const audio_codec_data_if_t *h, esp_codec_dev_type_t dev_type, esp_codec_dev_sample_info_t *fs)
|
||||
{
|
||||
adc_data_t *adc_data = (adc_data_t *)h;
|
||||
ESP_RETURN_ON_FALSE(adc_data != NULL && fs != NULL, ESP_CODEC_DEV_INVALID_ARG, TAG, "Invalid argument");
|
||||
ESP_RETURN_ON_FALSE(adc_data->is_open, ESP_CODEC_DEV_WRONG_STATE, TAG, "ADC data not open");
|
||||
ESP_RETURN_ON_FALSE(dev_type == ESP_CODEC_DEV_TYPE_IN, ESP_CODEC_DEV_INVALID_ARG, TAG, "Invalid dev_type");
|
||||
ESP_RETURN_ON_FALSE(fs->bits_per_sample == 16 || fs->bits_per_sample == 32,
|
||||
ESP_CODEC_DEV_INVALID_ARG, TAG, "Only support 16-bit or 32-bit");
|
||||
ESP_RETURN_ON_FALSE(fs->sample_rate > 0, ESP_CODEC_DEV_INVALID_ARG, TAG, "Sample_rate should be > 0");
|
||||
|
||||
uint8_t active_idx[SOC_ADC_PATT_LEN_MAX] = {0};
|
||||
uint8_t active_num = 0;
|
||||
uint8_t old_active_idx[SOC_ADC_PATT_LEN_MAX] = {0};
|
||||
uint8_t old_active_num = adc_data->active_pattern_num;
|
||||
uint8_t old_out_bits = adc_data->out_bits;
|
||||
uint32_t old_sample_rate_hz = adc_data->current_sample_rate_hz;
|
||||
memcpy(old_active_idx, adc_data->active_pattern_idx, old_active_num);
|
||||
|
||||
if (fs->channel_mask == 0) {
|
||||
/* No mask: use first N channels from base order, where N == fs->channel. */
|
||||
ESP_RETURN_ON_FALSE(fs->channel > 0 && fs->channel <= adc_data->base_pattern_num,
|
||||
ESP_CODEC_DEV_INVALID_ARG, TAG, "Channel out of range");
|
||||
active_num = fs->channel;
|
||||
for (int i = 0; i < active_num; i++) {
|
||||
active_idx[i] = i;
|
||||
}
|
||||
if (fs->channel != adc_data->base_pattern_num) {
|
||||
ESP_LOGW(TAG, "Channel(%u) mismatch with configured channels(%u) while mask=0, use first %u channels",
|
||||
fs->channel, adc_data->base_pattern_num, fs->channel);
|
||||
}
|
||||
} else {
|
||||
/* Mask mode: bit i maps to base_patterns[i]. */
|
||||
if ((fs->channel_mask >> adc_data->base_pattern_num) != 0) {
|
||||
ESP_LOGE(TAG, "Channel_mask out of range");
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
for (uint8_t i = 0; i < adc_data->base_pattern_num; i++) {
|
||||
if (fs->channel_mask & (1U << i)) {
|
||||
active_idx[active_num++] = i;
|
||||
}
|
||||
}
|
||||
ESP_RETURN_ON_FALSE(active_num > 0, ESP_CODEC_DEV_INVALID_ARG, TAG, "Empty channel_mask");
|
||||
ESP_RETURN_ON_FALSE(fs->channel == active_num, ESP_CODEC_DEV_INVALID_ARG, TAG, "Channel mismatch");
|
||||
}
|
||||
|
||||
/* Reconfigure sequence: stop -> config -> flush -> optional restart. */
|
||||
bool was_enabled = adc_data->enabled;
|
||||
if (was_enabled) {
|
||||
esp_err_t stop_ret = adc_continuous_stop(adc_data->handle);
|
||||
if (stop_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_stop failed before set_fmt reconfig: %s", esp_err_to_name(stop_ret));
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
adc_data->enabled = false;
|
||||
}
|
||||
|
||||
esp_err_t ret = adc_apply_patterns(adc_data, active_idx, active_num, fs->sample_rate);
|
||||
if (ret == ESP_OK) {
|
||||
adc_data->out_bits = fs->bits_per_sample;
|
||||
adc_data->current_sample_rate_hz = fs->sample_rate;
|
||||
}
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
|
||||
if (ret == ESP_OK) {
|
||||
/* Remove old-format frames that may remain in internal pool after reconfig. */
|
||||
esp_err_t flush_ret = adc_continuous_flush_pool(adc_data->handle);
|
||||
if (flush_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_flush_pool failed after set_fmt: %s", esp_err_to_name(flush_ret));
|
||||
ret = flush_ret;
|
||||
}
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) */
|
||||
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Set_fmt failed (%s), fallback to previous configuration",
|
||||
esp_err_to_name(ret));
|
||||
esp_err_t rollback_ret = adc_apply_patterns(adc_data, old_active_idx, old_active_num, old_sample_rate_hz);
|
||||
if (rollback_ret == ESP_OK) {
|
||||
adc_data->out_bits = old_out_bits;
|
||||
adc_data->current_sample_rate_hz = old_sample_rate_hz;
|
||||
ESP_LOGW(TAG, "Fallback done: keep previous ADC format and channel settings");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Fallback to previous configuration failed (%s)", esp_err_to_name(rollback_ret));
|
||||
}
|
||||
}
|
||||
|
||||
if (was_enabled) {
|
||||
esp_err_t start_ret = adc_continuous_start(adc_data->handle);
|
||||
if (start_ret == ESP_OK) {
|
||||
adc_data->enabled = true;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_start failed after set_fmt: %s", esp_err_to_name(start_ret));
|
||||
ret = start_ret;
|
||||
}
|
||||
}
|
||||
return ret == ESP_OK ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
static int _adc_data_close(const audio_codec_data_if_t *h)
|
||||
{
|
||||
adc_data_t *adc_data = (adc_data_t *)h;
|
||||
ESP_RETURN_ON_FALSE(adc_data != NULL, ESP_CODEC_DEV_INVALID_ARG, TAG, "ADC data is NULL");
|
||||
|
||||
if (adc_data->enabled) {
|
||||
esp_err_t stop_ret = adc_continuous_stop(adc_data->handle);
|
||||
if (stop_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_stop failed during close: %s", esp_err_to_name(stop_ret));
|
||||
} else {
|
||||
adc_data->enabled = false;
|
||||
}
|
||||
}
|
||||
/* Only destroy ADC handle when it was created locally by this data_if. */
|
||||
if (!adc_data->use_ext_handle) {
|
||||
esp_err_t deinit_ret = adc_continuous_deinit(adc_data->handle);
|
||||
if (deinit_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Call to adc_continuous_deinit failed during close: %s", esp_err_to_name(deinit_ret));
|
||||
}
|
||||
}
|
||||
if (adc_data->parsed_buf) {
|
||||
free(adc_data->parsed_buf);
|
||||
adc_data->parsed_buf = NULL;
|
||||
}
|
||||
if (adc_data->raw_read_buf) {
|
||||
free(adc_data->raw_read_buf);
|
||||
adc_data->raw_read_buf = NULL;
|
||||
}
|
||||
adc_data->is_open = false;
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
const audio_codec_data_if_t *audio_codec_new_adc_data(audio_codec_adc_cfg_t *adc_cfg)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(adc_cfg != NULL, NULL, TAG, "ADC cfg is NULL");
|
||||
ESP_RETURN_ON_FALSE(adc_cfg->continuous_cfg.pattern_num > 0, NULL, TAG, "Pattern_num should be > 0");
|
||||
ESP_RETURN_ON_FALSE(adc_cfg->continuous_cfg.pattern_num <= SOC_ADC_PATT_LEN_MAX, NULL, TAG, "Pattern_num too large");
|
||||
ESP_RETURN_ON_FALSE(adc_cfg->continuous_cfg.conv_frame_size > 0, NULL, TAG, "Conv_frame_size should be > 0");
|
||||
ESP_RETURN_ON_FALSE(adc_cfg->continuous_cfg.sample_freq_hz > 0, NULL, TAG, "Sample_freq_hz should be > 0");
|
||||
ESP_RETURN_ON_FALSE(adc_cfg->continuous_cfg.cfg_mode == AUDIO_CODEC_ADC_CFG_MODE_SINGLE_UNIT || adc_cfg->continuous_cfg.cfg_mode == AUDIO_CODEC_ADC_CFG_MODE_PATTERN,
|
||||
NULL, TAG, "Invalid cfg_mode");
|
||||
|
||||
/* One allocation owns all runtime state; deleted by audio_codec_delete_data_if(). */
|
||||
adc_data_t *adc_data = calloc(1, sizeof(adc_data_t));
|
||||
ESP_RETURN_ON_FALSE(adc_data != NULL, NULL, TAG, "Allocate adc_data failed");
|
||||
|
||||
adc_data->cfg = adc_cfg->continuous_cfg;
|
||||
adc_data->base_pattern_num = adc_cfg->continuous_cfg.pattern_num;
|
||||
adc_data->out_bits = 16;
|
||||
adc_data->current_sample_rate_hz = adc_cfg->continuous_cfg.sample_freq_hz;
|
||||
memset(adc_data->base_pattern_bits, 0, sizeof(adc_data->base_pattern_bits));
|
||||
memset(adc_data->active_pattern_bits, 0, sizeof(adc_data->active_pattern_bits));
|
||||
adc_data->raw_read_buf_size = adc_data->cfg.conv_frame_size;
|
||||
if (adc_data->raw_read_buf_size < SOC_ADC_DIGI_RESULT_BYTES) {
|
||||
adc_data->raw_read_buf_size = SOC_ADC_DIGI_RESULT_BYTES;
|
||||
}
|
||||
|
||||
adc_data->raw_read_buf_size = (adc_data->raw_read_buf_size / SOC_ADC_DIGI_RESULT_BYTES) * SOC_ADC_DIGI_RESULT_BYTES;
|
||||
|
||||
if (adc_data->cfg.cfg_mode == AUDIO_CODEC_ADC_CFG_MODE_SINGLE_UNIT) {
|
||||
/* Expand legacy single_unit config to uniform pattern table. */
|
||||
uint8_t sample_bits = adc_get_effective_sample_bits(adc_data->cfg.cfg.single_unit.bit_width);
|
||||
if (!adc_data_bit_width_valid(sample_bits)) {
|
||||
ESP_LOGE(TAG, "Invalid bit_width");
|
||||
goto new_err;
|
||||
}
|
||||
for (int i = 0; i < adc_data->base_pattern_num; i++) {
|
||||
if (!adc_unit_supported((adc_unit_t)adc_data->cfg.cfg.single_unit.unit_id)) {
|
||||
ESP_LOGE(TAG, "ADC unit %u not supported in continuous mode", adc_data->cfg.cfg.single_unit.unit_id);
|
||||
goto new_err;
|
||||
}
|
||||
if (adc_data->cfg.cfg.single_unit.channel_id[i] >= SOC_ADC_CHANNEL_NUM((adc_unit_t)adc_data->cfg.cfg.single_unit.unit_id)) {
|
||||
ESP_LOGE(TAG, "Invalid ADC channel %u for unit %u", adc_data->cfg.cfg.single_unit.channel_id[i],
|
||||
adc_data->cfg.cfg.single_unit.unit_id);
|
||||
goto new_err;
|
||||
}
|
||||
adc_data->base_patterns[i].unit = adc_data->cfg.cfg.single_unit.unit_id;
|
||||
adc_data->base_patterns[i].atten = adc_data->cfg.cfg.single_unit.atten;
|
||||
adc_data->base_patterns[i].bit_width = adc_data->cfg.cfg.single_unit.bit_width;
|
||||
adc_data->base_patterns[i].channel = adc_data->cfg.cfg.single_unit.channel_id[i];
|
||||
adc_store_pattern_bits(adc_data->base_pattern_bits,
|
||||
(adc_unit_t)adc_data->base_patterns[i].unit,
|
||||
(adc_channel_t)adc_data->base_patterns[i].channel,
|
||||
sample_bits);
|
||||
}
|
||||
} else {
|
||||
/* Pattern mode: use caller-provided unit/channel entries directly. */
|
||||
memcpy(adc_data->base_patterns, adc_data->cfg.cfg.patterns,
|
||||
adc_data->base_pattern_num * sizeof(adc_digi_pattern_config_t));
|
||||
for (int i = 0; i < adc_data->base_pattern_num; i++) {
|
||||
if (!adc_data_bit_width_valid(adc_data->base_patterns[i].bit_width)) {
|
||||
ESP_LOGE(TAG, "Invalid bit_width in patterns[%d]", i);
|
||||
goto new_err;
|
||||
}
|
||||
if (!adc_unit_supported((adc_unit_t)adc_data->base_patterns[i].unit)) {
|
||||
ESP_LOGE(TAG, "ADC unit %u not supported in continuous mode", adc_data->base_patterns[i].unit);
|
||||
goto new_err;
|
||||
}
|
||||
if (adc_data->base_patterns[i].channel >= SOC_ADC_CHANNEL_NUM((adc_unit_t)adc_data->base_patterns[i].unit)) {
|
||||
ESP_LOGE(TAG, "Invalid ADC channel %u for unit %u", adc_data->base_patterns[i].channel,
|
||||
adc_data->base_patterns[i].unit);
|
||||
goto new_err;
|
||||
}
|
||||
adc_store_pattern_bits(adc_data->base_pattern_bits,
|
||||
(adc_unit_t)adc_data->base_patterns[i].unit,
|
||||
(adc_channel_t)adc_data->base_patterns[i].channel,
|
||||
adc_get_effective_sample_bits(adc_data->base_patterns[i].bit_width));
|
||||
}
|
||||
}
|
||||
|
||||
adc_data->raw_read_buf = calloc(1, adc_data->raw_read_buf_size);
|
||||
if (adc_data->raw_read_buf == NULL) {
|
||||
free(adc_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t parsed_buf_count = adc_data->raw_read_buf_size / SOC_ADC_DIGI_RESULT_BYTES;
|
||||
if (parsed_buf_count == 0) {
|
||||
parsed_buf_count = 1;
|
||||
}
|
||||
adc_data->parsed_buf = calloc(parsed_buf_count, sizeof(codec_adc_parsed_data_t));
|
||||
if (adc_data->parsed_buf == NULL) {
|
||||
free(adc_data->raw_read_buf);
|
||||
free(adc_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (adc_cfg->handle == NULL) {
|
||||
/* Local handle mode: lifecycle owned by this module. */
|
||||
adc_continuous_handle_cfg_t adc_handle_cfg = {
|
||||
.max_store_buf_size = adc_data->cfg.max_store_buf_size,
|
||||
.conv_frame_size = adc_data->cfg.conv_frame_size,
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
|
||||
.flags.flush_pool = true,
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) */
|
||||
};
|
||||
if (adc_continuous_new_handle(&adc_handle_cfg, &adc_data->handle) != ESP_OK) {
|
||||
goto new_err;
|
||||
}
|
||||
adc_data->use_ext_handle = false;
|
||||
} else {
|
||||
/* Reuse mode: external owner manages deinit, we only start/stop/config. */
|
||||
adc_data->handle = (adc_continuous_handle_t)adc_cfg->handle;
|
||||
adc_data->use_ext_handle = true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < adc_data->base_pattern_num; i++) {
|
||||
adc_data->active_pattern_idx[i] = i;
|
||||
}
|
||||
if (adc_apply_patterns(adc_data, adc_data->active_pattern_idx, adc_data->base_pattern_num,
|
||||
adc_data->cfg.sample_freq_hz) != ESP_OK) {
|
||||
goto new_err;
|
||||
}
|
||||
|
||||
adc_data->base.open = NULL;
|
||||
adc_data->base.is_open = _adc_data_is_open;
|
||||
adc_data->base.enable = _adc_data_enable;
|
||||
adc_data->base.read = _adc_data_read;
|
||||
adc_data->base.write = NULL;
|
||||
adc_data->base.set_fmt = _adc_data_set_fmt;
|
||||
adc_data->base.close = _adc_data_close;
|
||||
adc_data->is_open = true;
|
||||
return &adc_data->base;
|
||||
|
||||
new_err:
|
||||
/* Centralized failure cleanup for all partially-initialized resources. */
|
||||
if (adc_data->handle && !adc_data->use_ext_handle) {
|
||||
adc_continuous_deinit(adc_data->handle);
|
||||
}
|
||||
if (adc_data->parsed_buf) {
|
||||
free(adc_data->parsed_buf);
|
||||
}
|
||||
if (adc_data->raw_read_buf) {
|
||||
free(adc_data->raw_read_buf);
|
||||
}
|
||||
free(adc_data);
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,780 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "audio_codec_data_if.h"
|
||||
#include "esp_codec_dev_defaults.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include "hal/i2s_types.h"
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#include "driver/i2s_std.h"
|
||||
#include "driver/i2s_tdm.h"
|
||||
#include "driver/i2s_pdm.h"
|
||||
#else
|
||||
#include "driver/i2s.h"
|
||||
#endif
|
||||
#include "esp_codec_dev_os.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define TAG "I2S_IF"
|
||||
|
||||
#define DEFAULT_WAIT_TIMEOUT (1000)
|
||||
|
||||
typedef struct {
|
||||
audio_codec_data_if_t base;
|
||||
bool is_open;
|
||||
uint8_t port;
|
||||
void *out_handle;
|
||||
void *in_handle;
|
||||
bool out_enable;
|
||||
bool in_enable;
|
||||
bool in_disable_pending;
|
||||
bool out_disable_pending;
|
||||
bool in_reconfig;
|
||||
bool out_reconfig;
|
||||
esp_codec_dev_sample_info_t in_fs;
|
||||
esp_codec_dev_sample_info_t out_fs;
|
||||
esp_codec_dev_sample_info_t fs;
|
||||
i2s_clock_src_t clk_src;
|
||||
} i2s_data_t;
|
||||
|
||||
typedef struct i2s_data_keep_t i2s_data_keep_t;
|
||||
struct i2s_data_keep_t {
|
||||
i2s_data_t *i2s_data;
|
||||
esp_codec_dev_mutex_handle_t mutex;
|
||||
i2s_data_keep_t *next;
|
||||
};
|
||||
|
||||
static i2s_data_keep_t *i2s_data_list = NULL;
|
||||
|
||||
static esp_codec_dev_mutex_handle_t try_alloc_mutex(uint8_t port)
|
||||
{
|
||||
i2s_data_keep_t *cur = i2s_data_list;
|
||||
while (cur != NULL) {
|
||||
if (cur->i2s_data->port == port) {
|
||||
return cur->mutex;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
return esp_codec_dev_mutex_create();
|
||||
}
|
||||
|
||||
static esp_codec_dev_mutex_handle_t get_mutex(i2s_data_t *i2s_data)
|
||||
{
|
||||
i2s_data_keep_t *cur = i2s_data_list;
|
||||
while (cur != NULL) {
|
||||
if (cur->i2s_data == i2s_data) {
|
||||
return cur->mutex;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void try_free_mutex(uint8_t port, esp_codec_dev_mutex_handle_t mutex)
|
||||
{
|
||||
uint8_t ref_count = 0;
|
||||
i2s_data_keep_t *cur = i2s_data_list;
|
||||
while (cur != NULL) {
|
||||
if (cur->i2s_data->port == port) {
|
||||
ref_count++;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
if (ref_count == 0) {
|
||||
esp_codec_dev_mutex_destroy(mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static void _i2s_lock(i2s_data_t *i2s_data, const char *func)
|
||||
{
|
||||
esp_codec_dev_mutex_handle_t mutex = get_mutex(i2s_data);
|
||||
if (mutex) {
|
||||
int ret = esp_codec_dev_mutex_lock(mutex, DEFAULT_WAIT_TIMEOUT);
|
||||
if (ret != ESP_CODEC_DEV_OK) {
|
||||
ESP_LOGE(TAG, "Lock failed in %s", func);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void _i2s_unlock(i2s_data_t *i2s_data)
|
||||
{
|
||||
esp_codec_dev_mutex_handle_t mutex = get_mutex(i2s_data);
|
||||
if (mutex) {
|
||||
esp_codec_dev_mutex_unlock(mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static void add_to_keeper(i2s_data_t *i2s_data)
|
||||
{
|
||||
i2s_data_keep_t *keep_info = (i2s_data_keep_t *) calloc(1, sizeof(i2s_data_keep_t));
|
||||
if (keep_info == NULL) {
|
||||
ESP_LOGE(TAG, "Out of memory for keeper");
|
||||
return;
|
||||
}
|
||||
keep_info->mutex = try_alloc_mutex(i2s_data->port);
|
||||
keep_info->i2s_data = i2s_data;
|
||||
if (i2s_data_list == NULL) {
|
||||
i2s_data_list = keep_info;
|
||||
} else {
|
||||
keep_info->next = i2s_data_list;
|
||||
i2s_data_list = keep_info;
|
||||
}
|
||||
}
|
||||
|
||||
static void remove_from_keeper(i2s_data_t *i2s_data)
|
||||
{
|
||||
i2s_data_keep_t *pre = NULL;
|
||||
i2s_data_keep_t *cur = i2s_data_list;
|
||||
while (cur != NULL) {
|
||||
if (cur->i2s_data == i2s_data) {
|
||||
if (pre == NULL) {
|
||||
i2s_data_list = cur->next;
|
||||
} else {
|
||||
pre->next = cur->next;
|
||||
}
|
||||
try_free_mutex(i2s_data->port, cur->mutex);
|
||||
free(cur);
|
||||
break;
|
||||
}
|
||||
pre = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
|
||||
static i2s_data_t *get_paired(i2s_data_t *i2s_data, bool playback)
|
||||
{
|
||||
i2s_data_keep_t *cur = i2s_data_list;
|
||||
/* Same port may list a split RX/TX pair first; defer single-node full duplex until no half-duplex peer matches. */
|
||||
i2s_data_t *duplex_fallback = NULL;
|
||||
while (cur != NULL) {
|
||||
if (cur->i2s_data->port == i2s_data->port) {
|
||||
if (cur->i2s_data->in_handle && cur->i2s_data->out_handle) {
|
||||
duplex_fallback = cur->i2s_data;
|
||||
cur = cur->next;
|
||||
continue;
|
||||
}
|
||||
if (playback && cur->i2s_data->in_handle) {
|
||||
return cur->i2s_data;
|
||||
} else if (playback == false && cur->i2s_data->out_handle) {
|
||||
return cur->i2s_data;
|
||||
}
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
return duplex_fallback;
|
||||
}
|
||||
|
||||
static bool _i2s_valid_fmt(esp_codec_dev_sample_info_t *fs)
|
||||
{
|
||||
if (fs->sample_rate == 0 ||
|
||||
fs->sample_rate >= 192000) {
|
||||
ESP_LOGE(TAG, "Bad sample_rate %d", (int) fs->sample_rate);
|
||||
}
|
||||
if (fs->channel == 0 ||
|
||||
(fs->channel >> 1 << 1) != fs->channel) {
|
||||
ESP_LOGE(TAG, "Not support channel %d", fs->channel);
|
||||
return false;
|
||||
}
|
||||
if (fs->bits_per_sample < 8 || fs->bits_per_sample > 32 ||
|
||||
(fs->bits_per_sample >> 3 << 3) != fs->bits_per_sample) {
|
||||
ESP_LOGE(TAG, "Not support bits_per_sample %d", fs->bits_per_sample);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int _i2s_drv_enable(i2s_data_t *i2s_data, bool playback, bool enable)
|
||||
{
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
i2s_chan_handle_t channel = (i2s_chan_handle_t) (
|
||||
playback ? i2s_data->out_handle : i2s_data->in_handle);
|
||||
if (channel == NULL) {
|
||||
return ESP_CODEC_DEV_NOT_FOUND;
|
||||
}
|
||||
int ret;
|
||||
if (enable) {
|
||||
ret = i2s_channel_enable(channel);
|
||||
} else {
|
||||
ret = i2s_channel_disable(channel);
|
||||
}
|
||||
return ret == ESP_OK ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
#endif
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
static uint8_t get_active_channel(esp_codec_dev_sample_info_t *fs)
|
||||
{
|
||||
if (fs->channel_mask == 0) {
|
||||
return fs->channel;
|
||||
}
|
||||
int channel = 0;
|
||||
uint16_t mask = fs->channel_mask;
|
||||
while (mask > 0) {
|
||||
if (mask & 1) {
|
||||
channel++;
|
||||
}
|
||||
mask >>= 1;
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
static uint8_t get_bits(i2s_data_t *i2s_data, bool playback)
|
||||
{
|
||||
uint8_t total_bits = i2s_data->fs.bits_per_sample * i2s_data->fs.channel;
|
||||
if (playback) {
|
||||
return total_bits / i2s_data->out_fs.channel;
|
||||
}
|
||||
return total_bits / i2s_data->in_fs.channel;
|
||||
}
|
||||
|
||||
static int set_drv_fs(i2s_chan_handle_t channel, bool playback, uint8_t slot_bits, i2s_clock_src_t clk_src, esp_codec_dev_sample_info_t *fs)
|
||||
{
|
||||
i2s_chan_info_t channel_info = {0};
|
||||
int ret = ESP_CODEC_DEV_OK;
|
||||
i2s_channel_get_info(channel, &channel_info);
|
||||
switch (channel_info.mode) {
|
||||
case I2S_COMM_MODE_STD: {
|
||||
uint8_t data_bits = fs->bits_per_sample;
|
||||
uint8_t active_channel = get_active_channel(fs);
|
||||
if (fs->channel > 2) {
|
||||
slot_bits = slot_bits * fs->channel / 2;
|
||||
active_channel = 2;
|
||||
data_bits = slot_bits;
|
||||
}
|
||||
i2s_std_slot_mask_t slot_mask = fs->channel_mask ? (i2s_std_slot_mask_t)fs->channel_mask : I2S_STD_SLOT_BOTH;
|
||||
i2s_std_slot_config_t slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(slot_bits, active_channel);
|
||||
slot_cfg.slot_mask = slot_mask & I2S_STD_SLOT_BOTH; // Only support 2 slots, the max mask is I2S_STD_SLOT_BOTH
|
||||
if (slot_bits > data_bits) {
|
||||
slot_cfg.data_bit_width = data_bits;
|
||||
}
|
||||
slot_cfg.slot_bit_width = slot_bits;
|
||||
i2s_std_clk_config_t clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(fs->sample_rate);
|
||||
if (clk_src) {
|
||||
clk_cfg.clk_src = clk_src;
|
||||
}
|
||||
if (fs->mclk_multiple) {
|
||||
clk_cfg.mclk_multiple = fs->mclk_multiple;
|
||||
}
|
||||
if (slot_bits == 24 && (clk_cfg.mclk_multiple % 3) != 0) {
|
||||
clk_cfg.mclk_multiple = I2S_MCLK_MULTIPLE_384;
|
||||
}
|
||||
ret = i2s_channel_reconfig_std_slot(channel, &slot_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to reconfig STD slot, ret: %d", ret);
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
ret = i2s_channel_reconfig_std_clock(channel, &clk_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to reconfig STD clock, ret: %d", ret);
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
ESP_LOGI(TAG, "STD: %s, data_bit: %d, slot_bit: %d, ws_width: %d, slot_mode: %s, slot_mask: 0x%x",
|
||||
channel_info.dir == I2S_DIR_RX ? "RX" : "TX", (int)slot_cfg.data_bit_width, (int)slot_cfg.slot_bit_width, (int)slot_cfg.ws_width,
|
||||
slot_cfg.slot_mode == I2S_SLOT_MODE_MONO ? "MONO" : "STEREO", (int)slot_cfg.slot_mask);
|
||||
ESP_LOGI(TAG, "STD: %s, sample_rate_hz: %d, mclk_multiple: %d, clk_src: %d",
|
||||
channel_info.dir == I2S_DIR_RX ? "RX" : "TX", (int)clk_cfg.sample_rate_hz, (int)clk_cfg.mclk_multiple, (int)clk_cfg.clk_src);
|
||||
}
|
||||
break;
|
||||
#if SOC_I2S_SUPPORTS_PDM
|
||||
case I2S_COMM_MODE_PDM: {
|
||||
if (playback == false) {
|
||||
#if SOC_I2S_SUPPORTS_PDM_RX
|
||||
i2s_pdm_rx_clk_config_t clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG(fs->sample_rate);
|
||||
if (clk_src) {
|
||||
clk_cfg.clk_src = clk_src;
|
||||
}
|
||||
if (fs->mclk_multiple) {
|
||||
clk_cfg.mclk_multiple = fs->mclk_multiple;
|
||||
}
|
||||
i2s_pdm_rx_slot_config_t slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(slot_bits, I2S_SLOT_MODE_STEREO);
|
||||
i2s_pdm_slot_mask_t slot_mask = fs->channel_mask ? (i2s_pdm_slot_mask_t)fs->channel_mask : I2S_PDM_SLOT_BOTH;
|
||||
// Stereo channel mask is ignored in driver, need use mono instead
|
||||
if (fs->channel_mask && fs->channel_mask < 3) {
|
||||
slot_cfg.slot_mode = I2S_SLOT_MODE_MONO;
|
||||
}
|
||||
slot_cfg.slot_mask = slot_mask;
|
||||
if (slot_bits > fs->bits_per_sample) {
|
||||
slot_cfg.data_bit_width = fs->bits_per_sample;
|
||||
slot_cfg.slot_bit_width = slot_bits;
|
||||
}
|
||||
ret = i2s_channel_reconfig_pdm_rx_clock(channel, &clk_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
ret = i2s_channel_reconfig_pdm_rx_slot(channel, &slot_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
ESP_LOGI(TAG, "PDM RX mode, data_bit: %d, slot_bit: %d, sample_rate: %d, slot_mask: 0x%x",
|
||||
fs->bits_per_sample, slot_bits, (int)fs->sample_rate, (int)fs->channel_mask);
|
||||
#else
|
||||
ESP_LOGE(TAG, "PDM RX not supported");
|
||||
return ESP_CODEC_DEV_NOT_SUPPORT;
|
||||
#endif /* SOC_I2S_SUPPORTS_PDM_RX */
|
||||
} else {
|
||||
#if SOC_I2S_SUPPORTS_PDM_TX
|
||||
i2s_pdm_tx_clk_config_t clk_cfg = I2S_PDM_TX_CLK_DEFAULT_CONFIG(fs->sample_rate);
|
||||
if (clk_src) {
|
||||
clk_cfg.clk_src = clk_src;
|
||||
}
|
||||
if (fs->mclk_multiple) {
|
||||
clk_cfg.mclk_multiple = fs->mclk_multiple;
|
||||
}
|
||||
clk_cfg.up_sample_fs = fs->sample_rate / 100;
|
||||
i2s_pdm_tx_slot_config_t slot_cfg = I2S_PDM_TX_SLOT_DEFAULT_CONFIG(slot_bits, I2S_SLOT_MODE_STEREO);
|
||||
// Stereo channel mask is ignored, need use mono instead
|
||||
if (fs->channel_mask && fs->channel_mask < 3) {
|
||||
slot_cfg.slot_mode = I2S_SLOT_MODE_MONO;
|
||||
}
|
||||
#if SOC_I2S_HW_VERSION_1
|
||||
i2s_pdm_slot_mask_t slot_mask = fs->channel_mask ?
|
||||
(i2s_pdm_slot_mask_t) fs->channel_mask : I2S_PDM_SLOT_BOTH;
|
||||
slot_cfg.slot_mask = slot_mask;
|
||||
#endif /* SOC_I2S_HW_VERSION_1 */
|
||||
if (slot_bits > fs->bits_per_sample) {
|
||||
slot_cfg.data_bit_width = fs->bits_per_sample;
|
||||
slot_cfg.slot_bit_width = slot_bits;
|
||||
}
|
||||
ret = i2s_channel_reconfig_pdm_tx_clock(channel, &clk_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
ret = i2s_channel_reconfig_pdm_tx_slot(channel, &slot_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
ESP_LOGI(TAG, "PDM TX mode, data_bit: %d, slot_bit: %d, sample_rate: %d, slot_mask: 0x%x",
|
||||
fs->bits_per_sample, slot_bits, (int)fs->sample_rate, (int)fs->channel_mask);
|
||||
#else
|
||||
ESP_LOGE(TAG, "PDM TX not supported");
|
||||
return ESP_CODEC_DEV_NOT_SUPPORT;
|
||||
#endif /* SOC_I2S_SUPPORTS_PDM_TX */
|
||||
}
|
||||
}
|
||||
break;
|
||||
#endif /* SOC_I2S_SUPPORTS_PDM */
|
||||
#if SOC_I2S_SUPPORTS_TDM
|
||||
case I2S_COMM_MODE_TDM: {
|
||||
i2s_tdm_clk_config_t clk_cfg = I2S_TDM_CLK_DEFAULT_CONFIG(fs->sample_rate);
|
||||
if (clk_src) {
|
||||
clk_cfg.clk_src = clk_src;
|
||||
}
|
||||
if (slot_bits == 24) {
|
||||
clk_cfg.mclk_multiple = I2S_MCLK_MULTIPLE_384;
|
||||
}
|
||||
if (fs->mclk_multiple) {
|
||||
clk_cfg.mclk_multiple = fs->mclk_multiple;
|
||||
}
|
||||
i2s_tdm_slot_config_t slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(
|
||||
slot_bits,
|
||||
I2S_SLOT_MODE_STEREO,
|
||||
(i2s_tdm_slot_mask_t)fs->channel_mask);
|
||||
slot_cfg.total_slot = fs->channel;
|
||||
// When use STD mode, left align is true by default, but when use TDM mode, left align is false by default
|
||||
// so need set to true to keep the same with STD mode
|
||||
slot_cfg.left_align = true;
|
||||
if (slot_bits > fs->bits_per_sample) {
|
||||
slot_cfg.data_bit_width = fs->bits_per_sample;
|
||||
}
|
||||
slot_cfg.slot_bit_width = slot_bits;
|
||||
slot_cfg.ws_width = slot_bits * fs->channel / 2;
|
||||
ret = i2s_channel_reconfig_tdm_slot(channel, &slot_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
ret = i2s_channel_reconfig_tdm_clock(channel, &clk_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
ESP_LOGI(TAG, "TDM: %s, data_bit: %d, slot_bit: %d, ws_width: %d, total_slot: %d, slot_mask: 0x%x",
|
||||
channel_info.dir == I2S_DIR_RX ? "RX" : "TX", (int)slot_cfg.data_bit_width, (int)slot_cfg.slot_bit_width,
|
||||
(int)slot_cfg.ws_width, (int)slot_cfg.total_slot, (int)slot_cfg.slot_mask);
|
||||
ESP_LOGI(TAG, "TDM: %s, sample_rate_hz: %d, mclk_multiple: %d, clk_src: %d",
|
||||
channel_info.dir == I2S_DIR_RX ? "RX" : "TX", (int)clk_cfg.sample_rate_hz, (int)clk_cfg.mclk_multiple, (int)clk_cfg.clk_src);
|
||||
}
|
||||
break;
|
||||
#endif /* SOC_I2S_SUPPORTS_TDM */
|
||||
default:
|
||||
return ESP_CODEC_DEV_NOT_SUPPORT;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int set_fs(i2s_data_t *i2s_data, bool playback, bool skip)
|
||||
{
|
||||
i2s_chan_handle_t channel = (i2s_chan_handle_t) (playback ? i2s_data->out_handle : i2s_data->in_handle);
|
||||
i2s_chan_info_t channel_info = {0};
|
||||
esp_codec_dev_sample_info_t *fs = playback ? &i2s_data->out_fs : &i2s_data->in_fs;
|
||||
uint8_t bits_per_sample = get_bits(i2s_data, playback);
|
||||
i2s_channel_get_info(channel, &channel_info);
|
||||
int ret = set_drv_fs(channel, playback, bits_per_sample, i2s_data->clk_src, fs);
|
||||
if (ret != ESP_CODEC_DEV_OK) {
|
||||
return ret;
|
||||
}
|
||||
i2s_data_t *paired = get_paired(i2s_data, playback);
|
||||
// Set RX clock will not take effect if in full duplex mode, need update TX clock also
|
||||
if (skip == false && playback == false && paired && paired->out_handle != NULL && paired->out_enable == false) {
|
||||
// TX is master, set to RX not take effect need reconfig TX also
|
||||
channel = (i2s_chan_handle_t) paired->out_handle;
|
||||
_i2s_drv_enable(paired, true, false);
|
||||
ret = set_drv_fs(channel, true, bits_per_sample, i2s_data->clk_src, fs);
|
||||
_i2s_drv_enable(paired, true, true);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int check_fs_compatible(i2s_data_t *i2s_data, bool playback, esp_codec_dev_sample_info_t *fs)
|
||||
{
|
||||
// Set fs directly when only enable one channel
|
||||
i2s_data_t *paired = get_paired(i2s_data, playback);
|
||||
esp_codec_dev_sample_info_t *channel_fs = playback ? &i2s_data->out_fs : &i2s_data->in_fs;
|
||||
if ((paired == NULL) ||
|
||||
(playback && paired->in_enable == false) ||
|
||||
(!playback && paired->out_enable == false)) {
|
||||
memcpy(&i2s_data->fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
if (paired && paired != i2s_data) {
|
||||
memcpy(&paired->fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
}
|
||||
memcpy(channel_fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
if (paired != NULL) {
|
||||
ESP_LOGI(TAG, "Paired data: %p, current mode: %s, paired in_enable: %d, paired out_enable: %d",
|
||||
paired, playback ? "playback" : "record", paired->in_enable, paired->out_enable);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "No paired data, current mode: %s", playback ? "playback" : "record");
|
||||
}
|
||||
return set_fs(i2s_data, playback, false);
|
||||
}
|
||||
if (fs->sample_rate != paired->fs.sample_rate) {
|
||||
ESP_LOGE(TAG, "Current mode %s conflict sample_rate %d with peer mode sample_rate %d",
|
||||
playback ? "playback" : "record", (int)fs->sample_rate, (int)i2s_data->fs.sample_rate);
|
||||
return ESP_CODEC_DEV_NOT_SUPPORT;
|
||||
}
|
||||
// Channel and bits same, set directly
|
||||
if (fs->channel == paired->fs.channel &&
|
||||
fs->bits_per_sample == paired->fs.bits_per_sample) {
|
||||
memcpy(&i2s_data->fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
memcpy(channel_fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
ESP_LOGI(TAG, "Current mode(%s) and peer mode have same sample_rate %d, channel %d, bits_per_sample %d",
|
||||
playback ? "playback" : "record", (int)fs->sample_rate, fs->channel, fs->bits_per_sample);
|
||||
return set_fs(i2s_data, playback, false);
|
||||
}
|
||||
memcpy(channel_fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
uint16_t want_bits = fs->channel * fs->bits_per_sample;
|
||||
uint16_t run_bits = paired->fs.channel * paired->fs.bits_per_sample;
|
||||
int ret;
|
||||
if (run_bits > want_bits) {
|
||||
// eg. Open with 4ch 16bit firstly, then open with 2ch 16bit, need extend slot bits to 2ch 32bit
|
||||
i2s_chan_handle_t channel = (i2s_chan_handle_t)(playback ? i2s_data->out_handle : i2s_data->in_handle);
|
||||
int slot_bits = run_bits / fs->channel;
|
||||
ret = set_drv_fs(channel, playback, slot_bits, i2s_data->clk_src, fs);
|
||||
if (ret != ESP_CODEC_DEV_OK) {
|
||||
ESP_LOGE(TAG, "set_drv_fs failed");
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Current mode %s has extended slot bits from %d to %d", playback ? "playback" : "record", want_bits, run_bits);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
// Need expand peer channel bits
|
||||
ESP_LOGI(TAG, "Peer mode %s need extend bits %d to %d", !playback ? "playback" : "record", run_bits, want_bits);
|
||||
do {
|
||||
if (want_bits > run_bits) {
|
||||
if (playback == false) {
|
||||
paired->out_reconfig = true;
|
||||
} else {
|
||||
paired->in_reconfig = true;
|
||||
}
|
||||
ret = _i2s_drv_enable(paired, !playback, false);
|
||||
if (ret != ESP_CODEC_DEV_OK) {
|
||||
break;
|
||||
}
|
||||
memcpy(&paired->fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
memcpy(&i2s_data->fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
}
|
||||
// Need set fs before enable
|
||||
ret = set_fs(i2s_data, playback, false);
|
||||
if (ret != ESP_CODEC_DEV_OK) {
|
||||
break;
|
||||
}
|
||||
if (want_bits > run_bits) {
|
||||
ret = set_fs(paired, !playback, true);
|
||||
if (ret != ESP_CODEC_DEV_OK) {
|
||||
break;
|
||||
}
|
||||
ret = _i2s_drv_enable(paired, !playback, true);
|
||||
if (ret != ESP_CODEC_DEV_OK) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
i2s_data->out_reconfig = i2s_data->in_reconfig = false;
|
||||
paired->out_reconfig = paired->in_reconfig = false;
|
||||
return ret;
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
|
||||
static int _i2s_data_open(const audio_codec_data_if_t *h, void *data_cfg, int cfg_size)
|
||||
{
|
||||
i2s_data_t *i2s_data = (i2s_data_t *)h;
|
||||
if (h == NULL || data_cfg == NULL || cfg_size != sizeof(audio_codec_i2s_cfg_t)) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
audio_codec_i2s_cfg_t *i2s_cfg = (audio_codec_i2s_cfg_t *)data_cfg;
|
||||
i2s_data->is_open = true;
|
||||
i2s_data->port = i2s_cfg->port;
|
||||
i2s_data->out_handle = i2s_cfg->tx_handle;
|
||||
i2s_data->in_handle = i2s_cfg->rx_handle;
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
if (i2s_data->in_handle && i2s_data->out_handle == NULL) {
|
||||
i2s_chan_info_t channel_info = {0};
|
||||
i2s_channel_get_info(i2s_data->in_handle, &channel_info);
|
||||
// If use duplex mode, out_handle is the pair of in_handle, otherwise out_handle is NULL
|
||||
i2s_data->out_handle = channel_info.pair_chan;
|
||||
ESP_LOGI(TAG, "Auto get out handle %p from in handle %p when duplex mode", i2s_data->out_handle, i2s_data->in_handle);
|
||||
}
|
||||
i2s_data->clk_src = (i2s_clock_src_t)i2s_cfg->clk_src;
|
||||
#endif
|
||||
add_to_keeper(i2s_data);
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
static bool _i2s_data_is_open(const audio_codec_data_if_t *h)
|
||||
{
|
||||
i2s_data_t *i2s_data = (i2s_data_t *) h;
|
||||
if (i2s_data) {
|
||||
return i2s_data->is_open;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int _i2s_data_enable(const audio_codec_data_if_t *h, esp_codec_dev_type_t dev_type, bool enable)
|
||||
{
|
||||
i2s_data_t *i2s_data = (i2s_data_t *) h;
|
||||
if (i2s_data == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
if (i2s_data->is_open == false) {
|
||||
return ESP_CODEC_DEV_WRONG_STATE;
|
||||
}
|
||||
int ret = ESP_CODEC_DEV_OK;
|
||||
_i2s_lock(i2s_data, __func__);
|
||||
if (dev_type == ESP_CODEC_DEV_TYPE_IN_OUT) {
|
||||
ret = _i2s_drv_enable(i2s_data, true, enable);
|
||||
ret = _i2s_drv_enable(i2s_data, false, enable);
|
||||
} else {
|
||||
bool playback = dev_type & ESP_CODEC_DEV_TYPE_OUT ? true : false;
|
||||
i2s_data_t *paired = get_paired(i2s_data, playback);
|
||||
// When RX is working TX disable should be blocked
|
||||
if (enable == false && playback && i2s_data->out_handle && paired && paired->in_enable) {
|
||||
ESP_LOGI(TAG, "Pending out channel for in channel running");
|
||||
i2s_data->out_disable_pending = true;
|
||||
}
|
||||
#if SOC_I2S_HW_VERSION_1
|
||||
// For ESP32 and ESP32S3 if disable RX, TX also not work need pending until TX not used
|
||||
else if (enable == false && playback == false && i2s_data->in_handle && paired && paired->out_enable) {
|
||||
ESP_LOGI(TAG, "Pending in channel for out channel running");
|
||||
i2s_data->in_disable_pending = true;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
ret = _i2s_drv_enable(i2s_data, playback, enable);
|
||||
// Disable TX when RX disable if TX disable is pending
|
||||
if (enable == false) {
|
||||
if (playback == false && paired && paired->out_disable_pending) {
|
||||
ret = _i2s_drv_enable(paired, true, enable);
|
||||
paired->out_disable_pending = false;
|
||||
}
|
||||
if (playback == true && paired && paired->in_disable_pending) {
|
||||
ret = _i2s_drv_enable(paired, false, enable);
|
||||
paired->in_disable_pending = false;
|
||||
}
|
||||
} else {
|
||||
// Force to clear pending flags if enable again
|
||||
if (playback) {
|
||||
i2s_data->out_disable_pending = false;
|
||||
} else {
|
||||
i2s_data->in_disable_pending = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dev_type & ESP_CODEC_DEV_TYPE_IN) {
|
||||
i2s_data->in_enable = enable;
|
||||
}
|
||||
if (dev_type & ESP_CODEC_DEV_TYPE_OUT) {
|
||||
i2s_data->out_enable = enable;
|
||||
}
|
||||
_i2s_unlock(i2s_data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int _i2s_data_set_fmt(const audio_codec_data_if_t *h, esp_codec_dev_type_t dev_type, esp_codec_dev_sample_info_t *fs)
|
||||
{
|
||||
i2s_data_t *i2s_data = (i2s_data_t *) h;
|
||||
if (i2s_data == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
if (i2s_data->is_open == false) {
|
||||
return ESP_CODEC_DEV_WRONG_STATE;
|
||||
}
|
||||
esp_codec_dev_sample_info_t eq_fs;
|
||||
if (fs->channel == 1) {
|
||||
// When using one channel replace to select channel 0 in default
|
||||
memcpy(&eq_fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
fs = &eq_fs;
|
||||
fs->channel = 2;
|
||||
fs->channel_mask = ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0);
|
||||
}
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
if (fs->channel_mask == 0) {
|
||||
// Add channel mask automatically when not set
|
||||
memcpy(&eq_fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
fs = &eq_fs;
|
||||
for (int i = 0; i < fs->channel; i++) {
|
||||
fs->channel_mask |= ESP_CODEC_DEV_MAKE_CHANNEL_MASK(i);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (_i2s_valid_fmt(fs) == false) {
|
||||
return ESP_CODEC_DEV_NOT_SUPPORT;
|
||||
}
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
int ret;
|
||||
_i2s_lock(i2s_data, __func__);
|
||||
// disable internally
|
||||
if (dev_type & ESP_CODEC_DEV_TYPE_OUT) {
|
||||
_i2s_drv_enable(i2s_data, true, false);
|
||||
}
|
||||
if (dev_type & ESP_CODEC_DEV_TYPE_IN) {
|
||||
_i2s_drv_enable(i2s_data, false, false);
|
||||
}
|
||||
if ((dev_type & ESP_CODEC_DEV_TYPE_IN) != 0 &&
|
||||
(dev_type & ESP_CODEC_DEV_TYPE_OUT) != 0) {
|
||||
// Device support playback and record at same time
|
||||
memcpy(&i2s_data->fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
memcpy(&i2s_data->in_fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
memcpy(&i2s_data->out_fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
ret = set_fs(i2s_data, true, true);
|
||||
ret = set_fs(i2s_data, false, true);
|
||||
} else {
|
||||
ret = check_fs_compatible(i2s_data, dev_type & ESP_CODEC_DEV_TYPE_OUT ? true : false, fs);
|
||||
}
|
||||
_i2s_unlock(i2s_data);
|
||||
return ret;
|
||||
#else
|
||||
// When use multichannel data
|
||||
if (fs->channel_mask) {
|
||||
i2s_channel_t sel_channel = 0;
|
||||
#if SOC_I2S_SUPPORTS_TDM
|
||||
sel_channel = (i2s_channel_t)(fs->channel_mask << 16);
|
||||
#else
|
||||
if (fs->channel_mask == ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0)) {
|
||||
sel_channel = 1;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "IC not support TDM");
|
||||
return ESP_CODEC_DEV_NOT_FOUND;
|
||||
}
|
||||
#endif
|
||||
i2s_set_clk(i2s_data->port, fs->sample_rate, fs->bits_per_sample, sel_channel);
|
||||
} else {
|
||||
i2s_set_clk(i2s_data->port, fs->sample_rate, fs->bits_per_sample, fs->channel);
|
||||
}
|
||||
i2s_zero_dma_buffer(i2s_data->port);
|
||||
memcpy(&i2s_data->fs, fs, sizeof(esp_codec_dev_sample_info_t));
|
||||
#endif
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
static int _i2s_data_read(const audio_codec_data_if_t *h, uint8_t *data, int size)
|
||||
{
|
||||
i2s_data_t *i2s_data = (i2s_data_t *) h;
|
||||
if (i2s_data == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
if (i2s_data->is_open == false) {
|
||||
return ESP_CODEC_DEV_WRONG_STATE;
|
||||
}
|
||||
size_t bytes_read = 0;
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
i2s_chan_handle_t rx_chan = (i2s_chan_handle_t) i2s_data->in_handle;
|
||||
if (rx_chan == NULL) {
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
if (i2s_data->in_reconfig) {
|
||||
memset(data, 0, size);
|
||||
esp_codec_dev_sleep(10);
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
int ret = i2s_channel_read(rx_chan, data, size, &bytes_read, DEFAULT_WAIT_TIMEOUT);
|
||||
#else
|
||||
int ret = i2s_read(i2s_data->port, data, size, &bytes_read, portMAX_DELAY);
|
||||
#endif
|
||||
return ret == 0 ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
static int _i2s_data_write(const audio_codec_data_if_t *h, uint8_t *data, int size)
|
||||
{
|
||||
i2s_data_t *i2s_data = (i2s_data_t *) h;
|
||||
if (i2s_data == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
if (i2s_data->is_open == false) {
|
||||
return ESP_CODEC_DEV_WRONG_STATE;
|
||||
}
|
||||
size_t bytes_written = 0;
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
i2s_chan_handle_t tx_chan = (i2s_chan_handle_t) i2s_data->out_handle;
|
||||
if (tx_chan == NULL) {
|
||||
return ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
if (i2s_data->out_reconfig) {
|
||||
esp_codec_dev_sleep(10);
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
int ret = i2s_channel_write(tx_chan, data, size, &bytes_written, DEFAULT_WAIT_TIMEOUT);
|
||||
#else
|
||||
int ret = i2s_write(i2s_data->port, data, size, &bytes_written, portMAX_DELAY);
|
||||
#endif
|
||||
return ret == 0 ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
static int _i2s_data_close(const audio_codec_data_if_t *h)
|
||||
{
|
||||
i2s_data_t *i2s_data = (i2s_data_t *) h;
|
||||
if (i2s_data == NULL) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
memset(&i2s_data->fs, 0, sizeof(esp_codec_dev_sample_info_t));
|
||||
memset(&i2s_data->in_fs, 0, sizeof(esp_codec_dev_sample_info_t));
|
||||
memset(&i2s_data->out_fs, 0, sizeof(esp_codec_dev_sample_info_t));
|
||||
i2s_data->is_open = false;
|
||||
remove_from_keeper(i2s_data);
|
||||
return ESP_CODEC_DEV_OK;
|
||||
}
|
||||
|
||||
const audio_codec_data_if_t *audio_codec_new_i2s_data(audio_codec_i2s_cfg_t *i2s_cfg)
|
||||
{
|
||||
i2s_data_t *i2s_data = calloc(1, sizeof(i2s_data_t));
|
||||
if (i2s_data == NULL) {
|
||||
ESP_LOGE(TAG, "No memory for instance");
|
||||
return NULL;
|
||||
}
|
||||
i2s_data->base.open = _i2s_data_open;
|
||||
i2s_data->base.is_open = _i2s_data_is_open;
|
||||
i2s_data->base.enable = _i2s_data_enable;
|
||||
i2s_data->base.read = _i2s_data_read;
|
||||
i2s_data->base.write = _i2s_data_write;
|
||||
i2s_data->base.set_fmt = _i2s_data_set_fmt;
|
||||
i2s_data->base.close = _i2s_data_close;
|
||||
int ret = _i2s_data_open(&i2s_data->base, i2s_cfg, sizeof(audio_codec_i2s_cfg_t));
|
||||
if (ret != 0) {
|
||||
free(i2s_data);
|
||||
return NULL;
|
||||
}
|
||||
return &i2s_data->base;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "audio_codec_gpio_if.h"
|
||||
#include "esp_codec_dev_types.h"
|
||||
#include "esp_err.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include <string.h>
|
||||
|
||||
#define TAG "GPIO_If"
|
||||
|
||||
static int _gpio_cfg(int16_t gpio, audio_gpio_dir_t dir, audio_gpio_mode_t mode)
|
||||
{
|
||||
if (gpio == -1) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
gpio_config_t io_conf;
|
||||
memset(&io_conf, 0, sizeof(io_conf));
|
||||
io_conf.mode = (dir == AUDIO_GPIO_DIR_OUT ? GPIO_MODE_OUTPUT : GPIO_MODE_INPUT);
|
||||
io_conf.pin_bit_mask = BIT64(gpio);
|
||||
io_conf.pull_down_en = ((mode & AUDIO_GPIO_MODE_PULL_DOWN) != 0);
|
||||
io_conf.pull_up_en = ((mode & AUDIO_GPIO_MODE_PULL_UP) != 0);
|
||||
esp_err_t ret = 0;
|
||||
ret |= gpio_config(&io_conf);
|
||||
return ret == 0 ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
static int _gpio_set(int16_t gpio, bool high)
|
||||
{
|
||||
if (gpio == -1) {
|
||||
return ESP_CODEC_DEV_INVALID_ARG;
|
||||
}
|
||||
int ret = gpio_set_level((gpio_num_t) gpio, high ? 1 : 0);
|
||||
return ret == 0 ? ESP_CODEC_DEV_OK : ESP_CODEC_DEV_DRV_ERR;
|
||||
}
|
||||
|
||||
static bool _gpio_get(int16_t gpio)
|
||||
{
|
||||
if (gpio == -1) {
|
||||
return false;
|
||||
}
|
||||
return (bool) gpio_get_level((gpio_num_t) gpio);
|
||||
}
|
||||
|
||||
const audio_codec_gpio_if_t *audio_codec_new_gpio(void)
|
||||
{
|
||||
audio_codec_gpio_if_t *gpio_if = (audio_codec_gpio_if_t *) calloc(1, sizeof(audio_codec_gpio_if_t));
|
||||
if (gpio_if == NULL) {
|
||||
ESP_LOGE(TAG, "No memory for instance");
|
||||
return NULL;
|
||||
}
|
||||
gpio_if->setup = _gpio_cfg;
|
||||
gpio_if->set = _gpio_set;
|
||||
gpio_if->get = _gpio_get;
|
||||
return gpio_if;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include "esp_codec_dev_os.h"
|
||||
|
||||
void esp_codec_dev_sleep(int ms)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(ms));
|
||||
}
|
||||
|
||||
esp_codec_dev_mutex_handle_t esp_codec_dev_mutex_create(void)
|
||||
{
|
||||
return (esp_codec_dev_mutex_handle_t)xSemaphoreCreateMutex();
|
||||
}
|
||||
|
||||
int esp_codec_dev_mutex_lock(esp_codec_dev_mutex_handle_t mutex, int timeout_ms)
|
||||
{
|
||||
if (mutex == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return xSemaphoreTake((SemaphoreHandle_t)mutex, pdMS_TO_TICKS(timeout_ms)) ? 0 : -1;
|
||||
}
|
||||
|
||||
int esp_codec_dev_mutex_unlock(esp_codec_dev_mutex_handle_t mutex)
|
||||
{
|
||||
if (mutex == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return xSemaphoreGive((SemaphoreHandle_t)mutex) ? 0 : -1;
|
||||
}
|
||||
|
||||
void esp_codec_dev_mutex_destroy(esp_codec_dev_mutex_handle_t mutex)
|
||||
{
|
||||
if (mutex == NULL) {
|
||||
return;
|
||||
}
|
||||
vSemaphoreDelete((SemaphoreHandle_t)mutex);
|
||||
}
|
||||
Reference in New Issue
Block a user