Added USE_PSRAM option to kconfig - allow small internal buffer on non PSRAM hardware

This commit is contained in:
jorgenkraghjakobsen
2020-06-05 00:23:57 +02:00
Unverified
parent 3b9e95dc1a
commit aa375e462a
5 changed files with 722 additions and 656 deletions

View File

@@ -1,6 +1,12 @@
# Config file for ESP32 DSP Processor
menu "ESP32 audio I2S config"
menu "ESP32 audio buffer and I2S pin config"
config USE_PSRAM
bool "Use PSRAM"
default true
help
Need wrover class modules with large SPRAM to have required buffers for Snapcast network delay
config MASTER_I2S_BCK_PIN
int "Master i2s bck"
default 23

View File

@@ -16,10 +16,12 @@
static xTaskHandle s_dsp_i2s_task_handle = NULL;
static RingbufHandle_t s_ringbuf_i2s = NULL;
extern xQueueHandle i2s_queue;
extern uint32_t buffer_ms;
extern uint8_t muteCH[4];
uint dspFlow = dspfStereo;
uint8_t muteCH[4];
ptype_t bq[6];
void setup_dsp_i2s(uint32_t sample_rate, bool slave_i2s)
@@ -101,11 +103,19 @@ static void dsp_i2s_task_handler(void *arg)
int32_t rwdif;
static int32_t avgcnt = 0;
uint32_t avgcntlen = 64; // x 960/4*1/fs = 320ms @48000 kHz
uint32_t avgarray[128];
uint32_t avgarray[128] = {0};
uint32_t sum;
float avg ;
for (;;) {
// Condition state Action
// Buffer is empty - because not being filled Stopped Wait
// Buffer is increasing and below target Filling Wait
// Buffer above target Playing Consume from buffer
// Buffer is below target Playing Short delay
cnt++;
audio = (uint8_t *)xRingbufferReceiveUpTo(s_ringbuf_i2s, &chunk_size,(portTickType) 20 ,960); // 200 ms timeout
vRingbufferGetInfo(s_ringbuf_i2s, &freeBuffer, &rbuf, &wbuf, NULL, &inBuffer );
if (avgcnt >= avgcntlen) { avgcnt = 0; }
@@ -116,19 +126,25 @@ static void dsp_i2s_task_handler(void *arg)
}
avg = sum / avgcntlen;
rwdif = (rbuf-wbuf);
if (rwdif < 0 ) {
rwdif = rwdif + 81920;
}
#ifndef CONFIG_USE_PSRAM
buffer_ms = 150;
#endif
if (inBuffer < (buffer_ms*48*4)) {vTaskDelay(1); }
audio = (uint8_t *)xRingbufferReceiveUpTo(s_ringbuf_i2s, &chunk_size, (portTickType)portMAX_DELAY,960);
//audio = (uint8_t *)xRingbufferReceiveUpTo(s_ringbuf_i2s, &chunk_size,(portTickType) 20 ,960); // 200 ms timeout
//audio = (uint8_t *)xRingbufferReceive(s_ringbuf_i2s, &chunk_size, (portTickType)portMAX_DELAY);
if (chunk_size !=0 ){
int16_t len = chunk_size/4;
if (chunk_size == 0)
{ printf("wait ... buffer : %d\n",inBuffer);
}
//else if (inBuffer < (buffer_ms*48*4))
//{ printf("Buffering ... buffer : %d\n",inBuffer);
//}
else
{ int16_t len = chunk_size/4;
if (cnt%200 < 16)
{ ESP_LOGI("I2S", "Chunk :%d (%d/%d) %d %.0f",chunk_size, inBuffer, freeBuffer,rwdif, avg );
{ ESP_LOGI("I2S", "Chunk :%d %d %.0f",chunk_size, inBuffer, avg );
//xRingbufferPrintInfo(s_ringbuf_i2s);
}
uint8_t *data_ptr = audio;
@@ -262,18 +278,22 @@ static void dsp_i2s_task_handler(void *arg)
// buffer size must hold 400ms-1000ms // for 2ch16b48000 that is 76800 - 192000 or 75-188 x 1024
#define BUFFER_SIZE 192*1024
#define BUFFER_TYPE RINGBUF_TYPE_BYTEBUF
void dsp_i2s_task_init(uint32_t sample_rate,bool slave)
{ setup_dsp_i2s(sample_rate,slave);
StaticRingbuffer_t *buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
printf("Buffer_struct ok\n");
#ifdef CONFIG_USE_PSRAM
printf("Setup ringbuffer using PSRAM \n");
StaticRingbuffer_t *buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
printf("Buffer_struct ok\n");
uint8_t *buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t)*BUFFER_SIZE, MALLOC_CAP_SPIRAM);
printf("Buffer_stoarge ok\n");
s_ringbuf_i2s = xRingbufferCreateStatic(BUFFER_SIZE, BUFFER_TYPE, buffer_storage, buffer_struct);
printf("Ringbuf ok\n");
//s_ringbuf_i2s = xRingbufferCreate(32*1024,RINGBUF_TYPE_BYTEBUF); // 8*1024
uint8_t *buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t)*BUFFER_SIZE, MALLOC_CAP_SPIRAM);
printf("Buffer_stoarge ok\n");
s_ringbuf_i2s = xRingbufferCreateStatic(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF, buffer_storage, buffer_struct);
printf("Ringbuf ok\n");
#else
printf("Setup ringbuffer using internal ram - only space for 150ms - Snapcast buffer_ms parameter ignored \n");
s_ringbuf_i2s = xRingbufferCreate(32*1024,RINGBUF_TYPE_BYTEBUF);
#endif
if (s_ringbuf_i2s == NULL) { printf("nospace for ringbuffer\n"); return; }
printf("Ringbuffer ok\n");
xTaskCreate(dsp_i2s_task_handler, "DSP_I2S", 48*1024, NULL, 6, &s_dsp_i2s_task_handle);

