Skip to content

ESP-IDF Release v4.0

Compare
Choose a tag to compare
@projectgus projectgus released this 11 Feb 00:00
· 27008 commits to master since this release

Documentation for release v4.0 is available at https://docs.espressif.com/projects/esp-idf/en/v4.0/

ESP-IDF v4.0 is a major update for ESP-IDF v3.x. Release v4.0 is mostly compatible with apps written for ESP-IDF v3.x, but there are some breaking changes (please consult the list below) and removal of deprecated functionality which will require code changes when updating projects

ESP-IDF v4.0.x will be supported for 18 months until October 2021, according to the ESP-IDF Support Policy. The current Long Term Support release is v3.3.1, v3.3.x will be supported until March 2022.

Note: No changes were made between the v4.0-rc release candidate and v4.0.

Major New Features

  • Bluetooth: Added alternative BLE-only host stack - NimBLE. Reduced resource usage compared to Bluedroid (but no dual mode support).
  • ESP-BLE-MESH: Added SIG Full-Feature Certified ESP-BLE-Mesh stack, supporting both Bluedroid Host and NimBLE Host.
  • Build system: New CMake-based build system, including native Windows support (MSYS2 is no longer required). IDE support includes VSCode and Eclipse. GNU Make build system from earlier ESP-IDF versions is still supported as a legacy build system.
  • ESP32: Support for ESP32 ECOV3. By default ESP-IDF projects still support all ECO version, but project can be configured with a minimum ECO version which may disable workaround and improve performance.
  • ESP-TLS: Added support for server side SSL/TLS connection
  • Ethernet: Refactored ethernet support into new esp_eth component, added support for DM9051 standalone Ethernet interface
  • Flash Encryption: New "Development" mode allows unlimited serial flashing of encrypted flash during development
  • lwIP: Updated lwIP version from v2.0.3 to v2.1.2
  • New "ESP Local Control" feature to provide capability to control an ESP32 over Wi-Fi+HTTPS or BLE using protocomm messages
  • SPI Flash: New SPI flash driver includes support for storage on additional SPI flash chips, partitions on external SPI flash chips
  • SPIFFS: New host based image generator tool
  • SystemView based heap and log tracing
  • toolchain: New "IDF Tools" scripts and mechanism for installing toolchain for a specific ESP-IDF version
  • toolchain: Upgraded ESP32 toolchain to gcc 8.2.0 (esp32-2019r2 toolchain release)
  • toolchain: Upgraded libc to newlib version 3
  • VFS: New semihosting driver for outputting via JTAG debugger
  • Websocket: New Websocket client component
  • Wi-Fi: New Wi-Fi Provisioning Manager feature
  • Wi-Fi: Refactor and improve softAP power saving support
  • Wi-Fi: wpa_supplicant code is now open source as part of ESP-IDF

