From 4d947e8495d9ad5474959398c066de8cf4cd8c5f Mon Sep 17 00:00:00 2001 From: noyceone0630 Date: Wed, 28 Apr 2021 20:10:02 +0800 Subject: [PATCH] add ESP32C3-DevKitM support to ESP32 all-clusters-app --- config/esp32/components/chip/CMakeLists.txt | 3 ++ .../all-clusters-app/esp32/CMakeLists.txt | 10 ++++-- examples/all-clusters-app/esp32/Makefile | 14 ++++---- examples/all-clusters-app/esp32/README.md | 36 ++++++++++++------- .../esp32/main/CMakeLists.txt | 31 ++++++++++++++-- .../esp32/main/Kconfig.projbuild | 3 ++ .../all-clusters-app/esp32/main/component.mk | 23 ++++++++++-- examples/all-clusters-app/esp32/main/main.cpp | 4 +++ src/lib/core/CHIPTLV.h | 22 ++++++++++++ src/platform/ESP32/PlatformManagerImpl.cpp | 2 +- src/platform/ESP32/nimble/BLEManagerImpl.cpp | 2 ++ 11 files changed, 124 insertions(+), 26 deletions(-) diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 46424f21a01a5d..1c8153fc34955a 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -178,6 +178,9 @@ if(NOT CONFIG_USE_MINIMAL_MDNS) list(APPEND chip_libraries $) endif() +idf_component_get_property(main_lib main COMPONENT_LIB) +list(APPEND chip_libraries $) + target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group ${chip_libraries} $ $ diff --git a/examples/all-clusters-app/esp32/CMakeLists.txt b/examples/all-clusters-app/esp32/CMakeLists.txt index f1e501c3c9fa46..81c43f32f73198 100644 --- a/examples/all-clusters-app/esp32/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/CMakeLists.txt @@ -22,11 +22,17 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake) # The list of extra component dirs must be in sync with that in all-clusters-app/esp32/Makefile set(EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components" - "${CMAKE_CURRENT_LIST_DIR}/../../common/m5stack-tft/repo/components" "${CMAKE_CURRENT_LIST_DIR}/../../common/QRCode" +) +if(${IDF_TARGET} STREQUAL "esp32") +set(EXTRA_COMPONENT_DIRS + ${EXTRA_COMPONENT_DIRS} + "${CMAKE_CURRENT_LIST_DIR}/../../common/m5stack-tft/repo/components/tft" + "${CMAKE_CURRENT_LIST_DIR}/../../common/m5stack-tft/repo/components/spidriver" "${CMAKE_CURRENT_LIST_DIR}/../../common/screen-framework" ) +endif() project(chip-all-clusters-app) -idf_build_set_property(CXX_COMPILE_OPTIONS "-std=c++14;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND) +idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++14;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND) idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND) diff --git a/examples/all-clusters-app/esp32/Makefile b/examples/all-clusters-app/esp32/Makefile index 7d46e246b8bd6e..60edc2f9616412 100644 --- a/examples/all-clusters-app/esp32/Makefile +++ b/examples/all-clusters-app/esp32/Makefile @@ -22,12 +22,14 @@ PROJECT_NAME := chip-all-clusters-app # The list of extra component dirs must be in sync with that in all-clusters-app/esp32/CMakeLists.txt -EXTRA_COMPONENT_DIRS += $(PROJECT_PATH)/third_party/connectedhomeip/config/esp32/components \ - $(PROJECT_PATH)/../../common/m5stack-tft/repo/components \ - $(PROJECT_PATH)/../../common/QRCode \ - $(PROJECT_PATH)/../../common/screen-framework \ - -CXXFLAGS += -std=c++14 -Os -DLWIP_IPV6_SCOPES=0 +EXTRA_COMPONENT_DIRS += $(PROJECT_PATH)/third_party/connectedhomeip/config/esp32/components \ + $(PROJECT_PATH)/../../common/QRCode \ +ifeq($(IDF_TARGET),'esp32') +EXTRA_COMPONENT_DIRS += $(PROJECT_PATH)/../../common/m5stack-tft/repo/components/tft \ + $(PROJECT_PATH)/../../common/m5stack-tft/repo/components/spidriver \ + $(PROJECT_PATH)/../../common/screen-framework \ +endif +CXXFLAGS += -std=gnu++14 -Os -DLWIP_IPV6_SCOPES=0 CPPFLAGS += -Os -DLWIP_IPV6_SCOPES=0 -DCHIP_HAVE_CONFIG_H CFLAGS += -Os -DLWIP_IPV6_SCOPES=0 diff --git a/examples/all-clusters-app/esp32/README.md b/examples/all-clusters-app/esp32/README.md index b690419922d3fc..d4cc569bc9e99e 100644 --- a/examples/all-clusters-app/esp32/README.md +++ b/examples/all-clusters-app/esp32/README.md @@ -1,8 +1,8 @@ # CHIP All Clusters Example -A prototype application that uses CHIP to setup WiFi on the ESP32 and runs an -Echo Server. This example will evolve as more complex messaging is supported in -CHIP. +A prototype application that uses CHIP to setup WiFi on the ESP32&ESP32C3 and +runs an Echo Server. This example will evolve as more complex messaging is +supported in CHIP. --- @@ -18,28 +18,30 @@ CHIP. ## Supported Devices -The CHIP demo application is intended to work on two categories of ESP32 -devices: the +The CHIP demo application is intended to work on two categories of ESP32 devices +and one categories ESP32C3 device: the [ESP32-DevKitC](https://www.espressif.com/en/products/hardware/esp32-devkitc/overview), -and the [M5Stack](http://m5stack.com). On the [M5Stack](http://m5stack.com) this -example displays a CHIP QRCode with the device's Soft-AP SSID encoded in the TLV -section. +the [M5Stack](http://m5stack.com) and the +[ESP32C3-DevKitM](https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html). +On the [M5Stack](http://m5stack.com) this example displays a CHIP QRCode with +the device's Soft-AP SSID encoded in the TLV section. ## Building the Example Application Building the example application requires the use of the Espressif ESP32 IoT -Development Framework and the xtensa-esp32-elf toolchain. +Development Framework and the xtensa-esp32-elf toolchain for ESP32 or the +riscv-esp32-elf toolchain for ESP32C3. The VSCode devcontainer has these components pre-installed, so you can skip this step. To install these components manually, follow these steps: -- Clone the Espressif ESP-IDF and checkout release/v4.2 branch +- Clone the Espressif ESP-IDF and checkout release/v4.3 branch $ mkdir ${HOME}/tools $ cd ${HOME}/tools $ git clone https://github.com/espressif/esp-idf.git $ cd esp-idf - $ git checkout release/v4.2 + $ git checkout release/v4.3 $ git submodule update --init $ export IDF_PATH=${HOME}/tools/esp-idf $ ./install.sh @@ -76,18 +78,28 @@ If packages are already installed then simply activate it. - Configuration Options + To choose the module, run set-target + + $ idf.py set-target esp32 + or + $ idf.py set-target esp32c3 + To choose from the different configuration options, run menuconfig $ idf.py menuconfig Select ESP32 based `Device Type` through `Demo`->`Device Type`. The device types that are currently supported include `ESP32-DevKitC` (default), - `ESP32-WROVER-KIT_V4.1` and `M5Stack` + `ESP32-WROVER-KIT_V4.1`, `M5Stack` and `ESP32C3-DevKitM` If you are using `standalone chip-tool` to communicate with the ESP32, bypass the Rendezvous mode so that the device can communicate over an insecure channel. This can be done through `Demo`->`Rendezvous Mode`->`Bypass` + If you are using `ESP32C3-DevKitM` and need BLEAdvertising, please enable host based + privacy for random address through `Component config`->`Bluetooth`->'NimBLE Options'-> + `enable host based privacy form random address`. + To connect the ESP32 to your network, configure the Wi-Fi SSID and Passphrase through `Component config`->`CHIP Device Layer`->`WiFi Station Options`->`Default WiFi SSID` and `Default WiFi Password` respectively. diff --git a/examples/all-clusters-app/esp32/main/CMakeLists.txt b/examples/all-clusters-app/esp32/main/CMakeLists.txt index 51e06d064f586e..1108c9996ab850 100644 --- a/examples/all-clusters-app/esp32/main/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/main/CMakeLists.txt @@ -17,12 +17,13 @@ # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) # The list of src and include dirs must be in sync with that in all-clusters-app/esp32/main/component.mk -idf_component_register(PRIV_INCLUDE_DIRS +set(PRIV_INCLUDE_DIRS_GROUPS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/all-clusters-app/all-clusters-common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/third_party/nlio/repo/include" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src" "${CMAKE_CURRENT_LIST_DIR}/include" - SRC_DIRS +) +set(SRC_DIRS_GROUPS "${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/all-clusters-app/all-clusters-common/gen" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server" @@ -51,7 +52,31 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/door-lock-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ias-zone-server" #${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ias-zone-client - PRIV_REQUIRES chip QRCode tft spidriver bt screen-framework) +) + +if(${CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM}) +set(PRIV_INCLUDE_DIRS_GROUPS ${PRIV_INCLUDE_DIRS_GROUPS} + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/screen-framework/include") +set(PRIV_REQUIRES_GROUPS chip QRCode bt) +endif() + +if(${CONFIG_DEVICE_TYPE_ESP32_DEVKITC}) +set(PRIV_INCLUDE_DIRS_GROUPS ${PRIV_INCLUDE_DIRS_GROUPS} + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/screen-framework/include") +set(PRIV_REQUIRES_GROUPS chip QRCode bt) +endif() + +if(${CONFIG_DEVICE_TYPE_ESP32_WROVER_KIT}) + set(PRIV_REQUIRES_GROUPS chip QRCode bt tft spidrier screen-framework) +endif() + +if(${CONFIG_DEVICE_TYPE_M5STACK}) + set(PRIV_REQUIRES_GROUPS chip QRCode bt tft spidrier screen-framework) +endif() + +idf_component_register(PRIV_INCLUDE_DIRS ${PRIV_INCLUDE_DIRS_GROUPS} + SRC_DIRS ${SRC_DIRS_GROUPS} + PRIV_REQUIRES ${PRIV_REQUIRES_GROUPS}) set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 14) target_compile_options(${COMPONENT_LIB} PRIVATE "-DLWIP_IPV6_SCOPES=0" "-DCHIP_HAVE_CONFIG_H") diff --git a/examples/all-clusters-app/esp32/main/Kconfig.projbuild b/examples/all-clusters-app/esp32/main/Kconfig.projbuild index fbaa3906f712a7..fb971f56873ea0 100644 --- a/examples/all-clusters-app/esp32/main/Kconfig.projbuild +++ b/examples/all-clusters-app/esp32/main/Kconfig.projbuild @@ -35,6 +35,8 @@ menu "Demo" bool "ESP32-WROVER-KIT_V4.1" config DEVICE_TYPE_M5STACK bool "M5Stack" + config DEVICE_TYPE_ESP32_C3_DEVKITM + bool "EXP32C3-DevKitM" endchoice choice @@ -71,6 +73,7 @@ menu "Demo" int range 0 5 default 0 if DEVICE_TYPE_ESP32_DEVKITC + default 0 if DEVICE_TYPE_ESP32_C3_DEVKITM default 3 if DEVICE_TYPE_M5STACK default 4 if DEVICE_TYPE_ESP32_WROVER_KIT diff --git a/examples/all-clusters-app/esp32/main/component.mk b/examples/all-clusters-app/esp32/main/component.mk index e8c22cb7db5fba..fcf5c91534c6ac 100644 --- a/examples/all-clusters-app/esp32/main/component.mk +++ b/examples/all-clusters-app/esp32/main/component.mk @@ -19,7 +19,15 @@ # # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) -COMPONENT_DEPENDS := chip QRCode tft spidriver +COMPONENT_DEPENDS := chip QRCode + +ifdef $(CONFIG_DEVICE_TYPE_M5STACK) +COMPONENT_DEPENDS += spidriver tft +endif + +ifdef $(CONFIG_DEVICE_TYPE_ESP32_WROVER_KIT) +COMPONENT_DEPENDS += spidriver tft +endif # The list of src and include dirs must be in sync with that in all-clusters-app/esp32/main/CMakeLists.txt COMPONENT_SRCDIRS := \ @@ -64,7 +72,18 @@ COMPONENT_EXTRA_INCLUDES := $(PROJECT_PATH)/third_party/connectedhomeip/src/app/ $(PROJECT_PATH)/third_party/connectedhomeip/src/app/server \ $(PROJECT_PATH)/third_party/connectedhomeip/examples/all-clusters-app/all-clusters-common \ $(PROJECT_PATH)/third_party/connectedhomeip/third_party/nlio/repo/include \ - $(PROJECT_PATH)/third_party/connectedhomeip/src + $(PROJECT_PATH)/third_party/connectedhomeip/src \ + +ifdef $(CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM) +COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/third_party/connectedhomeip/examples/common/screen-framework/include \ + +endif + +ifdef $(CONFIG_DEVICE_TYPE_ESP32_DEVKITC) +COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/third_party/connectedhomeip/examples/common/screen-framework/include \ + +endif + # So "gen/*" files are found by the src/app bits. COMPONENT_PRIV_INCLUDEDIRS := . diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 051f9f5cba0bf9..c7ffd0fd679f1f 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -80,6 +80,10 @@ using namespace ::chip::DeviceLayer; #define STATUS_LED_GPIO_NUM GPIO_NUM_2 // Use LED1 (blue LED) as status LED on DevKitC +#elif CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM + +#define STATUS_LED_GPIO_NUM GPIO_NUM_2 + #else // !CONFIG_DEVICE_TYPE_ESP32_DEVKITC #error "Unsupported device type selected" diff --git a/src/lib/core/CHIPTLV.h b/src/lib/core/CHIPTLV.h index 1993b17d3d7488..1ff178b7253336 100644 --- a/src/lib/core/CHIPTLV.h +++ b/src/lib/core/CHIPTLV.h @@ -942,6 +942,17 @@ class DLL_EXPORT TLVWriter */ CHIP_ERROR Put(uint64_t tag, int32_t v); + /* + * @overload CHIP_ERROR TLVWriter::Put(uint64_t tag, int8_t v) + * riscv toolchain of esp-idf defines int32_t from long int instead of int + */ +#ifdef __riscv + CHIP_ERROR Put(uint64_t tag, int v) + { + return Put(tag, static_cast(v)); + }; +#endif + /** * @overload CHIP_ERROR TLVWriter::Put(uint64_t tag, int8_t v, bool preserveSize) */ @@ -1032,6 +1043,17 @@ class DLL_EXPORT TLVWriter */ CHIP_ERROR Put(uint64_t tag, uint32_t v); + /* + * @overload CHIP_ERROR TLVWriter::Put(uint64_t tag, int8_t v) + * riscv toolchain of esp-idf defines int32_t from long int instead of int + */ +#ifdef __riscv + CHIP_ERROR Put(uint64_t tag, unsigned int v) + { + return Put(tag, static_cast(v)); + }; +#endif + /** * @overload CHIP_ERROR TLVWriter::Put(uint64_t tag, uint8_t v, bool preserveSize) */ diff --git a/src/platform/ESP32/PlatformManagerImpl.cpp b/src/platform/ESP32/PlatformManagerImpl.cpp index b90115c70d999e..2b35fdd82b866a 100644 --- a/src/platform/ESP32/PlatformManagerImpl.cpp +++ b/src/platform/ESP32/PlatformManagerImpl.cpp @@ -90,7 +90,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) esp_fill_random(ap_mac, sizeof(ap_mac)); /* Bit 0 of the first octet of MAC Address should always be 0 */ ap_mac[0] &= (uint8_t) ~0x01; - err = esp_wifi_set_mac(ESP_IF_WIFI_AP, ap_mac); + err = esp_wifi_set_mac(WIFI_IF_AP, ap_mac); SuccessOrExit(err); } diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index d551b64f839fa4..6f9b83e1a2419f 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -1117,6 +1117,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) return err; } } +#if CONFIG_BT_NIMBLE_HOST_BASED_PRIVACY else { err = MapBLEError(ble_hs_pvcy_rpa_config(NIMBLE_HOST_ENABLE_RPA)); @@ -1126,6 +1127,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) return err; } } +#endif err = MapBLEError(ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params, ble_svr_gap_event, NULL)); if (err == CHIP_NO_ERROR) {