Miscellaneous System APIs

[中文]

Software Reset

To perform software reset of the chip, the esp_restart() function is provided. When the function is called, execution of the program stops, both CPUs are reset, and the application is loaded by the bootloader and starts execution again.

Additionally, the esp_register_shutdown_handler() function can register a routine that will be automatically called before a restart (that is triggered by esp_restart()) occurs. This is similar to the functionality of atexit POSIX function.

Reset Reason

ESP-IDF applications can be started or restarted due to a variety of reasons. To get the last reset reason, call esp_reset_reason() function. See description of esp_reset_reason_t for the list of possible reset reasons.

Heap Memory

Two heap-memory-related functions are provided:

Note that ESP-IDF supports multiple heaps with different capabilities. The functions mentioned in this section return the size of heap memory that can be allocated using the malloc family of functions. For further information about heap memory, see Heap Memory Allocation.

MAC Address

These APIs allow querying and customizing MAC addresses for different supported network interfaces (e.g., Wi-Fi, Bluetooth, Ethernet).

To fetch the MAC address for a specific network interface (e.g., Wi-Fi, Bluetooth, Ethernet), call the function esp_read_mac().

In ESP-IDF, the MAC addresses for the various network interfaces are calculated from a single base MAC address. By default, the Espressif base MAC address is used. This base MAC address is pre-programmed into the ESP32 eFuse in the factory during production.

Interface

MAC Address (4 universally administered, default)

MAC Address (2 universally administered)

Wi-Fi Station

base_mac

base_mac

Wi-Fi SoftAP

base_mac, +1 to the last octet

Local MAC (derived from Wi-Fi Station MAC)

Bluetooth

base_mac, +2 to the last octet

base_mac, +1 to the last octet

Ethernet

base_mac, +3 to the last octet

Local MAC (derived from Bluetooth MAC)

Note

The configuration configures the number of universally administered MAC addresses that are provided by Espressif.

Custom Base MAC

The default base MAC is pre-programmed by Espressif in eFuse BLK0. To set a custom base MAC instead, call the function esp_base_mac_addr_set() before initializing any network interfaces or calling the esp_read_mac() function. The custom MAC address can be stored in any supported storage device (e.g., flash, NVS).

The custom base MAC addresses should be allocated such that derived MAC addresses will not overlap. Based on the table above, users can configure the option CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES to set the number of valid universal MAC addresses that can be derived from the custom base MAC.

Note

It is also possible to call the function esp_netif_set_mac() to set the specific MAC used by a network interface after network initialization. But it is recommended to use the base MAC approach documented here to avoid the possibility of the original MAC address briefly appearing on the network before being changed.

Custom MAC Address in eFuse

When reading custom MAC addresses from eFuse, ESP-IDF provides a helper function esp_efuse_mac_get_custom(). This loads the MAC address from eFuse BLK3. This function assumes that the custom base MAC address is stored in the following format:

Field

# of bits

Range of bits

Notes

Version

8

191:184

0: invalid, others — valid

Reserved

128

183:56

MAC address

48

55:8

MAC address CRC

8

7:0

CRC-8-CCITT, polynomial 0x07

Note

If the 3/4 coding scheme is enabled, all eFuse fields in this block must be burnt at the same time.

Once MAC address has been obtained using esp_efuse_mac_get_custom(), call esp_base_mac_addr_set() to set this MAC address as base MAC address.

Local vs Universal MAC Addresses

ESP32 comes pre-programmed with enough valid Espressif universally administered MAC addresses for all internal interfaces. The table above shows how to calculate and derive the MAC address for a specific interface according to the base MAC address.

When using a custom MAC address scheme, it is possible that not all interfaces can be assigned with a universally administered MAC address. In these cases, a locally administered MAC address is assigned. Note that these addresses are intended for use on a single local network only.

See this article for the definition of locally and universally administered MAC addresses.

Function esp_derive_local_mac() is called internally to derive a local MAC address from a universal MAC address. The process is as follows:

  1. The U/L bit (bit value 0x2) is set in the first octet of the universal MAC address, creating a local MAC address.

  2. If this bit is already set in the supplied universal MAC address (i.e., the supplied “universal” MAC address was in fact already a local MAC address), then the first octet of the local MAC address is XORed with 0x4.

Chip Version