Breaking Changes Since v3.x

  • APIs deprecated in ESP-IDF v3.x and earlier have been removed. If building a project output warnings about deprecated functionality when building for ESP-IDF v3.x, these warnings will now be errors as the functionality has been removed.
  • AWS IoT: The AWS IoT SDK is now an independent component repository: https://github.com/espressif/esp-aws-iot
  • Bluetooth: Added new parameter for updating whitelist API
  • Bluetooth: Adding AVRCP Target (TG) role means interfaces of AVRCP Controller(CT) have changed
  • Bluetooth: API of esp_bt_gap_set_scan_mode() is changed to allow setting connection modes as well as discoverable modes
  • CMake: idf.py argument parsing has changed, and not all argument orders that work with idf.py v3.x are still valid
  • CMake: idf.py defconfig and bootloader-clean targets are removed (these existed but did not work correctly in earlier idf.py versions)
  • CMake: CMake "external project" integration API is different to ESP-IDF v3.x and not compatible.
  • CMake: Variables like IDF_PATH, IDF_TARGET, PYTHON etc are now read-only inside the IDF build system. Instead, modify build properties of the same name using the idf_build_set_property function.
  • Core: Some function-like macros (ESP_ERROR_CHECK, ESP_PM_TRACE_ENTER/EXIT) had incorrect trailing semicolons which have been removed. May cause problems if relying on non-single-statement macro behaviour.
  • ESP32: The hwcrypto AES & SHA APIs are now internal to the mbedTLS component. Suggest refactoring existing code to use the mbedTLS APIs directly, if hardware acceleration is enabled in project configuration then the same implementations will be used.
  • Ethernet: v3.x APIs are no longer supported, Ethernet initialization steps has been changed to using the new esp_eth component.
  • FATFS no longer includes integer.h. Applications should either include "ff.h", or redefine types such as BYTE, DWORD, etc.
  • Flash encryption workflow has changed to develop/release workflow. In "develop" mode unlimited serial flashing is supported, but the device is not secure. It is still possible to OTA update devices with flash encryption to ESP-IDF V4.0, but set Release mode in config before building a project for a production device, including for an OTA update.
  • freemodbus: Refactor the "port" API to have a common master and slave interface, port mode is now selected via API not in KConfig.
  • ldgen: The fragment grammar has changed to simplify the syntax. Linker fragments written for ESP-IDF V3.x will need to be updated.
  • LWIP: Multicast UDP socket interface binding has changed, and multicast sockets can no longer bind to all interfaces in a single call
  • micro-ecc: The micro-ecc component is now part of the ESP-IDF bootloader only. For existing projects which use micro-ecc, recommend refactoring to use mbedTLS for the same functionality. If this is not possible, micro-ecc can be added to the project source files directly.
  • nvs_util & mfg_util command lines are not 100% backwards compatible with ESP-IDF v3.x
  • protocomm: Session security now supports multiple instances of the same type (security APIs have changed to add a "handle" parameter).
  • ringbuffer: Interface to vRingbufferGetInfo has an additional parameter
  • SmartConfig: esp_smartconfig_start has been changed to esp_err_t esp_smartconfig_start(const smartconfig_start_config_t *config);
  • SmartConfig: SmartConfig has been refactored to use IDF new event mechanism. Old events SC_STATUS_WAIT, SC_STATUS_FIND_CHANNEL, SC_STATUS_GETTING_SSID_PSWD, SC_STATUS_LINK, SC_STATUS_LINK_OVER have been removed, and new events SC_EVENT_SCAN_DONE, SC_EVENT_FOUND_CHANNEL, SC_EVENT_GOT_SSID_PSWD, SC_EVENT_SEND_ACK_DONE have been added.
  • Storage: esp_partition encrypted reads/writes are now disabled if flash encryption is disabled in project config
  • VFS: fcntl() argument is now an int not va_list, in line with newlib
  • VFS: Custom drivers which implemented select support will need to implement a new select API
  • ESP-WIFI-MESH: use the new esp_event library and remove event_cb from mesh_cfg_t

Major Non-Breaking Changes Since v3.x

The following major changes are non-breaking but are not compatible if downgrading to ESP-IDF v3.x:

  • CMake-based build system no longer uses "linker groups" to allow circular dependencies in the project at link time. Instead, components are expected to declare which other components they require. See build system docs
  • CMake: New component registration API. The API that was used by CMake for ESP-IDF v3.x is still supported but is deprecated
  • Many header files have been consolidated or moved, as part of reorganization to support multiple chip targets. These are not breaking changes in ESP-IDF V4.0, but the compiler will emit a warning which includes the new header path. The old headers will be completely removed in ESP-IDF V5.0
  • KConfig config item names have been standardized, meaning many items were renamed. The build system will automatically rename old config names to the new names on first build. Take care if you plan to downgrade a project to v3.x again, as the sdkconfig names will no longer be recognized.

Tip: If writing code that needs to work with ESP-IDF v3.x and v4.0, include the esp_idf_version.h header and check against the provided macros.

List of changes since ESP-IDF v3.3:

