[ESP32] Update esp-idf to v5.1 release (#28326)

* [ESP32] Update esp-idf to v5.1 release

* Fix the requirements for v5.1

* Fix the pyparsing version and explaining comment

* Update the container image to use the latest one

* remove gdbgui from few more places

* fixup constraints for pyparsing

* Fix kconfig module

* Fix the esp32 qemu

* Fix freertos path

* Fix format specifier in esp32 pigweed logger

* Restyled by clang-format

* Fix the shell app

* Move freertos function from IRAM to flash for chef app

* Fixes for building all-clusters-app with Target m5stack.

Also, IRAM usage shot up so moved the freertos functions to flash

* Changes to fix all-clusters-minimal app

* Remove gdbgui references

* Changes in lighting-app

* Examples supporting display must build for non esp32 targets

* Pull in few dram optimizations from temperature app

* Reduce the critical logging buffer

---------

Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/.github/workflows/chef.yaml b/.github/workflows/chef.yaml
index 35d62da..76bbb9c 100644
--- a/.github/workflows/chef.yaml
+++ b/.github/workflows/chef.yaml
@@ -54,7 +54,7 @@
         if: github.actor != 'restyled-io[bot]'
 
         container:
-            image: ghcr.io/project-chip/chip-build-esp32:1
+            image: ghcr.io/project-chip/chip-build-esp32:2
             options: --user root
 
         steps:
@@ -64,8 +64,6 @@
               uses: ./.github/actions/checkout-submodules-and-bootstrap
               with:
                 platform: esp32
-            - name: Fixup GdbGui requirement
-              run: perl -i -pe 's/^gdbgui==/# gdbgui==/' /opt/espressif/esp-idf/requirements.txt
             - name: CI Examples ESP32
               shell: bash
               run: |
diff --git a/.github/workflows/examples-esp32.yaml b/.github/workflows/examples-esp32.yaml
index 99c74c8..3757d95 100644
--- a/.github/workflows/examples-esp32.yaml
+++ b/.github/workflows/examples-esp32.yaml
@@ -34,7 +34,7 @@
         if: github.actor != 'restyled-io[bot]'
 
         container:
-            image: ghcr.io/project-chip/chip-build-esp32:1
+            image: ghcr.io/project-chip/chip-build-esp32:2
             volumes:
                 - "/tmp/bloat_reports:/tmp/bloat_reports"
 
@@ -46,9 +46,6 @@
               with:
                 platform: esp32
 
-            - name: Fixup GdbGui requirement
-              run: perl -i -pe 's/^gdbgui==/# gdbgui==/' /opt/espressif/esp-idf/requirements.txt
-
             - name: Set up environment for size reports
               uses: ./.github/actions/setup-size-reports
               if: ${{ !env.ACT }}
@@ -130,7 +127,7 @@
         if: github.actor != 'restyled-io[bot]'
 
         container:
-            image: ghcr.io/project-chip/chip-build-esp32:1
+            image: ghcr.io/project-chip/chip-build-esp32:2
             volumes:
                 - "/tmp/bloat_reports:/tmp/bloat_reports"
 
@@ -142,9 +139,6 @@
               with:
                 platform: esp32
 
-            - name: Fixup GdbGui requirement
-              run: perl -i -pe 's/^gdbgui==/# gdbgui==/' /opt/espressif/esp-idf/requirements.txt
-
             - name: Build example Bridge App
               run: scripts/examples/esp_example.sh bridge-app
 
diff --git a/.github/workflows/qemu.yaml b/.github/workflows/qemu.yaml
index 078b4dd..13badfc 100644
--- a/.github/workflows/qemu.yaml
+++ b/.github/workflows/qemu.yaml
@@ -38,7 +38,7 @@
         if: github.actor != 'restyled-io[bot]'
 
         container:
-            image: ghcr.io/project-chip/chip-build-esp32-qemu:1
+            image: ghcr.io/project-chip/chip-build-esp32-qemu:2
             volumes:
                 - "/tmp/log_output:/tmp/test_logs"
 
@@ -50,9 +50,6 @@
               with:
                 platform: esp32
 
-            - name: Fixup GdbGui requirement
-              run: perl -i -pe 's/^gdbgui==/# gdbgui==/' /opt/espressif/esp-idf/requirements.txt
-
             - name: Build ESP32 QEMU test images
               run: |
                   scripts/run_in_build_env.sh "         \
diff --git a/.github/workflows/release_artifacts.yaml b/.github/workflows/release_artifacts.yaml
index a6c14d8..5cbcc9b 100644
--- a/.github/workflows/release_artifacts.yaml
+++ b/.github/workflows/release_artifacts.yaml
@@ -32,7 +32,7 @@
         runs-on: ubuntu-latest
 
         container:
-            image: ghcr.io/project-chip/chip-build-esp32:1
+            image: ghcr.io/project-chip/chip-build-esp32:2
 
         steps:
             - name: Checkout
diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt
index d35791c..dfda628 100644
--- a/config/esp32/components/chip/CMakeLists.txt
+++ b/config/esp32/components/chip/CMakeLists.txt
@@ -462,26 +462,11 @@
     list(APPEND chip_libraries "${esp_phy_dir}/lib/${target_name}/lib${phy_blob}.a")
 endforeach()
 
-idf_component_get_property(esp_event_lib esp_event COMPONENT_LIB)
-list(APPEND chip_libraries $<TARGET_FILE:${esp_event_lib}>)
-
-idf_component_get_property(hal_lib hal COMPONENT_LIB)
-list(APPEND chip_libraries $<TARGET_FILE:${hal_lib}>)
-
-idf_component_get_property(esp_system_lib esp_system COMPONENT_LIB)
-list(APPEND chip_libraries $<TARGET_FILE:${esp_system_lib}>)
-
-idf_component_get_property(soc_lib soc COMPONENT_LIB)
-list(APPEND chip_libraries $<TARGET_FILE:${soc_lib}>)
-
-idf_component_get_property(efuse_lib efuse COMPONENT_LIB)
-list(APPEND chip_libraries $<TARGET_FILE:${efuse_lib}>)
-
-idf_component_get_property(vfs_lib vfs COMPONENT_LIB)
-list(APPEND chip_libraries $<TARGET_FILE:${vfs_lib}>)
-
-idf_component_get_property(driver_lib driver COMPONENT_LIB)
-list(APPEND chip_libraries $<TARGET_FILE:${driver_lib}>)
+set(components_to_link esp_event hal esp_system soc efuse vfs driver esp_coex freertos)
+foreach(component ${components_to_link})
+    idf_component_get_property(lib_name ${component} COMPONENT_LIB)
+    list(APPEND chip_libraries $<TARGET_FILE:${lib_name}>)
+endforeach()
 
 target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group
                                                 ${chip_libraries}
diff --git a/docs/guides/esp32/setup_idf_chip.md b/docs/guides/esp32/setup_idf_chip.md
index ff5ee40..ef57e61 100644
--- a/docs/guides/esp32/setup_idf_chip.md
+++ b/docs/guides/esp32/setup_idf_chip.md
@@ -13,42 +13,32 @@
 
 ### Install Prerequisites
 
--   [Linux](https://docs.espressif.com/projects/esp-idf/en/v4.4.4/esp32/get-started/linux-setup.html)
--   [macOS](https://docs.espressif.com/projects/esp-idf/en/v4.4.4/esp32/get-started/macos-setup.html)
+-   [Linux](https://docs.espressif.com/projects/esp-idf/en/v5.1/esp32/get-started/linux-macos-setup.html#for-linux-users)
+-   [macOS](https://docs.espressif.com/projects/esp-idf/en/v5.1/esp32/get-started/linux-macos-setup.html#for-macos-users)
 
-### Get IDF v4.4.4
+### Get IDF v5.1
 
 -   Clone ESP-IDF
-    [v4.4.4 release](https://github.com/espressif/esp-idf/releases/tag/v4.4.4)
+    [v5.1 release](https://github.com/espressif/esp-idf/releases/tag/v5.1)
 
     ```
-    $ git clone -b v4.4.4 --recursive https://github.com/espressif/esp-idf.git
+    $ git clone -b v5.1 --recursive --depth 1 --shallow-submodule https://github.com/espressif/esp-idf.git
     $ cd esp-idf
     $ ./install.sh
     ```
 
--   To update an existing esp-idf toolchain to v4.4.4:
+-   To update an existing esp-idf toolchain to v5.1:
 
     ```
     $ cd path/to/esp-idf
     $ git fetch origin
-    $ git checkout v4.4.4
-    $ git reset --hard origin/v4.4.4
+    $ git checkout v5.1
+    $ git reset --hard origin/v5.1
     $ git submodule update --recursive --init
     $ git clean -fdx
     $ ./install.sh
     ```
 
--   For ESP32C6 & ESP32H2, please use commit
-    [ea5e0ff](https://github.com/espressif/esp-idf/tree/ea5e0ff).
-
-    ```
-    $ cd esp-idf
-    $ git checkout ea5e0ff
-    $ git submodule update --init
-    $ ./install.sh
-    ```
-
 ## Setup Matter environment
 
 ### Install Prerequisites
@@ -80,22 +70,6 @@
 python3 -m pip install esptool
 ```
 
-For MacOS, `gdbgui` python package will not be installed using `bootstrap.sh`
-script as it is restricted only for x64 Linux platforms. It is restricted
-because, building wheels for `gevent` (dependency of `gdbgui`) fails on MacOS.
-
-For ARM-based Mac, no further installation steps are necessary if Python3
-version is greater than or equal to 3.11.
-
-If Python3 version is less than 3.11 or you are using x86(Intel-based) Mac then
-please run the below commands after every bootstrapping to install gdbgui wheels
-as binary
-
-```
-python3 -m pip install -c scripts/setup/constraints.txt --no-cache --prefer-binary gdbgui==0.13.2.0
-deactivate
-```
-
 ---
 
 Once IDF and Matter environment is set up, head over to
diff --git a/examples/all-clusters-app/esp32/CMakeLists.txt b/examples/all-clusters-app/esp32/CMakeLists.txt
index dc92f0c..d5508c4 100644
--- a/examples/all-clusters-app/esp32/CMakeLists.txt
+++ b/examples/all-clusters-app/esp32/CMakeLists.txt
@@ -26,12 +26,11 @@
 
 set(EXTRA_COMPONENT_DIRS
     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components"
-    "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/QRCode"
+    "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common"
 )
+
 if(${IDF_TARGET} STREQUAL "esp32")
-    list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components/tft"
-                                     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components/spidriver"
-                                     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/screen-framework")
+    list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components")
 endif()
 
 project(chip-all-clusters-app)
diff --git a/examples/all-clusters-app/esp32/main/CMakeLists.txt b/examples/all-clusters-app/esp32/main/CMakeLists.txt
index 1401533..1b4b6ff 100644
--- a/examples/all-clusters-app/esp32/main/CMakeLists.txt
+++ b/examples/all-clusters-app/esp32/main/CMakeLists.txt
@@ -110,8 +110,14 @@
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/lib/support"
-                     "${IDF_PATH}/components/freertos/include/freertos"
 )
+
+if (${IDF_VERSION_MAJOR} LESS 5)
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/include/freertos")
+else()
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos")
+endif()
+
 set(SRC_DIRS_LIST  "${SRC_DIRS_LIST}"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
@@ -119,9 +125,10 @@
 )
 endif (CONFIG_ENABLE_PW_RPC)
 
-set(PRIV_REQUIRES_LIST chip QRCode bt driver app_update nvs_flash spi_flash openthread)
-if(CONFIG_HAVE_DISPLAY)
-    list(APPEND PRIV_REQUIRES_LIST tft spidrier screen-framework)
+set(PRIV_REQUIRES_LIST chip QRCode bt app_update nvs_flash spi_flash openthread)
+
+if(${IDF_TARGET} STREQUAL "esp32")
+    list(APPEND PRIV_REQUIRES_LIST spidriver screen-framework)
 endif()
 
 if(CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM)
diff --git a/examples/all-clusters-app/esp32/sdkconfig.defaults b/examples/all-clusters-app/esp32/sdkconfig.defaults
index d824c99..c3b1092 100644
--- a/examples/all-clusters-app/esp32/sdkconfig.defaults
+++ b/examples/all-clusters-app/esp32/sdkconfig.defaults
@@ -66,3 +66,6 @@
 # This example uses the older version of RMT driver to work with both
 # idf-v4.4.3 and idf-v5.0, so supressing the warnings by setting below option
 CONFIG_RMT_SUPPRESS_DEPRECATE_WARN=y
+
+# Move functions from IRAM to flash
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
diff --git a/examples/all-clusters-app/esp32/sdkconfig_c3devkit.defaults b/examples/all-clusters-app/esp32/sdkconfig_c3devkit.defaults
index 24d4fe7..8d7455d 100644
--- a/examples/all-clusters-app/esp32/sdkconfig_c3devkit.defaults
+++ b/examples/all-clusters-app/esp32/sdkconfig_c3devkit.defaults
@@ -50,3 +50,6 @@
 
 # Enable HKDF in mbedtls
 CONFIG_MBEDTLS_HKDF_C=y
+
+# Move functions from IRAM to flash
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
diff --git a/examples/all-clusters-app/esp32/sdkconfig_m5stack.defaults b/examples/all-clusters-app/esp32/sdkconfig_m5stack.defaults
index a7d058b..287262d 100644
--- a/examples/all-clusters-app/esp32/sdkconfig_m5stack.defaults
+++ b/examples/all-clusters-app/esp32/sdkconfig_m5stack.defaults
@@ -70,3 +70,6 @@
 
 # Build chip tests
 CONFIG_BUILD_CHIP_TESTS=y
+
+# Move functions from IRAM to flash
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
diff --git a/examples/all-clusters-app/esp32/sdkconfig_m5stack_rpc.defaults b/examples/all-clusters-app/esp32/sdkconfig_m5stack_rpc.defaults
index e0cf1a0..213d44f 100644
--- a/examples/all-clusters-app/esp32/sdkconfig_m5stack_rpc.defaults
+++ b/examples/all-clusters-app/esp32/sdkconfig_m5stack_rpc.defaults
@@ -74,3 +74,9 @@
 
 # Build chip tests
 CONFIG_BUILD_CHIP_TESTS=y
+
+# Move functions from IRAM to flash
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
+
+# to avoid dram overflow, reduce the critical loggin buffer to 1K
+CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE=1024
diff --git a/examples/all-clusters-minimal-app/esp32/CMakeLists.txt b/examples/all-clusters-minimal-app/esp32/CMakeLists.txt
index 0d6aa7b..0af660c 100644
--- a/examples/all-clusters-minimal-app/esp32/CMakeLists.txt
+++ b/examples/all-clusters-minimal-app/esp32/CMakeLists.txt
@@ -26,12 +26,11 @@
 
 set(EXTRA_COMPONENT_DIRS
     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components"
-    "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/QRCode"
+    "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common"
 )
+
 if(${IDF_TARGET} STREQUAL "esp32")
-    list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components/tft"
-                                     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components/spidriver"
-                                     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/screen-framework")
+    list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components")
 endif()
 
 project(chip-all-clusters-minimal-app)
diff --git a/examples/all-clusters-minimal-app/esp32/main/CMakeLists.txt b/examples/all-clusters-minimal-app/esp32/main/CMakeLists.txt
index c445c54..3358292 100644
--- a/examples/all-clusters-minimal-app/esp32/main/CMakeLists.txt
+++ b/examples/all-clusters-minimal-app/esp32/main/CMakeLists.txt
@@ -100,8 +100,14 @@
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/lib/support"
-                     "${IDF_PATH}/components/freertos/include/freertos"
 )
+
+if (${IDF_VERSION_MAJOR} LESS 5)
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/include/freertos")
+else()
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos")
+endif()
+
 set(SRC_DIRS_LIST  "${SRC_DIRS_LIST}"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
@@ -110,8 +116,9 @@
 endif (CONFIG_ENABLE_PW_RPC)
 
 set(PRIV_REQUIRES_LIST chip QRCode bt driver app_update nvs_flash spi_flash openthread)
-if(CONFIG_HAVE_DISPLAY)
-    list(APPEND PRIV_REQUIRES_LIST tft spidrier screen-framework)
+
+if(${IDF_TARGET} STREQUAL "esp32")
+    list(APPEND PRIV_REQUIRES_LIST spidriver screen-framework)
 endif()
 
 if(CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM)
diff --git a/examples/all-clusters-minimal-app/esp32/partitions.csv b/examples/all-clusters-minimal-app/esp32/partitions.csv
index 7d399d0..47172e2 100644
--- a/examples/all-clusters-minimal-app/esp32/partitions.csv
+++ b/examples/all-clusters-minimal-app/esp32/partitions.csv
@@ -3,6 +3,6 @@
 nvs,      data, nvs,     ,        0xC000,
 otadata,  data, ota,     ,        0x2000,
 phy_init, data, phy,     ,        0x1000,
-ota_0,    app,  ota_0,   ,        1500K,
-ota_1,    app,  ota_1,   ,        1500K,
+ota_0,    app,  ota_0,   ,        1900K,
+ota_1,    app,  ota_1,   ,        1900K,
 ot_storage, data, 0x3a,            , 0x2000,
diff --git a/examples/all-clusters-minimal-app/esp32/sdkconfig.defaults b/examples/all-clusters-minimal-app/esp32/sdkconfig.defaults
index dcceac3..fda02fb 100644
--- a/examples/all-clusters-minimal-app/esp32/sdkconfig.defaults
+++ b/examples/all-clusters-minimal-app/esp32/sdkconfig.defaults
@@ -66,3 +66,6 @@
 
 # Enable HKDF in mbedtls
 CONFIG_MBEDTLS_HKDF_C=y
+
+# Move functions from IRAM to flash
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
diff --git a/examples/all-clusters-minimal-app/esp32/sdkconfig_c3devkit.defaults b/examples/all-clusters-minimal-app/esp32/sdkconfig_c3devkit.defaults
index 24d4fe7..8d7455d 100644
--- a/examples/all-clusters-minimal-app/esp32/sdkconfig_c3devkit.defaults
+++ b/examples/all-clusters-minimal-app/esp32/sdkconfig_c3devkit.defaults
@@ -50,3 +50,6 @@
 
 # Enable HKDF in mbedtls
 CONFIG_MBEDTLS_HKDF_C=y
+
+# Move functions from IRAM to flash
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
diff --git a/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack.defaults b/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack.defaults
index a7d058b..287262d 100644
--- a/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack.defaults
+++ b/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack.defaults
@@ -70,3 +70,6 @@
 
 # Build chip tests
 CONFIG_BUILD_CHIP_TESTS=y
+
+# Move functions from IRAM to flash
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
diff --git a/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack_rpc.defaults b/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack_rpc.defaults
index e0cf1a0..213d44f 100644
--- a/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack_rpc.defaults
+++ b/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack_rpc.defaults
@@ -74,3 +74,9 @@
 
 # Build chip tests
 CONFIG_BUILD_CHIP_TESTS=y
+
+# Move functions from IRAM to flash
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
+
+# to avoid dram overflow, reduce the critical loggin buffer to 1K
+CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE=1024
diff --git a/examples/chef/esp32/sdkconfig.defaults b/examples/chef/esp32/sdkconfig.defaults
index ca2cd09..bd714ab 100644
--- a/examples/chef/esp32/sdkconfig.defaults
+++ b/examples/chef/esp32/sdkconfig.defaults
@@ -59,3 +59,6 @@
 
 # Enable HKDF in mbedtls
 CONFIG_MBEDTLS_HKDF_C=y
+
+# IRAM optimizations
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
diff --git a/examples/chef/esp32/sdkconfig_rpc.defaults b/examples/chef/esp32/sdkconfig_rpc.defaults
index caac021..a1506dd 100644
--- a/examples/chef/esp32/sdkconfig_rpc.defaults
+++ b/examples/chef/esp32/sdkconfig_rpc.defaults
@@ -58,3 +58,6 @@
 
 # Enable HKDF in mbedtls
 CONFIG_MBEDTLS_HKDF_C=y
+
+# IRAM optimizations
+CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
diff --git a/examples/common/m5stack-tft/repo b/examples/common/m5stack-tft/repo
index 3344a81..a6299b6 160000
--- a/examples/common/m5stack-tft/repo
+++ b/examples/common/m5stack-tft/repo
@@ -1 +1 @@
-Subproject commit 3344a8105f8dfecb95832f369733cf6f863a1662
+Subproject commit a6299b6c7c0b2e3eb62fa08ee4bf7155c39bad1f
diff --git a/examples/common/screen-framework/CMakeLists.txt b/examples/common/screen-framework/CMakeLists.txt
index 5b5739e..af30445 100644
--- a/examples/common/screen-framework/CMakeLists.txt
+++ b/examples/common/screen-framework/CMakeLists.txt
@@ -1,6 +1,12 @@
+set(require_list chip driver)
+
+if(${IDF_TARGET} STREQUAL "esp32")
+    list(APPEND require_list tft)
+endif()
+
 idf_component_register(SRCS "Display.cpp" 
                             "ScreenManager.cpp" 
                             "Screen.cpp" 
                             "ListScreen.cpp"
                        INCLUDE_DIRS "include"
-                       PRIV_REQUIRES tft chip driver)
+                       REQUIRES ${require_list})
diff --git a/examples/lighting-app/esp32/CMakeLists.txt b/examples/lighting-app/esp32/CMakeLists.txt
index b37c815..356a3cd 100644
--- a/examples/lighting-app/esp32/CMakeLists.txt
+++ b/examples/lighting-app/esp32/CMakeLists.txt
@@ -26,15 +26,14 @@
 
 set(EXTRA_COMPONENT_DIRS
     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components"
-    "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/QRCode"
+    "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common"
 )
 
 if(${IDF_TARGET} STREQUAL "esp32")
-    list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components/tft"
-                                     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components/spidriver"
-                                     "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/screen-framework")
+    list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components")
 endif()
 
+
 project(chip-lighting-app)
 
 # WARNING: This is just an example for using key for decrypting the encrypted OTA image
diff --git a/examples/lighting-app/esp32/main/CMakeLists.txt b/examples/lighting-app/esp32/main/CMakeLists.txt
index b6e2969..fcb0121 100644
--- a/examples/lighting-app/esp32/main/CMakeLists.txt
+++ b/examples/lighting-app/esp32/main/CMakeLists.txt
@@ -69,6 +69,10 @@
 
 set(PRIV_REQUIRES_LIST chip QRCode bt led_strip app_update openthread driver nvs_flash spi_flash)
 
+if(${IDF_TARGET} STREQUAL "esp32")
+    list(APPEND PRIV_REQUIRES_LIST spidriver screen-framework)
+endif()
+
 if (CONFIG_ENABLE_PW_RPC)
 # Append additional directories for RPC build
 set(PRIV_INCLUDE_DIRS_LIST  "${PRIV_INCLUDE_DIRS_LIST}"
@@ -77,8 +81,14 @@
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/lib/support"
-                     "${IDF_PATH}/components/freertos/include/freertos"
 )
+
+if (${IDF_VERSION_MAJOR} LESS 5)
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/include/freertos")
+else()
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos")
+endif()
+
 set(SRC_DIRS_LIST  "${SRC_DIRS_LIST}"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
@@ -86,10 +96,6 @@
 )
 endif (CONFIG_ENABLE_PW_RPC)
 
-if (CONFIG_HAVE_DISPLAY)
-    list(APPEND PRIV_REQUIRES_LIST tft screen-framework)
-endif()
-
 idf_component_register(PRIV_INCLUDE_DIRS ${PRIV_INCLUDE_DIRS_LIST}
                        SRC_DIRS ${SRC_DIRS_LIST}
                        PRIV_REQUIRES ${PRIV_REQUIRES_LIST})
diff --git a/examples/lock-app/esp32/main/CMakeLists.txt b/examples/lock-app/esp32/main/CMakeLists.txt
index bf9edad..dc5fa50 100644
--- a/examples/lock-app/esp32/main/CMakeLists.txt
+++ b/examples/lock-app/esp32/main/CMakeLists.txt
@@ -19,6 +19,13 @@
 get_filename_component(CHIP_ROOT ${CMAKE_SOURCE_DIR}/third_party/connectedhomeip REALPATH)
 
 if (CONFIG_ENABLE_PW_RPC)
+
+if (${IDF_VERSION_MAJOR} LESS 5)
+    set(freertos_include_path "${IDF_PATH}/components/freertos/include/freertos")
+else()
+    set(freertos_include_path "${IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos")
+endif()
+
 idf_component_register(INCLUDE_DIRS
                      "${CMAKE_CURRENT_LIST_DIR}"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/pw_sys_io/public"
@@ -30,7 +37,7 @@
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/lib/support"
                      "${CMAKE_CURRENT_LIST_DIR}/include"
-                     "${IDF_PATH}/components/freertos/include/freertos"
+                     "${freertos_include_path}"
                       SRC_DIRS
                      "${CMAKE_CURRENT_LIST_DIR}"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
diff --git a/examples/ota-requestor-app/esp32/main/CMakeLists.txt b/examples/ota-requestor-app/esp32/main/CMakeLists.txt
index 6a3633f..10c61e1 100644
--- a/examples/ota-requestor-app/esp32/main/CMakeLists.txt
+++ b/examples/ota-requestor-app/esp32/main/CMakeLists.txt
@@ -72,8 +72,14 @@
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/lib/support"
-                     "${IDF_PATH}/components/freertos/include/freertos"
 )
+
+if (${IDF_VERSION_MAJOR} LESS 5)
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/include/freertos")
+else()
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos")
+endif()
+
 set(SRC_DIRS_LIST  "${SRC_DIRS_LIST}"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
diff --git a/examples/pigweed-app/esp32/main/CMakeLists.txt b/examples/pigweed-app/esp32/main/CMakeLists.txt
index 005f587..e06bb5a 100644
--- a/examples/pigweed-app/esp32/main/CMakeLists.txt
+++ b/examples/pigweed-app/esp32/main/CMakeLists.txt
@@ -14,6 +14,12 @@
 #    See the License for the specific language governing permissions and
 #    limitations under the License.
 
+if (${IDF_VERSION_MAJOR} LESS 5)
+    set(freertos_include_path "${IDF_PATH}/components/freertos/include/freertos")
+else()
+    set(freertos_include_path "${IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos")
+endif()
+
 idf_component_register(INCLUDE_DIRS
                      "${CMAKE_CURRENT_LIST_DIR}"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/pw_sys_io/public"
@@ -21,7 +27,7 @@
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/lib/support"
-                     "${IDF_PATH}/components/freertos/include/freertos"
+                     "${freertos_include_path}"
 
                       SRC_DIRS
                      "${CMAKE_CURRENT_LIST_DIR}"
diff --git a/examples/platform/esp32/PigweedLogger.cpp b/examples/platform/esp32/PigweedLogger.cpp
index e7bb260..d10b89b 100644
--- a/examples/platform/esp32/PigweedLogger.cpp
+++ b/examples/platform/esp32/PigweedLogger.cpp
@@ -138,7 +138,7 @@
         PigweedLogger::putString(logColor, strlen(logColor));
 
         char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
-        size_t len = snprintf(formattedMsg, sizeof formattedMsg, " (%u) %s: ", esp_log_timestamp(), tag);
+        size_t len = snprintf(formattedMsg, sizeof formattedMsg, " (%" PRIu32 ") %s: ", esp_log_timestamp(), tag);
         PigweedLogger::putString(formattedMsg, len);
 
         memset(formattedMsg, 0, sizeof formattedMsg);
diff --git a/examples/shell/esp32/main/CMakeLists.txt b/examples/shell/esp32/main/CMakeLists.txt
index 8fac151..5a565ed 100644
--- a/examples/shell/esp32/main/CMakeLists.txt
+++ b/examples/shell/esp32/main/CMakeLists.txt
@@ -23,4 +23,4 @@
                       "${CHIP_SHELL_DIR}/shell_common/globals.cpp"
                       PRIV_INCLUDE_DIRS
                       "${CHIP_SHELL_DIR}/shell_common/include"
-                      PRIV_REQUIRES chip nvs_flash bt console)
+                      PRIV_REQUIRES chip nvs_flash bt)
diff --git a/examples/temperature-measurement-app/esp32/main/CMakeLists.txt b/examples/temperature-measurement-app/esp32/main/CMakeLists.txt
index 6136d15..06b274e 100644
--- a/examples/temperature-measurement-app/esp32/main/CMakeLists.txt
+++ b/examples/temperature-measurement-app/esp32/main/CMakeLists.txt
@@ -64,8 +64,14 @@
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/lib/support"
-                     "${IDF_PATH}/components/freertos/include/freertos"
 )
+
+if (${IDF_VERSION_MAJOR} LESS 5)
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/include/freertos")
+else()
+    list(APPEND PRIV_INCLUDE_DIRS_LIST "${IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos")
+endif()
+
 set(SRC_DIRS_LIST  "${SRC_DIRS_LIST}"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32"
                      "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed"
diff --git a/scripts/setup/constraints.txt b/scripts/setup/constraints.txt
index 5573d0b..fa15e9d 100644
--- a/scripts/setup/constraints.txt
+++ b/scripts/setup/constraints.txt
@@ -248,7 +248,7 @@
     # via west
 pylink-square==0.5.0
     # via bflb-iot-tool
-pyparsing==2.3.1
+pyparsing==3.0.9
     # via -r requirements.esp32.txt
 pypng==0.0.21
     # via -r requirements.telink.txt
diff --git a/scripts/setup/requirements.esp32.txt b/scripts/setup/requirements.esp32.txt
index f688b17..d0d6d92 100644
--- a/scripts/setup/requirements.esp32.txt
+++ b/scripts/setup/requirements.esp32.txt
@@ -1,18 +1,15 @@
 click>=7.0
 future>=0.15.2
-pyparsing>=2.0.3,<2.4.0
+# pyparsing: Min version was set based on https://github.com/pyparsing/pyparsing/issues/319
+# pyparsing: Max version was set to avoid breaking changes
+pyparsing>=3.0.3,<3.1
 idf-component-manager
 pygdbmi<=0.9.0.2
 reedsolo>=1.5.3,<=1.5.4
 bitstring>=3.1.6,<4
 ecdsa>=0.16.0
-kconfiglib==13.7.1
 construct==2.10.54
 python-socketio<5
 itsdangerous<2.1 ; python_version < "3.11"
-#
-# gdbgui pulls in gevent which fails to compile due to cython updates.
-# Could not find a good way to fix this dependency, so commenting it
-# out here.
-#
-# gdbgui==0.13.2.0 ; python_version < "3.11" and platform_machine != 'aarch64' and sys_platform == 'linux'
+esp_idf_monitor==1.1.1
+esp-idf-kconfig==1.2.0
diff --git a/src/test_driver/esp32/main/main_app.cpp b/src/test_driver/esp32/main/main_app.cpp
index 1eef2ae..0ff3ce4 100644
--- a/src/test_driver/esp32/main/main_app.cpp
+++ b/src/test_driver/esp32/main/main_app.cpp
@@ -19,12 +19,12 @@
 #include "esp_heap_caps_init.h"
 #include "esp_log.h"
 #include "esp_netif.h"
-#include "esp_spi_flash.h"
 #include "esp_system.h"
 #include "esp_wifi.h"
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "nvs_flash.h"
+#include "spi_flash_mmap.h"
 
 #include <stdio.h>
 
@@ -48,17 +48,6 @@
 
 extern "C" void app_main()
 {
-    esp_chip_info_t chip_info;
-    esp_chip_info(&chip_info);
-
-    ESP_LOGI(TAG, "This is ESP32 chip with %d CPU cores, WiFi%s%s, ", chip_info.cores,
-             (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
-
-    ESP_LOGI(TAG, "silicon revision %d, ", chip_info.revision);
-
-    ESP_LOGI(TAG, "%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
-             (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
-
     // Initialize the ESP NVS layer.
     esp_err_t err = nvs_flash_init();
     if (err != ESP_OK)