Skip to content

Espressif IDF as a Component

Phil Schatzmann edited this page Jun 4, 2024 · 16 revisions

This project can be used as component with the Espressif IDF framework. This has the advantage that you can use all the features with all the latest corrections that Expressive has made available.

I assume you have the IDF framework already installed and sourced (see the official get started documentation ).

Here is a quick summary on how to set up a new project using the ESP32-A2DP

  • Create a new project (I used esp32-a2dp as project name)
idf.py create_project esp32-a2dp
cd esp32-a2dp
  • set the Target Processor type (esp32)
idf.py set-target esp32
  • Configure the Project

You can run idf.py menuconfig and activate Bluedroid and A2DP or add sdkconfig.defaults with the following content into the root of your project

CONFIG_BT_ENABLED=y
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=n
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=y
CONFIG_BTDM_CTRL_MODE_BTDM=n
CONFIG_BT_BLUEDROID_ENABLED=y
CONFIG_BT_CLASSIC_ENABLED=y
CONFIG_BT_A2DP_ENABLE=y
CONFIG_BT_SPP_ENABLED=y
CONFIG_BT_BLE_ENABLED=n
  • Add the "ESP32-A2DP" component to your project
mkdir components
cd components
git clone https://github.com/pschatzmann/ESP32-A2DP.git
  • Define your main program component in the main project directory

First we need to convert the generated esp32-a2dp.c into a cpp file

cd main
mv esp32-a2dp.c esp32-a2dp.cpp
  • Define the content of your main program esp32-a2dp.cpp. Example:
#include "BluetoothA2DPSink.h"

BluetoothA2DPSink a2dp_sink;

void setup(){
  a2dp_sink.start("MyMusic");  
}

void loop(){
   a2dp_sink.delay_ms( 500 ); // or use vTaskDelay()
}


extern "C" void app_main(void){
  setup();
  while(true){
    loop();
  }
}

  • Correct the CMakeLists.txt in main (if necessary) to consist of the following line!
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)

idf_component_register(SRCS ${app_sources})

  • Check your project: It should have the following structure now
- esp32-a2dp
  - CMakeLists.txt
  - sdkconfig.defaults (and or sdkconfig)
  - main
    - CMakeLists.txt
    - esp32-a2dp.cpp
  - components
    - ESP32-A2DP

  • Compile the project from the project root directory with
idf.py build
  • Deploy the project to your device by calling idf.py -p PORT [-b BAUD] flash

Example:

idf.py -p /dev/ttyUSB0 flash

  • Monitor the project to your device by calling idf.py -p PORT [-b BAUD] monitor

Example:

idf.py -p /dev/ttyUSB0 monitor

This example can also be found on Github.

Defining the Output

The CMakeList.txt in A2DP/src is defining the legacy I2S API to be used:

idf_component_register(
    SRC_DIRS src
    INCLUDE_DIRS src 
    REQUIRES bt esp_common freertos hal log nvs_flash driver
)

target_compile_options(${COMPONENT_LIB} PUBLIC -DA2DP_LEGACY_I2S_SUPPORT=1 -DA2DP_I2S_AUDIOTOOLS=0 -Wno-error -Wno-format -fpermissive)

If you want to use the output functionality with the AudioTools as output library, you will need something like the follows:

idf_component_register(
    SRC_DIRS "src"
    INCLUDE_DIRS src /home/pschatzmann/Development/Arduino/libraries/arduino-audio-tools/src
    REQUIRES bt esp_common freertos hal log nvs_flash driver
)

target_compile_options(${COMPONENT_LIB} PUBLIC -DA2DP_LEGACY_I2S_SUPPORT=0 -DA2DP_I2S_AUDIOTOOLS=1 -DESP32_CMAKE=1 -Wno-error -Wno-format -fpermissive)
  • The INCLUDE_DIRS must contain the src directory of the audio tools
  • A2DP_LEGACY_I2S_SUPPORT must be false and A2DP_I2S_AUDIOTOOLS must be true
  • Add DESP32_CMAKE=1 to tell the AudioTools that you compile with IDF w/o Arduino
  • You will also might need to adjust the partition table and/or remove the debug information to make the sketch fit with the help of some custom configuration by running menuconfig or pio run -t menuconfig if you are using PlatformIO
Clone this wiki locally