How to add paths that depend on selected framework version?

(This is an old question that didn’t get a response, edited for simplicity)

In the -I paths below, the ‘espidf’ part refer to the framework version used and should be changed manuall if a different framework version is selected.

Is there a way to define paths such that they will refer to the current framework in use? E.g. using a $FRAMEWORK_DIR like variable.

(These paths are required because ‘jump to definitions’ doesn’t work in stock ESP32 projects).

; Base
[env]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
debug_tool=esp-prog
upload_port=COM6
board_build.partitions = partitions_singleapp_large.csv
upload_speed=921600
; For IntelliSense.
build_flags =
    -I$PROJECT_CORE_DIR/packages/framework-espidf/components/driver
    -I$PROJECT_CORE_DIR/packages/framework-espidf/components/freertos

[env:release]
build_type = release

[env:debug]
build_type = debug

I have not found any answer to this question, so I would like to bump it.

I tried doing some extra_script scripting to find the used framework from the environment and add it to CPPPATH but I haven’t gotten that to work.

This should work. What is the code you’re attempting to make work?

First I see these variables when I run pio run -t envdump, but they are unfortunately empty.

'FRAMEWORKPATH': [],
'FRAMEWORKS': [],

I am trying to add a slightly different include for freertos so that I don’t have to change a library I use. I am trying two different ways currently.

env = DefaultEnvironment()  #global environment

from pathlib import Path

def patch_freertos_include():
    # The pio framework doesnt give us the usual freertos include dir, so we add it.
    # The strings use "/" when I envdumped CPPATH on windows so we use that too.
    for path in env["CPPPATH"]:
        if path.endswith("/components/freertos/include"):
            env.Append(CPPPATH=[f"{path}/freertos"])
            env["FRAMEWORK_ESPIDF_PATH"] = Path(path).parent.parent.parent.as_posix()
            print(f"Added {path}")
            break
    else:
        raise Exception("Could not patch freertos include")

# Doesn't seem to do anything even though the envdump has the patch
patch_freertos_include()

This looks right to me in envdump. The other item in CPPPATH is from my platformio.ini.

'FRAMEWORK_ESPIDF_PATH': 'C:/Users/patri/.platformio/packages/framework-espidf',
'CPPPATH': [..., '$FRAMEWORK_ESPIDF_PATH/components/freertos/include/freertos', ..., 'C:/Users/patri/.platformio/packages/framework-espidf/components/freertos/include/freertos', ...]
build_flags = ... -I$FRAMEWORK_ESPIDF_PATH/components/freertos/include/freertos ...

With pio run -v I can see this weird flag, but I haven’t gotten it to do what i want. $FRAMEWORK_ESPIDF_PATH seems to be replaced with “C:”

xtensa-esp32s3-elf-g++ ... -IC:/components/freertos/include/freertos ...

What’s the platformio.ini?

I quoted the part of build_flags that I thought relevant.

It’s a bit of a mess, especially since I’m in the middle of switching frameworks.

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html


[platformio]
default_envs = skh3

[env:skh3]
# PlatformIO Espressif32 6.4.0 (https://github.com/platformio/platform-espressif32/releases)
# uses ESP-IDF v5.1.1
platform = espressif32 @ ^6.4.0
; platform = https://github.com/platformio/platform-espressif32.git
framework = espidf, arduino
board = skh3
monitor_speed = 115200
monitor_raw = yes
; ESP32 will reset when disconnecting the monitor without this.
; Doesn't happen with framework = arduino. So there should be some way to fix it
; without needing this?
monitor_rts = 0
monitor_dtr = 0
debug_tool = esp-builtin
#debug_load_mode = manual
; sometimes failed at 96-100% with the default esptool.
; esptool uses USB-CDC-ACM. esp-builtin uses USB-JTAG.
upload_protocol = esp-builtin  
; debug by default so that you dont need to build twice 
; for debugging and other flashing.
build_type = debug

# To use the dedicated ESP-PROG programmer:
# debug_tool = esp-prog
#Enable to make the debugger break in setup()
; debug_init_break = tbreak main
#upload_protocol = esp-prog  #Optional, to upload via JTAG instead of USB
#debug_speed = 2000  #Fast speeds can fail with the 45cm cable

#upload_port = /dev/ttyACM1
#monitor_port = /dev/ttyACM1
; monitor_port = COM16

