ESPHome 2026.3.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 <sched.h>
9#include <time.h>
10#include <cmath>
11#include <cstdlib>
12
13namespace esphome {
14
15void HOT yield() { ::sched_yield(); }
16uint32_t IRAM_ATTR HOT millis() {
17 struct timespec spec;
18 clock_gettime(CLOCK_MONOTONIC, &spec);
19 time_t seconds = spec.tv_sec;
20 uint32_t ms = round(spec.tv_nsec / 1e6);
21 return ((uint32_t) seconds) * 1000U + ms;
22}
23uint64_t millis_64() {
24 struct timespec spec;
25 clock_gettime(CLOCK_MONOTONIC, &spec);
26 return static_cast<uint64_t>(spec.tv_sec) * 1000ULL + static_cast<uint64_t>(spec.tv_nsec) / 1000000ULL;
27}
28void HOT delay(uint32_t ms) {
29 struct timespec ts;
30 ts.tv_sec = ms / 1000;
31 ts.tv_nsec = (ms % 1000) * 1000000;
32 int res;
33 do {
34 res = nanosleep(&ts, &ts);
35 } while (res != 0 && errno == EINTR);
36}
37uint32_t IRAM_ATTR HOT micros() {
38 struct timespec spec;
39 clock_gettime(CLOCK_MONOTONIC, &spec);
40 time_t seconds = spec.tv_sec;
41 uint32_t us = round(spec.tv_nsec / 1e3);
42 return ((uint32_t) seconds) * 1000000U + us;
43}
44void IRAM_ATTR HOT delayMicroseconds(uint32_t us) {
45 struct timespec ts;
46 ts.tv_sec = us / 1000000U;
47 ts.tv_nsec = (us % 1000000U) * 1000U;
48 int res;
49 do {
50 res = nanosleep(&ts, &ts);
51 } while (res != 0 && errno == EINTR);
52}
53void arch_restart() { exit(0); }
54void arch_init() {
55 // pass
56}
57void HOT arch_feed_wdt() {
58 // pass
59}
60
61uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
62uint16_t progmem_read_uint16(const uint16_t *addr) { return *addr; }
64 struct timespec spec;
65 clock_gettime(CLOCK_MONOTONIC, &spec);
66 time_t seconds = spec.tv_sec;
67 uint32_t us = spec.tv_nsec;
68 return ((uint32_t) seconds) * 1000000000U + us;
69}
70uint32_t arch_get_cpu_freq_hz() { return 1000000000U; }
71
72} // namespace esphome
73
74void setup();
75void loop();
76int main() {
78 setup();
79 while (true) {
80 loop();
81 }
82}
83
84#endif // USE_HOST
void setup()
void loop()
int main()
Definition core.cpp:76
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:52
void arch_init()
Definition core.cpp:38
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:29
void HOT yield()
Definition core.cpp:24
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:53
uint64_t HOT millis_64()
Definition core.cpp:26
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:28
void HOT arch_feed_wdt()
Definition core.cpp:48
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition core.cpp:51
void HOT delay(uint32_t ms)
Definition core.cpp:27
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
void arch_restart()
Definition core.cpp:30
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:50