esp_chip_info() function fills esp_chip_info_t structure with information about the chip. This includes the chip revision, number of CPU cores, and a bit mask of features enabled in the chip.

SDK Version

esp_get_idf_version() returns a string describing the ESP-IDF version which is used to compile the application. This is the same value as the one available through IDF_VER variable of the build system. The version string generally has the format of git describe output.

To get the version at build time, additional version macros are provided. They can be used to enable or disable parts of the program depending on the ESP-IDF version.

App Version

The application version is stored in esp_app_desc_t structure. It is located in DROM sector and has a fixed offset from the beginning of the binary file. The structure is located after esp_image_header_t and esp_image_segment_header_t structures. The type of the field version is string and it has a maximum length of 32 chars.

To set the version in your project manually, you need to set the PROJECT_VER variable in the CMakeLists.txt of your project. In application CMakeLists.txt, put set(PROJECT_VER "0.1.0.1") before including project.cmake.

If the CONFIG_APP_PROJECT_VER_FROM_CONFIG option is set, the value of CONFIG_APP_PROJECT_VER will be used. Otherwise, if the PROJECT_VER variable is not set in the project, it will be retrieved either from the $(PROJECT_PATH)/version.txt file (if present) or using git command git describe. If neither is available, PROJECT_VER will be set to “1”. Application can make use of this by calling esp_ota_get_app_description() or esp_ota_get_partition_description() functions.

API Reference

Functions

esp_err_t esp_register_shutdown_handler(shutdown_handler_t handle)

Register shutdown handler.

This function allows you to register a handler that gets invoked before the application is restarted using esp_restart function.

Parameters

handle – function to execute on restart

Returns

  • ESP_OK on success

  • ESP_ERR_INVALID_STATE if the handler has already been registered

  • ESP_ERR_NO_MEM if no more shutdown handler slots are available

esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handle)

Unregister shutdown handler.

This function allows you to unregister a handler which was previously registered using esp_register_shutdown_handler function.

  • ESP_OK on success

  • ESP_ERR_INVALID_STATE if the given handler hasn’t been registered before

void esp_restart(void)

Restart PRO and APP CPUs.

This function can be called both from PRO and APP CPUs. After successful restart, CPU reset reason will be SW_CPU_RESET. Peripherals (except for Wi-Fi, BT, UART0, SPI1, and legacy timers) are not reset. This function does not return.

esp_reset_reason_t esp_reset_reason(void)

Get reason of last reset.

Returns

See description of esp_reset_reason_t for explanation of each value.

uint32_t esp_get_free_heap_size(void)

Get the size of available heap.

Note

Note that the returned value may be larger than the maximum contiguous block which can be allocated.

Returns

Available heap size, in bytes.

uint32_t esp_get_free_internal_heap_size(void)

Get the size of available internal heap.

Note

Note that the returned value may be larger than the maximum contiguous block which can be allocated.

Returns

Available internal heap size, in bytes.

uint32_t esp_get_minimum_free_heap_size(void)

Get the minimum heap that has ever been available.

Returns

Minimum free heap ever available

void esp_system_abort(const char *details)

Trigger a software abort.

Parameters

details – Details that will be displayed during panic handling.

Type Definitions

typedef void (*shutdown_handler_t)(void)

Shutdown handler type

Enumerations

enum esp_reset_reason_t

Reset reasons.

Values:

enumerator ESP_RST_UNKNOWN

Reset reason can not be determined.

enumerator ESP_RST_POWERON

Reset due to power-on event.

enumerator ESP_RST_EXT

Reset by external pin (not applicable for ESP32)

enumerator ESP_RST_SW

Software reset via esp_restart.

enumerator ESP_RST_PANIC

Software reset due to exception/panic.

enumerator ESP_RST_INT_WDT

Reset (software or hardware) due to interrupt watchdog.

enumerator ESP_RST_TASK_WDT

Reset due to task watchdog.

enumerator ESP_RST_WDT

Reset due to other watchdogs.

enumerator ESP_RST_DEEPSLEEP

Reset after exiting deep sleep mode.

enumerator ESP_RST_BROWNOUT

Brownout reset (software or hardware)

enumerator ESP_RST_SDIO

Reset over SDIO.

Functions

const char *esp_get_idf_version(void)

Return full IDF version string, same as ‘git describe’ output.

Note

