ESPHome  2023.8.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 #if ESP_IDF_VERSION_MAJOR >= 4
14 #include <hal/cpu_hal.h>
15 #endif
16 
17 #ifdef USE_ARDUINO
18 #include <esp32-hal.h>
19 #endif
20 
21 void setup();
22 void loop();
23 
24 namespace esphome {
25 
26 void IRAM_ATTR HOT yield() { vPortYield(); }
27 uint32_t IRAM_ATTR HOT millis() { return (uint32_t) (esp_timer_get_time() / 1000ULL); }
28 void IRAM_ATTR HOT delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); }
29 uint32_t IRAM_ATTR HOT micros() { return (uint32_t) esp_timer_get_time(); }
30 void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
31 void arch_restart() {
32  esp_restart();
33  // restart() doesn't always end execution
34  while (true) { // NOLINT(clang-diagnostic-unreachable-code)
35  yield();
36  }
37 }
38 
39 void arch_init() {
40  // Enable the task watchdog only on the loop task (from which we're currently running)
41 #if defined(USE_ESP_IDF)
42  esp_task_wdt_add(nullptr);
43  // Idle task watchdog is disabled on ESP-IDF
44 #elif defined(USE_ARDUINO)
45  enableLoopWDT();
46  // Disable idle task watchdog on the core we're using (Arduino pins the task to a core)
47 #if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0) && CONFIG_ARDUINO_RUNNING_CORE == 0
48  disableCore0WDT();
49 #endif
50 #if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1) && CONFIG_ARDUINO_RUNNING_CORE == 1
51  disableCore1WDT();
52 #endif
53 #endif
54 }
55 void IRAM_ATTR HOT arch_feed_wdt() { esp_task_wdt_reset(); }
56 
57 uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
59 #if ESP_IDF_VERSION_MAJOR >= 4
60  return cpu_hal_get_cycle_count();
61 #else
62  uint32_t ccount;
63  __asm__ __volatile__("esync; rsr %0,ccount" : "=a"(ccount));
64  return ccount;
65 #endif
66 }
67 uint32_t arch_get_cpu_freq_hz() { return rtc_clk_apb_freq_get(); }
68 
69 #ifdef USE_ESP_IDF
70 TaskHandle_t loop_task_handle = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
71 
72 void loop_task(void *pv_params) {
73  setup();
74  while (true) {
75  loop();
76  }
77 }
78 
79 extern "C" void app_main() {
81  xTaskCreate(loop_task, "loopTask", 8192, nullptr, 1, &loop_task_handle);
82 }
83 #endif // USE_ESP_IDF
84 
85 #ifdef USE_ARDUINO
86 extern "C" void init() { esp32::setup_preferences(); }
87 #endif // USE_ARDUINO
88 
89 } // namespace esphome
90 
91 #endif // USE_ESP32
void setup()
void loop()
void app_main()
Definition: core.cpp:79
void IRAM_ATTR HOT arch_feed_wdt()
Definition: core.cpp:55
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:574
uint32_t IRAM_ATTR HOT micros()
Definition: core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:27
void setup_preferences()
void loop_task(void *pv_params)
Definition: core.cpp:72
uint32_t arch_get_cpu_freq_hz()
Definition: core.cpp:67
void arch_restart()
Definition: core.cpp:31
TaskHandle_t loop_task_handle
Definition: core.cpp:70
uint8_t progmem_read_byte(const uint8_t *addr)
Definition: core.cpp:57
void IRAM_ATTR HOT yield()
Definition: core.cpp:26
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:30
void init()
Definition: core.cpp:86
void arch_init()
Definition: core.cpp:39
uint32_t arch_get_cpu_cycle_count()
Definition: core.cpp:58
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:28