esp_system.h
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef __ESP_SYSTEM_H__ 16 #define __ESP_SYSTEM_H__ 17 18 #include <stdint.h> 19 #include <stdbool.h> 20 #include "esp_err.h" 21 #include "esp_attr.h" 22 #include "esp_bit_defs.h" 23 #include "esp_idf_version.h" 24 25 #include "sdkconfig.h" 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 typedef enum { 32 ESP_MAC_WIFI_STA, 33 ESP_MAC_WIFI_SOFTAP, 34 ESP_MAC_BT, 35 ESP_MAC_ETH, 36 } esp_mac_type_t; 37 38 /** @cond */ 39 #define TWO_UNIVERSAL_MAC_ADDR 2 40 #define FOUR_UNIVERSAL_MAC_ADDR 4 41 #if CONFIG_IDF_TARGET_ESP32 42 #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES 43 #elif CONFIG_IDF_TARGET_ESP32S2 44 #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32S2_UNIVERSAL_MAC_ADDRESSES 45 #endif 46 /** @endcond */ 47 48 /** 49 * @brief Reset reasons 50 */ 51 typedef enum { 52 ESP_RST_UNKNOWN, //!< Reset reason can not be determined 53 ESP_RST_POWERON, //!< Reset due to power-on event 54 ESP_RST_EXT, //!< Reset by external pin (not applicable for ESP32) 55 ESP_RST_SW, //!< Software reset via esp_restart 56 ESP_RST_PANIC, //!< Software reset due to exception/panic 57 ESP_RST_INT_WDT, //!< Reset (software or hardware) due to interrupt watchdog 58 ESP_RST_TASK_WDT, //!< Reset due to task watchdog 59 ESP_RST_WDT, //!< Reset due to other watchdogs 60 ESP_RST_DEEPSLEEP, //!< Reset after exiting deep sleep mode 61 ESP_RST_BROWNOUT, //!< Brownout reset (software or hardware) 62 ESP_RST_SDIO, //!< Reset over SDIO 63 } esp_reset_reason_t; 64 65 /** 66 * Shutdown handler type 67 */ 68 typedef void (*shutdown_handler_t)(void); 69 70 /** 71 * @brief Register shutdown handler 72 * 73 * This function allows you to register a handler that gets invoked before 74 * the application is restarted using esp_restart function. 75 * @param handle function to execute on restart 76 * @return 77 * - ESP_OK on success 78 * - ESP_ERR_INVALID_STATE if the handler has already been registered 79 * - ESP_ERR_NO_MEM if no more shutdown handler slots are available 80 */ 81 esp_err_t esp_register_shutdown_handler(shutdown_handler_t handle); 82 83 /** 84 * @brief Unregister shutdown handler 85 * 86 * This function allows you to unregister a handler which was previously 87 * registered using esp_register_shutdown_handler function. 88 * - ESP_OK on success 89 * - ESP_ERR_INVALID_STATE if the given handler hasn't been registered before 90 */ 91 esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handle); 92 93 94 /** 95 * @brief Restart PRO and APP CPUs. 96 * 97 * This function can be called both from PRO and APP CPUs. 98 * After successful restart, CPU reset reason will be SW_CPU_RESET. 99 * Peripherals (except for WiFi, BT, UART0, SPI1, and legacy timers) are not reset. 100 * This function does not return. 101 */ 102 void esp_restart(void) __attribute__ ((noreturn)); 103 104 /** 105 * @brief Get reason of last reset 106 * @return See description of esp_reset_reason_t for explanation of each value. 107 */ 108 esp_reset_reason_t esp_reset_reason(void); 109 110 /** 111 * @brief Get the size of available heap. 112 * 113 * Note that the returned value may be larger than the maximum contiguous block 114 * which can be allocated. 115 * 116 * @return Available heap size, in bytes. 117 */ 118 uint32_t esp_get_free_heap_size(void); 119 120 /** 121 * @brief Get the size of available internal heap. 122 * 123 * Note that the returned value may be larger than the maximum contiguous block 124 * which can be allocated. 125 * 126 * @return Available internal heap size, in bytes. 127 */ 128 uint32_t esp_get_free_internal_heap_size(void); 129 130 /** 131 * @brief Get the minimum heap that has ever been available 132 * 133 * @return Minimum free heap ever available 134 */ 135 uint32_t esp_get_minimum_free_heap_size( void ); 136 137 /** 138 * @brief Get one random 32-bit word from hardware RNG 139 * 140 * The hardware RNG is fully functional whenever an RF subsystem is running (ie Bluetooth or WiFi is enabled). For 141 * random values, call this function after WiFi or Bluetooth are started. 142 * 143 * If the RF subsystem is not used by the program, the function bootloader_random_enable() can be called to enable an 144 * entropy source. bootloader_random_disable() must be called before RF subsystem or I2S peripheral are used. See these functions' 145 * documentation for more details. 146 * 147 * Any time the app is running without an RF subsystem (or bootloader_random) enabled, RNG hardware should be 148 * considered a PRNG. A very small amount of entropy is available due to pre-seeding while the IDF 149 * bootloader is running, but this should not be relied upon for any use. 150 * 151 * @return Random value between 0 and UINT32_MAX 152 */ 153 uint32_t esp_random(void); 154 155 /** 156 * @brief Fill a buffer with random bytes from hardware RNG 157 * 158 * @note This function has the same restrictions regarding available entropy as esp_random() 159 * 160 * @param buf Pointer to buffer to fill with random numbers. 161 * @param len Length of buffer in bytes 162 */ 163 void esp_fill_random(void *buf, size_t len); 164 165 /** 166 * @brief Set base MAC address with the MAC address which is stored in BLK3 of EFUSE or 167 * external storage e.g. flash and EEPROM. 168 * 169 * Base MAC address is used to generate the MAC addresses used by the networking interfaces. 170 * If using base MAC address stored in BLK3 of EFUSE or external storage, call this API to set base MAC 171 * address with the MAC address which is stored in BLK3 of EFUSE or external storage before initializing 172 * WiFi/BT/Ethernet. 173 * 174 * @note Base MAC must be a unicast MAC (least significant bit of first byte must be zero). 175 * 176 * @note If not using a valid OUI, set the "locally administered" bit 177 * (bit value 0x02 in the first byte) to avoid collisions. 178 * 179 * @param mac base MAC address, length: 6 bytes. 180 * 181 * @return ESP_OK on success 182 * ESP_ERR_INVALID_ARG If mac is NULL or is not a unicast MAC 183 */ 184 esp_err_t esp_base_mac_addr_set(const uint8_t *mac); 185 186 /** 187 * @brief Return base MAC address which is set using esp_base_mac_addr_set. 188 * 189 * @param mac base MAC address, length: 6 bytes. 190 * 191 * @return ESP_OK on success 192 * ESP_ERR_INVALID_MAC base MAC address has not been set 193 */ 194 esp_err_t esp_base_mac_addr_get(uint8_t *mac); 195 196 /** 197 * @brief Return base MAC address which was previously written to BLK3 of EFUSE. 198 * 199 * Base MAC address is used to generate the MAC addresses used by the networking interfaces. 200 * This API returns the custom base MAC address which was previously written to BLK3 of EFUSE. 201 * Writing this EFUSE allows setting of a different (non-Espressif) base MAC address. It is also 202 * possible to store a custom base MAC address elsewhere, see esp_base_mac_addr_set() for details. 203 * 204 * @param mac base MAC address, length: 6 bytes. 205 * 206 * @return ESP_OK on success 207 * ESP_ERR_INVALID_VERSION An invalid MAC version field was read from BLK3 of EFUSE 208 * ESP_ERR_INVALID_CRC An invalid MAC CRC was read from BLK3 of EFUSE 209 */ 210 esp_err_t esp_efuse_mac_get_custom(uint8_t *mac); 211 212 /** 213 * @brief Return base MAC address which is factory-programmed by Espressif in BLK0 of EFUSE. 214 * 215 * @param mac base MAC address, length: 6 bytes. 216 * 217 * @return ESP_OK on success 218 */ 219 esp_err_t esp_efuse_mac_get_default(uint8_t *mac); 220 221 /** 222 * @brief Read base MAC address and set MAC address of the interface. 223 * 224 * This function first get base MAC address using esp_base_mac_addr_get or reads base MAC address 225 * from BLK0 of EFUSE. Then set the MAC address of the interface including wifi station, wifi softap, 226 * bluetooth and ethernet. 227 * 228 * @param mac MAC address of the interface, length: 6 bytes. 229 * @param type type of MAC address, 0:wifi station, 1:wifi softap, 2:bluetooth, 3:ethernet. 230 * 231 * @return ESP_OK on success 232 */ 233 esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type); 234 235 /** 236 * @brief Derive local MAC address from universal MAC address. 237 * 238 * This function derives a local MAC address from an universal MAC address. 239 * A `definition of local vs universal MAC address can be found on Wikipedia 240 * <https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local>`. 241 * In ESP32, universal MAC address is generated from base MAC address in EFUSE or other external storage. 242 * Local MAC address is derived from the universal MAC address. 243 * 244 * @param local_mac Derived local MAC address, length: 6 bytes. 245 * @param universal_mac Source universal MAC address, length: 6 bytes. 246 * 247 * @return ESP_OK on success 248 */ 249 esp_err_t esp_derive_local_mac(uint8_t* local_mac, const uint8_t* universal_mac); 250 251 /** 252 * @brief Trigger a software abort 253 * 254 * @param details Details that will be displayed during panic handling. 255 */ 256 void __attribute__((noreturn)) esp_system_abort(const char* details); 257 258 /** 259 * @brief Chip models 260 */ 261 typedef enum { 262 CHIP_ESP32 = 1, //!< ESP32 263 CHIP_ESP32S2 = 2, //!< ESP32-S2 264 CHIP_ESP32S3 = 4, //!< ESP32-S3 265 } esp_chip_model_t; 266 267 /* Chip feature flags, used in esp_chip_info_t */ 268 #define CHIP_FEATURE_EMB_FLASH BIT(0) //!< Chip has embedded flash memory 269 #define CHIP_FEATURE_WIFI_BGN BIT(1) //!< Chip has 2.4GHz WiFi 270 #define CHIP_FEATURE_BLE BIT(4) //!< Chip has Bluetooth LE 271 #define CHIP_FEATURE_BT BIT(5) //!< Chip has Bluetooth Classic 272 273 /** 274 * @brief The structure represents information about the chip 275 */ 276 typedef struct { 277 esp_chip_model_t model; //!< chip model, one of esp_chip_model_t 278 uint32_t features; //!< bit mask of CHIP_FEATURE_x feature flags 279 uint8_t cores; //!< number of CPU cores 280 uint8_t revision; //!< chip revision number 281 } esp_chip_info_t; 282 283 /** 284 * @brief Fill an esp_chip_info_t structure with information about the chip 285 * @param[out] out_info structure to be filled 286 */ 287 void esp_chip_info(esp_chip_info_t* out_info); 288 289 290 #if CONFIG_ESP32_ECO3_CACHE_LOCK_FIX 291 /** 292 * @brief Cache lock bug exists or not 293 * 294 * @return 295 * - ture : bug exists 296 * - false : bug not exists 297 */ 298 bool soc_has_cache_lock_bug(void); 299 #endif 300 301 #ifdef __cplusplus 302 } 303 #endif 304 305 #endif /* __ESP_SYSTEM_H__ */