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