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 // For backward compatibility. These headers
28 // contains hardware operation functions and definitions
29 // that were originally declared in this header.
30 #include "esp_mac.h"
31 #include "esp_chip_info.h"
32 #include "esp_random.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @brief Reset reasons
40  */
41 typedef enum {
42     ESP_RST_UNKNOWN,    //!< Reset reason can not be determined
43     ESP_RST_POWERON,    //!< Reset due to power-on event
44     ESP_RST_EXT,        //!< Reset by external pin (not applicable for ESP32)
45     ESP_RST_SW,         //!< Software reset via esp_restart
46     ESP_RST_PANIC,      //!< Software reset due to exception/panic
47     ESP_RST_INT_WDT,    //!< Reset (software or hardware) due to interrupt watchdog
48     ESP_RST_TASK_WDT,   //!< Reset due to task watchdog
49     ESP_RST_WDT,        //!< Reset due to other watchdogs
50     ESP_RST_DEEPSLEEP,  //!< Reset after exiting deep sleep mode
51     ESP_RST_BROWNOUT,   //!< Brownout reset (software or hardware)
52     ESP_RST_SDIO,       //!< Reset over SDIO
53 } esp_reset_reason_t;
54 
55 /**
56  * Shutdown handler type
57  */
58 typedef void (*shutdown_handler_t)(void);
59 
60 /**
61   * @brief  Register shutdown handler
62   *
63   * This function allows you to register a handler that gets invoked before
64   * the application is restarted using esp_restart function.
65   * @param handle function to execute on restart
66   * @return
67   *   - ESP_OK on success
68   *   - ESP_ERR_INVALID_STATE if the handler has already been registered
69   *   - ESP_ERR_NO_MEM if no more shutdown handler slots are available
70   */
71 esp_err_t esp_register_shutdown_handler(shutdown_handler_t handle);
72 
73 /**
74   * @brief  Unregister shutdown handler
75   *
76   * This function allows you to unregister a handler which was previously
77   * registered using esp_register_shutdown_handler function.
78   *   - ESP_OK on success
79   *   - ESP_ERR_INVALID_STATE if the given handler hasn't been registered before
80   */
81 esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handle);
82 
83 
84 /**
85   * @brief  Restart PRO and APP CPUs.
86   *
87   * This function can be called both from PRO and APP CPUs.
88   * After successful restart, CPU reset reason will be SW_CPU_RESET.
89   * Peripherals (except for WiFi, BT, UART0, SPI1, and legacy timers) are not reset.
90   * This function does not return.
91   */
92 void esp_restart(void) __attribute__ ((noreturn));
93 
94 /**
95  * @brief  Get reason of last reset
96  * @return See description of esp_reset_reason_t for explanation of each value.
97  */
98 esp_reset_reason_t esp_reset_reason(void);
99 
100 /**
101   * @brief  Get the size of available heap.
102   *
103   * Note that the returned value may be larger than the maximum contiguous block
104   * which can be allocated.
105   *
106   * @return Available heap size, in bytes.
107   */
108 uint32_t esp_get_free_heap_size(void);
109 
110 /**
111   * @brief  Get the size of available internal 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 internal heap size, in bytes.
117   */
118 uint32_t esp_get_free_internal_heap_size(void);
119 
120 /**
121   * @brief Get the minimum heap that has ever been available
122   *
123   * @return Minimum free heap ever available
124   */
125 uint32_t esp_get_minimum_free_heap_size( void );
126 
127 /**
128  * @brief Trigger a software abort
129  *
130  * @param details Details that will be displayed during panic handling.
131  */
132 void  __attribute__((noreturn)) esp_system_abort(const char* details);
133 
134 #ifdef __cplusplus
135 }
136 #endif
137 
138 #endif /* __ESP_SYSTEM_H__ */
139