rtctime_internal.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * Copyright 2015 Dius Computing Pty Ltd. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * - Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the
  13. * distribution.
  14. * - Neither the name of the copyright holders nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  27. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  29. * OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * @author Bernd Meyer <bmeyer@dius.com.au>
  32. * @author Johny Mattsson <jmattsson@dius.com.au>
  33. */
  34. #ifndef _RTCTIME_INTERNAL_H_
  35. #define _RTCTIME_INTERNAL_H_
  36. /*
  37. * The ESP8266 has four distinct power states:
  38. *
  39. * 1) Active --- CPU and modem are powered and running
  40. * 2) Modem Sleep --- CPU is active, but the RF section is powered down
  41. * 3) Light Sleep --- CPU is halted, RF section is powered down. CPU gets reactivated by interrupt
  42. * 4) Deep Sleep --- CPU and RF section are powered down, restart requires a full reset
  43. *
  44. * There are also three (relevant) sources of time information
  45. *
  46. * A) CPU Cycle Counter --- this is incremented at the CPU frequency in modes (1) and (2), but is
  47. * halted in state (3), and gets reset in state (4). Highly precise 32 bit counter
  48. * which overflows roughly every minute. Starts counting as soon as the CPU becomes
  49. * active after a reset. Can cause an interrupt when it hits a particular value;
  50. * This interrupt (and the register that determines the comparison value) are not
  51. * used by the system software, and are available for user code to use.
  52. *
  53. * B) Free Running Counter 2 --- This is a peripheral which gets configured to run at 1/256th of the
  54. * CPU frequency. It is also active in states (1) and (2), and is halted in state (3).
  55. * However, the ESP system code will adjust its value across periods of Light Sleep
  56. * that it initiates, so *in effect*, this counter kind-of remains active in (3).
  57. * While in states (1) and (2), it is as precise as the CPU Cycle. While in state (3),
  58. * however, it is only as precise as the system's knowledge of how long the sleep
  59. * period was. This knowledge is limited (it is based on (C), see below).
  60. * The Free Running Counter 2 is a 32 bit counter which overflows roughly every
  61. * 4 hours, and typically has a resolution of 3.2us. It starts counting as soon as
  62. * it gets configured, which is considerably *after* the time of reset, and in fact
  63. * is not done by the ESP boot loader, but rather by the loaded-from-SPI-flash system
  64. * code. This means it is not yet running when the boot loader calls the configured
  65. * entry point, and the time between reset and the counter starting to run depends on
  66. * the size of code/data to be copied into RAM from the flash.
  67. * The FRC2 is also used by the system software for its internal time keeping, i.e. for
  68. * dealing with any registered ETS_Timers (and derived-from-them timer functionality).
  69. *
  70. * C) "Real Time Clock" --- This peripheral runs from an internal low power RC oscillator, at a frequency
  71. * somewhere in the 120-200kHz range. It keeps running in all power states, and is in
  72. * fact the time source responsible for generating an interrupt (state (3)) or reset
  73. * (state (4)) to end Light and Deep Sleep periods. However, it *does* get reset to
  74. * zero after a reset, even one it caused itself.
  75. * The major issue with the RTC is that it is not using a crystal (support for an
  76. * external 32.768kHz crystal was planned at one point, but was removed from the
  77. * final ESP8266 design), and thus the frequency of the oscillator is dependent on
  78. * a number of parameters, including the chip temperature. The ESP's system software
  79. * contains code to "calibrate" exactly how long one cycle of the oscillator is, and
  80. * uses that calibration to work out how many cycles to sleep for modes (3) and (4).
  81. * However, once the chip has entered a low power state, it quickly cools down, which
  82. * results in the oscillator running faster than during calibration, leading to early
  83. * wakeups. This effect is small (even in relative terms) for short sleep periods (because
  84. * the temperature does not change much over a few hundred milliseconds), but can get
  85. * quite large for extended sleeps.
  86. *
  87. * For added fun, a typical ESP8266 module starts up running the CPU (and thus the cycle counter) at 52MHz,
  88. * but usually this will be switched to 80MHz on application startup, and can later be switched to 160MHz
  89. * under user control. Meanwhile, the FRC2 is usually kept running at 80MHz/256, regardless of the CPU
  90. * clock.
  91. *
  92. *
  93. *
  94. * The code in this file implements a best-effort time keeping solution for the ESP. It keeps track of time
  95. * by switching between various time sources. All state is kept in RAM associated with the RTC, which is
  96. * maintained across Deep Sleep periods.
  97. *
  98. * Internally, time is managed in units of cycles of a (hypothetical) 2080MHz clock, e.g. in units
  99. * of 0.4807692307ns. The reason for this choice is that this covers both the FRC2 and the cycle
  100. * counter periods, while running at 52MHz, 80MHz or 160MHz.
  101. *
  102. * At any given time, the time status indicates whether the FRC2 or the Cycle Counter is the current time
  103. * source, how many unit cycles each LSB of the chosen time source "is worth", and what the unix time in
  104. * unit cycles was when the time source was at 0.
  105. * Given that either time source overflows its 32 bit counter in a relatively short time, the code also
  106. * maintains a "last read 32 bit value" for the selected time source, and on each subsequent read will
  107. * check for overflow and, if necessary, adjust the unix-time-at-time-source-being-zero appropriately.
  108. * In order to avoid missing overflows, a timer gets installed which requests time every 40 seconds.
  109. *
  110. * To avoid race conditions, *none* of the code here must be called from an interrupt context unless
  111. * the user can absolutely guarantee that there will never be a clock source rollover (which can be the
  112. * case for sensor applications that only stay awake for a few seconds). And even then, do so at your
  113. * own risk.
  114. *
  115. *
  116. * Deep sleep is handled by moving the time offset forward *before* the sleep to the scheduled wakeup
  117. * time. Due to the nature of the RTC, the actual wakeup time may be a little bit different, but
  118. * it's the best that can be done. The code attempts to come up with a better calibration value if
  119. * authoritative time is available both before and after a sleep; This works reasonably well, but of
  120. * course is still merely a guess, which may well be somewhat wrong.
  121. *
  122. */
  123. #include <osapi.h>
  124. #include <ets_sys.h>
  125. #include "rom.h"
  126. #include "rtcaccess.h"
  127. #include "user_interface.h"
  128. // Layout of the RTC storage space:
  129. //
  130. // 0: Magic, and time source. Meaningful values are
  131. // * RTC_TIME_MAGIC_SLEEP: Indicates that the device went to sleep under RTCTIME control.
  132. // This is the magic expected on deep sleep wakeup; Any other status means we lost track
  133. // of time, and whatever time offset is stored in state is invalid and must be cleared.
  134. // * RTC_TIME_MAGIC_CCOUNT: Time offset is relative to the Cycle Counter.
  135. // * RTC_TIME_MAGIC_FRC2: Time offset is relative to the Free Running Counter.
  136. // Any values other than these indicate that RTCTIME is not in use and no state is available, nor should
  137. // RTCTIME make any changes to any of the RTC memory space.
  138. //
  139. // 1/2: UNIX time in Unit Cycles when time source had value 0 (64 bit, lower 32 bit in 1, upper in 2).
  140. // If 0, then time is unknown.
  141. // 3: Last used value of time source (32 bit unsigned). If current time source is less, then a rollover happened
  142. // 4: Length of a time source cycle in Unit Cycles.
  143. // 5: cached result of sleep clock calibration. Has the format of system_rtc_clock_cali_proc(),
  144. // or 0 if not available (see 6/7 below)
  145. // 6: Number of microseconds we tried to sleep, or 0 if we didn't sleep since last calibration, ffffffff if invalid
  146. // 7: Number of RTC cycles we decided to sleep, or 0 if we didn't sleep since last calibration, ffffffff if invalid
  147. // 8: Number of microseconds which we add to (1/2) to avoid time going backwards
  148. // 9: microsecond value returned in the last gettimeofday() to "user space".
  149. //
  150. // Entries 6-9 are needed because the RTC cycles/second appears quite temperature dependent,
  151. // and thus is heavily influenced by what else the chip is doing. As such, any calibration against
  152. // the crystal-provided clock (which necessarily would have to happen while the chip is active and
  153. // burning a few milliwatts) will be significantly different from the actual frequency during deep
  154. // sleep.
  155. // Thus, in order to calibrate for deep sleep conditions, we keep track of total sleep microseconds
  156. // and total sleep clock cycles between settimeofday() calls (which presumably are NTP driven), and
  157. // adjust the calibration accordingly on each settimeofday(). This will also track frequency changes
  158. // due to ambient temperature changes.
  159. // 8/9 get used when a settimeofday() would result in turning back time. As that can cause all sorts
  160. // of ugly issues, we *do* adjust (1/2), but compensate by making the same adjustment to (8). Then each
  161. // time gettimeofday() is called, we inspect (9) and determine how much time has passed since the last
  162. // call (yes, this gets it wrong if more than a second has passed, but not in a way that causes issues)
  163. // and try to take up to 6% of that time away from (8) until (8) reaches 0. Also, whenever we go to
  164. // deep sleep, we add (8) to the sleep time, thus catching up all in one go.
  165. // Note that for calculating the next sample-aligned wakeup, we need to use the post-adjustment
  166. // timeofday(), but for calculating actual sleep time, we use the pre-adjustment one, thus bringing
  167. // things back into line.
  168. //
  169. #define RTC_TIME_BASE 0 // Where the RTC timekeeping block starts in RTC user memory slots
  170. #define RTC_TIME_MAGIC_CCOUNT 0x44695573
  171. #define RTC_TIME_MAGIC_FRC2 (RTC_TIME_MAGIC_CCOUNT+1)
  172. #define RTC_TIME_MAGIC_SLEEP (RTC_TIME_MAGIC_CCOUNT+2)
  173. #define UNITCYCLE_MHZ 2080
  174. #define CPU_OVERCLOCK_MHZ 160
  175. #define CPU_DEFAULT_MHZ 80
  176. #define CPU_BOOTUP_MHZ 52
  177. // RTCTIME storage
  178. #define RTC_TIME_MAGIC_POS (RTC_TIME_BASE+0)
  179. #define RTC_CYCLEOFFSETL_POS (RTC_TIME_BASE+1)
  180. #define RTC_CYCLEOFFSETH_POS (RTC_TIME_BASE+2)
  181. #define RTC_LASTSOURCEVAL_POS (RTC_TIME_BASE+3)
  182. #define RTC_SOURCECYCLEUNITS_POS (RTC_TIME_BASE+4)
  183. #define RTC_CALIBRATION_POS (RTC_TIME_BASE+5)
  184. #define RTC_SLEEPTOTALUS_POS (RTC_TIME_BASE+6)
  185. #define RTC_SLEEPTOTALCYCLES_POS (RTC_TIME_BASE+7)
  186. #define RTC_TODOFFSETUS_POS (RTC_TIME_BASE+8)
  187. #define RTC_LASTTODUS_POS (RTC_TIME_BASE+9)
  188. struct rtc_timeval
  189. {
  190. uint32_t tv_sec;
  191. uint32_t tv_usec;
  192. };
  193. static inline uint64_t rtc_time_get_now_us_adjusted();
  194. static inline uint32_t rtc_time_get_magic(void)
  195. {
  196. return rtc_mem_read(RTC_TIME_MAGIC_POS);
  197. }
  198. static inline bool rtc_time_check_sleep_magic(void)
  199. {
  200. uint32_t magic=rtc_time_get_magic();
  201. return (magic==RTC_TIME_MAGIC_SLEEP);
  202. }
  203. static inline bool rtc_time_check_wake_magic(void)
  204. {
  205. uint32_t magic=rtc_time_get_magic();
  206. return (magic==RTC_TIME_MAGIC_FRC2 || magic==RTC_TIME_MAGIC_CCOUNT);
  207. }
  208. static inline bool rtc_time_check_magic(void)
  209. {
  210. uint32_t magic=rtc_time_get_magic();
  211. return (magic==RTC_TIME_MAGIC_FRC2 || magic==RTC_TIME_MAGIC_CCOUNT || magic==RTC_TIME_MAGIC_SLEEP);
  212. }
  213. static inline void rtc_time_set_magic(uint32_t new_magic)
  214. {
  215. rtc_mem_write(RTC_TIME_MAGIC_POS,new_magic);
  216. }
  217. static inline void rtc_time_set_sleep_magic(void)
  218. {
  219. rtc_time_set_magic(RTC_TIME_MAGIC_SLEEP);
  220. }
  221. static inline void rtc_time_set_ccount_magic(void)
  222. {
  223. rtc_time_set_magic(RTC_TIME_MAGIC_CCOUNT);
  224. }
  225. static inline void rtc_time_set_frc2_magic(void)
  226. {
  227. rtc_time_set_magic(RTC_TIME_MAGIC_FRC2);
  228. }
  229. static inline void rtc_time_unset_magic(void)
  230. {
  231. rtc_mem_write(RTC_TIME_MAGIC_POS,0);
  232. }
  233. static inline uint32_t rtc_time_read_raw(void)
  234. {
  235. return rtc_reg_read(RTC_COUNTER_ADDR);
  236. }
  237. static inline uint32_t rtc_time_read_raw_ccount(void)
  238. {
  239. return xthal_get_ccount();
  240. }
  241. static inline uint32_t rtc_time_read_raw_frc2(void)
  242. {
  243. return NOW();
  244. }
  245. // Get us the number of Unit Cycles that have elapsed since the source was 0.
  246. // Note: This may in fact adjust the stored cycles-when-source-was-0 entry, so
  247. // we need to make sure we call this before reading that entry
  248. static inline uint64_t rtc_time_source_offset(void)
  249. {
  250. uint32_t magic=rtc_time_get_magic();
  251. uint32_t raw=0;
  252. switch (magic)
  253. {
  254. case RTC_TIME_MAGIC_CCOUNT: raw=rtc_time_read_raw_ccount(); break;
  255. case RTC_TIME_MAGIC_FRC2: raw=rtc_time_read_raw_frc2(); break;
  256. default: return 0; // We are not in a position to offer time
  257. }
  258. uint32_t multiplier=rtc_mem_read(RTC_SOURCECYCLEUNITS_POS);
  259. uint32_t previous=rtc_mem_read(RTC_LASTSOURCEVAL_POS);
  260. if (raw<previous)
  261. { // We had a rollover.
  262. uint64_t to_add=(1ULL<<32)*multiplier;
  263. uint64_t base=rtc_mem_read64(RTC_CYCLEOFFSETL_POS);
  264. if (base)
  265. rtc_mem_write64(RTC_CYCLEOFFSETL_POS,base+to_add);
  266. }
  267. rtc_mem_write(RTC_LASTSOURCEVAL_POS,raw);
  268. return ((uint64_t)raw)*multiplier;
  269. }
  270. static inline uint64_t rtc_time_unix_unitcycles(void)
  271. {
  272. // Note: The order of these two must be maintained, as the first call might change the outcome of the second
  273. uint64_t offset=rtc_time_source_offset();
  274. uint64_t base=rtc_mem_read64(RTC_CYCLEOFFSETL_POS);
  275. if (!base)
  276. return 0; // No known time
  277. return base+offset;
  278. }
  279. static inline uint64_t rtc_time_unix_us(void)
  280. {
  281. return rtc_time_unix_unitcycles()/UNITCYCLE_MHZ;
  282. }
  283. static inline void rtc_time_register_time_reached(uint32_t s, uint32_t us)
  284. {
  285. rtc_mem_write(RTC_LASTTODUS_POS,us);
  286. }
  287. static inline uint32_t rtc_time_us_since_time_reached(uint32_t s, uint32_t us)
  288. {
  289. uint32_t lastus=rtc_mem_read(RTC_LASTTODUS_POS);
  290. if (us<lastus)
  291. us+=1000000;
  292. return us-lastus;
  293. }
  294. // A small sanity check so sleep times go completely nuts if someone
  295. // has provided wrong timestamps to gettimeofday.
  296. static inline bool rtc_time_calibration_is_sane(uint32_t cali)
  297. {
  298. return (cali>=(4<<12)) && (cali<=(10<<12));
  299. }
  300. static inline uint32_t rtc_time_get_calibration(void)
  301. {
  302. uint32_t cal=rtc_time_check_magic()?rtc_mem_read(RTC_CALIBRATION_POS):0;
  303. if (!cal)
  304. {
  305. // Make a first guess, most likely to be rather bad, but better then nothing.
  306. #ifndef BOOTLOADER_CODE // This will pull in way too much of the system for the bootloader to handle.
  307. ets_delay_us(200);
  308. cal=system_rtc_clock_cali_proc();
  309. rtc_mem_write(RTC_CALIBRATION_POS,cal);
  310. #else
  311. cal=6<<12;
  312. #endif
  313. }
  314. return cal;
  315. }
  316. static inline void rtc_time_invalidate_calibration(void)
  317. {
  318. rtc_mem_write(RTC_CALIBRATION_POS,0);
  319. }
  320. static inline uint64_t rtc_time_us_to_ticks(uint64_t us)
  321. {
  322. uint32_t cal=rtc_time_get_calibration();
  323. return (us<<12)/cal;
  324. }
  325. static inline uint64_t rtc_time_get_now_us_raw(void)
  326. {
  327. if (!rtc_time_check_magic())
  328. return 0;
  329. return rtc_time_unix_us();
  330. }
  331. static inline uint64_t rtc_time_get_now_us_adjusted(void)
  332. {
  333. uint64_t raw=rtc_time_get_now_us_raw();
  334. if (!raw)
  335. return 0;
  336. return raw+rtc_mem_read(RTC_TODOFFSETUS_POS);
  337. }
  338. static inline void rtc_time_add_sleep_tracking(uint32_t us, uint32_t cycles)
  339. {
  340. if (rtc_time_check_magic())
  341. {
  342. // us is the one that will grow faster...
  343. uint32_t us_before=rtc_mem_read(RTC_SLEEPTOTALUS_POS);
  344. uint32_t us_after=us_before+us;
  345. uint32_t cycles_after=rtc_mem_read(RTC_SLEEPTOTALCYCLES_POS)+cycles;
  346. if (us_after<us_before) // Give up if it would cause an overflow
  347. {
  348. us_after=cycles_after=0xffffffff;
  349. }
  350. rtc_mem_write(RTC_SLEEPTOTALUS_POS, us_after);
  351. rtc_mem_write(RTC_SLEEPTOTALCYCLES_POS,cycles_after);
  352. }
  353. }
  354. extern void rtc_time_enter_deep_sleep_final(void);
  355. static void rtc_time_enter_deep_sleep_us(uint32_t us)
  356. {
  357. if (rtc_time_check_wake_magic())
  358. rtc_time_set_sleep_magic();
  359. rtc_reg_write(0,0);
  360. rtc_reg_write(0,rtc_reg_read(0)&0xffffbfff);
  361. rtc_reg_write(0,rtc_reg_read(0)|0x30);
  362. rtc_reg_write(0x44,4);
  363. rtc_reg_write(0x0c,0x00010010);
  364. rtc_reg_write(0x48,(rtc_reg_read(0x48)&0xffff01ff)|0x0000fc00);
  365. rtc_reg_write(0x48,(rtc_reg_read(0x48)&0xfffffe00)|0x00000080);
  366. rtc_reg_write(RTC_TARGET_ADDR,rtc_time_read_raw()+136);
  367. rtc_reg_write(0x18,8);
  368. rtc_reg_write(0x08,0x00100010);
  369. ets_delay_us(20);
  370. rtc_reg_write(0x9c,17);
  371. rtc_reg_write(0xa0,3);
  372. rtc_reg_write(0x0c,0x640c8);
  373. rtc_reg_write(0,rtc_reg_read(0)&0xffffffcf);
  374. uint32_t cycles=rtc_time_us_to_ticks(us);
  375. rtc_time_add_sleep_tracking(us,cycles);
  376. rtc_reg_write(RTC_TARGET_ADDR,rtc_time_read_raw()+cycles);
  377. rtc_reg_write(0x9c,17);
  378. rtc_reg_write(0xa0,3);
  379. volatile uint32_t* dport4=(volatile uint32_t*)0x3ff00004;
  380. *dport4&=0xfffffffe;
  381. rtc_reg_write(0x40,-1);
  382. rtc_reg_write(0x44,32);
  383. rtc_reg_write(0x10,0);
  384. rtc_time_enter_deep_sleep_final();
  385. }
  386. static inline void rtc_time_deep_sleep_us(uint32_t us)
  387. {
  388. if (rtc_time_check_magic())
  389. {
  390. uint32_t to_adjust=rtc_mem_read(RTC_TODOFFSETUS_POS);
  391. if (to_adjust)
  392. {
  393. us+=to_adjust;
  394. rtc_mem_write(RTC_TODOFFSETUS_POS,0);
  395. }
  396. uint64_t now=rtc_time_get_now_us_raw(); // Now the same as _adjusted()
  397. if (now)
  398. { // Need to maintain the clock first. When we wake up, counter will be 0
  399. uint64_t wakeup=now+us;
  400. uint64_t wakeup_cycles=wakeup*UNITCYCLE_MHZ;
  401. rtc_mem_write64(RTC_CYCLEOFFSETL_POS,wakeup_cycles);
  402. }
  403. }
  404. rtc_time_enter_deep_sleep_us(us);
  405. }
  406. static inline void rtc_time_deep_sleep_until_aligned(uint32_t align, uint32_t min_sleep_us)
  407. {
  408. uint64_t now=rtc_time_get_now_us_adjusted();
  409. uint64_t then=now+min_sleep_us;
  410. if (align)
  411. {
  412. then+=align-1;
  413. then-=(then%align);
  414. }
  415. rtc_time_deep_sleep_us(then-now);
  416. }
  417. static inline void rtc_time_reset(bool clear_cali)
  418. {
  419. rtc_mem_write64(RTC_CYCLEOFFSETL_POS,0);
  420. rtc_mem_write(RTC_SLEEPTOTALUS_POS,0);
  421. rtc_mem_write(RTC_SLEEPTOTALCYCLES_POS,0);
  422. rtc_mem_write(RTC_TODOFFSETUS_POS,0);
  423. rtc_mem_write(RTC_LASTTODUS_POS,0);
  424. rtc_mem_write(RTC_SOURCECYCLEUNITS_POS,0);
  425. rtc_mem_write(RTC_LASTSOURCEVAL_POS,0);
  426. if (clear_cali)
  427. rtc_mem_write(RTC_CALIBRATION_POS,0);
  428. }
  429. static inline bool rtc_time_have_time(void)
  430. {
  431. return (rtc_time_check_magic() && rtc_mem_read64(RTC_CYCLEOFFSETL_POS)!=0);
  432. }
  433. static inline void rtc_time_select_frc2_source()
  434. {
  435. // FRC2 always runs at 1/256th of the default 80MHz clock, even if the actual clock is different
  436. uint32_t new_multiplier=(256*UNITCYCLE_MHZ+CPU_DEFAULT_MHZ/2)/CPU_DEFAULT_MHZ;
  437. uint64_t now;
  438. uint32_t before;
  439. uint32_t after;
  440. // Deal with race condition here...
  441. do {
  442. before=rtc_time_read_raw_frc2();
  443. now=rtc_time_unix_unitcycles();
  444. after=rtc_time_read_raw_frc2();
  445. } while (before>after);
  446. if (rtc_time_have_time())
  447. {
  448. uint64_t offset=(uint64_t)after*new_multiplier;
  449. rtc_mem_write64(RTC_CYCLEOFFSETL_POS,now-offset);
  450. rtc_mem_write(RTC_LASTSOURCEVAL_POS,after);
  451. }
  452. rtc_mem_write(RTC_SOURCECYCLEUNITS_POS,new_multiplier);
  453. rtc_mem_write(RTC_TIME_MAGIC_POS,RTC_TIME_MAGIC_FRC2);
  454. }
  455. static inline void rtc_time_select_ccount_source(uint32_t mhz, bool first)
  456. {
  457. uint32_t new_multiplier=(UNITCYCLE_MHZ+mhz/2)/mhz;
  458. // Check that
  459. if (new_multiplier*mhz!=UNITCYCLE_MHZ)
  460. ets_printf("Trying to use unsuitable frequency: %dMHz\n",mhz);
  461. if (first)
  462. { // The ccounter has been running at this rate since startup, and the offset is set accordingly
  463. rtc_mem_write(RTC_LASTSOURCEVAL_POS,0);
  464. rtc_mem_write(RTC_SOURCECYCLEUNITS_POS,new_multiplier);
  465. rtc_mem_write(RTC_TIME_MAGIC_POS,RTC_TIME_MAGIC_CCOUNT);
  466. return;
  467. }
  468. uint64_t now;
  469. uint32_t before;
  470. uint32_t after;
  471. // Deal with race condition here...
  472. do {
  473. before=rtc_time_read_raw_ccount();
  474. now=rtc_time_unix_unitcycles();
  475. after=rtc_time_read_raw_ccount();
  476. } while (before>after);
  477. if (rtc_time_have_time())
  478. {
  479. uint64_t offset=(uint64_t)after*new_multiplier;
  480. rtc_mem_write64(RTC_CYCLEOFFSETL_POS,now-offset);
  481. rtc_mem_write(RTC_LASTSOURCEVAL_POS,after);
  482. }
  483. rtc_mem_write(RTC_SOURCECYCLEUNITS_POS,new_multiplier);
  484. rtc_mem_write(RTC_TIME_MAGIC_POS,RTC_TIME_MAGIC_CCOUNT);
  485. }
  486. static inline void rtc_time_switch_to_ccount_frequency(uint32_t mhz)
  487. {
  488. if (rtc_time_check_magic())
  489. rtc_time_select_ccount_source(mhz,false);
  490. }
  491. static inline void rtc_time_switch_to_system_clock(void)
  492. {
  493. if (rtc_time_check_magic())
  494. rtc_time_select_frc2_source();
  495. }
  496. static inline void rtc_time_tmrfn(void* arg)
  497. {
  498. rtc_time_source_offset();
  499. }
  500. static inline void rtc_time_install_timer(void)
  501. {
  502. static ETSTimer tmr;
  503. os_timer_setfn(&tmr,rtc_time_tmrfn,NULL);
  504. os_timer_arm(&tmr,10000,1);
  505. }
  506. #if 0 // Kept around for reference....
  507. static inline void rtc_time_ccount_wrap_handler(void* dst_v, uint32_t sp)
  508. {
  509. uint32_t off_h=rtc_mem_read(RTC_CYCLEOFFSETH_POS);
  510. if (rtc_time_check_magic() && off_h)
  511. {
  512. rtc_mem_write(RTC_CYCLEOFFSETH_POS,off_h+1);
  513. }
  514. xthal_set_ccompare(0,0); // This resets the interrupt condition
  515. }
  516. static inline void rtc_time_install_wrap_handler(void)
  517. {
  518. xthal_set_ccompare(0,0); // Recognise a ccounter wraparound
  519. ets_isr_attach(RTC_TIME_CCOMPARE_INT,rtc_time_ccount_wrap_handler,NULL);
  520. ets_isr_unmask(1<<RTC_TIME_CCOMPARE_INT);
  521. }
  522. #endif
  523. // This switches from MAGIC_SLEEP to MAGIC_CCOUNT, with ccount running at bootup frequency (i.e. 52MHz).
  524. // To be called as early as possible, potententially as the first thing in an overridden entry point.
  525. static inline void rtc_time_register_bootup(void)
  526. {
  527. uint32_t reset_reason=rtc_get_reset_reason();
  528. #ifndef BOOTLOADER_CODE
  529. static const bool erase_calibration=true;
  530. #else
  531. // In the boot loader, any leftover calibration is going to be better than anything we can
  532. // come up with....
  533. static const bool erase_calibration=false;
  534. #endif
  535. if (rtc_time_check_sleep_magic())
  536. {
  537. if (reset_reason!=2) // This was *not* a proper wakeup from a deep sleep. All our time keeping is f*cked!
  538. rtc_time_reset(erase_calibration); // Possibly keep the calibration, it should still be good
  539. rtc_time_select_ccount_source(CPU_BOOTUP_MHZ,true);
  540. return;
  541. }
  542. if (rtc_time_check_magic())
  543. {
  544. // We did not go to sleep properly. All our time keeping is f*cked!
  545. rtc_time_reset(erase_calibration); // Possibly keep the calibration, it should still be good
  546. }
  547. }
  548. // Call this from the nodemcu entry point, i.e. just before we switch from 52MHz to 80MHz
  549. static inline void rtc_time_switch_clocks(void)
  550. {
  551. rtc_time_switch_to_ccount_frequency(CPU_DEFAULT_MHZ);
  552. }
  553. // Call this exactly once, from user_init, i.e. once the operating system is up and running
  554. static inline void rtc_time_switch_system(void)
  555. {
  556. rtc_time_install_timer();
  557. rtc_time_switch_to_system_clock();
  558. }
  559. static inline void rtc_time_prepare(void)
  560. {
  561. rtc_time_reset(true);
  562. rtc_time_select_frc2_source();
  563. }
  564. static inline void rtc_time_gettimeofday(struct rtc_timeval* tv)
  565. {
  566. uint64_t now=rtc_time_get_now_us_adjusted();
  567. uint32_t sec=now/1000000;
  568. uint32_t usec=now%1000000;
  569. uint32_t to_adjust=rtc_mem_read(RTC_TODOFFSETUS_POS);
  570. if (to_adjust)
  571. {
  572. uint32_t us_passed=rtc_time_us_since_time_reached(sec,usec);
  573. uint32_t adjust=us_passed>>4;
  574. if (adjust)
  575. {
  576. if (adjust>to_adjust)
  577. adjust=to_adjust;
  578. to_adjust-=adjust;
  579. now-=adjust;
  580. now/1000000;
  581. now%1000000;
  582. rtc_mem_write(RTC_TODOFFSETUS_POS,to_adjust);
  583. }
  584. }
  585. tv->tv_sec=sec;
  586. tv->tv_usec=usec;
  587. rtc_time_register_time_reached(sec,usec);
  588. }
  589. static inline void rtc_time_settimeofday(const struct rtc_timeval* tv)
  590. {
  591. if (!rtc_time_check_magic())
  592. return;
  593. uint32_t sleep_us=rtc_mem_read(RTC_SLEEPTOTALUS_POS);
  594. uint32_t sleep_cycles=rtc_mem_read(RTC_SLEEPTOTALCYCLES_POS);
  595. // At this point, the CPU clock will definitely be at the default rate (nodemcu fully booted)
  596. uint64_t now_esp_us=rtc_time_get_now_us_adjusted();
  597. uint64_t now_ntp_us=((uint64_t)tv->tv_sec)*1000000+tv->tv_usec;
  598. int64_t diff_us=now_esp_us-now_ntp_us;
  599. // Store the *actual* time.
  600. uint64_t target_unitcycles=now_ntp_us*UNITCYCLE_MHZ;
  601. uint64_t sourcecycles=rtc_time_source_offset();
  602. rtc_mem_write64(RTC_CYCLEOFFSETL_POS,target_unitcycles-sourcecycles);
  603. // calibrate sleep period based on difference between expected time and actual time
  604. if (sleep_us>0 && sleep_us<0xffffffff &&
  605. sleep_cycles>0 && sleep_cycles<0xffffffff)
  606. {
  607. uint64_t actual_sleep_us=sleep_us-diff_us;
  608. uint32_t cali=(actual_sleep_us<<12)/sleep_cycles;
  609. if (rtc_time_calibration_is_sane(cali))
  610. rtc_mem_write(RTC_CALIBRATION_POS,cali);
  611. }
  612. rtc_mem_write(RTC_SLEEPTOTALUS_POS,0);
  613. rtc_mem_write(RTC_SLEEPTOTALCYCLES_POS,0);
  614. // Deal with time adjustment if necessary
  615. if (diff_us>0) // Time went backwards. Avoid that....
  616. {
  617. if (diff_us>0xffffffffULL)
  618. diff_us=0xffffffffULL;
  619. now_ntp_us+=diff_us;
  620. }
  621. else
  622. diff_us=0;
  623. rtc_mem_write(RTC_TODOFFSETUS_POS,diff_us);
  624. uint32_t now_s=now_ntp_us/1000000;
  625. uint32_t now_us=now_ntp_us%1000000;
  626. rtc_time_register_time_reached(now_s,now_us);
  627. }
  628. #endif