If you are printing the ESP-IDF version in a log file or other information, this function provides more information than using the numerical version macros. For example, numerical version macros don’t differentiate between development, pre-release and release versions, but the output of this function does.

Returns

constant string from IDF_VER

Macros

ESP_IDF_VERSION_MAJOR

Major version number (X.x.x)

ESP_IDF_VERSION_MINOR

Minor version number (x.X.x)

ESP_IDF_VERSION_PATCH

Patch version number (x.x.X)

ESP_IDF_VERSION_VAL(major, minor, patch)

Macro to convert IDF version number into an integer

To be used in comparisons, such as ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)

ESP_IDF_VERSION

Current IDF version, as an integer

To be used in comparisons, such as ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)

Functions

esp_err_t esp_base_mac_addr_set(const uint8_t *mac)

Set base MAC address with the MAC address which is stored in BLK3 of EFUSE or external storage e.g. flash and EEPROM.

Base MAC address is used to generate the MAC addresses used by network interfaces.

If using a custom base MAC address, call this API before initializing any network interfaces. Refer to the ESP-IDF Programming Guide for details about how the Base MAC is used.

Note

Base MAC must be a unicast MAC (least significant bit of first byte must be zero).

Note

If not using a valid OUI, set the “locally administered” bit (bit value 0x02 in the first byte) to avoid collisions.

Parameters

mac – base MAC address, length: 6 bytes/8 bytes. length: 6 bytes for MAC-48 8 bytes for EUI-64(used for IEEE 802.15.4)

Returns

ESP_OK on success ESP_ERR_INVALID_ARG If mac is NULL or is not a unicast MAC

esp_err_t esp_base_mac_addr_get(uint8_t *mac)

Return base MAC address which is set using esp_base_mac_addr_set.

Note

If no custom Base MAC has been set, this returns the pre-programmed Espressif base MAC address.

Parameters

mac – base MAC address, length: 6 bytes/8 bytes. length: 6 bytes for MAC-48 8 bytes for EUI-64(used for IEEE 802.15.4)

Returns

ESP_OK on success ESP_ERR_INVALID_ARG mac is NULL ESP_ERR_INVALID_MAC base MAC address has not been set

esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)

Return base MAC address which was previously written to BLK3 of EFUSE.

Base MAC address is used to generate the MAC addresses used by the networking interfaces. This API returns the custom base MAC address which was previously written to EFUSE BLK3 in a specified format.

Writing this EFUSE allows setting of a different (non-Espressif) base MAC address. It is also possible to store a custom base MAC address elsewhere, see esp_base_mac_addr_set() for details.

Note

This function is currently only supported on ESP32.

Parameters

mac – base MAC address, length: 6 bytes/8 bytes. length: 6 bytes for MAC-48 8 bytes for EUI-64(used for IEEE 802.15.4)

Returns

ESP_OK on success ESP_ERR_INVALID_ARG mac is NULL ESP_ERR_INVALID_MAC CUSTOM_MAC address has not been set, all zeros (for esp32-xx) ESP_ERR_INVALID_VERSION An invalid MAC version field was read from BLK3 of EFUSE (for esp32) ESP_ERR_INVALID_CRC An invalid MAC CRC was read from BLK3 of EFUSE (for esp32)

esp_err_t esp_efuse_mac_get_default(uint8_t *mac)

Return base MAC address which is factory-programmed by Espressif in EFUSE.

Parameters

mac – base MAC address, length: 6 bytes/8 bytes. length: 6 bytes for MAC-48 8 bytes for EUI-64(used for IEEE 802.15.4)

Returns

ESP_OK on success ESP_ERR_INVALID_ARG mac is NULL

esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type)

Read base MAC address and set MAC address of the interface.

This function first get base MAC address using esp_base_mac_addr_get(). Then calculates the MAC address of the specific interface requested, refer to ESP-IDF Programming Guide for the algorithm.

Parameters
  • mac – base MAC address, length: 6 bytes/8 bytes. length: 6 bytes for MAC-48 8 bytes for EUI-64(used for IEEE 802.15.4)

  • type – Type of MAC address to return

Returns

ESP_OK on success

esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac)

Derive local MAC address from universal MAC address.

This function copies a universal MAC address and then sets the “locally

administered” bit (bit 0x2) in the first octet, creating a locally administered MAC address.

If the universal MAC address argument is already a locally administered MAC address, then the first octet is XORed with 0x4 in order to create a different locally administered MAC address.

