esp_system.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "sdkconfig.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. typedef enum {
  25. ESP_MAC_WIFI_STA,
  26. ESP_MAC_WIFI_SOFTAP,
  27. ESP_MAC_BT,
  28. ESP_MAC_ETH,
  29. } esp_mac_type_t;
  30. /** @cond */
  31. #define TWO_UNIVERSAL_MAC_ADDR 2
  32. #define FOUR_UNIVERSAL_MAC_ADDR 4
  33. #if CONFIG_IDF_TARGET_ESP32
  34. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES
  35. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  36. #define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32S2_UNIVERSAL_MAC_ADDRESSES
  37. #endif
  38. /** @endcond */
  39. /**
  40. * @brief Reset reasons
  41. */
  42. typedef enum {
  43. ESP_RST_UNKNOWN, //!< Reset reason can not be determined
  44. ESP_RST_POWERON, //!< Reset due to power-on event
  45. ESP_RST_EXT, //!< Reset by external pin (not applicable for ESP32)
  46. ESP_RST_SW, //!< Software reset via esp_restart
  47. ESP_RST_PANIC, //!< Software reset due to exception/panic
  48. ESP_RST_INT_WDT, //!< Reset (software or hardware) due to interrupt watchdog
  49. ESP_RST_TASK_WDT, //!< Reset due to task watchdog
  50. ESP_RST_WDT, //!< Reset due to other watchdogs
  51. ESP_RST_DEEPSLEEP, //!< Reset after exiting deep sleep mode
  52. ESP_RST_BROWNOUT, //!< Brownout reset (software or hardware)
  53. ESP_RST_SDIO, //!< Reset over SDIO
  54. } esp_reset_reason_t;
  55. /** @cond */
  56. /**
  57. * @attention Applications don't need to call this function anymore. It does nothing and will
  58. * be removed in future version.
  59. */
  60. void system_init(void) __attribute__ ((deprecated));
  61. /**
  62. * @brief Reset to default settings.
  63. *
  64. * Function has been deprecated, please use esp_wifi_restore instead.
  65. * This name will be removed in a future release.
  66. */
  67. void system_restore(void) __attribute__ ((deprecated));
  68. /** @endcond */
  69. /**
  70. * Shutdown handler type
  71. */
  72. typedef void (*shutdown_handler_t)(void);
  73. /**
  74. * @brief Register shutdown handler
  75. *
  76. * This function allows you to register a handler that gets invoked before
  77. * the application is restarted using esp_restart function.
  78. * @param handle function to execute on restart
  79. * @return
  80. * - ESP_OK on success
  81. * - ESP_ERR_INVALID_STATE if the handler has already been registered
  82. * - ESP_ERR_NO_MEM if no more shutdown handler slots are available
  83. */
  84. esp_err_t esp_register_shutdown_handler(shutdown_handler_t handle);
  85. /**
  86. * @brief Unregister shutdown handler
  87. *
  88. * This function allows you to unregister a handler which was previously
  89. * registered using esp_register_shutdown_handler function.
  90. * - ESP_OK on success
  91. * - ESP_ERR_INVALID_STATE if the given handler hasn't been registered before
  92. */
  93. esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handle);
  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. /** @cond */
  104. /**
  105. * @brief Restart system.
  106. *
  107. * Function has been renamed to esp_restart.
  108. * This name will be removed in a future release.
  109. */
  110. void system_restart(void) __attribute__ ((deprecated, noreturn));
  111. /** @endcond */
  112. /**
  113. * @brief Get reason of last reset
  114. * @return See description of esp_reset_reason_t for explanation of each value.
  115. */
  116. esp_reset_reason_t esp_reset_reason(void);
  117. /** @cond */
  118. /**
  119. * @brief Get system time, unit: microsecond.
  120. *
  121. * This function is deprecated. Use 'gettimeofday' function for 64-bit precision.
  122. * This definition will be removed in a future release.
  123. */
  124. uint32_t system_get_time(void) __attribute__ ((deprecated));
  125. /** @endcond */
  126. /**
  127. * @brief Get the size of available heap.
  128. *
  129. * Note that the returned value may be larger than the maximum contiguous block
  130. * which can be allocated.
  131. *
  132. * @return Available heap size, in bytes.
  133. */
  134. uint32_t esp_get_free_heap_size(void);
  135. /** @cond */
  136. /**
  137. * @brief Get the size of available heap.
  138. *
  139. * Function has been renamed to esp_get_free_heap_size.
  140. * This name will be removed in a future release.
  141. *
  142. * @return Available heap size, in bytes.
  143. */
  144. uint32_t system_get_free_heap_size(void) __attribute__ ((deprecated));
  145. /** @endcond */
  146. /**
  147. * @brief Get the minimum heap that has ever been available
  148. *
  149. * @return Minimum free heap ever available
  150. */
  151. uint32_t esp_get_minimum_free_heap_size( void );
  152. /**
  153. * @brief Get one random 32-bit word from hardware RNG
  154. *
  155. * The hardware RNG is fully functional whenever an RF subsystem is running (ie Bluetooth or WiFi is enabled). For
  156. * random values, call this function after WiFi or Bluetooth are started.
  157. *
  158. * If the RF subsystem is not used by the program, the function bootloader_random_enable() can be called to enable an
  159. * entropy source. bootloader_random_disable() must be called before RF subsystem or I2S peripheral are used. See these functions'
  160. * documentation for more details.
  161. *
  162. * Any time the app is running without an RF subsystem (or bootloader_random) enabled, RNG hardware should be
  163. * considered a PRNG. A very small amount of entropy is available due to pre-seeding while the IDF
  164. * bootloader is running, but this should not be relied upon for any use.
  165. *
  166. * @return Random value between 0 and UINT32_MAX
  167. */
  168. uint32_t esp_random(void);
  169. /**
  170. * @brief Fill a buffer with random bytes from hardware RNG
  171. *
  172. * @note This function has the same restrictions regarding available entropy as esp_random()
  173. *
  174. * @param buf Pointer to buffer to fill with random numbers.
  175. * @param len Length of buffer in bytes
  176. */
  177. void esp_fill_random(void *buf, size_t len);
  178. /**
  179. * @brief Set base MAC address with the MAC address which is stored in BLK3 of EFUSE or
  180. * external storage e.g. flash and EEPROM.
  181. *
  182. * Base MAC address is used to generate the MAC addresses used by the networking interfaces.
  183. * If using base MAC address stored in BLK3 of EFUSE or external storage, call this API to set base MAC
  184. * address with the MAC address which is stored in BLK3 of EFUSE or external storage before initializing
  185. * WiFi/BT/Ethernet.
  186. *
  187. * @param mac base MAC address, length: 6 bytes.
  188. *
  189. * @return ESP_OK on success
  190. */
  191. esp_err_t esp_base_mac_addr_set(uint8_t *mac);
  192. /**
  193. * @brief Return base MAC address which is set using esp_base_mac_addr_set.
  194. *
  195. * @param mac base MAC address, length: 6 bytes.
  196. *
  197. * @return ESP_OK on success
  198. * ESP_ERR_INVALID_MAC base MAC address has not been set
  199. */
  200. esp_err_t esp_base_mac_addr_get(uint8_t *mac);
  201. /**
  202. * @brief Return base MAC address which was previously written to BLK3 of EFUSE.
  203. *
  204. * Base MAC address is used to generate the MAC addresses used by the networking interfaces.
  205. * This API returns the custom base MAC address which was previously written to BLK3 of EFUSE.
  206. * Writing this EFUSE allows setting of a different (non-Espressif) base MAC address. It is also
  207. * possible to store a custom base MAC address elsewhere, see esp_base_mac_addr_set() for details.
  208. *
  209. * @param mac base MAC address, length: 6 bytes.
  210. *
  211. * @return ESP_OK on success
  212. * ESP_ERR_INVALID_VERSION An invalid MAC version field was read from BLK3 of EFUSE
  213. * ESP_ERR_INVALID_CRC An invalid MAC CRC was read from BLK3 of EFUSE
  214. */
  215. esp_err_t esp_efuse_mac_get_custom(uint8_t *mac);
  216. /**
  217. * @brief Return base MAC address which is factory-programmed by Espressif in BLK0 of EFUSE.
  218. *
  219. * @param mac base MAC address, length: 6 bytes.
  220. *
  221. * @return ESP_OK on success
  222. */
  223. esp_err_t esp_efuse_mac_get_default(uint8_t *mac);
  224. /** @cond */
  225. /**
  226. * @brief Read hardware MAC address from efuse.
  227. *
  228. * Function has been renamed to esp_efuse_mac_get_default.
  229. * This name will be removed in a future release.
  230. *
  231. * @param mac hardware MAC address, length: 6 bytes.
  232. *
  233. * @return ESP_OK on success
  234. */
  235. esp_err_t esp_efuse_read_mac(uint8_t *mac) __attribute__ ((deprecated));
  236. /**
  237. * @brief Read hardware MAC address.
  238. *
  239. * Function has been renamed to esp_efuse_mac_get_default.
  240. * This name will be removed in a future release.
  241. *
  242. * @param mac hardware MAC address, length: 6 bytes.
  243. * @return ESP_OK on success
  244. */
  245. esp_err_t system_efuse_read_mac(uint8_t *mac) __attribute__ ((deprecated));
  246. /** @endcond */
  247. /**
  248. * @brief Read base MAC address and set MAC address of the interface.
  249. *
  250. * This function first get base MAC address using esp_base_mac_addr_get or reads base MAC address
  251. * from BLK0 of EFUSE. Then set the MAC address of the interface including wifi station, wifi softap,
  252. * bluetooth and ethernet.
  253. *
  254. * @param mac MAC address of the interface, length: 6 bytes.
  255. * @param type type of MAC address, 0:wifi station, 1:wifi softap, 2:bluetooth, 3:ethernet.
  256. *
  257. * @return ESP_OK on success
  258. */
  259. esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type);
  260. /**
  261. * @brief Derive local MAC address from universal MAC address.
  262. *
  263. * This function derives a local MAC address from an universal MAC address.
  264. * A `definition of local vs universal MAC address can be found on Wikipedia
  265. * <https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local>`.
  266. * In ESP32, universal MAC address is generated from base MAC address in EFUSE or other external storage.
  267. * Local MAC address is derived from the universal MAC address.
  268. *
  269. * @param local_mac Derived local MAC address, length: 6 bytes.
  270. * @param universal_mac Source universal MAC address, length: 6 bytes.
  271. *
  272. * @return ESP_OK on success
  273. */
  274. esp_err_t esp_derive_local_mac(uint8_t* local_mac, const uint8_t* universal_mac);
  275. /** @cond */
  276. /**
  277. * Get SDK version
  278. *
  279. * This function is deprecated and will be removed in a future release.
  280. *
  281. * @return constant string "master"
  282. */
  283. const char* system_get_sdk_version(void) __attribute__ ((deprecated));
  284. /** @endcond */
  285. /**
  286. * Get IDF version
  287. *
  288. * @return constant string from IDF_VER
  289. */
  290. const char* esp_get_idf_version(void);
  291. /**
  292. * @brief Chip models
  293. */
  294. typedef enum {
  295. CHIP_ESP32 = 1, //!< ESP32
  296. CHIP_ESP32S2BETA = 2, //!< ESP32S2BETA
  297. } esp_chip_model_t;
  298. /* Chip feature flags, used in esp_chip_info_t */
  299. #define CHIP_FEATURE_EMB_FLASH BIT(0) //!< Chip has embedded flash memory
  300. #define CHIP_FEATURE_WIFI_BGN BIT(1) //!< Chip has 2.4GHz WiFi
  301. #define CHIP_FEATURE_BLE BIT(4) //!< Chip has Bluetooth LE
  302. #define CHIP_FEATURE_BT BIT(5) //!< Chip has Bluetooth Classic
  303. /**
  304. * @brief The structure represents information about the chip
  305. */
  306. typedef struct {
  307. esp_chip_model_t model; //!< chip model, one of esp_chip_model_t
  308. uint32_t features; //!< bit mask of CHIP_FEATURE_x feature flags
  309. uint8_t cores; //!< number of CPU cores
  310. uint8_t revision; //!< chip revision number
  311. } esp_chip_info_t;
  312. /**
  313. * @brief Fill an esp_chip_info_t structure with information about the chip
  314. * @param[out] out_info structure to be filled
  315. */
  316. void esp_chip_info(esp_chip_info_t* out_info);
  317. #ifdef __cplusplus
  318. }
  319. #endif
  320. #endif /* __ESP_SYSTEM_H__ */