From 4f84d0f5e3b53f9475731d174cb8bcf13e037bf6 Mon Sep 17 00:00:00 2001 From: gemu2015 Date: Thu, 7 Jul 2022 13:28:45 +0200 Subject: [PATCH] add sbox codecs --- lib/lib_audio/es7243e/library.json | 7 + lib/lib_audio/es7243e/library.properties | 9 ++ lib/lib_audio/es7243e/src/es7243e.cpp | 98 ++++++++++++ lib/lib_audio/es7243e/src/es7243e.h | 80 ++++++++++ lib/lib_audio/es8156/library.json | 7 + lib/lib_audio/es8156/library.properties | 9 ++ lib/lib_audio/es8156/src/audio_hal.h | 136 ++++++++++++++++ lib/lib_audio/es8156/src/es8156.cpp | 160 +++++++++++++++++++ lib/lib_audio/es8156/src/es8156.h | 180 ++++++++++++++++++++++ lib/lib_audio/es8156/src/esxxx_common.h | 188 +++++++++++++++++++++++ 10 files changed, 874 insertions(+) create mode 100644 lib/lib_audio/es7243e/library.json create mode 100644 lib/lib_audio/es7243e/library.properties create mode 100644 lib/lib_audio/es7243e/src/es7243e.cpp create mode 100644 lib/lib_audio/es7243e/src/es7243e.h create mode 100644 lib/lib_audio/es8156/library.json create mode 100644 lib/lib_audio/es8156/library.properties create mode 100755 lib/lib_audio/es8156/src/audio_hal.h create mode 100644 lib/lib_audio/es8156/src/es8156.cpp create mode 100644 lib/lib_audio/es8156/src/es8156.h create mode 100644 lib/lib_audio/es8156/src/esxxx_common.h diff --git a/lib/lib_audio/es7243e/library.json b/lib/lib_audio/es7243e/library.json new file mode 100644 index 000000000..1ce32c11e --- /dev/null +++ b/lib/lib_audio/es7243e/library.json @@ -0,0 +1,7 @@ +{ + "name": "ES7243e", + "description": "Audio codec", + "keywords": "ESP32, MP3, AAC, WAV, MOD, FLAC, RTTTL, MIDI, I2S, DAC, Delta-Sigma, TTS", + "version": "1.0.0", + "frameworks": "Arduino" +} diff --git a/lib/lib_audio/es7243e/library.properties b/lib/lib_audio/es7243e/library.properties new file mode 100644 index 000000000..61b7ded09 --- /dev/null +++ b/lib/lib_audio/es7243e/library.properties @@ -0,0 +1,9 @@ +name=ES7243e +version=1.0 +author= +maintainer= +sentence=Audio fcodec for ESP32 +paragraph= +category=Signal Output +url= +architectures=esp32 diff --git a/lib/lib_audio/es7243e/src/es7243e.cpp b/lib/lib_audio/es7243e/src/es7243e.cpp new file mode 100644 index 000000000..536c237ec --- /dev/null +++ b/lib/lib_audio/es7243e/src/es7243e.cpp @@ -0,0 +1,98 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2021 + * + * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +#ifdef ESP32 + + #include + #include + #include "string.h" + #include "esp_log.h" +#include "es7243e.h" + + +static const char *TAG = "DRV7243E"; +static TwoWire *es7243e_wire; + +static esp_err_t es7243e_write_reg(uint8_t reg_addr, uint8_t data) +{ + //return i2c_bus_write_byte(i2c_handle, reg_addr, data); + es7243e_wire->beginTransmission(ES7243_ADDR); + es7243e_wire->write(reg_addr); + es7243e_wire->write(data); + es7243e_wire->endTransmission(); + return 0; +} + +esp_err_t es7243e_adc_init(TwoWire *tw, audio_hal_codec_config_t *codec_cfg) +{ + esp_err_t ret = ESP_OK; + + es7243e_wire = tw; + + ret |= es7243e_write_reg(0x01, 0x3A); + ret |= es7243e_write_reg(0x00, 0x80); + ret |= es7243e_write_reg(0xF9, 0x00); + ret |= es7243e_write_reg(0x04, 0x02); + ret |= es7243e_write_reg(0x04, 0x01); + ret |= es7243e_write_reg(0xF9, 0x01); + ret |= es7243e_write_reg(0x00, 0x1E); + ret |= es7243e_write_reg(0x01, 0x00); + + ret |= es7243e_write_reg(0x02, 0x00); + ret |= es7243e_write_reg(0x03, 0x20); + ret |= es7243e_write_reg(0x04, 0x01); + ret |= es7243e_write_reg(0x0D, 0x00); + ret |= es7243e_write_reg(0x05, 0x00); + ret |= es7243e_write_reg(0x06, 0x03); // SCLK=MCLK/4 + ret |= es7243e_write_reg(0x07, 0x00); // LRCK=MCLK/256 + ret |= es7243e_write_reg(0x08, 0xFF); // LRCK=MCLK/256 + + ret |= es7243e_write_reg(0x09, 0xCA); + ret |= es7243e_write_reg(0x0A, 0x85); + ret |= es7243e_write_reg(0x0B, 0x00); + ret |= es7243e_write_reg(0x0E, 0xBF); + ret |= es7243e_write_reg(0x0F, 0x80); + ret |= es7243e_write_reg(0x14, 0x0C); + ret |= es7243e_write_reg(0x15, 0x0C); + ret |= es7243e_write_reg(0x17, 0x02); + ret |= es7243e_write_reg(0x18, 0x26); + ret |= es7243e_write_reg(0x19, 0x77); + ret |= es7243e_write_reg(0x1A, 0xF4); + ret |= es7243e_write_reg(0x1B, 0x66); + ret |= es7243e_write_reg(0x1C, 0x44); + ret |= es7243e_write_reg(0x1E, 0x00); + ret |= es7243e_write_reg(0x1F, 0x0C); + ret |= es7243e_write_reg(0x20, 0x1E); //PGA gain +30dB + ret |= es7243e_write_reg(0x21, 0x1E); //PGA gain +30dB + + ret |= es7243e_write_reg(0x00, 0x80); //Slave Mode + ret |= es7243e_write_reg(0x01, 0x3A); + ret |= es7243e_write_reg(0x16, 0x3F); + ret |= es7243e_write_reg(0x16, 0x00); + if (ret) { + ESP_LOGE(TAG, "Es7243e initialize failed!"); + return ESP_FAIL; + } + return ret; +} +#endif diff --git a/lib/lib_audio/es7243e/src/es7243e.h b/lib/lib_audio/es7243e/src/es7243e.h new file mode 100644 index 000000000..bef4b3ba6 --- /dev/null +++ b/lib/lib_audio/es7243e/src/es7243e.h @@ -0,0 +1,80 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2021 + * + * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef _ES7243E_H_ +#define _ES7243E_H_ + +#include "audio_hal.h" +#include "esp_types.h" +#include "esxxx_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ES7243_ADDR 0x10 + +/** + * @brief Initialize ES7243E adc chip + * + * @param codec_cfg configuration of ES7243E + * + * @return + * - ESP_OK + * - ESP_FAIL + */ +esp_err_t es7243e_adc_init(TwoWire *tw, audio_hal_codec_config_t *codec_cfg); + +/** + * @brief Set gain of given mask + * + * @param[in] mic_mask Mask of MIC channel + * + * @param[in] gain: gain + * + * gain : value + * GAIN_0DB : 1 + * GAIN_3DB : 2 + * GAIN_6DB : 3 + * · + * · + * · + * GAIN_30DB : 10 + * GAIN_33DB : 11 + * GAIN_34_5DB : 12 + * GAIN_36DB : 13 + * GAIN_37_5DB : 14 + * + * @return + * - ESP_OK + * - ESP_FAIL + */ +// esp_err_t es7243_adc_set_gain(es7243_input_mics_t mic_mask, es7243_gain_value_t gain); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/lib_audio/es8156/library.json b/lib/lib_audio/es8156/library.json new file mode 100644 index 000000000..8a00f5423 --- /dev/null +++ b/lib/lib_audio/es8156/library.json @@ -0,0 +1,7 @@ +{ + "name": "ES8156", + "description": "Audio codec", + "keywords": "ESP8266, ESP32, MP3, AAC, WAV, MOD, FLAC, RTTTL, MIDI, I2S, DAC, Delta-Sigma, TTS", + "version": "1.0.0", + "frameworks": "Arduino" +} diff --git a/lib/lib_audio/es8156/library.properties b/lib/lib_audio/es8156/library.properties new file mode 100644 index 000000000..bd6db1442 --- /dev/null +++ b/lib/lib_audio/es8156/library.properties @@ -0,0 +1,9 @@ +name=ES8156 +version=1.0 +author= +maintainer= +sentence=Audio fcodec for ESP8266, ESP32 +paragraph= +category=Signal Output +url= +architectures=* diff --git a/lib/lib_audio/es8156/src/audio_hal.h b/lib/lib_audio/es8156/src/audio_hal.h new file mode 100755 index 000000000..ef43c0f67 --- /dev/null +++ b/lib/lib_audio/es8156/src/audio_hal.h @@ -0,0 +1,136 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2018 + * + * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef _AUDIO_HAL_H_ +#define _AUDIO_HAL_H_ + +#define AUDIO_HAL_VOL_DEFAULT 60 + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Select media hal codec mode + */ +typedef enum { + AUDIO_HAL_CODEC_MODE_ENCODE = 1, /*!< select adc */ + AUDIO_HAL_CODEC_MODE_DECODE, /*!< select dac */ + AUDIO_HAL_CODEC_MODE_BOTH, /*!< select both adc and dac */ + AUDIO_HAL_CODEC_MODE_LINE_IN, /*!< set adc channel */ +} audio_hal_codec_mode_t; + +/** + * @brief Select adc channel for input mic signal + */ +typedef enum { + AUDIO_HAL_ADC_INPUT_LINE1 = 0x00, /*!< mic input to adc channel 1 */ + AUDIO_HAL_ADC_INPUT_LINE2, /*!< mic input to adc channel 2 */ + AUDIO_HAL_ADC_INPUT_ALL, /*!< mic input to both channels of adc */ + AUDIO_HAL_ADC_INPUT_DIFFERENCE, /*!< mic input to adc difference channel */ +} audio_hal_adc_input_t; + +/** + * @brief Select channel for dac output + */ +typedef enum { + AUDIO_HAL_DAC_OUTPUT_LINE1 = 0x00, /*!< dac output signal to channel 1 */ + AUDIO_HAL_DAC_OUTPUT_LINE2, /*!< dac output signal to channel 2 */ + AUDIO_HAL_DAC_OUTPUT_ALL, /*!< dac output signal to both channels */ +} audio_hal_dac_output_t; + +/** + * @brief Select operating mode i.e. start or stop for audio codec chip + */ +typedef enum { + AUDIO_HAL_CTRL_STOP = 0x00, /*!< set stop mode */ + AUDIO_HAL_CTRL_START = 0x01, /*!< set start mode */ +} audio_hal_ctrl_t; + +/** + * @brief Select I2S interface operating mode i.e. master or slave for audio codec chip + */ +typedef enum { + AUDIO_HAL_MODE_SLAVE = 0x00, /*!< set slave mode */ + AUDIO_HAL_MODE_MASTER = 0x01, /*!< set master mode */ +} audio_hal_iface_mode_t; + +/** + * @brief Select I2S interface samples per second + */ +typedef enum { + AUDIO_HAL_08K_SAMPLES, /*!< set to 8k samples per second */ + AUDIO_HAL_11K_SAMPLES, /*!< set to 11.025k samples per second */ + AUDIO_HAL_16K_SAMPLES, /*!< set to 16k samples in per second */ + AUDIO_HAL_22K_SAMPLES, /*!< set to 22.050k samples per second */ + AUDIO_HAL_24K_SAMPLES, /*!< set to 24k samples in per second */ + AUDIO_HAL_32K_SAMPLES, /*!< set to 32k samples in per second */ + AUDIO_HAL_44K_SAMPLES, /*!< set to 44.1k samples per second */ + AUDIO_HAL_48K_SAMPLES, /*!< set to 48k samples per second */ +} audio_hal_iface_samples_t; + +/** + * @brief Select I2S interface number of bits per sample + */ +typedef enum { + AUDIO_HAL_BIT_LENGTH_16BITS = 1, /*!< set 16 bits per sample */ + AUDIO_HAL_BIT_LENGTH_24BITS, /*!< set 24 bits per sample */ + AUDIO_HAL_BIT_LENGTH_32BITS, /*!< set 32 bits per sample */ +} audio_hal_iface_bits_t; + +/** + * @brief Select I2S interface format for audio codec chip + */ +typedef enum { + AUDIO_HAL_I2S_NORMAL = 0, /*!< set normal I2S format */ + AUDIO_HAL_I2S_LEFT, /*!< set all left format */ + AUDIO_HAL_I2S_RIGHT, /*!< set all right format */ + AUDIO_HAL_I2S_DSP, /*!< set dsp/pcm format */ +} audio_hal_iface_format_t; + +/** + * @brief I2s interface configuration for audio codec chip + */ +typedef struct { + audio_hal_iface_mode_t mode; /*!< audio codec chip mode */ + audio_hal_iface_format_t fmt; /*!< I2S interface format */ + audio_hal_iface_samples_t samples; /*!< I2S interface samples per second */ + audio_hal_iface_bits_t bits; /*!< i2s interface number of bits per sample */ +} audio_hal_codec_i2s_iface_t; + +/** + * @brief Configure media hal for initialization of audio codec chip + */ +typedef struct { + audio_hal_adc_input_t adc_input; /*!< set adc channel */ + audio_hal_dac_output_t dac_output; /*!< set dac channel */ + audio_hal_codec_mode_t codec_mode; /*!< select codec mode: adc, dac or both */ + audio_hal_codec_i2s_iface_t i2s_iface; /*!< set I2S interface configuration */ +} audio_hal_codec_config_t; + +#ifdef __cplusplus +} +#endif + +#endif //__AUDIO_HAL_H__ diff --git a/lib/lib_audio/es8156/src/es8156.cpp b/lib/lib_audio/es8156/src/es8156.cpp new file mode 100644 index 000000000..1de1ed716 --- /dev/null +++ b/lib/lib_audio/es8156/src/es8156.cpp @@ -0,0 +1,160 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2021 + * + * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifdef ESP32 + +#include +#include +#include "string.h" +#include "esp_log.h" +#include "es8156.h" + +/* +typedef struct { + int mck_io_num; //!< MCK in out pin + int bck_io_num; //!< BCK in out pin + int ws_io_num; //!< WS in out pin + int data_out_num; //!< DATA out pin + int data_in_num; //!< DATA in pin +} i2s_pin_config_t; +*/ + +static const char *TAG = "es8156"; + +static TwoWire *es8156_wire; + + +static esp_err_t es8156_write_reg(uint8_t reg_addr, uint8_t data) +{ + es8156_wire->beginTransmission(ES8156_ADDR); + es8156_wire->write(reg_addr); + es8156_wire->write(data); + es8156_wire->endTransmission(); + return 0; +} + +static uint8_t es8156_read_reg(uint8_t reg_addr) +{ + uint8_t data; + es8156_wire->beginTransmission(ES8156_ADDR); + es8156_wire->write(reg_addr); + es8156_wire->endTransmission(false); + es8156_wire->requestFrom(ES8156_ADDR, (size_t)1); + data = es8156_wire->read(); + //i2c_bus_read_byte(i2c_handle, reg_addr, &data); + return data; +} + +esp_err_t es8156_codec_init(TwoWire *tw, audio_hal_codec_config_t *cfg) +{ + + es8156_wire = tw; + esp_err_t ret_val = ESP_OK; + //ret_val |= bsp_i2c_add_device(&i2c_handle, ES8156_ADDR); + + uint8_t misc_ctrl_reg_val = 0x00; + uint8_t dac_iface_reg_val = 0x00; + + if (AUDIO_HAL_MODE_MASTER == cfg->i2s_iface.mode) { + misc_ctrl_reg_val |= 0b00100000; + } else { + misc_ctrl_reg_val |= 0b00000000; + } + + switch (cfg->i2s_iface.bits) { + case AUDIO_HAL_BIT_LENGTH_16BITS: + dac_iface_reg_val |= 0b00110000; + break; + case AUDIO_HAL_BIT_LENGTH_24BITS: + dac_iface_reg_val |= 0b00000000; + break; + case AUDIO_HAL_BIT_LENGTH_32BITS: + dac_iface_reg_val |= 0b01000000; + break; + default: /* Use 32 bit as default */ + dac_iface_reg_val |= 0b01000000; + break; + } + + ret_val |= es8156_write_reg(0x09, misc_ctrl_reg_val); // MCLK from pad, Slave mode, power down DLL, enable pin pull up + ret_val |= es8156_write_reg(0x11, dac_iface_reg_val); // DAC Interface Config + ret_val |= es8156_write_reg(0x14, ES8156_VOL_MIN_3dB); // Set default volume to 0dB + + return ret_val; +} + +esp_err_t es8156_codec_deinit(void) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t es8156_codec_set_voice_mute(bool enable) +{ + int regv = es8156_read_reg(ES8156_DAC_MUTE_REG13); + if (enable) { + regv = regv | BIT(1) | BIT(2); + } else { + regv = regv & (~(BIT(1) | BIT(2))) ; + } + es8156_write_reg(ES8156_DAC_MUTE_REG13, regv); + return ESP_OK; +} + +esp_err_t es8156_codec_set_voice_volume(uint8_t volume) +{ + if (volume > 100) { + volume = 100; + } + uint8_t d = 0xBF - 60 + 6 * volume / 10; + if (0 == volume) { + d = 0; + } + return es8156_write_reg(ES8156_VOLUME_CONTROL_REG14, d); +} + +esp_err_t es8156_codec_get_voice_volume(uint8_t *volume) +{ + *volume = es8156_read_reg(ES8156_VOLUME_CONTROL_REG14); + + return ESP_OK; +} + +esp_err_t es8156_config_fmt(es_i2s_fmt_t fmt) +{ + ESP_LOGW(TAG, "Not support config format for es8156 now"); + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t es8156_set_channel(uint8_t is_right) +{ + uint8_t reg = es8156_read_reg(ES8156_DAC_SDP_REG11); + if (is_right) { + reg |= 0b00000100; + } else { + reg &= 0b11111011; + } + return es8156_write_reg(ES8156_DAC_SDP_REG11, reg); +} + +#endif diff --git a/lib/lib_audio/es8156/src/es8156.h b/lib/lib_audio/es8156/src/es8156.h new file mode 100644 index 000000000..6ceba46d2 --- /dev/null +++ b/lib/lib_audio/es8156/src/es8156.h @@ -0,0 +1,180 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2021 + * + * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef _ES8156_H +#define _ES8156_H + +#include "audio_hal.h" +#include "esp_types.h" +#include "esxxx_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +#define ES8156_ADDR (0x08) + +/* ES8156 register space */ +/* +* RESET Control +*/ +#define ES8156_RESET_REG00 0x00 +/* +* Clock Managerment +*/ +#define ES8156_MAINCLOCK_CTL_REG01 0x01 +#define ES8156_SCLK_MODE_REG02 0x02 +#define ES8156_LRCLK_DIV_H_REG03 0x03 +#define ES8156_LRCLK_DIV_L_REG04 0x04 +#define ES8156_SCLK_DIV_REG05 0x05 +#define ES8156_NFS_CONFIG_REG06 0x06 +#define ES8156_MISC_CONTROL1_REG07 0x07 +#define ES8156_CLOCK_ON_OFF_REG08 0x08 +#define ES8156_MISC_CONTROL2_REG09 0x09 +#define ES8156_TIME_CONTROL1_REG0A 0x0a +#define ES8156_TIME_CONTROL2_REG0B 0x0b +/* +* System Control +*/ +#define ES8156_CHIP_STATUS_REG0C 0x0c +#define ES8156_P2S_CONTROL_REG0D 0x0d +#define ES8156_DAC_OSR_COUNTER_REG10 0x10 +/* +* SDP Control +*/ +#define ES8156_DAC_SDP_REG11 0x11 +#define ES8156_AUTOMUTE_SET_REG12 0x12 +#define ES8156_DAC_MUTE_REG13 0x13 +#define ES8156_VOLUME_CONTROL_REG14 0x14 + +/* +* ALC Control +*/ +#define ES8156_ALC_CONFIG1_REG15 0x15 +#define ES8156_ALC_CONFIG2_REG16 0x16 +#define ES8156_ALC_CONFIG3_REG17 0x17 +#define ES8156_MISC_CONTROL3_REG18 0x18 +#define ES8156_EQ_CONTROL1_REG19 0x19 +#define ES8156_EQ_CONTROL2_REG1A 0x1a +/* +* Analog System Control +*/ +#define ES8156_ANALOG_SYS1_REG20 0x20 +#define ES8156_ANALOG_SYS2_REG21 0x21 +#define ES8156_ANALOG_SYS3_REG22 0x22 +#define ES8156_ANALOG_SYS4_REG23 0x23 +#define ES8156_ANALOG_LP_REG24 0x24 +#define ES8156_ANALOG_SYS5_REG25 0x25 +/* +* Chip Information +*/ +#define ES8156_I2C_PAGESEL_REGFC 0xFC +#define ES8156_CHIPID1_REGFD 0xFD +#define ES8156_CHIPID0_REGFE 0xFE +#define ES8156_CHIP_VERSION_REGFF 0xFF + +typedef enum { + ES8156_VOL_MIN = 0x00, /*!< -95.5dB */ + ES8156_VOL_MIN_10dB = 0xAB, /*!< -10dB */ + ES8156_VOL_MIN_9dB = 0xAD, /*!< -9dB */ + ES8156_VOL_MIN_6dB = 0xB3, /*!< -6dB */ + ES8156_VOL_MIN_3dB = 0xB9, /*!< -3dB */ + ES8156_VOL_0dB = 0xBF, /*!< 0dB */ + ES8156_VOL_3dB = 0xC5, /*!< +3dB */ + ES8156_VOL_10dB = 0xD3, /*!< +10dB */ + ES8156_VOL_MAX = 0xFF, /*!< +32dB */ +} es8156_volume_t; + +/* + * @brief Initialize ES8156 codec chip + * + * @param codec_cfg configuration of ES8156 + * + * @return + * - ESP_OK + * - ESP_FAIL + */ +esp_err_t es8156_codec_init(TwoWire *tw, audio_hal_codec_config_t *codec_cfg); + +/** + * @brief Deinitialize ES8156 codec chip + * + * @return + * - ESP_OK + * - ESP_FAIL + */ +esp_err_t es8156_codec_deinit(void); + + +esp_err_t es8156_config_fmt(es_i2s_fmt_t fmt); + +/** + * @brief Configure ES8156 DAC mute or not. Basically you can use this function to mute the output or unmute + * + * @param enable enable(1) or disable(0) + * + * @return + * - ESP_FAIL Parameter error + * - ESP_OK Success + */ +esp_err_t es8156_codec_set_voice_mute(bool enable); + +/** + * @brief Set voice volume + * + * @param volume: voice volume (0~100) + * + * @return + * - ESP_OK + * - ESP_FAIL + */ +esp_err_t es8156_codec_set_voice_volume(uint8_t volume); + +/** + * @brief Get voice volume + * + * @param[out] *volume: voice volume (0~255) + * + * @return + * - ESP_OK + * - ESP_FAIL + */ +esp_err_t es8156_codec_get_voice_volume(uint8_t *volume); + +/** + * @brief + * + * @param is_right + * @return esp_err_t + */ +esp_err_t es8156_set_channel(uint8_t is_right); + + + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/lib/lib_audio/es8156/src/esxxx_common.h b/lib/lib_audio/es8156/src/esxxx_common.h new file mode 100644 index 000000000..34c042ed6 --- /dev/null +++ b/lib/lib_audio/es8156/src/esxxx_common.h @@ -0,0 +1,188 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2019 + * + * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef _ESXXX_COMMON_H_ +#define _ESXXX_COMMON_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BIT_LENGTH_MIN = -1, + BIT_LENGTH_16BITS = 0x03, + BIT_LENGTH_18BITS = 0x02, + BIT_LENGTH_20BITS = 0x01, + BIT_LENGTH_24BITS = 0x00, + BIT_LENGTH_32BITS = 0x04, + BIT_LENGTH_MAX, +} es_bits_length_t; + +typedef enum { + MCLK_DIV_MIN = -1, + MCLK_DIV_1 = 1, + MCLK_DIV_2 = 2, + MCLK_DIV_3 = 3, + MCLK_DIV_4 = 4, + MCLK_DIV_6 = 5, + MCLK_DIV_8 = 6, + MCLK_DIV_9 = 7, + MCLK_DIV_11 = 8, + MCLK_DIV_12 = 9, + MCLK_DIV_16 = 10, + MCLK_DIV_18 = 11, + MCLK_DIV_22 = 12, + MCLK_DIV_24 = 13, + MCLK_DIV_33 = 14, + MCLK_DIV_36 = 15, + MCLK_DIV_44 = 16, + MCLK_DIV_48 = 17, + MCLK_DIV_66 = 18, + MCLK_DIV_72 = 19, + MCLK_DIV_5 = 20, + MCLK_DIV_10 = 21, + MCLK_DIV_15 = 22, + MCLK_DIV_17 = 23, + MCLK_DIV_20 = 24, + MCLK_DIV_25 = 25, + MCLK_DIV_30 = 26, + MCLK_DIV_32 = 27, + MCLK_DIV_34 = 28, + MCLK_DIV_7 = 29, + MCLK_DIV_13 = 30, + MCLK_DIV_14 = 31, + MCLK_DIV_MAX, +} es_sclk_div_t; + +typedef enum { + LCLK_DIV_MIN = -1, + LCLK_DIV_128 = 0, + LCLK_DIV_192 = 1, + LCLK_DIV_256 = 2, + LCLK_DIV_384 = 3, + LCLK_DIV_512 = 4, + LCLK_DIV_576 = 5, + LCLK_DIV_768 = 6, + LCLK_DIV_1024 = 7, + LCLK_DIV_1152 = 8, + LCLK_DIV_1408 = 9, + LCLK_DIV_1536 = 10, + LCLK_DIV_2112 = 11, + LCLK_DIV_2304 = 12, + + LCLK_DIV_125 = 16, + LCLK_DIV_136 = 17, + LCLK_DIV_250 = 18, + LCLK_DIV_272 = 19, + LCLK_DIV_375 = 20, + LCLK_DIV_500 = 21, + LCLK_DIV_544 = 22, + LCLK_DIV_750 = 23, + LCLK_DIV_1000 = 24, + LCLK_DIV_1088 = 25, + LCLK_DIV_1496 = 26, + LCLK_DIV_1500 = 27, + LCLK_DIV_MAX, +} es_lclk_div_t; + +typedef enum { + D2SE_PGA_GAIN_MIN = -1, + D2SE_PGA_GAIN_DIS = 0, + D2SE_PGA_GAIN_EN = 1, + D2SE_PGA_GAIN_MAX = 2, +} es_d2se_pga_t; + +typedef enum { + ADC_INPUT_MIN = -1, + ADC_INPUT_LINPUT1_RINPUT1 = 0x00, + ADC_INPUT_MIC1 = 0x05, + ADC_INPUT_MIC2 = 0x06, + ADC_INPUT_LINPUT2_RINPUT2 = 0x50, + ADC_INPUT_DIFFERENCE = 0xf0, + ADC_INPUT_MAX, +} es_adc_input_t; + +typedef enum { + DAC_OUTPUT_MIN = -1, + DAC_OUTPUT_LOUT1 = 0x04, + DAC_OUTPUT_LOUT2 = 0x08, + DAC_OUTPUT_SPK = 0x09, + DAC_OUTPUT_ROUT1 = 0x10, + DAC_OUTPUT_ROUT2 = 0x20, + DAC_OUTPUT_ALL = 0x3c, + DAC_OUTPUT_MAX, +} es_dac_output_t; + +typedef enum { + MIC_GAIN_MIN = -1, + MIC_GAIN_0DB = 0, + MIC_GAIN_3DB = 3, + MIC_GAIN_6DB = 6, + MIC_GAIN_9DB = 9, + MIC_GAIN_12DB = 12, + MIC_GAIN_15DB = 15, + MIC_GAIN_18DB = 18, + MIC_GAIN_21DB = 21, + MIC_GAIN_24DB = 24, + MIC_GAIN_MAX, +} es_mic_gain_t; + +typedef enum { + ES_MODULE_MIN = -1, + ES_MODULE_ADC = 0x01, + ES_MODULE_DAC = 0x02, + ES_MODULE_ADC_DAC = 0x03, + ES_MODULE_LINE = 0x04, + ES_MODULE_MAX +} es_module_t; + +typedef enum { + ES_MODE_MIN = -1, + ES_MODE_SLAVE = 0x00, + ES_MODE_MASTER = 0x01, + ES_MODE_MAX, +} es_mode_t; + +typedef enum { + ES_I2S_MIN = -1, + ES_I2S_NORMAL = 0, + ES_I2S_LEFT = 1, + ES_I2S_RIGHT = 2, + ES_I2S_DSP = 3, + ES_I2S_MAX +} es_i2s_fmt_t; + +/** + * @brief Configure ES8388 clock + */ +typedef struct { + es_sclk_div_t sclk_div; /*!< bits clock divide */ + es_lclk_div_t lclk_div; /*!< WS clock divide */ +} es_i2s_clock_t; + +#ifdef __cplusplus +} +#endif + +#endif