esp_system.h 10 KB

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