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