View File

@@ -34,6 +34,7 @@
xQueueHandle i2s_queue;
uint32_t buffer_ms = 400;
uint8_t muteCH[4] = {0};
/* The examples use simple WiFi configuration that you can set via
'make menuconfig'.
@@ -467,6 +468,10 @@ static void http_get_task(void *pvParameters)
ESP_LOGI(TAG, "Latency: %d", server_settings_message.latency);
ESP_LOGI(TAG, "Mute: %d", server_settings_message.muted);
ESP_LOGI(TAG, "Setting volume: %d", server_settings_message.volume);
muteCH[0] = server_settings_message.muted;
muteCH[1] = server_settings_message.muted;
muteCH[2] = server_settings_message.muted;
muteCH[3] = server_settings_message.muted;
// TODO abstract volume setting for various output
uint8_t cmd[4];
cmd[0] = 128-server_settings_message.volume ;

View File

@@ -2,7 +2,6 @@
# Automatically generated file. DO NOT EDIT.
# Espressif IoT Development Framework (ESP-IDF) Project Configuration
#
CONFIG_IDF_CMAKE=y
CONFIG_IDF_TARGET="esp32"
CONFIG_IDF_TARGET_ESP32=y
CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000
@@ -11,6 +10,8 @@ CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000
# SDK tool configuration
#
CONFIG_SDK_TOOLPREFIX="xtensa-esp32-elf-"
CONFIG_SDK_PYTHON="python"
CONFIG_SDK_MAKE_WARN_UNDEFINED_VARIABLES=y
# CONFIG_SDK_TOOLCHAIN_SUPPORTS_TIME_WIDE_64_BITS is not set
# end of SDK tool configuration
@@ -24,6 +25,19 @@ CONFIG_APP_BUILD_BOOTLOADER=y
CONFIG_APP_BUILD_USE_FLASH_SECTIONS=y
# end of Build type
#
# Merus MA120x0 interface Configuration
#
CONFIG_MA120X0_NENABLE_PIN=13
CONFIG_MA120X0_NMUTE_PIN=12
CONFIG_MA120X0_ENABLE_PIN=5
CONFIG_MA120X0_NERR_PIN=34
CONFIG_MA120X0_NCLIP_PIN=35
CONFIG_MA120X0_SCL_PIN=32
CONFIG_MA120X0_SDA_PIN=33
CONFIG_MA120X0_I2C_ADDR=0x20
# end of Merus MA120x0 interface Configuration
#
# Application manager
#
@@ -67,10 +81,30 @@ CONFIG_BOOTLOADER_RESERVE_RTC_SIZE=0
# CONFIG_SECURE_FLASH_ENC_ENABLED is not set
# end of Security features
#
# ESP32 audio buffer and I2S pin config
#
CONFIG_USE_PSRAM=y
CONFIG_MASTER_I2S_BCK_PIN=25
CONFIG_MASTER_I2S_LRCK_PIN=26
CONFIG_MASTER_I2S_DATAOUT_PIN=27
CONFIG_SLAVE_I2S_BCK_PIN=26
CONFIG_SLAVE_I2S_LRCK_PIN=12
CONFIG_SLAVE_I2S_DATAOUT_PIN=5
# end of ESP32 audio buffer and I2S pin config
#
# Serial flasher config
#
CONFIG_ESPTOOLPY_PORT="/dev/ttyUSB0"
CONFIG_ESPTOOLPY_BAUD_115200B=y
# CONFIG_ESPTOOLPY_BAUD_230400B is not set
# CONFIG_ESPTOOLPY_BAUD_921600B is not set
# CONFIG_ESPTOOLPY_BAUD_2MB is not set
# CONFIG_ESPTOOLPY_BAUD_OTHER is not set
CONFIG_ESPTOOLPY_BAUD_OTHER_VAL=115200
CONFIG_ESPTOOLPY_BAUD=115200
CONFIG_ESPTOOLPY_COMPRESSED=y
# CONFIG_ESPTOOLPY_FLASHMODE_QIO is not set
# CONFIG_ESPTOOLPY_FLASHMODE_QOUT is not set
CONFIG_ESPTOOLPY_FLASHMODE_DIO=y
@@ -117,30 +151,6 @@ CONFIG_PARTITION_TABLE_OFFSET=0x8000
# CONFIG_PARTITION_TABLE_MD5 is not set
# end of Partition Table
#
# Merus MA120x0 interface Configuration
#
CONFIG_MA120X0_NENABLE_PIN=13
CONFIG_MA120X0_NMUTE_PIN=12
CONFIG_MA120X0_ENABLE_PIN=5
CONFIG_MA120X0_NERR_PIN=34
CONFIG_MA120X0_NCLIP_PIN=35
CONFIG_MA120X0_SCL_PIN=32
CONFIG_MA120X0_SDA_PIN=33
CONFIG_MA120X0_I2C_ADDR=0x20
# end of Merus MA120x0 interface Configuration
#
# ESP32 audio I2S config
#
CONFIG_MASTER_I2S_BCK_PIN=25
CONFIG_MASTER_I2S_LRCK_PIN=26
CONFIG_MASTER_I2S_DATAOUT_PIN=27
CONFIG_SLAVE_I2S_BCK_PIN=26
CONFIG_SLAVE_I2S_LRCK_PIN=12
CONFIG_SLAVE_I2S_DATAOUT_PIN=5
# end of ESP32 audio I2S config
#
# Compiler options
#
@@ -273,7 +283,7 @@ CONFIG_SPIRAM_SIZE=-1
CONFIG_SPIRAM_SPEED_40M=y
CONFIG_SPIRAM=y
CONFIG_SPIRAM_BOOT_INIT=y
# CONFIG_SPIRAM_IGNORE_NOTFOUND is not set
CONFIG_SPIRAM_IGNORE_NOTFOUND=y
# CONFIG_SPIRAM_USE_MEMMAP is not set
CONFIG_SPIRAM_USE_CAPS_ALLOC=y
# CONFIG_SPIRAM_USE_MALLOC is not set
@@ -363,6 +373,23 @@ CONFIG_ESP32_DPORT_DIS_INTERRUPT_LVL=5
# CONFIG_PM_ENABLE is not set
# end of Power Management
#
# Memory protection
#
CONFIG_ESP32S2_MEMPROT_FEATURE=y
CONFIG_ESP32S2_MEMPROT_FEATURE_LOCK=y
# end of Memory protection
#
# Cache config
#
# end of Cache config
CONFIG_ESP32S2_TRACEMEM_RESERVE_DRAM=0x0
CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=0
CONFIG_ESP32S2_DEBUG_OCDAWARE=y
CONFIG_ESP32S2_BROWNOUT_DET=y
#
# ADC-Calibration
#
@@ -1006,18 +1033,6 @@ CONFIG_SPIFFS_USE_MTIME=y
# end of Debug Configuration
# end of SPIFFS Configuration
#
# TinyUSB
#
#
# Descriptor configuration
#
CONFIG_USB_DESC_CUSTOM_VID=0x1234
CONFIG_USB_DESC_CUSTOM_PID=0x5678
# end of Descriptor configuration
# end of TinyUSB
#
# Unity unit testing library
#
@@ -1080,6 +1095,8 @@ CONFIG_WPA_MBEDTLS_CRYPTO=y
# Deprecated options for backward compatibility
CONFIG_TOOLPREFIX="xtensa-esp32-elf-"
CONFIG_PYTHON="python"
CONFIG_MAKE_WARN_UNDEFINED_VARIABLES=y
# CONFIG_LOG_BOOTLOADER_LEVEL_NONE is not set
# CONFIG_LOG_BOOTLOADER_LEVEL_ERROR is not set
# CONFIG_LOG_BOOTLOADER_LEVEL_WARN is not set
@@ -1148,6 +1165,7 @@ CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_RC=y
# CONFIG_DISABLE_BASIC_ROM_CONSOLE is not set
# CONFIG_NO_BLOBS is not set
# CONFIG_COMPATIBLE_PRE_V2_1_BOOTLOADERS is not set
# CONFIG_WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST is not set
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304
CONFIG_MAIN_TASK_STACK_SIZE=3584

File diff suppressed because it is too large Load Diff