Parameters
  • local_mac – base MAC address, length: 6 bytes/8 bytes. length: 6 bytes for MAC-48 8 bytes for EUI-64(used for IEEE 802.15.4)

  • universal_mac – Source universal MAC address, length: 6 bytes.

Returns

ESP_OK on success

Macros

MAC2STR(a)
MACSTR

Enumerations

enum esp_mac_type_t

Values:

enumerator ESP_MAC_WIFI_STA
enumerator ESP_MAC_WIFI_SOFTAP
enumerator ESP_MAC_BT
enumerator ESP_MAC_ETH
enumerator ESP_MAC_IEEE802154

Functions

void esp_chip_info(esp_chip_info_t *out_info)

Fill an esp_chip_info_t structure with information about the chip.

Parameters

out_info[out] structure to be filled

Structures

struct esp_chip_info_t

The structure represents information about the chip.

Public Members

esp_chip_model_t model

chip model, one of esp_chip_model_t

uint32_t features

bit mask of CHIP_FEATURE_x feature flags

uint8_t cores

number of CPU cores

uint8_t revision

chip revision number

Macros

CHIP_FEATURE_EMB_FLASH

Chip has embedded flash memory.

CHIP_FEATURE_WIFI_BGN

Chip has 2.4GHz WiFi.

CHIP_FEATURE_BLE

Chip has Bluetooth LE.

CHIP_FEATURE_BT

Chip has Bluetooth Classic.

CHIP_FEATURE_IEEE802154

Chip has IEEE 802.15.4.

CHIP_FEATURE_EMB_PSRAM

Chip has embedded psram.

Enumerations

enum esp_chip_model_t

Chip models.

Values:

enumerator CHIP_ESP32

ESP32.

enumerator CHIP_ESP32S2

ESP32-S2.

enumerator CHIP_ESP32S3

ESP32-S3.

enumerator CHIP_ESP32C3

ESP32-C3.

enumerator CHIP_ESP32H2

ESP32-H2.

enumerator CHIP_ESP32C2

ESP32-C2.

Functions

static inline void *esp_cpu_get_sp(void)

Read current stack pointer address.

void esp_cpu_stall(int cpu_id)

Stall CPU using RTC controller.

Parameters

cpu_id – ID of the CPU to stall (0 = PRO, 1 = APP)

void esp_cpu_unstall(int cpu_id)

Un-stall CPU using RTC controller.

Parameters

cpu_id – ID of the CPU to un-stall (0 = PRO, 1 = APP)

void esp_cpu_reset(int cpu_id)

Reset CPU using RTC controller.

Parameters

cpu_id – ID of the CPU to reset (0 = PRO, 1 = APP)

bool esp_cpu_in_ocd_debug_mode(void)

Returns true if a JTAG debugger is attached to CPU OCD (on chip debug) port.

Note

If “Make exception and panic handlers JTAG/OCD aware” is disabled, this function always returns false.

static inline esp_cpu_ccount_t esp_cpu_get_ccount(void)
static inline void esp_cpu_set_ccount(esp_cpu_ccount_t val)
void esp_cpu_configure_region_protection(void)

Configure CPU to disable access to invalid memory regions.

esp_err_t esp_cpu_set_watchpoint(int no, void *adr, int size, int flags)

Set a watchpoint to break/panic when a certain memory range is accessed.

Warning

The ESP32 watchpoint hardware watches a region of bytes by effectively masking away the lower n bits for a region with size 2^n. If adr does not have zero for these lower n bits, you may not be watching the region you intended.

Parameters
  • no – Watchpoint number. On the ESP32, this can be 0 or 1.

  • adr – Base address to watch

  • size – Size of the region, starting at the base address, to watch. Must be one of 2^n, with n in [0..6].

  • flags – One of ESP_CPU_WATCHPOINT_* flags

Returns

ESP_ERR_INVALID_ARG on invalid arg, ESP_OK otherwise

void esp_cpu_clear_watchpoint(int no)

Clear a watchpoint.

Parameters

no – Watchpoint to clear

Macros

ESP_CPU_WATCHPOINT_LOAD
ESP_CPU_WATCHPOINT_STORE
ESP_CPU_WATCHPOINT_ACCESS

Type Definitions

typedef uint32_t esp_cpu_ccount_t