add some code

This commit is contained in:
2025-09-05 13:25:11 +08:00
parent 9ff0a99e7a
commit 3cf1229a85
8911 changed files with 2535396 additions and 0 deletions

View File

@@ -0,0 +1,258 @@
/*
* 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 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 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

View File

@@ -0,0 +1,113 @@
/*
* 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_ES8311_SUPPORT
#include "es8311_codec.h"
#endif
#ifdef CONFIG_CODEC_ES7210_SUPPORT
#include "es7210_adc.h"
#endif
#ifdef CONFIG_CODEC_ES7243_SUPPORT
#include "es7243_adc.h"
#endif
#ifdef CONFIG_CODEC_ES7243E_SUPPORT
#include "es7243e_adc.h"
#endif
#ifdef CONFIG_CODEC_ES8156_SUPPORT
#include "es8156_dac.h"
#endif
#ifdef CONFIG_CODEC_AW88298_SUPPORT
#include "aw88298_dac.h"
#endif
#ifdef CONFIG_CODEC_ES8389_SUPPORT
#include "es8389_codec.h"
#endif
#ifdef CONFIG_CODEC_ES8374_SUPPORT
#include "es8374_codec.h"
#endif
#ifdef CONFIG_CODEC_ES8388_SUPPORT
#include "es8388_codec.h"
#endif
#ifdef CONFIG_CODEC_TAS5805M_SUPPORT
#include "tas5805m_dac.h"
#endif
#ifdef CONFIG_CODEC_ZL38063_SUPPORT
#include "zl38063_codec.h"
#endif
#if CONFIG_CODEC_CJC8910_SUPPORT
#include "cjc8910_codec.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* @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) */
} 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 */
} 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

View File

@@ -0,0 +1,23 @@
/*
* 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 Sleep in milliseconds
* @param ms: Sleep time (unit ms)
*/
void esp_codec_dev_sleep(int ms);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,79 @@
/*
* 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.3.1"
/**
* @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_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

View File

@@ -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