rtctime.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Module for RTC time keeping
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "rtc/rtctime_internal.h"
  5. #include "rtc/rtctime.h"
  6. /* seconds per day */
  7. #define SPD 24*60*60
  8. /* days per month -- nonleap! */
  9. static const short __spm[13] =
  10. { 0,
  11. (31),
  12. (31+28),
  13. (31+28+31),
  14. (31+28+31+30),
  15. (31+28+31+30+31),
  16. (31+28+31+30+31+30),
  17. (31+28+31+30+31+30+31),
  18. (31+28+31+30+31+30+31+31),
  19. (31+28+31+30+31+30+31+31+30),
  20. (31+28+31+30+31+30+31+31+30+31),
  21. (31+28+31+30+31+30+31+31+30+31+30),
  22. (31+28+31+30+31+30+31+31+30+31+30+31),
  23. };
  24. static int __isleap (int year) {
  25. /* every fourth year is a leap year except for century years that are
  26. * not divisible by 400. */
  27. /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
  28. return (!(year % 4) && ((year % 100) || !(year % 400)));
  29. }
  30. // ******* C API functions *************
  31. void __attribute__((noreturn)) TEXT_SECTION_ATTR rtc_time_enter_deep_sleep_final (void)
  32. {
  33. ets_intr_lock();
  34. Cache_Read_Disable();
  35. rtc_reg_write(0x18,8);
  36. rtc_reg_write_and_loop(0x08,0x00100000); // go to sleep
  37. __builtin_unreachable();
  38. }
  39. void rtctime_early_startup (void)
  40. {
  41. Cache_Read_Enable (0, 0, 1);
  42. rtc_time_register_bootup ();
  43. rtc_time_switch_clocks ();
  44. Cache_Read_Disable ();
  45. }
  46. void rtctime_late_startup (void)
  47. {
  48. rtc_time_switch_system ();
  49. }
  50. void rtctime_gettimeofday (struct rtc_timeval *tv)
  51. {
  52. rtc_time_gettimeofday (tv);
  53. }
  54. void rtctime_settimeofday (const struct rtc_timeval *tv)
  55. {
  56. if (!rtc_time_check_magic ())
  57. rtc_time_prepare ();
  58. rtc_time_settimeofday (tv);
  59. }
  60. bool rtctime_have_time (void)
  61. {
  62. return rtc_time_have_time ();
  63. }
  64. void rtctime_deep_sleep_us (uint32_t us)
  65. {
  66. rtc_time_deep_sleep_us (us);
  67. }
  68. void rtctime_deep_sleep_until_aligned_us (uint32_t align_us, uint32_t min_us)
  69. {
  70. rtc_time_deep_sleep_until_aligned (align_us, min_us);
  71. }
  72. void rtctime_gmtime (const int32 stamp, struct rtc_tm *r)
  73. {
  74. int32_t i;
  75. int32_t work = stamp % (SPD);
  76. r->tm_sec = work % 60; work /= 60;
  77. r->tm_min = work % 60; r->tm_hour = work / 60;
  78. work = stamp / (SPD);
  79. r->tm_wday = (4 + work) % 7;
  80. for (i = 1970; ; ++i) {
  81. int32_t k = __isleap (i) ? 366 : 365;
  82. if (work >= k) {
  83. work -= k;
  84. } else {
  85. break;
  86. }
  87. }
  88. r->tm_year = i - 1900;
  89. r->tm_yday = work;
  90. r->tm_mday = 1;
  91. if (__isleap (i) && (work > 58)) {
  92. if (work == 59) r->tm_mday = 2; /* 29.2. */
  93. work -= 1;
  94. }
  95. for (i = 11; i && (__spm[i] > work); --i) ;
  96. r->tm_mon = i;
  97. r->tm_mday += work - __spm[i];
  98. }
  99. // ******* Lua API functions *************
  100. // rtctime.set (sec, usec)
  101. static int rtctime_set (lua_State *L)
  102. {
  103. if (!rtc_time_check_magic ())
  104. rtc_time_prepare ();
  105. uint32_t sec = luaL_checknumber (L, 1);
  106. uint32_t usec = 0;
  107. if (lua_isnumber (L, 2))
  108. usec = lua_tonumber (L, 2);
  109. struct rtc_timeval tv = { sec, usec };
  110. rtctime_settimeofday (&tv);
  111. return 0;
  112. }
  113. // sec, usec = rtctime.get ()
  114. static int rtctime_get (lua_State *L)
  115. {
  116. struct rtc_timeval tv;
  117. rtctime_gettimeofday (&tv);
  118. lua_pushnumber (L, tv.tv_sec);
  119. lua_pushnumber (L, tv.tv_usec);
  120. return 2;
  121. }
  122. static void do_sleep_opt (lua_State *L, int idx)
  123. {
  124. if (lua_isnumber (L, idx))
  125. {
  126. uint32_t opt = lua_tonumber (L, idx);
  127. if (opt < 0 || opt > 4)
  128. luaL_error (L, "unknown sleep option");
  129. system_deep_sleep_set_option (opt);
  130. }
  131. }
  132. // rtctime.dsleep (usec, option)
  133. static int rtctime_dsleep (lua_State *L)
  134. {
  135. uint32_t us = luaL_checknumber (L, 1);
  136. do_sleep_opt (L, 2);
  137. rtctime_deep_sleep_us (us); // does not return
  138. return 0;
  139. }
  140. // rtctime.dsleep_aligned (aligned_usec, min_usec, option)
  141. static int rtctime_dsleep_aligned (lua_State *L)
  142. {
  143. if (!rtctime_have_time ())
  144. return luaL_error (L, "time not available, unable to align");
  145. uint32_t align_us = luaL_checknumber (L, 1);
  146. uint32_t min_us = luaL_checknumber (L, 2);
  147. do_sleep_opt (L, 3);
  148. rtctime_deep_sleep_until_aligned_us (align_us, min_us); // does not return
  149. return 0;
  150. }
  151. #define ADD_TABLE_ITEM(L, key, val) \
  152. lua_pushinteger (L, val); \
  153. lua_setfield (L, -2, key);
  154. // rtctime.epoch2cal (stamp)
  155. static int rtctime_epoch2cal (lua_State *L)
  156. {
  157. struct rtc_tm date;
  158. int32_t stamp = luaL_checkint (L, 1);
  159. luaL_argcheck (L, stamp >= 0, 1, "wrong arg range");
  160. rtctime_gmtime (stamp, &date);
  161. /* construct Lua table */
  162. lua_createtable (L, 0, 8);
  163. ADD_TABLE_ITEM (L, "yday", date.tm_yday + 1);
  164. ADD_TABLE_ITEM (L, "wday", date.tm_wday + 1);
  165. ADD_TABLE_ITEM (L, "year", date.tm_year + 1900);
  166. ADD_TABLE_ITEM (L, "mon", date.tm_mon + 1);
  167. ADD_TABLE_ITEM (L, "day", date.tm_mday);
  168. ADD_TABLE_ITEM (L, "hour", date.tm_hour);
  169. ADD_TABLE_ITEM (L, "min", date.tm_min);
  170. ADD_TABLE_ITEM (L, "sec", date.tm_sec);
  171. return 1;
  172. }
  173. // Module function map
  174. static const LUA_REG_TYPE rtctime_map[] = {
  175. { LSTRKEY("set"), LFUNCVAL(rtctime_set) },
  176. { LSTRKEY("get"), LFUNCVAL(rtctime_get) },
  177. { LSTRKEY("dsleep"), LFUNCVAL(rtctime_dsleep) },
  178. { LSTRKEY("dsleep_aligned"), LFUNCVAL(rtctime_dsleep_aligned) },
  179. { LSTRKEY("epoch2cal"), LFUNCVAL(rtctime_epoch2cal) },
  180. { LNILKEY, LNILVAL }
  181. };
  182. NODEMCU_MODULE(RTCTIME, "rtctime", rtctime_map, NULL);