Creataing the Hodor project.

This commit is contained in:
2026-10-01 13:59:27 +03:30
commit 58715ec527
1850 changed files with 342657 additions and 0 deletions
@@ -0,0 +1,75 @@
/*
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _ESP_CODEC_ADC_H_
#define _ESP_CODEC_ADC_H_
#ifdef CONFIG_CODEC_DATA_ADC_SUPPORT
#include "esp_idf_version.h"
#include "soc/soc_caps.h"
#include "audio_codec_data_if.h"
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_ADC_SUPPORTED
#include "hal/adc_types.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* @brief Codec ADC continuous configuration mode
*/
typedef enum {
AUDIO_CODEC_ADC_CFG_MODE_SINGLE_UNIT = 0, /*!< Single unit + channel list */
AUDIO_CODEC_ADC_CFG_MODE_PATTERN = 1, /*!< Full adc_digi_pattern_config_t list */
} audio_codec_adc_cfg_mode_t;
/**
* @brief Codec ADC continuous configuration
*/
typedef struct {
size_t max_store_buf_size; /*!< ADC conversion pool size in bytes */
size_t conv_frame_size; /*!< ADC conversion frame size in bytes */
uint32_t sample_freq_hz; /*!< ADC sample frequency in Hz */
adc_digi_convert_mode_t conv_mode; /*!< ADC conversion mode */
adc_digi_output_format_t format; /*!< ADC conversion output format */
uint32_t pattern_num; /*!< Number of valid pattern entries */
audio_codec_adc_cfg_mode_t cfg_mode; /*!< ADC continuous config mode */
union {
struct {
adc_unit_t unit_id; /*!< Single-unit ADC unit */
adc_atten_t atten; /*!< Single-unit attenuation */
adc_bitwidth_t bit_width; /*!< Single-unit bit width */
uint8_t channel_id[SOC_ADC_PATT_LEN_MAX]; /*!< Single-unit channel list */
} single_unit;
adc_digi_pattern_config_t patterns[SOC_ADC_PATT_LEN_MAX]; /*!< Full pattern list */
} cfg;
} audio_codec_adc_continuous_cfg_t;
/**
* @brief Codec ADC configuration
*/
typedef struct {
void *handle; /*!< Reuse external adc_continuous_handle_t directly if not NULL, otherwise create local handle */
audio_codec_adc_continuous_cfg_t continuous_cfg; /*!< ADC continuous configuration */
} audio_codec_adc_cfg_t;
/**
* @brief Get default ADC data interface
*
* @return
* - NULL Failed
* - Others ADC data interface
*/
const audio_codec_data_if_t *audio_codec_new_adc_data(audio_codec_adc_cfg_t *adc_cfg);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_ADC_SUPPORTED */
#endif /* CONFIG_CODEC_DATA_ADC_SUPPORT */
#endif /* _ESP_CODEC_ADC_H_ */
@@ -0,0 +1,315 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _ESP_CODEC_DEV_H_
#define _ESP_CODEC_DEV_H_
#include "audio_codec_if.h"
#include "audio_codec_data_if.h"
#include "esp_codec_dev_types.h"
#include "esp_codec_dev_vol.h"
#include "audio_codec_vol_if.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Codec device configuration
*/
typedef struct {
esp_codec_dev_type_t dev_type; /*!< Codec device type */
const audio_codec_if_t *codec_if; /*!< Codec interface */
const audio_codec_data_if_t *data_if; /*!< Codec data interface */
} esp_codec_dev_cfg_t;
/**
* @brief Codec device handle
*/
typedef void *esp_codec_dev_handle_t;
/**
* @brief Get `esp_codec_dev` version string
* @return Version information
*/
const char *esp_codec_dev_get_version(void);
/**
* @brief New codec device
* @param codec_dev_cfg: Codec device configuration
* @return NULL: Fail to new codec device
* -Others: Codec device handle
*/
esp_codec_dev_handle_t esp_codec_dev_new(esp_codec_dev_cfg_t *codec_dev_cfg);
/**
* @brief Open codec device
* @param codec: Codec device handle
* @param fs: Audio sample information
* @return ESP_CODEC_DEV_OK: Open success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support or driver not ready yet
*/
int esp_codec_dev_open(esp_codec_dev_handle_t codec, esp_codec_dev_sample_info_t *fs);
/**
* @brief Read register value from codec
* @param codec: Codec device handle
* @param reg: Register address to be read
* @param val: Value to be read
* @return ESP_CODEC_DEV_OK: Read success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
*/
int esp_codec_dev_read_reg(esp_codec_dev_handle_t codec, int reg, int *val);
/**
* @brief Write register value to codec
* @param codec: Codec device handle
* @param reg: Register address to be wrote
* @param val: Value to be wrote
* @return ESP_CODEC_DEV_OK: Read success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
*/
int esp_codec_dev_write_reg(esp_codec_dev_handle_t codec, int reg, int val);
/**
* @brief Dump registers from codec device
* @param codec: Codec device handle
* @return ESP_CODEC_DEV_OK: Read success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Not supported
*/
int esp_codec_dev_dump_reg(esp_codec_dev_handle_t codec);
/**
* @brief Read data from codec
* @param codec: Codec device handle
* @param data: Data to be read
* @param len: Data length to be read
* @return ESP_CODEC_DEV_OK: Read success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_read(esp_codec_dev_handle_t codec, void *data, int len);
/**
* @brief Configure input mirror for codec input data
*
* Input mirror copies data returned by esp_codec_dev_read() into an internal
* ring buffer. It is single consumer only, not ISR-safe, and older data may
* be overwritten if it is not read in time.
*
* @note If input mirror has already been configured, this API returns
* ESP_CODEC_DEV_OK without resizing or recreating the buffer.
* esp_codec_dev_close() automatically frees mirror resources.
*
* @param codec: Codec device handle
* @param size: Input mirror ringbuffer capacity in bytes
* @return ESP_CODEC_DEV_OK: Configure input mirror success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support input mode
* ESP_CODEC_DEV_NO_MEM: Not enough memory for input mirror
*/
int esp_codec_dev_mirror_cfg(esp_codec_dev_handle_t codec, int size);
/**
* @brief Read data from input mirror
*
* This API blocks until the requested size is filled or the timeout
* expires. It may allocate no user-visible resources and is single
* consumer only.
*
* @note Do not call esp_codec_dev_close() or esp_codec_dev_delete() while
* this API is blocking. Stop the mirror read task before closing
* or deleting the codec device.
*
* @param codec: Codec device handle
* @param buffer: Data buffer to fill
* @param size: Data length to read
* @param timeout_ms: Timeout in milliseconds, 0 no wait, negative wait forever
* @param[out] bytes_read: Actual bytes copied to buffer
* @return ESP_CODEC_DEV_OK: Read requested data success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support input mode
* ESP_CODEC_DEV_TIMEOUT: Timeout before requested data is filled
* ESP_CODEC_DEV_WRONG_STATE: Input mirror is not enabled
*/
int esp_codec_dev_mirror_read(esp_codec_dev_handle_t codec,
uint8_t *buffer,
int size,
int timeout_ms,
int *bytes_read);
/**
* @brief Write data to codec
* Notes: when enable software volume, it will change input data level directly without copy
* Make sure that input data is writable
* @param codec: Codec device handle
* @param data: Data to be wrote
* @param len: Data length to be wrote
* @return ESP_CODEC_DEV_OK: Write success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_write(esp_codec_dev_handle_t codec, void *data, int len);
/**
* @brief Set codec hardware gain
* @param codec: Codec device handle
* @param volume: Volume setting
* @return ESP_CODEC_DEV_OK: Set output volume success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support output mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_set_out_vol(esp_codec_dev_handle_t codec, int volume);
/**
* @brief Set codec software volume handler
* Notes: it is not needed when codec support volume adjust in hardware
* If not provided, it will use internally software volume process handler instead
* @param codec: Codec device handle
* @param vol_handler: Software volume process interface
* @return ESP_CODEC_DEV_OK: Set volume handler success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support output mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_set_vol_handler(esp_codec_dev_handle_t codec, const audio_codec_vol_if_t* vol_handler);
/**
* @brief Set codec volume curve
* Notes: When volume curve not provided, it will use internally volume curve which is:
* 1 - "-49.5dB", 100 - "0dB"
* Need to call this API if you want to customize volume curve
* @param codec: Codec device handle
* @param curve: Volume curve setting
* @return ESP_CODEC_DEV_OK: Set curve success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support output mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
* ESP_CODEC_DEV_NO_MEM: Not enough memory to hold volume curve
*/
int esp_codec_dev_set_vol_curve(esp_codec_dev_handle_t codec, esp_codec_dev_vol_curve_t *curve);
/**
* @brief Get codec output volume
* @param codec: Codec device handle
* @param[out] volume: Volume to get
* @return ESP_CODEC_DEV_OK: Get volume success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support output mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_get_out_vol(esp_codec_dev_handle_t codec, int *volume);
/**
* @brief Set codec output mute
* @param codec: Codec device handle
* @param mute: Whether mute output or not
* @return ESP_CODEC_DEV_OK: Set output mute success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support output mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_set_out_mute(esp_codec_dev_handle_t codec, bool mute);
/**
* @brief Get codec output mute setting
* @param codec: Codec device handle
* @param[out] muted: Mute status to get
* @return ESP_CODEC_DEV_OK: Get output mute success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support output mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_get_out_mute(esp_codec_dev_handle_t codec, bool *muted);
/**
* @brief Set codec input gain
* @param codec: Codec device handle
* @param db_value: Input gain setting
* @return ESP_CODEC_DEV_OK: Set input gain success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support input mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_set_in_gain(esp_codec_dev_handle_t codec, float db_value);
/**
* @brief Set codec input gain by channel
* @param codec: Codec device handle
* @param channel_mask: Mask for channel to be set
* @param db_value: Input gain setting
* @return ESP_CODEC_DEV_OK: Set input gain success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support input mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_set_in_channel_gain(esp_codec_dev_handle_t codec, uint16_t channel_mask, float db_value);
/**
* @brief Get codec input gain
* @param codec: Codec device handle
* @param db_value: Input gain to get
* @return ESP_CODEC_DEV_OK: Get input gain success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support input mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_get_in_gain(esp_codec_dev_handle_t codec, float *db_value);
/**
* @brief Set codec input mute
* @param codec: Codec device handle
* @param mute: Whether mute code input or not
* @return ESP_CODEC_DEV_OK: Set input mute success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support input mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_set_in_mute(esp_codec_dev_handle_t codec, bool mute);
/**
* @brief Get codec input mute
* @param codec: Codec device handle
* @param muted: Mute value to get
* @return ESP_CODEC_DEV_OK: Set input mute success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
* ESP_CODEC_DEV_NOT_SUPPORT: Codec not support input mode
* ESP_CODEC_DEV_WRONG_STATE: Driver not open yet
*/
int esp_codec_dev_get_in_mute(esp_codec_dev_handle_t codec, bool *muted);
/**
* @brief Whether disable codec when closed
* @param codec: Codec device handle
* @param disable: Disable when closed (default is true)
* @return ESP_CODEC_DEV_OK: Setting success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
*/
int esp_codec_set_disable_when_closed(esp_codec_dev_handle_t codec, bool disable);
/**
* @brief Close codec device
* @param codec: Codec device handle
* @return ESP_CODEC_DEV_OK: Close success
* ESP_CODEC_DEV_INVALID_ARG: Invalid arguments
*/
int esp_codec_dev_close(esp_codec_dev_handle_t codec);
/**
* @brief Delete the specified codec device instance
* @param codec: Codec device handle
*/
void esp_codec_dev_delete(esp_codec_dev_handle_t codec);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,122 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _ESP_CODEC_DEV_DEFAULTS_H_
#define _ESP_CODEC_DEV_DEFAULTS_H_
#include "audio_codec_if.h"
#include "audio_codec_ctrl_if.h"
#include "audio_codec_data_if.h"
#include "audio_codec_gpio_if.h"
#ifdef CONFIG_CODEC_DATA_ADC_SUPPORT
#include "esp_codec_adc.h"
#endif /* CONFIG_CODEC_DATA_ADC_SUPPORT */
#ifdef CONFIG_CODEC_ES8311_SUPPORT
#include "es8311_codec.h"
#endif /* CONFIG_CODEC_ES8311_SUPPORT */
#ifdef CONFIG_CODEC_ES7210_SUPPORT
#include "es7210_adc.h"
#endif /* CONFIG_CODEC_ES7210_SUPPORT */
#ifdef CONFIG_CODEC_ES7243_SUPPORT
#include "es7243_adc.h"
#endif /* CONFIG_CODEC_ES7243_SUPPORT */
#ifdef CONFIG_CODEC_ES7243E_SUPPORT
#include "es7243e_adc.h"
#endif /* CONFIG_CODEC_ES7243E_SUPPORT */
#ifdef CONFIG_CODEC_ES8156_SUPPORT
#include "es8156_dac.h"
#endif /* CONFIG_CODEC_ES8156_SUPPORT */
#ifdef CONFIG_CODEC_AW88298_SUPPORT
#include "aw88298_dac.h"
#endif /* CONFIG_CODEC_AW88298_SUPPORT */
#ifdef CONFIG_CODEC_ES8389_SUPPORT
#include "es8389_codec.h"
#endif /* CONFIG_CODEC_ES8389_SUPPORT */
#ifdef CONFIG_CODEC_ES8374_SUPPORT
#include "es8374_codec.h"
#endif /* CONFIG_CODEC_ES8374_SUPPORT */
#ifdef CONFIG_CODEC_ES8388_SUPPORT
#include "es8388_codec.h"
#endif /* CONFIG_CODEC_ES8388_SUPPORT */
#ifdef CONFIG_CODEC_TAS5805M_SUPPORT
#include "tas5805m_dac.h"
#endif /* CONFIG_CODEC_TAS5805M_SUPPORT */
#ifdef CONFIG_CODEC_ZL38063_SUPPORT
#include "zl38063_codec.h"
#endif /* CONFIG_CODEC_ZL38063_SUPPORT */
#if CONFIG_CODEC_CJC8910_SUPPORT
#include "cjc8910_codec.h"
#endif /* CONFIG_CODEC_CJC8910_SUPPORT */
#if CONFIG_CODEC_DUMMY_SUPPORT
#include "dummy_codec.h"
#endif /* CONFIG_CODEC_DUMMY_SUPPORT */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* @brief Codec I2C configuration
*/
typedef struct {
uint8_t port; /*!< I2C port, this port need pre-installed by other modules */
uint8_t addr; /*!< I2C address, default address can be gotten from codec head files */
void *bus_handle; /*!< I2C Master bus handle (for IDFv5.3 or higher version) */
int clock_speed_hz; /*!< I2C clock in Hz for default control interface, use 100kHz if set to 0 */
} audio_codec_i2c_cfg_t;
/**
* @brief Codec I2S configuration
*/
typedef struct {
uint8_t port; /*!< I2S port, this port need pre-installed by other modules */
void *rx_handle; /*!< I2S rx handle, need provide on IDF 5.x */
void *tx_handle; /*!< I2S tx handle, need provide on IDF 5.x */
int clk_src; /*!< I2S clock source, need converted from `i2s_clock_src_t`. If set to 0 will use default clock source */
} audio_codec_i2s_cfg_t;
/**
* @brief Codec SPI configuration
*/
typedef struct {
uint8_t spi_port; /*!< SPI port, this port need pre-installed by other modules */
int16_t cs_pin; /*!< SPI CS GPIO pin setting */
int clock_speed; /*!< SPI clock unit hz (use 10MHZif set to 0)*/
} audio_codec_spi_cfg_t;
/**
* @brief Get default codec GPIO interface
* @return NULL: Failed
* Others: Codec GPIO interface
*/
const audio_codec_gpio_if_t *audio_codec_new_gpio(void);
/**
* @brief Get default SPI control interface
* @return NULL: Failed
* Others: SPI control interface
*/
const audio_codec_ctrl_if_t *audio_codec_new_spi_ctrl(audio_codec_spi_cfg_t *spi_cfg);
/**
* @brief Get default I2C control interface
* @return NULL: Failed
* Others: I2C control interface
*/
const audio_codec_ctrl_if_t *audio_codec_new_i2c_ctrl(audio_codec_i2c_cfg_t *i2c_cfg);
/**
* @brief Get default I2S data interface
* @return NULL: Failed
* Others: I2S data interface
*/
const audio_codec_data_if_t *audio_codec_new_i2s_data(audio_codec_i2s_cfg_t *i2s_cfg);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,68 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _ESP_CODEC_DEV_OS_H_
#define _ESP_CODEC_DEV_OS_H_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Codec device mutex handle
*/
typedef void *esp_codec_dev_mutex_handle_t;
/**
* @brief Create mutex for codec device
*
* @return
* - NULL No available resource
* - Others Mutex handle
*/
esp_codec_dev_mutex_handle_t esp_codec_dev_mutex_create(void);
/**
* @brief Lock mutex for codec device
*
* @param[in] mutex Mutex handle
* @param[in] timeout_ms Wait timeout for mutex (unit ms)
*
* @return
* - 0 On success
* - Others Failed to lock
*/
int esp_codec_dev_mutex_lock(esp_codec_dev_mutex_handle_t mutex, int timeout_ms);
/**
* @brief Unlock mutex for codec device
*
* @param[in] mutex Mutex handle
*
* @return
* - 0 On success
* - Others Failed to unlock
*/
int esp_codec_dev_mutex_unlock(esp_codec_dev_mutex_handle_t mutex);
/**
* @brief Destroy mutex for codec device
*
* @param[in] mutex Mutex handle
*/
void esp_codec_dev_mutex_destroy(esp_codec_dev_mutex_handle_t mutex);
/**
* @brief Sleep in milliseconds
*
* @param[in] ms Sleep time (unit ms)
*/
void esp_codec_dev_sleep(int ms);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,80 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _ESP_CODEC_DEV_TYPES_H_
#define _ESP_CODEC_DEV_TYPES_H_
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_CODEC_DEV_VERSION "1.6.0"
/**
* @brief Define error number of codec device module
* Inherit from `esp_err_t`
*/
#define ESP_CODEC_DEV_OK (0)
#define ESP_CODEC_DEV_DRV_ERR (ESP_FAIL)
#define ESP_CODEC_DEV_INVALID_ARG (ESP_ERR_INVALID_ARG)
#define ESP_CODEC_DEV_NO_MEM (ESP_ERR_NO_MEM)
#define ESP_CODEC_DEV_NOT_SUPPORT (ESP_ERR_NOT_SUPPORTED)
#define ESP_CODEC_DEV_NOT_FOUND (ESP_ERR_NOT_FOUND)
#define ESP_CODEC_DEV_WRONG_STATE (ESP_ERR_INVALID_STATE)
#define ESP_CODEC_DEV_TIMEOUT (ESP_ERR_TIMEOUT)
#define ESP_CODEC_DEV_WRITE_FAIL (0x10D)
#define ESP_CODEC_DEV_READ_FAIL (0x10E)
#define ESP_CODEC_DEV_MAKE_CHANNEL_MASK(channel) ((uint16_t)1 << (channel))
/**
* @brief Codec Device type
*/
typedef enum {
ESP_CODEC_DEV_TYPE_NONE,
ESP_CODEC_DEV_TYPE_IN = (1 << 0), /*!< Codec input device like ADC (capture data from microphone) */
ESP_CODEC_DEV_TYPE_OUT = (1 << 1), /*!< Codec output device like DAC (output analog signal to speaker) */
ESP_CODEC_DEV_TYPE_IN_OUT = (ESP_CODEC_DEV_TYPE_IN | ESP_CODEC_DEV_TYPE_OUT), /*!< Codec input and output device */
} esp_codec_dev_type_t;
/**
* @brief Codec audio sample information
* Notes: channel_mask is used to filter wanted channels in driver side
* when set to 0, default filter all channels
* when channel is 2, can filter channel 0 (set to 1) or channel 1 (set to 2)
* when channel is 4, can filter either 3,2 channels or 1 channel
*/
typedef struct {
uint8_t bits_per_sample; /*!< Bit lengths of one channel data */
uint8_t channel; /*!< Channels of sample */
uint16_t channel_mask; /*!< Channel mask indicate which channel to be selected */
uint32_t sample_rate; /*!< Sample rate of sample */
int mclk_multiple; /*!< The multiple of MCLK to the sample rate
If value is 0, mclk = sample_rate * 256
If bits_per_sample is 24bit, mclk_multiple should be the multiple of 3
*/
} esp_codec_dev_sample_info_t;
/**
* @brief Codec working mode
*/
typedef enum {
ESP_CODEC_DEV_WORK_MODE_NONE,
ESP_CODEC_DEV_WORK_MODE_ADC = (1 << 0), /*!< Enable ADC, only support input */
ESP_CODEC_DEV_WORK_MODE_DAC = (1 << 1), /*!< Enable DAC, only support output */
ESP_CODEC_DEV_WORK_MODE_BOTH =
(ESP_CODEC_DEV_WORK_MODE_ADC | ESP_CODEC_DEV_WORK_MODE_DAC), /*!< Support both DAC and ADC */
ESP_CODEC_DEV_WORK_MODE_LINE = (1 << 2), /*!< Line mode */
} esp_codec_dec_work_mode_t;
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,106 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _ESP_CODEC_DEV_VOL_H_
#define _ESP_CODEC_DEV_VOL_H_
#include "esp_codec_dev_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Codec volume map to decibel
*/
typedef struct {
int vol; /*!< Volume value */
float db_value; /*!< Volume decibel value */
} esp_codec_dev_vol_map_t;
/**
* @brief Codec volume range setting
*/
typedef struct {
esp_codec_dev_vol_map_t min_vol; /*!< Minimum volume setting */
esp_codec_dev_vol_map_t max_vol; /*!< Maximum volume setting */
} esp_codec_dev_vol_range_t;
/**
* @brief Codec volume curve configuration
*/
typedef struct {
esp_codec_dev_vol_map_t *vol_map; /*!< Point of volume curve */
int count; /*!< Curve point number */
} esp_codec_dev_vol_curve_t;
/*
* Audio gain overview:
* |----------------Software Gain--------------|--Hardware Gain--|
*
* |--------------------| |--------------------| |------------------| |---------| |----------------|
* | Digital Audio Data |-->| Audio Process Gain |-->| Codec DAC Volume |-->| PA Gain |-->| Speaker Output |
* |--------------------| |--------------------| |------------------| |---------| |----------------|
*
* Final speaker loudness is affected by both Software Gain and Hardware Gain.
*
* Software Gain (Adjustable):
* Audio Process Gain: Gain by audio post processor, such as ALC, AGC, DRC target MAX Gain.
* Codec DAC Volume: The audio codec DAC volume control, such as ES8311 DAC_Volume control register.
*
* Hardware Gain (Fixed):
* PA Gain: The speaker power amplifier Gain, which is determined by the hardware circuit.
*
* The speaker playback route gain (Audio Process Gain + Codec DAC Volume + PA Gain) needs to ensure that the
* speaker PA output is not saturated and exceeds the speaker rated power. We define the maximum route gain
* as MAX_GAIN. To ensure the speaker PA output is not saturated, MAX_GAIN can be calculated simply by the formula.
* MAX_GAIN = 20 * log(Vpa/Vdac)
* Vpa: PA power supply
* Vdac: Codec DAC power supply
* e.g., Vpa = 5V, Vdac = 3.3V, then MAX_GAIN = 20 * log(5/3.3) = 3.6 dB.
* If the speaker rated power is lower than the speaker PA MAX power, MAX_GAIN should be defined according to
* the speaker rated power.
*/
/**
* @brief Codec hardware gain setting
* Notes: Hardware gain generally consists of 2 parts
* 1. Codec DAC voltage and PA voltage to get MAX_GAIN
* 2. PA gain can be calculate by connected resistors
*/
typedef struct {
float pa_voltage; /*!< PA voltage: typical 5.0v */
float codec_dac_voltage; /*!< Codec chip DAC voltage: typical 3.3v */
float pa_gain; /*!< PA amplify coefficient in decibel unit */
} esp_codec_dev_hw_gain_t;
/**
* @brief Convert decibel value to register settings
* @param vol_range: Volume range
* @param db: Volume decibel
* @return Codec register value
*/
int esp_codec_dev_vol_calc_reg(const esp_codec_dev_vol_range_t *vol_range, float db);
/**
* @brief Convert codec register setting to decibel value
* @param vol_range: Volume range
* @param vol: Volume register setting
* @return Codec volume in decibel unit
*/
float esp_codec_dev_vol_calc_db(const esp_codec_dev_vol_range_t *vol_range, int vol);
/**
* @brief Calculate codec hardware gain value
* @param hw_gain: Hardware gain settings
* @return Codec hardware gain in decibel unit
*/
float esp_codec_dev_col_calc_hw_gain(esp_codec_dev_hw_gain_t* hw_gain);
#ifdef __cplusplus
}
#endif
#endif