Looking for advice on efficient code for a lot of moving parts

Hello,
I wanted to reach out to see if anyone had advice on how to work on code with a lot of different things happening in regards to timing, speed and size. I'm having a bit of trouble finding a good work flow to be able to monitor where the bottlenecks are and what types of methods are slowing things down. Right now, I'm asking a lot of this ESP32-WROVER-16MB. I'm trying to use three pins for analogRead, writing certain readings into LittleSPIFFS, SPI port connected along with three digital pins to a VS1053 streaming audio from an icecast2 stream out through headphones. I'm using a WiFi manager library with a captive portal and a short TCP connection at the beginning and end of the program to send small bits of data to my server e.g. the mac address, millis() to track time, etc. I'm wondering for example, whether there will be a significant difference in using nested if statements as opposed to a switch case, how much resources the WiFi manager is using and while I've been isolating each section of code, I'm still having a hard time locating the culprit. I was also wondering if anyone has any advice on ways to best utilize SPRAM in this type of situation. I read on the ESP32 forum that the Arduino compiler will only allocate space in SPRAM if it is consecutive and over 16kB. Since there is audio flowing in real time from the server, my whole mess is very timing sensitive so any advice is greatly appreciated. Thanks for reading!

Why not use the built in OS, freeRTOS, that comes with an ESP32?

Don't you mean PSRAM?

Yes thanks. I do mean PSRAM. I've tried messing around with some of the freeRTOS methods that were ported to the Arduino IDE like pinning tasks to specific cores, etc but I have the suspicion that my issue is ram related, not processing, although I could absolutely be wrong. I know even less about coding for freeRTOS with the ESP-IDF than I do about getting things working in the Arduino IDE, but if it is not possible with Arduino, I'll take the plunge if I have to. I just figured that there might be some options from the Arduino IDE world.

I use freeRTOS under the Arduino IDE.

I've used PSRAM under the Arduino IDE. The Arduino IDE is limited to using only 4MB of PSRAM.

If there is something to gain there usually the optimizer does it for you.

What’s the problem you face exactly. Crash? Latency?

Thanks. Thats all good to know. I will see if I can sort out some of the freeRTOS stuff to get a better glimpse into what is going on. If I can use more of the hardware without the Arduino IDE, then I guess I've got even more of a learning curve in front me....

The problem that I'm facing is that I cannot effectively receive the icecast2 stream over http, stick it in a ring buffer and output it at intervals that the vs1053 is able to handle in any consistent way with all the other things going on. In other words, my stream stutters at seemingly random intervals and seemingly random durations. I have a few sanity check sketches that I use which include an example from the Adafruit VS1053 library and an example from the ESP_VS1053 library. They are alright on their own. I've got my sketch arranged so that the handling of the icecast2 stream is prioritized and other methods are only called when the buffer has been emptied and there isn't another batch of stream data yet. Unfortunately it never gets that far because with the added weight of LittleFS, WiFi Manager and the rest, I can't even play a stream without it breaking up.

Using the ESP32 and the official Espressif core for Arduino, you are using FreeRTOS in Espressif's default configuration: core_0 is dedicated to the Espressif RF stacks, etc. Core_1 is dedicated to Arduino.

Yea, You are writing to simulated EEPROM. You need external storage, something fast. Maybe FRAM or CMOS RAM.

Maybe review what others have done.
Search VS1053 + ESP32

The ESP32 has a RingBuffer that operates in the background and can do things upon presets.

The ESP32's ring buffer works under the Arduino IDE.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/freertos_additions.html#ring-buffers

I've used PSRAM to store the ringbuffer data into.

Hi, @ifiififiifiif

I'm sorry but I had to spread your post to make it easier to read.
Can I suggest you post in point form rather than continuous sentences.

Tom... :smiley: :+1: :coffee: :australia:

further editing :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.