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