From d48bcf278bb6ff88a6e46989447ad90b8da48c1f Mon Sep 17 00:00:00 2001 From: Sweety Date: Wed, 24 Feb 2021 22:17:28 +0530 Subject: [PATCH] Optimize the temperature-measurement-app to use less RAM and Flash. (#4756) --- config/esp32/components/chip/component.mk | 3 + .../esp32/Makefile | 4 + .../esp32/README.md | 126 +++++++++++++++++- .../esp32/sdkconfig.defaults | 31 +++++ .../esp32/sdkconfig.optimize.defaults | 75 +++++++++++ .../esp32/sdkconfig_devkit.defaults | 32 +++++ .../esp32/sdkconfig_m5stack.defaults | 32 +++++ 7 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults diff --git a/config/esp32/components/chip/component.mk b/config/esp32/components/chip/component.mk index 36e1768a91d36f..3d903b06f59f4a 100644 --- a/config/esp32/components/chip/component.mk +++ b/config/esp32/components/chip/component.mk @@ -125,6 +125,9 @@ install-chip : $(OUTPUT_DIR) echo esp32_cc = \"$(CC)\" >> $(OUTPUT_DIR)/args.gn echo esp32_cxx = \"$(CXX)\" >> $(OUTPUT_DIR)/args.gn echo esp32_cpu = \"esp32\" >> $(OUTPUT_DIR)/args.gn +ifeq ($(is_debug),false) + @echo "is_debug = false" >> $(OUTPUT_DIR)/args.gn +endif if [[ "$(CONFIG_ENABLE_PW_RPC)" = "y" ]]; then \ echo "chip_build_pw_rpc_lib = true" >> $(OUTPUT_DIR)/args.gn ;\ echo "pw_log_BACKEND = \"//third_party/connectedhomeip/third_party/pigweed/repo/pw_log_basic\"" >> $(OUTPUT_DIR)/args.gn ;\ diff --git a/examples/temperature-measurement-app/esp32/Makefile b/examples/temperature-measurement-app/esp32/Makefile index ce4f9c52f659a1..17e13f40e3af9d 100644 --- a/examples/temperature-measurement-app/esp32/Makefile +++ b/examples/temperature-measurement-app/esp32/Makefile @@ -32,6 +32,10 @@ CFLAGS += -Os -DLWIP_IPV6_SCOPES=0 top: all flashing_script +ifeq ($(is_debug),false) +export SDKCONFIG_DEFAULTS = sdkconfig.optimize.defaults +endif + include $(IDF_PATH)/make/project.mk FLASHING_SCRIPT=$(BUILD_DIR_BASE)/$(PROJECT_NAME).flash.py diff --git a/examples/temperature-measurement-app/esp32/README.md b/examples/temperature-measurement-app/esp32/README.md index 40e933e1d17ca7..82dca0ee9b2784 100644 --- a/examples/temperature-measurement-app/esp32/README.md +++ b/examples/temperature-measurement-app/esp32/README.md @@ -1,3 +1,127 @@ # Temperature Sensor Example -// TODO Fill this out +## Building the Example Application + +Building the example application requires the use of the Espressif ESP32 IoT +Development Framework and the xtensa-esp32-elf toolchain. + +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 + + $ mkdir ${HOME}/tools + $ cd ${HOME}/tools + $ git clone https://github.com/espressif/esp-idf.git + $ cd esp-idf + $ git checkout release/v4.2 + $ git submodule update --init + $ export IDF_PATH=${HOME}/tools/esp-idf + $ ./install.sh + +- Install ninja-build + + $ sudo apt-get install ninja-build + +### To build the application, follow these steps: + +Currently building in VSCode _and_ deploying from native is not supported, so +make sure the IDF_PATH has been exported(See the manual setup steps above). + +- In the root of the example directory, sync the CHIP tree's submodules and + source `idf.sh`. Note: This does not have to be repeated for incremental + builds. + + $ source idf.sh + +- Configuration Options + + To choose from the different configuration options, run menuconfig + + $ idf make menuconfig + + Select ESP32 based `Device Type` through `Demo`->`Device Type`. + The device types that are currently supported include `ESP32-DevKitC` (default), + and `M5Stack` + + 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` + + 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. + + To use the default configuration options, run the default config + $ idf make defconfig + +- Run make to build the demo application. + + $ idf make + +- After building the application, to flash it outside of VSCode, connect your + device via USB. Then run the following command to flash the demo application + onto the device and then monitor its output. If necessary, replace + `/dev/tty.SLAB_USBtoUART`(MacOS) with the correct USB device name for your + system(like `/dev/ttyUSB0` on Linux). Note that sometimes you might have to + press and hold the `boot` button on the device while it's trying to connect + before flashing. For ESP32-DevKitC devices this is labeled in the + [functional description diagram](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-devkitc.html#functional-description). + + $ idf make flash monitor ESPPORT=/dev/ttyUSB0 + + Note: Some users might have to install the + [VCP driver](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers) + before the device shows up on `/dev/tty`. + +- Quit the monitor by hitting `Ctrl+]`. + + Note: You can see a menu of various monitor commands by hitting + `Ctrl+t Ctrl+h` while the monitor is running. + +- If desired, the monitor can be run again like so: + + $ idf make monitor ESPPORT=/dev/ttyUSB0 + +## Using the Echo Server + +### Connect the ESP32 to a 2.4GHz Network of your choice + +1. If the `WiFi Station Options` mentioned above are populated through + menuconfig, then ESP32 connects to the AP with those credentials (STA mode). + +2. Now flash the device with the same command as before. (Use the right `/dev` + device) + + $ idf make flash monitor ESPPORT=/dev/ttyUSB0 + +3. The device should boot up and connect to your network. When that happens you + will see a log like this in the monitor. + + I (5524) chip[DL]: SYSTEM_EVENT_STA_GOT_IP + I (5524) chip[DL]: IPv4 address changed on WiFi station interface: ... + + Note: If you are using the M5Stack, the screen will display the server's IP + Address if it successfully connects to the configured 2.4GHz Network. + +4. Use + [standalone chip-tool](https://github.com/project-chip/connectedhomeip/tree/master/examples/chip-tool) + or + [iOS chip-tool app](https://github.com/project-chip/connectedhomeip/tree/master/src/darwin) + to communicate with the device. + +Note: The ESP32 does not support 5GHz networks. Also, the Device will persist +your network configuration. To erase it, simply run. + + $ idf make erase_flash ESPPORT=/dev/ttyUSB0 + +The demo application supports temperaturemeasurement and basic cluster. + +## Optimization + +Optimization related to WiFi, BLuetooth, Asserts etc are the part of this +example by default. To enable this option set is_debug=false from command-line. + +``` +idf make build flash monitor 'is_debug=false' +``` diff --git a/examples/temperature-measurement-app/esp32/sdkconfig.defaults b/examples/temperature-measurement-app/esp32/sdkconfig.defaults index 200cb42be73e04..83918a71e5963e 100644 --- a/examples/temperature-measurement-app/esp32/sdkconfig.defaults +++ b/examples/temperature-measurement-app/esp32/sdkconfig.defaults @@ -37,3 +37,34 @@ CONFIG_LWIP_IPV6_AUTOCONFIG=y # Use a custom partition table CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" + +# Compiler options +CONFIG_COMPILER_OPTIMIZATION_SIZE=y + +# Bluetooth controller +CONFIG_BTDM_CTRL_BLE_MAX_CONN=1 +CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF=1 + +# NimBLE Options +CONFIG_BT_NIMBLE_MAX_CONNECTIONS=1 + +# Wi-Fi +CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=4 +CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=8 +CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=16 + +# FreeRTOS +CONFIG_FREERTOS_UNICORE=y + +# LWIP +CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=16 + +# TCP +CONFIG_LWIP_TCP_SYNMAXRTX=6 + +# Compatibility options +CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN=1 +CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF=1 +CONFIG_NIMBLE_MAX_CONNECTIONS=1 +CONFIG_TCPIP_RECVMBOX_SIZE=16 +CONFIG_TCP_SYNMAXRTX=6 diff --git a/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults b/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults new file mode 100644 index 00000000000000..db3481aa618f37 --- /dev/null +++ b/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults @@ -0,0 +1,75 @@ +# +# Copyright (c) 2020 Project CHIP Authors +# Copyright (c) 2018 Nest Labs, Inc. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Description: +# Some useful defaults for the optimizing temperature-measurement-app configuration. +# + + +# Default to 921600 baud when flashing and monitoring device +CONFIG_ESPTOOLPY_BAUD_921600B=y +CONFIG_ESPTOOLPY_BAUD=921600 +CONFIG_ESPTOOLPY_COMPRESSED=y +CONFIG_ESPTOOLPY_MONITOR_BAUD_115200B=y +CONFIG_ESPTOOLPY_MONITOR_BAUD=115200 + +#enable BT +CONFIG_BT_ENABLED=y +CONFIG_BT_NIMBLE_ENABLED=y + +#enable lwip ipv6 autoconfig +CONFIG_LWIP_IPV6_AUTOCONFIG=y + +# Use a custom partition table +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" + +# Compiler options +CONFIG_COMPILER_OPTIMIZATION_SIZE=y +CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y + +# Bluetooth controller +CONFIG_BTDM_CTRL_BLE_MAX_CONN=1 +CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF=1 + +# NimBLE Options +CONFIG_BT_NIMBLE_MAX_CONNECTIONS=1 + +# Wi-Fi +CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=4 +CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=8 +CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=16 + +# FreeRTOS +CONFIG_FREERTOS_UNICORE=y + +# Log output +CONFIG_LOG_DEFAULT_LEVEL_NONE=y + +# LWIP +CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=16 + +# TCP +CONFIG_LWIP_TCP_SYNMAXRTX=6 + +# Compatibility options +CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED=y +CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN=1 +CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF=1 +CONFIG_NIMBLE_MAX_CONNECTIONS=1 +CONFIG_TCPIP_RECVMBOX_SIZE=16 +CONFIG_TCP_SYNMAXRTX=6 diff --git a/examples/temperature-measurement-app/esp32/sdkconfig_devkit.defaults b/examples/temperature-measurement-app/esp32/sdkconfig_devkit.defaults index 336d38da857b99..e2062b2796a175 100644 --- a/examples/temperature-measurement-app/esp32/sdkconfig_devkit.defaults +++ b/examples/temperature-measurement-app/esp32/sdkconfig_devkit.defaults @@ -30,6 +30,7 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200 #enable BT CONFIG_BT_ENABLED=y +CONFIG_BT_NIMBLE_ENABLED=y #enable lwip ipv6 autoconfig CONFIG_LWIP_IPV6_AUTOCONFIG=y @@ -37,3 +38,34 @@ CONFIG_LWIP_IPV6_AUTOCONFIG=y # Use a custom partition table CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" + +# Compiler options +CONFIG_COMPILER_OPTIMIZATION_SIZE=y + +# Bluetooth controller +CONFIG_BTDM_CTRL_BLE_MAX_CONN=1 +CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF=1 + +# NimBLE Options +CONFIG_BT_NIMBLE_MAX_CONNECTIONS=1 + +# Wi-Fi +CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=4 +CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=8 +CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=16 + +# FreeRTOS +CONFIG_FREERTOS_UNICORE=y + +# LWIP +CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=16 + +# TCP +CONFIG_LWIP_TCP_SYNMAXRTX=6 + +# Compatibility options +CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN=1 +CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF=1 +CONFIG_NIMBLE_MAX_CONNECTIONS=1 +CONFIG_TCPIP_RECVMBOX_SIZE=16 +CONFIG_TCP_SYNMAXRTX=6 diff --git a/examples/temperature-measurement-app/esp32/sdkconfig_m5stack.defaults b/examples/temperature-measurement-app/esp32/sdkconfig_m5stack.defaults index ca53405dcc9842..736a9c2f8de6e3 100644 --- a/examples/temperature-measurement-app/esp32/sdkconfig_m5stack.defaults +++ b/examples/temperature-measurement-app/esp32/sdkconfig_m5stack.defaults @@ -30,6 +30,7 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200 #enable BT CONFIG_BT_ENABLED=y +CONFIG_BT_NIMBLE_ENABLED=y #enable lwip ipv6 autoconfig CONFIG_LWIP_IPV6_AUTOCONFIG=y @@ -37,3 +38,34 @@ CONFIG_LWIP_IPV6_AUTOCONFIG=y # Use a custom partition table CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" + +# Compiler options +CONFIG_COMPILER_OPTIMIZATION_SIZE=y + +# Bluetooth controller +CONFIG_BTDM_CTRL_BLE_MAX_CONN=1 +CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF=1 + +# NimBLE Options +CONFIG_BT_NIMBLE_MAX_CONNECTIONS=1 + +# Wi-Fi +CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=4 +CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=8 +CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=16 + +# FreeRTOS +CONFIG_FREERTOS_UNICORE=y + +# LWIP +CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=16 + +# TCP +CONFIG_LWIP_TCP_SYNMAXRTX=6 + +# Compatibility options +CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN=1 +CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF=1 +CONFIG_NIMBLE_MAX_CONNECTIONS=1 +CONFIG_TCPIP_RECVMBOX_SIZE=16 +CONFIG_TCP_SYNMAXRTX=6