ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
core.cpp
Go to the documentation of this file.
1#ifdef USE_HOST
2
4#include "esphome/core/hal.h"
6#include "preferences.h"
7
8#include <csignal>
9#include <sched.h>
10#include <time.h>
11#include <cmath>
12#include <cstdlib>
13
14namespace {
15volatile sig_atomic_t s_signal_received = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
16void signal_handler(int signal) { s_signal_received = signal; }
17} // namespace
18
19namespace esphome {
20
21void HOT yield() { ::sched_yield(); }
22uint32_t IRAM_ATTR HOT millis() {
23 struct timespec spec;
24 clock_gettime(CLOCK_MONOTONIC, &spec);
25 time_t seconds = spec.tv_sec;
26 uint32_t ms = round(spec.tv_nsec / 1e6);
27 return ((uint32_t) seconds) * 1000U + ms;
28}
29uint64_t millis_64() {
30 struct timespec spec;
31 clock_gettime(CLOCK_MONOTONIC, &spec);
32 return static_cast<uint64_t>(spec.tv_sec) * 1000ULL + static_cast<uint64_t>(spec.tv_nsec) / 1000000ULL;
33}
34void HOT delay(uint32_t ms) {
35 struct timespec ts;
36 ts.tv_sec = ms / 1000;
37 ts.tv_nsec = (ms % 1000) * 1000000;
38 int res;
39 do {
40 res = nanosleep(&ts, &ts);
41 } while (res != 0 && errno == EINTR);
42}
43uint32_t IRAM_ATTR HOT micros() {
44 struct timespec spec;
45 clock_gettime(CLOCK_MONOTONIC, &spec);
46 time_t seconds = spec.tv_sec;
47 uint32_t us = round(spec.tv_nsec / 1e3);
48 return ((uint32_t) seconds) * 1000000U + us;
49}
50void IRAM_ATTR HOT delayMicroseconds(uint32_t us) {
51 struct timespec ts;
52 ts.tv_sec = us / 1000000U;
53 ts.tv_nsec = (us % 1000000U) * 1000U;
54 int res;
55 do {
56 res = nanosleep(&ts, &ts);
57 } while (res != 0 && errno == EINTR);
58}
59void arch_restart() { exit(0); }
60void arch_init() {
61 // pass
62}
63void HOT arch_feed_wdt() {
64 // pass
65}
66
68 struct timespec spec;
69 clock_gettime(CLOCK_MONOTONIC, &spec);
70 time_t seconds = spec.tv_sec;
71 uint32_t us = spec.tv_nsec;
72 return ((uint32_t) seconds) * 1000000000U + us;
73}
74uint32_t arch_get_cpu_freq_hz() { return 1000000000U; }
75
76} // namespace esphome
77
78void setup();
79void loop();
80int main() {
81 // Install signal handlers for graceful shutdown (flushes preferences to disk)
82 std::signal(SIGINT, signal_handler);
83 std::signal(SIGTERM, signal_handler);
84
86 setup();
87 while (s_signal_received == 0) {
88 loop();
89 }
91 return 0;
92}
93
94#endif // USE_HOST
void setup()
int main()
Definition core.cpp:80
void loop()
void setup_preferences()
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t arch_get_cpu_cycle_count()
Definition core.cpp:56
void arch_init()
Definition core.cpp:39
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
void HOT yield()
Definition core.cpp:25
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:57
uint64_t HOT millis_64()
Definition core.cpp:27
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:29
void HOT arch_feed_wdt()
Definition core.cpp:54
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
void arch_restart()
Definition core.cpp:31
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t