ESPHome  2024.11.3
core.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32
2 
3 #include "esphome/core/hal.h"
4 #include "esphome/core/helpers.h"
5 #include "preferences.h"
6 #include <freertos/FreeRTOS.h>
7 #include <freertos/task.h>
8 #include <esp_idf_version.h>
9 #include <esp_task_wdt.h>
10 #include <esp_timer.h>
11 #include <soc/rtc.h>
12 
13 #include <hal/cpu_hal.h>
14 
15 #ifdef USE_ARDUINO
16 #include <esp32-hal.h>
17 #endif
18 
19 void setup();
20 void loop();
21 
22 namespace esphome {
23 
24 void IRAM_ATTR HOT yield() { vPortYield(); }
25 uint32_t IRAM_ATTR HOT millis() { return (uint32_t) (esp_timer_get_time() / 1000ULL); }
26 void IRAM_ATTR HOT delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); }
27 uint32_t IRAM_ATTR HOT micros() { return (uint32_t) esp_timer_get_time(); }
28 void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
29 void arch_restart() {
30  esp_restart();
31  // restart() doesn't always end execution
32  while (true) { // NOLINT(clang-diagnostic-unreachable-code)
33  yield();
34  }
35 }
36 
37 void arch_init() {
38  // Enable the task watchdog only on the loop task (from which we're currently running)
39 #if defined(USE_ESP_IDF)
40  esp_task_wdt_add(nullptr);
41  // Idle task watchdog is disabled on ESP-IDF
42 #elif defined(USE_ARDUINO)
43  enableLoopWDT();
44  // Disable idle task watchdog on the core we're using (Arduino pins the task to a core)
45 #if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0) && CONFIG_ARDUINO_RUNNING_CORE == 0
46  disableCore0WDT();
47 #endif
48 #if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1) && CONFIG_ARDUINO_RUNNING_CORE == 1
49  disableCore1WDT();
50 #endif
51 #endif
52 }
53 void IRAM_ATTR HOT arch_feed_wdt() { esp_task_wdt_reset(); }
54 
55 uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
56 #if ESP_IDF_VERSION_MAJOR >= 5
57 uint32_t arch_get_cpu_cycle_count() { return esp_cpu_get_cycle_count(); }
58 #else
59 uint32_t arch_get_cpu_cycle_count() { return cpu_hal_get_cycle_count(); }
60 #endif
61 uint32_t arch_get_cpu_freq_hz() { return rtc_clk_apb_freq_get(); }
62 
63 #ifdef USE_ESP_IDF
64 TaskHandle_t loop_task_handle = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
65 
66 void loop_task(void *pv_params) {
67  setup();
68  while (true) {
69  loop();
70  }
71 }
72 
73 extern "C" void app_main() {
75  xTaskCreate(loop_task, "loopTask", 8192, nullptr, 1, &loop_task_handle);
76 }
77 #endif // USE_ESP_IDF
78 
79 #ifdef USE_ARDUINO
80 extern "C" void init() { esp32::setup_preferences(); }
81 #endif // USE_ARDUINO
82 
83 } // namespace esphome
84 
85 #endif // USE_ESP32
void setup()
void loop()
void app_main()
Definition: core.cpp:73
void IRAM_ATTR HOT arch_feed_wdt()
Definition: core.cpp:53
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait...
Definition: helpers.cpp:765
uint32_t IRAM_ATTR HOT micros()
Definition: core.cpp:27
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void setup_preferences()
void loop_task(void *pv_params)
Definition: core.cpp:66
uint32_t arch_get_cpu_freq_hz()
Definition: core.cpp:61
void arch_restart()
Definition: core.cpp:29
TaskHandle_t loop_task_handle
Definition: core.cpp:64
uint8_t progmem_read_byte(const uint8_t *addr)
Definition: core.cpp:55
void IRAM_ATTR HOT yield()
Definition: core.cpp:24
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
void init()
Definition: core.cpp:80
void arch_init()
Definition: core.cpp:37
uint32_t arch_get_cpu_cycle_count()
Definition: core.cpp:57
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26