Wi-Fi

  • Documented that SSID & Password fields should be NULL terminated strings (#1176)
  • Maximum TX power range changed from 0-20dBm to 10-20dBm to avoid poor performance
  • Refactored event system to use new esp_event library. Includes backwards compatibility for legacy system_event functions
  • Now includes DMA reserved pool when allocating internal-only memory for Wi-Fi internal use (#3592)
  • Fixed a WiFi receiving bug which is caused by disabling WiFi/BT common clock
  • Fixed the bug that WPA2 Enterprise STA can connect open AP
  • Fixed TKIP/CCMP PN replay attack detection bug
  • Increased buffer number type from uint8_t to uint16_t to support more than 255 buffers
  • Avoided recalculating PMK if esp_wifi_set_config() is not called
  • Added WiFi menuconfig option to put more WiFi receiving code into IRAM
  • Fixed blocking scan crash issue
  • Fixed crash if association request RSN information length too big
  • Fixed crash issue caused by placing g_promis_buf to PSRAM memory
  • Fixed a bug about esp_wifi_set_channel()
  • Fixed the bug for WPA_WPA2_PSK connection fail
  • Fixed the bug that ESP32 sends broadcast to phone after smartconfig is done
  • Fixed WiFi beacon timeout and station fails to connect to AP sometimes when WiFi and Bluetooth run togerther
  • Fixed a smartconfig crash when no AP is found
  • Added check that ESP32_WIFI_RX_BA_WIN is less than ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM
  • Fixed the bug that esp_wifi_set_country() fails to set policy WIFI_COUNTRY_POLICY_MANUAL
  • Fixed the bug SmartConfig crashes if it fails to scan any APs.
  • Fixed the bug WPS leaks some memory each time it completes the WiFi connection.
  • Fixed the bug esp_wifi_stop() generates error log E(18732) event: system_event_ap_stop_handle_default 172 esp_wifi_internal_reg_rxcb ret = 0x3014.
  • Fixed a crash if esp_wifi_stop() called while sniffer was enabled
  • Fixed the bug that soft-AP doesn't return fail when setting to WIFI_AUTH_WPA2_ENTERPRISE or WIFI_AUTH_WEP
  • Fixed the bug that WiFi/LWIP fails to allocate memories in SPIRAM first when the option is enabled.
  • Fixed WPS handshake for routers who support but incorrectly advertise WPS2.0
  • Fixed failure to connect to some Apple APs in WPA2-PSK mode
  • Fixed bug where softAP set authmode would fail
  • Fixed WPS handshake for routers who support but incorrectly advertise WPS2.0
  • Fixed failure to connect to some Apple APs in WPA2-PSK mode
  • Fixed the bug related to WiFi init sequence
  • Fixed the bug that sniffer causes system crash because of accessing SPIRAM when cache is disabled
  • Fixed the bug that SmartConfig may occasionally emit the log message wifi ipc: failed to post wifi task and then crash

Bluetooth

ESP-BLE-Mesh (SIG Full-Feature Certified)

Guide to the new BLE Mesh support

  • All SIG BLE Mesh Core Features
    • PB-ADV and PB-GATT
    • Provisioner and Node roles
    • Friend and Low Power Node
    • Proxy Server & Client
    • Configuration Server & Client Models
    • Health Server & Client Models
    • Relay, Segmentation and Reassembly, Key Refresh, IV Update, etc
  • All SIG BLE Mesh Client Models
    • Generic Client Models
    • Sensor Client Models
    • Time and Scenes Client Models
    • Lighting Client Models
  • All SIG BLE Mesh Server Models
    • Generic Server Models
    • Sensor Server Models
    • Time and Scenes Server Models
    • Lighting Server Models
  • BLE Mesh and Wi-Fi Coexistence
  • Support both Bluedroid Host and NimBLE Host
  • Includes tutorials and technical documents
  • Performance Optimizations (Fast Provisioning Solution demo video, 100 nodes provisioned within 50s)

Bluedroid BLE Host

  • Added API to set minimum encryption key size
  • Added API to clean GATTC cache
  • Added discover service complete event
  • Added BLE MESH beacon adv and svc UUID for duplicate scan exceptional list
  • Added new API to get connection parameters in GATTC and GATTS connection events
  • Added svc_inst_id param for create service table event (#3256)
  • Added BLE link timeout config in menuconfig
  • Added new parameter for updating whitelist API
  • Added missing definitions for GATT descriptors as defined by the current Bluetooth GATT Descriptors Specification
  • Fixed BLE data loss and occasional double-send of a packet
  • Fixed ADV data error under some circumstances
  • Fixed ADV start/stop crash under some circumstances
  • Fixed UUID value for ESP_GATT_UUID_ENVIRONMENTAL_SENSING_SVC
  • Fixed Blufi preparation write crash
  • Fixed memory leak when setting TX power
  • Fixed unpack error for HCI_enhance_connection_complete event
  • Fixed BLE SMP free without init
  • Fixed build warning when user disable log
  • Fixed setting stack size for Bluedroid tasks such as BTU task not succeed through menuconfig
  • Fixed wrong usage of BLUFI_TYPE_DATA_x in Blufi
  • Fixed memcpy buffer overflow risk in Bluedroid
  • Fixed issue that memcpy was wrongly used by different types of data in LE_KEY callback handler
  • Fixed the issue with pointer type conversion in GATTC

NimBLE BLE Host

New NimBLE alternative BLE host stack. Documentation.

Classic Bluetooth

  • Added AVRCP Target Role (TG) support
  • Added AVRCP absolute volume feature
  • Added API to set Extended Inquiry Response(EIR) data
  • Fixed AVRCP command not executed while playing music
  • Fixed error checking if AVRC is connected
  • Added Wide Branch Speech HFP support
  • Fixed connection failure with some speakers
  • Disabled HFP Wide Bind Speech when SCO data path is selected as PCM instead of HCI
  • Fixed assert if A2DP deinitialized while music is playing
  • Fixed data flow control issue in A2DP source that leads to packet loss due to TX data queue overflow
  • Fixed A2DP example use of unallocated memory
  • Fixed PLC use of unallocated memory
  • Fixed A2DP function esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_SUSPEND) not getting an ACK from callback on A2DP sink

Controller

  • Added support for BTDM modem sleep when using 32kHz external crystal and light sleep mode
  • Fixed controller may repeatedly send LMP_feature_req_ext with specific devices
  • Fixed support for controller taking a semaphore inside a critical section
  • Fixed controller can't enter sleep after end of wake up request
  • ble: Add BLE adv report flow control
  • ble: Fixed no adv report when scanning with sleep enabled
  • ble: Fixed disconnection issue with some apple devices
  • coexistence: Fixed rxwinsz assert in BLE and WiFi coex
  • coexistence: Fixed BT BB reset lock may cause assert when WiFi do start/stop
  • coexistence: Fixed WiFi MAC reset may block and cause task watch dog timeout
  • coexistence: Fixed BT BB reset lock may cause assert when WiFi do start/stop.
  • coexistence: Fixed WiFi MAC reset may block and cause task watch dog timeout.

Bluedroid

  • Consolidated Bluedroid tasks, reduce overall memory overhead

Ethernet

  • Refactored ethernet component into new esp_eth component
  • Refactored event system to use new esp_event library. Includes backwards compatibility for legacy system_event functions
  • Added support for DM9051 SPI Ethernet Controller
  • Fixed ethernet APLL clock configuration (#3704)

Peripheral Drivers

  • drivers: Reset each driver peripheral module before enabling the driver (fixes issues with invalid peripheral state following a CPU reset)
  • can: Added support for lower bit rates when building for ESP32 ECO V3
  • can: Fixed skipped function calls if assertions are disabled
  • gpio, rmt, pcnt: Optimize ISR function behaviour for better performance (#2861)
  • i2c: Fix an issue with single byte register writes when using GCC 8.2.0
  • i2c: Power management support
  • i2s: Power management support
  • ledc: Fixed LEDC low speed clock selection bugs
  • ledc: Added feature allowing low speed channel to select CLK8M clock source, meaning it can work in sleep mode
  • sdio: Added support for SDIO over SPI peripheral interface
  • sdmmc: Add support to print CIS information from a card
  • SPI drivers: Refactored to use new driver HAL layer
  • spi_master: Added support for variable dummy length per transaction

Core System & FreeRTOS

  • Fixed error code collision in some error code values
  • efuse: Improved timing settings when burning efuses
  • esp32: Added new backtrace API to print a backtrace at any point (esp_backtrace_print())
  • esp32: The esp_sha() API now uses mbedTLS internally.
  • esp32: Now uses FPU for floating point divide, power, complex multiplications provided by libgcc
  • esp32: Added support for a fixed static RAM size and DRAM heap size (#3222)
  • esp32: Added new esp_idf_version.h and macros to check ESP-IDF version
  • esp32: Disable DPORT workaround if running on (forthcoming) revision 2 silicon version
  • esp_event: Added API to allow posting events from ISRs
  • esp_event: Fixed issue where post data could be incorrectly supplied to the handler
  • esp_flash: Fixed get/set write protection functions
  • esp_flash: Support encrypted read and write when using new API
  • esp_flash: Fixed spi_flash_read if source buffer not in internal memory and size < 16 (#4010)
  • esp_timer: Simplified timer deletion logic, resolving deadlock if a timer callback became blocked on another task that tried to delete a timer (#3458)
  • esp_timer: Fixed bug where system time could jump back ~54secs (#2513)
  • flash encryption: Flash encryption is now permanently enabled if secure boot is also enabled
  • flash encryption: Fixed booting with flash encryption enabled
  • flash encryption: Ensure flash encryption is permanently enabled if secure boot is enabled (-15894)
  • freertos: Added explicit check that FreeRTOS mutex semaphores must be given by the owner task. This is required by FreeRTOS but was not explicitly checked before. Behaviour can be disabled, but not recommended as incorrect RTOS behaviour may result.
  • freertos: Removed stray semicolon in xTaskNotifyGive() macro definition
  • freertos: Fixed configASSERT thread safety around current CPU core
  • freertos: Fixed possibly inconsistent configASSERTs used around scheduler state checks
  • heap: Fix printf usage in heap poisoning check failure
  • log: Fixed CCOUNT register overflow during boot, leading to incorrect "jump" backwards in startup log timestamps
  • psram: Optimize PSRAM SPI timings when used in DIO mode
  • pthread: Fix linker issues causes by pthread component linker order (#3709)
  • ringbuffer: Added static allocation support, removed some deprecated types/functions
  • ringbuffer: Added new API to allow allocating memory in the ringbuffer before it is actually sent
  • soc: All peripherals now have a soc/name_periph.h header which includes all SoC-level declarations (registers, characteristics, etc)

Bootloader

  • Disabled factory reset GPIO check when waking from deep sleep
  • Fixed a bug where disabling bootloader RNG may cause problems for I2S ADC mode in app

LWIP

tcpip_adapter

  • Refactored event system to use new esp_event library. Includes backwards compatibility for legacy system_event functions

mbedTLS Related

  • Added fault injection checks for hardware AES & SHA peripherals
  • Added configuration option to set the debug level (#3521)
  • Enable asymmetric TLS buffer lengths by default to reduce default RAM usage
  • SHA hardware acceleration is now enabled by default

Provisioning

  • protocomm: Fixes in handling BLE read/write requests
  • protocomm: Added support for NimBLE in BLE provisioning, for reduced resource usage

mDNS

  • Fixed mdns crashing on reception of txt packet without a corresponding service (#2866)
  • Fixed mdns txt item types to const char * to avoid
  • Updated mdns_out_question_s to be in line with mdns_parsed_question_s struct (#1568)
  • Fixed ignoring an mDNS packet if it contained an invalid name entry in the question field
  • Fixed possible race condition when checking DHCP status on WIFI_EVENT_STA_CONNECTED
  • Fixed size of AAAA answer for IPV6 queries
  • Fixed crash if event received during deinit

MQTT

  • Added support for global CA store
  • Extended error structure to receive mqtt specific errors

Console

  • Support buffered stdout

Debugger/JTAG

  • New semihosting driver and example using VFS layer
  • Added SystemView based heap and log tracing features

Storage

  • NVS: Fixed issue where an item was always written even if unmodified (#3310)
  • NVS: Appended _t to nvs_handle & nvs_open_mode types (#3239)
  • NVS: Added new NVS Iterators API
  • NVS: nvs_util now has support for decrypting encrypted NVS partitions on the host
  • NVS: Fixed issue where non-matching iterator types would not work
  • NVS: Fixed issue where NVS did not check write mode in nvs_set_str/blob
  • VFS: Added support for pread() and pwrite() (#3515)
  • VFS: Added support for concurrent calls to select()
  • VFS: Fixed use after free bug in select()
  • VFS: Fixed memory leak if esp_vfs_fat_sdmmc_mount() fails

ESP-TLS

  • Added support to override certificate Common Name via config, instead of using hostname
  • Fixed incorrect closing of FD 0 on connection failure (#3149)
  • Added new error reporting API to provide error codes to application code (reference)

HTTP Client

  • Fixed lost username when setting new URL with a path (#3250)
  • Fixed reading HTTP header in chunked mode (#3461)
  • New separate buffer_size config option for transmit (#3528)

HTTPS OTA Client

  • Fixed HTTP URL redirection issue (#3218)
  • Fixed basic/digest auth issue with HTTP URL (#2921)
  • Fixed issue with lost HTTP_EVENT_HEADER_SENT event (#3095)
  • Added support for adding custom HTTP header
  • Added support for reading firmware image version
  • Added support for monitoring upgrade status

HTTP Server

  • Added ability to select core for HTTP server task (#3190)
  • Fixed parsing of empty header values
  • Fixed closing of invalid session

tcp_transport Related

  • Fixed minor memory leak found by static analyzer (#3602)
  • Fixed minor memory leak when SSL transport used separately (#3541)

Build System & Tooling

  • Added Dockerfile to build docker images using ESP-IDF
  • cmake: Use of ccache is now opt-in, pass idf.py --cache or cmake -DUSE_CCACHE=1 to enable
  • cmake: Now passes environment variables internally using a file, to avoid command line length limitations on Windows
  • cmake: Fixed issues with encrypted project flasher args
  • cmake: Fixed issue where component properties not set correctly
  • cmake mbedtls: Fixed issue where some files not added to correct component library
  • cmake: Set ESP_PLATFORM variable when processing CMake files
  • cmake: Added function to import precompiled libraries into project
  • cmake: Fixed but where partition table MD5 checksum was not generated
  • esptool: Updated to v2.8
  • idf_monitor: Added feature to create log files (keyboard shortcuts)
  • idf.py: Fix behaviour under PowerShell
  • idf.py --print-filter option allows filtering idf_monitor output when running under idf.py
  • idf_monitor: Support Ctrl-T + A as well as Ctrl-T + Ctrl-A, as latter cannot be captured under Windows command prompt
  • idf_monitor: Support Ctrl-T Ctrl-X as an alternative sequence to exit the monitor (as Ctrl-] hard to type on some keyboards) (#4167)
  • Pass -DOUTPUT_JSON=1 to get JSON formatted output from size-related targets
  • GNU Make: make V=0 is now the same as make V= (no verbose output at all)
  • GNU Make: Fixed make read_otadata and erase-otadata not using esptool config correctly (#3135)
  • GNU Make: Fixed 'make' errors rebuilding mconf after switching IDF versions
  • GNU Make: Fixed build errors due to path backslash escaping on Windows
  • mfg_util: Optimise command line interface by adding subparser options (not compatible with ESP-IDF v3.x)
  • mconf: Added 'simple expand' logic for environment variables instead of using wordexp(), to make consistent with the mconf-idf shipped in the IDF Tools installer.
  • parttool & otatool: Now have baud rate command line option
  • toolchain: "Release" optimization level now passes -freorder-blocks to reorder code blocks in output, can improve both performance and code size when using gcc 8.2.0

Confserver

  • New confserver V2 protocol with additional visibility & config ID changes
  • Fixed a crash trying to re-open a temporary file on Windows

External Libraries

  • Update CJSON library to v1.7.12
  • Update libcoap to version 4.2.0
  • Updated libexpat to v2.2.9
  • Update FATFS to R0.13c patch 4

Documentation

  • New Chinese translation of many documentation sections

Examples

  • Added HTTP RESTful web server example, with modern Web UI

  • OTA Example now supports either Wi-Fi or Ethernet

  • All protocol examples now support either Wi-Fi or Ethernet

  • New BLE GATTC and GATTS coexistence example

  • New BLE ANCS demo

  • Bluetooth examples have been restructured, examples for NimBLE & BLE Mesh are added

  • Fixed crash issue caused by mutex checking in FreeRTOS during Bluetooth LE throughput testing.

  • Added external flash chip FATFS example

  • Wi-Fi station & esp_local_ctrl: Fixed and improved use of event group bits for Wi-Fi connection

  • UDP Multicast example: Now handles errors correctly

  • Wifi-AP example now includes Ethernet Layer 2 forwarding functionality

Obtaining v4.0

For full installation instructions, see the ESP-IDF Programming Guide.

The source files attached to this release will not work due to our use of git submodules. Use one of the following methods instead:

Using git

To get this release, use the following commands:

git clone -b v4.0 --recursive https://github.com/espressif/esp-idf.git esp-idf-v4.0
cd esp-idf-v4.0/

This is the recommended way of obtaining v4.0 of ESP-IDF.

Download an archive with submodules included

Attached to this release is an esp-idf-v4.0.zip archive. It includes .git directory and all the submodules, so can be used out of the box. This archive is provided for users who have connectivity issues preventing them from cloning from GitHub.

This archive can also be downloaded from Espressif's download server:
https://dl.espressif.com/dl/esp-idf/releases/esp-idf-v4.0.zip