build_src_filter = 
  +<*>
  -<ssp_lib/chal/>
  +<ssp_lib/chal/esp32/>
  +<ssp_lib/chal/ssp_eeprom.c>
  +<ssp_lib/chal/printf.c>
  -<ssp_lib/test/>
  -<ssp_lib/unused/>
  -<ssp_lib/drivers/>
  -<emulator>
  -<.git/>
  +<../images/>

lib_deps =
  bmp5_api
  bodmer/TFT_eSPI@^2.5.31
  SPI
extra_scripts = 
  pre:support/extra_build.py
  support/extra_script.py

build_unflags = 
  -Werror=format
  -Werror
  -Werror=format-extra-args
;   -Wunused-variable

build_flags =
  -Os
  ; snprintf may truncate but will still add the '\0' so no need for warnings
  -Wno-format-truncation
  ; should warn on this
  -Wno-format
  -Wno-error=format-extra-args
  -Wno-error=format
  -Wno-error=format=
  -Wno-error=comment
  -Wno-comment
  -Wno-unused-value
  -Wno-maybe-uninitialized
  ; ESP-IDF + Arduino configuration
  -DARDUINO_USB_MODE=1
  -DARDUINO_USB_CDC_ON_BOOT=1

  ; difference in includes: "freertos/FreeRTOS.h" vs "FreeRTOS.h"
  -I "$PROJECT_PACKAGES_DIR\framework-espidf\components\freertos\include\freertos"
  ; Ideally should look more like this 
;   -I$FRAMEWORK_ESPIDF_PATH/components/freertos/include/freertos

  # Configure TFT_eSPI
  -DUSER_SETUP_LOADED=1
  -DST7789_DRIVER=1
  -DTFT_WIDTH=240
  -DTFT_HEIGHT=320
  -DTFT_MISO=13  #"Q"
  -DTFT_MOSI=11  #"D"
  -DTFT_SCLK=12
  -DTFT_CS=10
  -DTFT_DC=14
  -DTFT_RST=-1  #Actually connected at GPIO expander P2_7
  ;-DTFT_BL=21
  ;-DTOUCH_CS=22
  ;-DLOAD_GLCD=1
  ;-DLOAD_FONT2=1
  ;-DLOAD_FONT4=1
  ;-DLOAD_FONT6=1
  ;-DLOAD_FONT7=1
  ;-DLOAD_FONT8=1
  ;-DLOAD_GFXFF=1
  #-DSMOOTH_FONT=1  #Requires SPIFFS
  -DSPI_FREQUENCY=40000000
  # Max read 6 Mhz? https://github.com/Bodmer/TFT_eSPI/issues/1800
  -DSPI_READ_FREQUENCY=6000000
  -DDISABLE_ALL_LIBRARY_WARNINGS  #Removes warning that touch is not enabled
  -DTFT_INVERSION_OFF
  -DTFT_RGB_ORDER=TFT_BGR

  #-DLFS_YES_TRACE

  -DCONFIG_TINYUSB=y
  -DCONFIG_TINYUSB_ENABLED=y
  -DCONFIG_TINYUSB_CDC_ENABLED=y

  #Project-specific code:
  -DCOTASK_PORT_FREERTOS
  -DHW_REV_A
  -Isrc/ssp_lib/co/freertos/
  -Iinclude 
  -Isrc/ssp_lib
  -Isrc/generated
  #To avoid warning:
  -Wl,--undefined=uxTopUsedPriority
  #Define to init uninitialized extension cards (or in extension.cpp)
  #-DINIT_EXTENSION_FN=Skh3Test

  #For debug:
  -DportHAS_STACK_OVERFLOW_CHECKING=2
  -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG

#board_build.filesystem = spiffs  #Default
board_build.filesystem = littlefs  #This could be better for user data?

Can you just use this as your script content.

from os.path import join
env = DefaultEnvironment()
platform = env.PioPlatform()
FRAMEWORK_DIR = platform.get_package_dir("framework-espidf")

env.Append(
    CPPPATH=[
        join(FRAMEWORK_DIR, "components", "freertos", "include", "freertos")
    ]
)
1 Like

That script is much better thanks.

I got it to work, I had the the script as a normal post extra_script, changing it to pre fixed the issue.