Merge pull request #16178 from gemu2015/i2supdate

I2supdate
This commit is contained in:
Theo Arends 2022-08-09 09:20:01 +02:00 committed by GitHub
commit 0b8489d342
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
56 changed files with 625 additions and 45 deletions

View File

@ -45,7 +45,7 @@ static es7210_gain_value_t gain;
/* /*
* Clock coefficient structer * Clock coefficient structer
*/ */
struct _coeff_div { struct _coeff_div_es7210 {
uint32_t mclk; /* mclk frequency */ uint32_t mclk; /* mclk frequency */
uint32_t lrck; /* lrck */ uint32_t lrck; /* lrck */
uint8_t ss_ds; uint8_t ss_ds;
@ -75,7 +75,7 @@ static es7210_input_mics_t mic_select = (es7210_input_mics_t)(ES7210_INPUT_MIC1
* lrckh: 0x04 * lrckh: 0x04
* lrckl: 0x05 * lrckl: 0x05
*/ */
static const struct _coeff_div coeff_div[] = { static const struct _coeff_div_es7210 coeff_div[] = {
//mclk lrck ss_ds adc_div dll doubler osr mclk_src lrckh lrckl //mclk lrck ss_ds adc_div dll doubler osr mclk_src lrckh lrckl
/* 8k */ /* 8k */
{12288000, 8000 , 0x00, 0x03, 0x01, 0x00, 0x20, 0x00, 0x06, 0x00}, {12288000, 8000 , 0x00, 0x03, 0x01, 0x00, 0x20, 0x00, 0x06, 0x00},

View File

@ -95,4 +95,11 @@ esp_err_t es7243e_adc_init(TwoWire *tw, audio_hal_codec_config_t *codec_cfg)
} }
return ret; return ret;
} }
void es7243e_setgain(uint8_t gain) {
uint8_t gaintab[8] = {0x10, 0x12, 0x20, 0x22, 0x04, 0x40, 0x06, 0x42};
es7243e_write_reg(0x08, gaintab[gain & 7] | 0x09);
}
#endif #endif

View File

@ -72,6 +72,7 @@ esp_err_t es7243e_adc_init(TwoWire *tw, audio_hal_codec_config_t *codec_cfg);
*/ */
// esp_err_t es7243_adc_set_gain(es7243_input_mics_t mic_mask, es7243_gain_value_t gain); // esp_err_t es7243_adc_set_gain(es7243_input_mics_t mic_mask, es7243_gain_value_t gain);
void es7243e_setgain(uint8_t gain);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,7 +1,8 @@
{ {
"name": "mp3_shine_esp32", "name":"mp3_shine_esp32",
"version": "1.0.0",
"description": "mp3 encoder", "description": "mp3 encoder",
"keywords": "ESP32, MP3", "keywords": "ESP32, MP3",
"version": "1.0.0", "frameworks": "Arduino",
"frameworks": "Arduino" "platforms": "espressif32"
} }

View File

@ -9,7 +9,6 @@
#include "types.h" #include "types.h"
#include "bitstream.h" #include "bitstream.h"
#if !defined(__APPLE__) && !defined(__FreeBSD__) #if !defined(__APPLE__) && !defined(__FreeBSD__)
#include <malloc.h> #include <malloc.h>
#endif #endif

View File

@ -0,0 +1,7 @@
{
"name": "WM8960",
"description": "Audio codec",
"keywords": "ESP8266, ESP32, MP3, AAC, WAV, MOD, FLAC, RTTTL, MIDI, I2S, DAC, Delta-Sigma, TTS",
"version": "1.0.0",
"frameworks": "Arduino"
}

View File

@ -0,0 +1,9 @@
name=WM8960
version=1.0
author=
maintainer=
sentence=Audio codec for ESP32
paragraph=
category=Signal Output
url=
architectures=esp32

View File

@ -0,0 +1,114 @@
#ifdef ESP32
#include <Wire.h>
#include <string.h>
#include "esp_log.h"
#include "wm8960.h"
static TwoWire *ws8960wire;
void W8960_Write(uint8_t reg_addr, uint16_t data) {
reg_addr <<= 1;
reg_addr |= ((data >> 8) & 1);
data &= 0xff;
ws8960wire->beginTransmission(W8960_ADDR);
ws8960wire->write(reg_addr);
ws8960wire->write(data);
ws8960wire->endTransmission();
}
void W8960_Init(TwoWire *tw) {
ws8960wire = tw;
// reset
W8960_Write(0x0f, 0x0000);
delay(10);
// enable dac and adc
W8960_Write(0x19, (1<<8)|(1<<7)|(1<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1));
// left speaker not used
W8960_Write(0x1A, (1<<8)|(1<<7)|(1<<6)|(1<<5)|(1<<4)|(1<<3));
W8960_Write(0x2F, (1<<5)|(1<<4)|(1<<3)|(1<<2));
// Configure clock
W8960_Write(0x04, 0x0000);
// Configure ADC/DAC
W8960_Write(0x05, 0x0000);
// Configure audio interface
// I2S format 16 bits word length
//W8960_Write(0x07 0x0002)
W8960_Write(0x07, 0x0022);
// Configure HP_L and HP_R OUTPUTS
W8960_Write(0x02, 0x017f);
W8960_Write(0x03, 0x017f);
// Configure SPK_RP and SPK_RN
W8960_Write(0x28, 0x0177);
W8960_Write(0x29, 0x0177);
// Enable the OUTPUTS, only right speaker is wired
W8960_Write(0x31, 0x0080);
// Configure DAC volume
W8960_Write(0x0a, 0x01FF);
W8960_Write(0x0b, 0x01FF);
// Configure MIXER
W8960_Write(0x22, (1<<8)|(1<<7));
W8960_Write(0x25, (1<<8)|(1<<7));
// Jack Detect
//W8960_Write(0x18, (1<<6)|(0<<5));
//W8960_Write(0x17, 0x01C3);
//W8960_Write(0x30, 0x0009);
// input volume
W8960_Write(0x00, 0x0127);
W8960_Write(0x01, 0x0127);
// set ADC Volume
W8960_Write(0x15, 0x01c3);
W8960_Write(0x16, 0x01c3);
// disable bypass switch
W8960_Write(0x2d, 0x0000);
W8960_Write(0x2e, 0x0000);
// connect LINPUT1 to PGA and set PGA Boost Gain.
W8960_Write(0x20, 0x0020|(1<<8)|(1<<3));
W8960_Write(0x21, 0x0020|(1<<8)|(1<<3));
}
void W8960_SetGain(uint8_t sel, uint16_t value) {
switch (sel) {
case 0:
// output dac in 0.5 db steps
value &= 0x00ff;
value |= 0x0100;
W8960_Write(0x0a, value);
W8960_Write(0x0b, value);
break;
case 1:
// input pga in 0.75 db steps
value &= 0x001f;
value |= 0x0120;
W8960_Write(0x00, value);
W8960_Write(0x01, value);
break;
case 2:
// adc in 0.5 db steps
value &= 0x00ff;
value |= 0x0100;
W8960_Write(0x15, value);
W8960_Write(0x16, value);
break;
}
}
#endif // ESP32

View File

@ -0,0 +1,372 @@
#ifndef _WM8960_H_
#define _WM8960_H_
#define W8960_ADDR 0x1a
//static const char *TAG = "WM8960";
#define SDA_PIN 21
#define SCL_PIN 22
#define I2C_BUS_NO 0
#define ACK_CHECK_EN 0
#define ACK_CHECK_DIS 0
#define ACK_VAL 0
#define NACK_VAL 1
#define R0_LEFT_INPUT_VOLUME_ADR 0x00
#define R1_RIGHT_INPUT_VOLUME_ADR 0x01
#define R2_LOUT1_VOLUME_ADR 0x02
#define R3_ROUT1_VOLUME_ADR 0x03
#define R4_CLOCKING_1_ADR 0x04
#define R5_ADC_DAC_CONTROL_CTR1_ADR 0x05
#define R6_ADC_DAC_CONTROL_CTR2_ADR 0x06
#define R7_AUDIO_INTERFACE_1_ADR 0x07
#define R8_CLOCKING_2_ADR 0x08
#define R10_LEFT_DAC_VOLUME_ADR 0x09
#define R11_RIGHT_DAC_VOLUME_ADR 0x0A
#define R15_RESET_ADR 0x0F
#define R16_3D_CONTROL_ADR 0x10
#define R17_ALC1_ADR 0x11
#define R18_ALC2_ADR 0x12
#define R19_ALC3_ADR 0x13
#define R20_NOISE_GATE_ADR 0x14
#define R21_LEFT_ADC_VOLUME_ADR 0x15
#define R22_RIGHT_ADC_VOLUME_ADR 0x16
#define R23_ADDITIONAL_CONTROL_1_ADR 0x17
#define R24_ADDITIONAL_CONTROL_2_ADR 0x18
#define R25_PWR_MGMT_1_ADR 0x19
#define R26_PWR_MGMT_2_ADR 0x1A
#define R27_ADDITIONAL_CONTROL_3_ADR 0x1B
#define R28_ANTI_POP_1_ADR 0x1C
#define R29_ANTI_POP_2_ADR 0x1D
#define R32_ADCL_SIGNAL_PATH 0x20
#define R33_ADCR_SIGNAL_PATH 0x21
#define R34_LEFT_OUT_MIX_2 0x22
#define R37_RIGHT_OUT_MIX_2 0x23
#define R38_MONO_OUT_MIX_1 0x26
#define R39_MONO_OUT_MIX_2 0x27
#define R40_LOUT2_VOLUME 0x28
#define R41_ROUT2_VOLUME 0x29
#define R42_MONOOUT_VOLUME 0x2A
#define R43_INPUT_BOOST_MIXER_1 0x2B
#define R44_INPUT_BOOST_MIXER_2 0x2C
#define R45_BYPASS_1 0x2D
#define R46_BYPASS_2 0x2E
#define R47_PWR_MGMT_3 0x2F
#define R48_ADDITONAL_CTRL_4 0x30
#define R49_CLASS_D_CTRL_1 0x31
#define R51_CLASS_D_CTRL_3 0x33
typedef struct R0_LEFT_INPUT_VOLUME_t
{
uint16_t LINVOL :6; //Bits 5:0
uint16_t LIZC :1; //Bits 6
uint16_t LINMUTE :1; //Bits 7
uint16_t IPUV :1; //Bits 8
} __attribute__((packed, aligned(2))) R0_LEFT_INPUT_VOLUME_t;
typedef struct R1_RIGHT_INPUT_VOLUME_t
{
uint16_t RINVOL :6; //Bits 5:0
uint16_t RIZC :1; //Bits 6
uint16_t RINMUTE :1; //Bits 7
uint16_t IPUV :1; //Bits 8
} __attribute__((packed, aligned(2))) R1_RIGHT_INPUT_VOLUME_t;
typedef struct R2_LOUT1_VOLUME_t
{
uint16_t LOUT1VOL :7; //Bits 6:0
uint16_t LO1ZC :1; //Bits 7
uint16_t OUT1VU :1; //Bits 8
} __attribute__((packed, aligned(2))) R2_LOUT1_VOLUME_t;
typedef struct R3_ROUT1_VOLUME_t
{
uint16_t ROUT1VOL :7; //Bits 6:0
uint16_t RO1ZC :1; //Bits 7
uint16_t OUT1VU :1; //Bits 8
} __attribute__((packed, aligned(2))) R3_ROUT1_VOLUME_t;
typedef struct R4_CLOCKING_1_t{
uint16_t ADCDIV :3; //Bits 8:6
uint16_t DACDIV :3; //Bits 5:3
uint16_t SYSCLKDIV :2; //Bits 2:1
uint16_t CLKSEL :1; //Bits 0
} __attribute__((packed, aligned(2))) R4_CLOCKING_1_t;
typedef struct R5_ADC_DAC_CONTROL_CTR1_t{
uint16_t ADCHPD :1; //Bits 0
uint16_t DEEMPH :2; //Bits 2:1
uint16_t DACMU :1; //Bits 3
uint16_t R5RES_4 :1;
uint16_t ADCPOL :2; //Bits 6:5
uint16_t DACDIV2 :1; //Bits 7
uint16_t R5RES_8 :1;
}__attribute__((packed, aligned(2))) R5_ADC_DAC_CONTROL_CTR1_t ;
typedef struct R6_ADC_DAC_CONTROL_CTR2_t{
uint16_t DACSLOPE :1; //Bits 1
uint16_t DACMR :1; //Bits 2
uint16_t DACSMM :1; //Bits 3
uint16_t DACPOL :2; //Bits 6:5
}__attribute__((packed, aligned(2))) R6_ADC_DAC_CONTROL_CTR2_t;
typedef struct R7_AUDIO_INTERFACE_t{
uint16_t FORMAT :2; // bit 1:0
uint16_t WL :2; // bit 1:0
uint16_t LRP :1; // bit 4
uint16_t DLRSWAP :1; // bit 5
uint16_t MS :1; // bit 6
uint16_t BCLKINV :1; // bit 7
uint16_t ALRSWAP :1; // bit 8
}__attribute__((packed, aligned(2))) R7_AUDIO_INTERFACE_t;
typedef struct R8_CLOCKING_2_t{
uint16_t BCLKDIV :4; // bit 3:0
uint16_t DCLKDIV :3; // bit 2:0
}__attribute__((packed, aligned(2))) R8_CLOCKING_2_t;
typedef struct R9_AUDIO_INTERFACE_t{
uint16_t LOOPBACK :1; // bit 0
uint16_t ADCCOMP :2; // bits 1:0
uint16_t DACCOMP :2; // bits 1:0
uint16_t WL8 :1; // bit 5
uint16_t ALRCGPIO :1; // bit 6
}__attribute__((packed, aligned(2))) R9_AUDIO_INTERFACE_t;
typedef struct R10_LEFT_DAC_VOLUME_t{
uint16_t LDACVOL :8; // bit 7:0
uint16_t DACVU :1; // bit 8
}__attribute__((packed, aligned(2))) R10_LEFT_DAC_VOLUME_t;
typedef struct R11_RIGHT_DAC_VOLUME_t{
uint16_t RDACVOL :8; // bit 7:0
uint16_t DACVU :1; // bit 8
}__attribute__((packed, aligned(2))) R11_RIGHT_DAC_VOLUME_t;
// typedef struct R15_RESET_t{};
typedef struct R16_3D_CONTROL_t{
uint16_t D3EN :1; // bit 0
uint16_t D3DEPTH :4; // bits 3:0
uint16_t D3LC :1; // bit 5
uint16_t D3UC :1; // bit 6
}__attribute__((packed, aligned(2))) R16_3D_CONTROL_t;
typedef struct R17_ALC1_t{
uint16_t ALCL :4; // bits 3:0
uint16_t MAXGAIN :3; // bits 2:0
uint16_t ALCSEL :2; // bits 1:0
}__attribute__((packed, aligned(2))) R17_ALC1_t;
typedef struct R18_ALC2_t{
uint16_t HLD :4; // bits 3:0
uint16_t MINGAIN :3; // bits 2:0
}__attribute__((packed, aligned(2))) R18_ALC2_t;
typedef struct R19_ALC3_t{
uint16_t ATK :4; // bits 3:0
uint16_t DCY :4; // bits 3:0
}__attribute__((packed, aligned(2))) R19_ALC3_t;
typedef struct R20_NOISE_GATE_t{
uint16_t NGAT :1; // bit 0
uint16_t NGTH :5; // bit 4:0
}__attribute__((packed, aligned(2))) R20_NOISE_GATE_t;
typedef struct R21_LEFT_ADC_VOLUME_t{
uint16_t LADCVOL :8; // bits 7:0
uint16_t ADCVU :1; // bit 8
}__attribute__((packed, aligned(2))) R21_LEFT_ADC_VOLUME_t;
typedef struct R22_RIGHT_ADC_VOLUME_t{
uint16_t RADCVOL :8; // bits 7:0
uint16_t ADCVU :1; // bit 8
}__attribute__((packed, aligned(2))) R22_RIGHT_ADC_VOLUME_t;
typedef struct R23_ADDITIONAL_CONTROL_1_t{
uint16_t TOEN :1; // bit 1
uint16_t TOCLKSEL :1; // bit 2
uint16_t DATSEL :2; // bits 1:0
uint16_t DMONOMIX :1; // bit 4
uint16_t VSEL :2; // bits 1:0
uint16_t TSDEN :1; // bit 8
}__attribute__((packed, aligned(2))) R23_ADDITIONAL_CONTROL_1_t;
typedef struct R24_ADDITIONAL_CONTROL_2_t{
uint16_t LRCM :1; // bit 2
uint16_t TRIS :1; // bit 3
uint16_t HPSWPOL :1; // bit 5
uint16_t HPSWEN :1; // bit 6
}__attribute__((packed, aligned(2))) R24_ADDITIONAL_CONTROL_2_t;
typedef struct R25_PWR_MGMT_1_t{
uint16_t DIGENB :1; // bit 0
uint16_t MICB :1; // bit 1
uint16_t ADCR :1; // bit 2
uint16_t ADCL :1; // bit 3
uint16_t AINR :1; // bit 4
uint16_t AINL :1; // bit 5
uint16_t VREF :1; // bit 6
uint16_t VMIDSEL :2; // bit 1:0
}__attribute__((packed, aligned(2))) R25_PWR_MGMT_1_t;
typedef struct R26_PWR_MGMT_2_t{
uint16_t PLL_EN :1; // bit 0
uint16_t OUT3 :1; // bit 1
uint16_t SPKR :1; // bit 3
uint16_t SPKL :1; // bit 4
uint16_t ROUT1 :1; // bit 5
uint16_t LOUT1 :1; // bit 6
uint16_t DACR :1; // bit 7
uint16_t DACL :1; // bit 8
}__attribute__((packed, aligned(2))) R26_PWR_MGMT_2_t;
typedef struct R27_ADDITIONAL_CONTROL_3_t{
uint16_t ADC_ALC_SR :3; // bits 2:0
uint16_t OUT3CAP :1; // bit 3
uint16_t VROI :1; // bit 6
}__attribute__((packed, aligned(2))) R27_ADDITIONAL_CONTROL_3_t;
typedef struct R28_ANTI_POP_1_t{
uint16_t HPSTBY :1; // bit 0
uint16_t SOFT_ST :1; // bit 2
uint16_t BUFIOEN :1; // bit 3
uint16_t BUFDCOPEN :1; // bit 4
uint16_t POBCTRL :1; // bit 7
}__attribute__((packed, aligned(2))) R28_ANTI_POP_1_t;
typedef struct R29_ANTI_POP_2_t{
uint16_t DRES :2; // bits 1:0
uint16_t DISOP :1; // bit 5
}__attribute__((packed, aligned(2))) R29_ANTI_POP_2_t;
typedef struct R32_ADCL_SIGNAL_PATH_t{
uint16_t LMIC2B :1; // bit 3
uint16_t LMICBOOST :2; // bits 1:0
uint16_t LMP2 :1; // bit 6
uint16_t LMP3 :1; // bit 7
uint16_t LMN1 :1; // bit 8
}__attribute__((packed, aligned(2))) R32_ADCL_SIGNAL_PATH_t;
typedef struct R33_ADCR_SIGNAL_PATH_t{
uint16_t RMIC2B :1; // bit 3
uint16_t RMICBOOST :2; // bits 1:0
uint16_t RMP2 :1; // bit 6
uint16_t RMP3 :1; // bit 7
uint16_t RMN1 :1; // bit 8
}__attribute__((packed, aligned(2))) R33_ADCR_SIGNAL_PATH_t;
typedef struct R34_LEFT_OUT_MIX_1_t{
uint16_t LI2LOVOL :3; // bits 2:0
uint16_t LI2LO :1; // bit 7
uint16_t LD2LO :1; // bit 8
}__attribute__((packed, aligned(2))) R34_LEFT_OUT_MIX_1_t;
typedef struct R37_RIGHT_OUT_MIX_2_t{
uint16_t RI2ROVOL :3; // bits 2:0
uint16_t RI2RO :1; // bit 7
uint16_t RD2RO :1; // bit 8
}__attribute__((packed, aligned(2))) R37_RIGHT_OUT_MIX_2_t;
typedef struct R38_MONO_OUT_MIX_1_t{
uint16_t L2MO :1; // bit 7
}__attribute__((packed, aligned(2))) R38_MONO_OUT_MIX_1_t;
typedef struct R39_MONO_OUT_MIX_2_t{
uint16_t R2MO :1; // bit 7
}__attribute__((packed, aligned(2))) R39_MONO_OUT_MIX_2_t;
typedef struct R40_LOUT2_VOLUME_t{
uint16_t SPKLVOL :7; // bits 6:0
uint16_t SPKLZC :1; // bit 7
uint16_t SPKVU :1; // bit 8
}__attribute__((packed, aligned(2))) R40_LOUT2_VOLUME_t;
typedef struct R41_ROUT2_VOLUME_t{
uint16_t SPKRVOL :7; // bits 6:0
uint16_t SPKLZC :1; // bit 7
uint16_t SPKVU :1; // bit 8
}__attribute__((packed, aligned(2))) R41_ROUT2_VOLUME_t;
typedef struct R42_MONOOUT_VOLUME_t{
uint16_t MOUTVOL :1; //bit 6
}__attribute__((packed, aligned(2))) R42_MONOOUT_VOLUME_t;
typedef struct R43_INPUT_BOOST_MIXER_1_t{
uint16_t LIN2BOOST :3; // bits 2:0
uint16_t LIN3BOOST :3; // bits 2:0
}__attribute__((packed, aligned(2))) R43_INPUT_BOOST_MIXER_1_t;
typedef struct R44_INPUT_BOOST_MIXER_2_t{
uint16_t RIN2BOOST :3; // bits 2:0
uint16_t RIN3BOOST :3; // bits 2:0
}__attribute__((packed, aligned(2))) R44_INPUT_BOOST_MIXER_2_t;
typedef struct R45_BYPASS_1_t{
uint16_t LB2LOVOL :3; // bits 2:0
uint16_t LB2LO :1; // bit 5
}__attribute__((packed, aligned(2))) R45_BYPASS_1_t;
typedef struct R46_BYPASS_2_t{
uint16_t RB2ROVOL :3; // bits 2:0
uint16_t RB2RO :1; // bit 5
}__attribute__((packed, aligned(2))) R46_BYPASS_2_t;
typedef struct R47_PWR_MGMT_3_t{
uint16_t ROMIX :1; // bit 2
uint16_t LOMIX :1; // bit 3
uint16_t RMIC :1; // bit 4
uint16_t LMIC :1; // bit 5
}__attribute__((packed, aligned(2))) R47_PWR_MGMT_3_t;
typedef struct R48_ADDITIONAL_CONTROL_4_t{
uint16_t MBSEL :1; // bit 0
uint16_t TSENSEN :1; // bit 1
uint16_t HPSEL :2; // bits 1:0
uint16_t GPIOSEL :3; // bits 2:0
uint16_t GPIOPOL :1; // bit 7
}__attribute__((packed, aligned(2))) R48_ADDITIONAL_CONTROL_4_t;
typedef struct R49_CLASS_D_CONTROL_1_t{
uint16_t SPK_OP_EN :2; // bits 1:0
}__attribute__((packed, aligned(2))) R49_CLASS_D_CONTROL_1_t;
typedef struct R51_CLASS_D_CONTROL_3_t{
uint16_t ACGAIN :3; // bits 2:0
uint16_t DCGAIN :3; // bits 2:0
}__attribute__((packed, aligned(2))) R51_CLASS_D_CONTROL_3_t;
typedef struct R52_PLL_N_t{
uint16_t PLLN :4; // bits 3:0
uint16_t PLLRESCALE :1; // bit 4
uint16_t SDM :1; // bit 5
uint16_t OPCLKDIV :3; // bits 2:0
}__attribute__((packed, aligned(2))) R52_PLL_N_t;
typedef struct R53_PLL_K_1_t{
uint16_t PLLK :8; // bits 23:16
}__attribute__((packed, aligned(2))) R53_PLL_K_1_t;
typedef struct R54_PLL_K_2_t{
uint16_t PLLK :8; // bits 15:8
}__attribute__((packed, aligned(2))) R54_PLL_K_2_t;
typedef struct R55_PLL_K_3_t{
uint16_t PLLK :8; // bits 7:0
}__attribute__((packed, aligned(2))) R55_PLL_K_3_t;
void W8960_Init(TwoWire *tw);
void W8960_SetGain(uint8_t sel, uint16_t value);
#endif

View File

@ -18,6 +18,7 @@ lib_extra_dirs = ${common.lib_extra_dirs}
lib/libesp32_lvgl lib/libesp32_lvgl
lib/libesp32_div lib/libesp32_div
lib/libesp32_eink lib/libesp32_eink
lib/libesp32_audio
lib_ignore = lib_ignore =
HTTPUpdateServer HTTPUpdateServer
ESP RainMaker ESP RainMaker

View File

@ -67,13 +67,13 @@
#define i2s_port_t uint8_t #define i2s_port_t uint8_t
#endif #endif
#ifdef ESP32
#define MODE_MIC 0 #define MODE_MIC 0
#define MODE_SPK 1 #define MODE_SPK 1
#ifndef MICSRATE #ifndef MICSRATE
#define MICSRATE 16000 #define MICSRATE 32000
#endif #endif
#endif // ESP32
struct AUDIO_I2S_t { struct AUDIO_I2S_t {
uint8_t is2_volume; // should be in settings uint8_t is2_volume; // should be in settings
@ -105,6 +105,7 @@ struct AUDIO_I2S_t {
#ifdef ESP32 #ifdef ESP32
TaskHandle_t mp3_task_h; TaskHandle_t mp3_task_h;
TaskHandle_t mic_task_h; TaskHandle_t mic_task_h;
#endif // ESP32
uint32_t mic_size; uint32_t mic_size;
uint32_t mic_rate; uint32_t mic_rate;
uint8_t *mic_buff; uint8_t *mic_buff;
@ -118,9 +119,10 @@ struct AUDIO_I2S_t {
int8_t mic_ws = -1; int8_t mic_ws = -1;
int8_t mic_din = -1; int8_t mic_din = -1;
int8_t mic_dout = -1; int8_t mic_dout = -1;
uint8_t mic_gain = 1;
bool use_stream = false; bool use_stream = false;
i2s_port_t mic_port; i2s_port_t mic_port;
#endif // ESP32
#ifdef USE_SHINE #ifdef USE_SHINE
uint32_t recdur; uint32_t recdur;
@ -130,10 +132,13 @@ struct AUDIO_I2S_t {
ESP8266WebServer *MP3Server; ESP8266WebServer *MP3Server;
#endif #endif
uint8_t mode;
} audio_i2s; } audio_i2s;
#ifndef MIC_CHANNELS
#define MIC_CHANNELS 1 #define MIC_CHANNELS 1
#endif
#ifdef USE_TTGO_WATCH #ifdef USE_TTGO_WATCH
#undef AUDIO_PWR_ON #undef AUDIO_PWR_ON
@ -198,6 +203,15 @@ void Cmd_MicRec(void);
void Cmd_wav2mp3(void); void Cmd_wav2mp3(void);
void Cmd_Time(void); void Cmd_Time(void);
void copy_micpars(uint32_t port) {
audio_i2s.mic_mclk = audio_i2s.mclk;
audio_i2s.mic_bclk = audio_i2s.bclk;
audio_i2s.mic_ws = audio_i2s.ws;
audio_i2s.mic_dout = audio_i2s.dout;
audio_i2s.mic_din = audio_i2s.din;
audio_i2s.mic_port = (i2s_port_t)port;
}
int32_t I2S_Init_0(void) { int32_t I2S_Init_0(void) {
audio_i2s.i2s_port = (i2s_port_t)0; audio_i2s.i2s_port = (i2s_port_t)0;
@ -216,6 +230,9 @@ int32_t I2S_Init_0(void) {
audio_i2s.ws = DAC_IIS_WS; audio_i2s.ws = DAC_IIS_WS;
audio_i2s.dout = DAC_IIS_DOUT; audio_i2s.dout = DAC_IIS_DOUT;
audio_i2s.din = DAC_IIS_DIN; audio_i2s.din = DAC_IIS_DIN;
copy_micpars(0);
#else #else
#ifdef USE_I2S_NO_DAC #ifdef USE_I2S_NO_DAC
if (PinUsed(GPIO_I2S_DOUT)) { if (PinUsed(GPIO_I2S_DOUT)) {
@ -235,12 +252,7 @@ int32_t I2S_Init_0(void) {
audio_i2s.dout = Pin(GPIO_I2S_DOUT); audio_i2s.dout = Pin(GPIO_I2S_DOUT);
audio_i2s.din = Pin(GPIO_I2S_DIN); audio_i2s.din = Pin(GPIO_I2S_DIN);
audio_i2s.mic_mclk = audio_i2s.mclk; copy_micpars(0);
audio_i2s.mic_bclk = audio_i2s.bclk;
audio_i2s.mic_ws = audio_i2s.ws;
audio_i2s.mic_dout = audio_i2s.dout;
audio_i2s.mic_din = audio_i2s.din;
audio_i2s.mic_port = (i2s_port_t)0;
// check if 2 ports used, use second for micro // check if 2 ports used, use second for micro
if (PinUsed(GPIO_I2S_BCLK, 1) && PinUsed(GPIO_I2S_WS, 1) && PinUsed(GPIO_I2S_DIN, 1)) { if (PinUsed(GPIO_I2S_BCLK, 1) && PinUsed(GPIO_I2S_WS, 1) && PinUsed(GPIO_I2S_DIN, 1)) {
@ -265,12 +277,7 @@ int32_t I2S_Init_0(void) {
audio_i2s.dout = Pin(GPIO_I2S_DOUT, 1); audio_i2s.dout = Pin(GPIO_I2S_DOUT, 1);
audio_i2s.din = Pin(GPIO_I2S_DIN, 1); audio_i2s.din = Pin(GPIO_I2S_DIN, 1);
audio_i2s.mic_mclk = audio_i2s.mclk; copy_micpars(1);
audio_i2s.mic_bclk = audio_i2s.bclk;
audio_i2s.mic_ws = audio_i2s.ws;
audio_i2s.mic_dout = audio_i2s.dout;
audio_i2s.mic_din = audio_i2s.din;
audio_i2s.mic_port = (i2s_port_t)1;
} else { } else {
return -1; return -1;
@ -299,21 +306,32 @@ int32_t I2S_Init_0(void) {
#endif // USE_I2S_EXTERNAL_DAC #endif // USE_I2S_EXTERNAL_DAC
audio_i2s.mode = MODE_SPK;
#ifdef USE_I2S_COMMON_IO
audio_i2s.out->SetRate(MICSRATE);
#endif
return 0; return 0;
} }
void I2S_Init(void) { void I2S_Init(void) {
#if defined(ESP32) && defined(ESP32S3_BOX)
S3boxInit();
#endif
if (I2S_Init_0()) { if (I2S_Init_0()) {
return; return;
} }
audio_i2s.is2_volume=10; #if defined(ESP32) && defined(ESP32S3_BOX)
audio_i2s.out->SetGain(((float)audio_i2s.is2_volume/100.0)*4.0); S3boxInit();
#endif
#ifdef USE_W8960
W8960_Init();
#endif
audio_i2s.is2_volume = 10;
audio_i2s.out->SetGain(((float)audio_i2s.is2_volume / 100.0) * 4.0);
audio_i2s.out->stop(); audio_i2s.out->stop();
audio_i2s.mp3ram = nullptr; audio_i2s.mp3ram = nullptr;
@ -517,12 +535,17 @@ void Play_mp3(const char *path) {
if (I2S_Task) { if (I2S_Task) {
xTaskCreatePinnedToCore(mp3_task, "MP3", 8192, NULL, 3, &audio_i2s.mp3_task_h, 1); xTaskCreatePinnedToCore(mp3_task, "MP3", 8192, NULL, 3, &audio_i2s.mp3_task_h, 1);
} else { } else {
#define MP3_TIMEOUT 30000
uint32_t tout = millis();
while (audio_i2s.mp3->isRunning()) { while (audio_i2s.mp3->isRunning()) {
if (!audio_i2s.mp3->loop()) { if (!audio_i2s.mp3->loop()) {
audio_i2s.mp3->stop(); audio_i2s.mp3->stop();
break; break;
} }
OsWatchLoop(); OsWatchLoop();
if (millis()-tout > MP3_TIMEOUT) {
break;
}
} }
audio_i2s.out->stop(); audio_i2s.out->stop();
mp3_delete(); mp3_delete();
@ -568,6 +591,7 @@ const char kI2SAudio_Commands[] PROGMEM = "I2S|"
#endif // USE_I2S_WEBRADIO #endif // USE_I2S_WEBRADIO
#if defined(USE_SHINE) && ( (defined(USE_I2S_AUDIO) && defined(USE_I2S_MIC)) || defined(USE_M5STACK_CORE2) || defined(ESP32S3_BOX) ) #if defined(USE_SHINE) && ( (defined(USE_I2S_AUDIO) && defined(USE_I2S_MIC)) || defined(USE_M5STACK_CORE2) || defined(ESP32S3_BOX) )
"|REC" "|REC"
"|MGain"
#ifdef MP3_MIC_STREAM #ifdef MP3_MIC_STREAM
"|STREAM" "|STREAM"
#endif // MP3_MIC_STREAM #endif // MP3_MIC_STREAM
@ -587,6 +611,7 @@ void (* const I2SAudio_Command[])(void) PROGMEM = {
#endif // USE_I2S_WEBRADIO #endif // USE_I2S_WEBRADIO
#if defined(USE_SHINE) && ( (defined(USE_I2S_AUDIO) && defined(USE_I2S_MIC)) || defined(USE_M5STACK_CORE2) || defined(ESP32S3_BOX) ) #if defined(USE_SHINE) && ( (defined(USE_I2S_AUDIO) && defined(USE_I2S_MIC)) || defined(USE_M5STACK_CORE2) || defined(ESP32S3_BOX) )
,&Cmd_MicRec ,&Cmd_MicRec
,&Cmd_MicGain
#ifdef MP3_MIC_STREAM #ifdef MP3_MIC_STREAM
,&Cmd_MP3Stream ,&Cmd_MP3Stream
#endif // MP3_MIC_STREAM #endif // MP3_MIC_STREAM

View File

@ -21,30 +21,36 @@
#ifdef ESP32 #ifdef ESP32
#if defined(USE_SHINE) && ( (defined(USE_I2S_AUDIO) && defined(USE_I2S_MIC)) || defined(USE_M5STACK_CORE2) || defined(ESP32S3_BOX) ) #if defined(USE_SHINE) && ( (defined(USE_I2S_AUDIO) && defined(USE_I2S_MIC)) || defined(USE_M5STACK_CORE2) || defined(ESP32S3_BOX) )
#define MP3_BOUNDARY "e8b8c539-047d-4777-a985-fbba6edff11e"
uint32_t SpeakerMic(uint8_t spkr) { uint32_t SpeakerMic(uint8_t spkr) {
esp_err_t err = ESP_OK; esp_err_t err = ESP_OK;
#ifndef USE_I2S_COMMON_IO
if (spkr == MODE_SPK) { if (spkr == MODE_SPK) {
if (audio_i2s.mic_port == 0) { if (audio_i2s.i2s_port == audio_i2s.mic_port) {
if (audio_i2s.mode != MODE_SPK) {
i2s_driver_uninstall(audio_i2s.mic_port);
}
I2S_Init_0(); I2S_Init_0();
audio_i2s.out->SetGain(((float)(audio_i2s.is2_volume-2)/100.0)*4.0); audio_i2s.out->SetGain(((float)(audio_i2s.is2_volume - 2) / 100.0) * 4.0);
audio_i2s.out->stop(); audio_i2s.out->stop();
} }
audio_i2s.mode = spkr;
return 0; return 0;
} }
// set micro // set micro
if (audio_i2s.mic_port == 0) { if (audio_i2s.i2s_port == audio_i2s.mic_port) {
// close audio out // close audio out
if (audio_i2s.out) { if (audio_i2s.out) {
audio_i2s.out->stop(); audio_i2s.out->stop();
delete audio_i2s.out; delete audio_i2s.out;
audio_i2s.out = nullptr; audio_i2s.out = nullptr;
} }
i2s_driver_uninstall(audio_i2s.i2s_port); if (audio_i2s.mode == MODE_SPK) {
i2s_driver_uninstall(audio_i2s.i2s_port);
}
} }
// config mic // config mic
@ -104,6 +110,10 @@ esp_err_t err = ESP_OK;
} }
err += i2s_set_clk(audio_i2s.mic_port, audio_i2s.mic_rate, I2S_BITS_PER_SAMPLE_16BIT, mode); err += i2s_set_clk(audio_i2s.mic_port, audio_i2s.mic_rate, I2S_BITS_PER_SAMPLE_16BIT, mode);
#endif // USE_I2S_COMMON_IO
audio_i2s.mode = spkr;
return err; return err;
} }
@ -112,8 +122,6 @@ esp_err_t err = ESP_OK;
#include <layer3.h> #include <layer3.h>
#include <types.h> #include <types.h>
#define MP3HANDLECLIENT audio_i2s.MP3Server->handleClient();
// micro to mp3 file or stream // micro to mp3 file or stream
void mic_task(void *arg){ void mic_task(void *arg){
int8_t error = 0; int8_t error = 0;
@ -144,7 +152,9 @@ void mic_task(void *arg){
audio_i2s.client.setTimeout(3); audio_i2s.client.setTimeout(3);
audio_i2s.client.print("HTTP/1.1 200 OK\r\n" audio_i2s.client.print("HTTP/1.1 200 OK\r\n"
"Content-Type: audio/mpeg;\r\n\r\n"); "Content-Type: audio/mpeg;\r\n\r\n");
MP3HANDLECLIENT
// Webserver->send(200, "application/octet-stream", "");
//"Content-Type: audio/mp3;\r\n\r\n");
} }
shine_set_config_mpeg_defaults(&config.mpeg); shine_set_config_mpeg_defaults(&config.mpeg);
@ -183,6 +193,13 @@ void mic_task(void *arg){
while (!audio_i2s.mic_stop) { while (!audio_i2s.mic_stop) {
uint32_t bytes_read; uint32_t bytes_read;
i2s_read(audio_i2s.mic_port, (char *)buffer, bytesize, &bytes_read, (100 / portTICK_RATE_MS)); i2s_read(audio_i2s.mic_port, (char *)buffer, bytesize, &bytes_read, (100 / portTICK_RATE_MS));
if (audio_i2s.mic_gain > 1) {
// set gain
for (uint32_t cnt = 0; cnt < bytes_read / 2; cnt++) {
buffer[cnt] *= audio_i2s.mic_gain;
}
}
ucp = shine_encode_buffer_interleaved(s, buffer, &written); ucp = shine_encode_buffer_interleaved(s, buffer, &written);
if (!audio_i2s.use_stream) { if (!audio_i2s.use_stream) {
@ -192,7 +209,7 @@ void mic_task(void *arg){
} }
} else { } else {
audio_i2s.client.write((const char*)ucp, written); audio_i2s.client.write((const char*)ucp, written);
MP3HANDLECLIENT
if (!audio_i2s.client.connected()) { if (!audio_i2s.client.connected()) {
break; break;
} }
@ -206,7 +223,6 @@ void mic_task(void *arg){
mp3_out.write(ucp, written); mp3_out.write(ucp, written);
} else { } else {
audio_i2s.client.write((const char*)ucp, written); audio_i2s.client.write((const char*)ucp, written);
MP3HANDLECLIENT
} }
@ -223,13 +239,12 @@ exit:
if (audio_i2s.use_stream) { if (audio_i2s.use_stream) {
audio_i2s.client.stop(); audio_i2s.client.stop();
MP3HANDLECLIENT
} }
SpeakerMic(MODE_SPK); SpeakerMic(MODE_SPK);
audio_i2s.mic_stop = 0; audio_i2s.mic_stop = 0;
audio_i2s.mic_error = error; audio_i2s.mic_error = error;
AddLog(LOG_LEVEL_INFO, PSTR("mp3task error: %d"), error); AddLog(LOG_LEVEL_INFO, PSTR("mp3task result code: %d"), error);
audio_i2s.mic_task_h = 0; audio_i2s.mic_task_h = 0;
audio_i2s.recdur = 0; audio_i2s.recdur = 0;
audio_i2s.stream_active = 0; audio_i2s.stream_active = 0;
@ -292,6 +307,14 @@ void Cmd_MicRec(void) {
} }
// mic gain in factor not percent
void Cmd_MicGain(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 256)) {
audio_i2s.mic_gain = XdrvMailbox.payload;
}
ResponseCmndNumber(audio_i2s.mic_gain);
}
#endif // USE_SHINE #endif // USE_SHINE
#endif // USE_I2S_AUDIO #endif // USE_I2S_AUDIO
#endif // ESP32 #endif // ESP32

View File

@ -4,6 +4,11 @@
#ifdef MP3_MIC_STREAM #ifdef MP3_MIC_STREAM
#ifndef MP3_STREAM_PORT
#define MP3_STREAM_PORT 81
#endif
void Stream_mp3(void) { void Stream_mp3(void) {
if (!audio_i2s.stream_enable) { if (!audio_i2s.stream_enable) {
return; return;
@ -28,10 +33,11 @@ void i2s_mp3_loop(void) {
void i2s_mp3_init(uint32_t on) { void i2s_mp3_init(uint32_t on) {
if (on) { if (on) {
if (!audio_i2s.MP3Server) { if (!audio_i2s.MP3Server) {
audio_i2s.MP3Server = new ESP8266WebServer(81); audio_i2s.MP3Server = new ESP8266WebServer(MP3_STREAM_PORT);
audio_i2s.MP3Server->on(PSTR("/stream.mp3"), Stream_mp3); audio_i2s.MP3Server->on(PSTR("/stream.mp3"), Stream_mp3);
audio_i2s.MP3Server->on(PSTR("/stream.m3a"), Stream_mp3);
audio_i2s.MP3Server->begin(); audio_i2s.MP3Server->begin();
AddLog(LOG_LEVEL_INFO, PSTR("MP3: server created")); AddLog(LOG_LEVEL_INFO, PSTR("MP3: server created on port: %d "), MP3_STREAM_PORT);
} }
} else { } else {
if (audio_i2s.MP3Server) { if (audio_i2s.MP3Server) {

View File

@ -70,7 +70,6 @@ uint32_t es7243e_init() {
return ret_val; return ret_val;
} }
// box adc init // box adc init
uint32_t es7210_init() { uint32_t es7210_init() {
uint32_t ret_val = ESP_OK; uint32_t ret_val = ESP_OK;
@ -124,7 +123,7 @@ uint32_t ES8311_init() {
return ret_val; return ret_val;
} }
void S3boxInit() { void S3boxInit(void) {
if (TasmotaGlobal.i2c_enabled_2) { if (TasmotaGlobal.i2c_enabled_2) {
// box lite // box lite
ES8156_init(); ES8156_init();
@ -137,4 +136,20 @@ void S3boxInit() {
} }
} }
#endif // ESP32S3_BOX #endif // ESP32S3_BOX
#ifdef USE_W8960
#include <wm8960.h>
void W8960_Init(void) {
if (TasmotaGlobal.i2c_enabled_2) {
if (I2cSetDevice(W8960_ADDR, 1)) {
I2cSetActiveFound(W8960_ADDR, "W8960-I2C", 1);
W8960_Init(&Wire1);
}
}
}
#endif // USE_W8960
#endif // ESP32 #endif // ESP32