rtctime.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_adjust_rate (int rate)
  51. {
  52. rtc_time_set_rate (rate);
  53. }
  54. void rtctime_gettimeofday (struct rtc_timeval *tv)
  55. {
  56. rtc_time_gettimeofday (tv);
  57. }
  58. void rtctime_settimeofday (const struct rtc_timeval *tv)
  59. {
  60. if (!rtc_time_check_magic ())
  61. rtc_time_prepare ();
  62. int32_t rate = rtc_time_get_rate();
  63. rtc_time_settimeofday (tv);
  64. rtc_time_set_rate(rate);
  65. }
  66. bool rtctime_have_time (void)
  67. {
  68. return rtc_time_have_time ();
  69. }
  70. void rtctime_deep_sleep_us (uint32_t us)
  71. {
  72. rtc_time_deep_sleep_us (us);
  73. }
  74. void rtctime_deep_sleep_until_aligned_us (uint32_t align_us, uint32_t min_us)
  75. {
  76. rtc_time_deep_sleep_until_aligned (align_us, min_us);
  77. }
  78. void rtctime_gmtime (const int32 stamp, struct rtc_tm *r)
  79. {
  80. int32_t i;
  81. int32_t work = stamp % (SPD);
  82. r->tm_sec = work % 60; work /= 60;
  83. r->tm_min = work % 60; r->tm_hour = work / 60;
  84. work = stamp / (SPD);
  85. r->tm_wday = (4 + work) % 7;
  86. for (i = 1970; ; ++i) {
  87. int32_t k = __isleap (i) ? 366 : 365;
  88. if (work >= k) {
  89. work -= k;
  90. } else {
  91. break;
  92. }
  93. }
  94. r->tm_year = i - 1900;
  95. r->tm_yday = work;
  96. r->tm_mday = 1;
  97. if (__isleap (i) && (work > 58)) {
  98. if (work == 59) r->tm_mday = 2; /* 29.2. */
  99. work -= 1;
  100. }
  101. for (i = 11; i && (__spm[i] > work); --i) ;
  102. r->tm_mon = i;
  103. r->tm_mday += work - __spm[i];
  104. }
  105. // ******* Lua API functions *************
  106. // rtctime.set (sec, usec)
  107. static int rtctime_set (lua_State *L)
  108. {
  109. if (!rtc_time_check_magic ())
  110. rtc_time_prepare ();
  111. uint32_t sec = luaL_checkinteger (L, 1);
  112. uint32_t usec = 0;
  113. if (lua_isnumber (L, 2))
  114. usec = lua_tointeger (L, 2);
  115. struct rtc_timeval tv = { sec, usec };
  116. rtctime_settimeofday (&tv);
  117. if (lua_isnumber(L, 3))
  118. rtc_time_set_rate(lua_tointeger(L, 3));
  119. return 0;
  120. }
  121. // sec, usec = rtctime.get ()
  122. static int rtctime_get (lua_State *L)
  123. {
  124. struct rtc_timeval tv;
  125. rtctime_gettimeofday (&tv);
  126. lua_pushinteger (L, tv.tv_sec);
  127. lua_pushinteger (L, tv.tv_usec);
  128. lua_pushinteger (L, rtc_time_get_rate());
  129. return 3;
  130. }
  131. static void do_sleep_opt (lua_State *L, int idx)
  132. {
  133. if (lua_isnumber (L, idx))
  134. {
  135. uint32_t opt = lua_tointeger (L, idx);
  136. if (opt < 0 || opt > 4)
  137. luaL_error (L, "unknown sleep option");
  138. system_deep_sleep_set_option (opt);
  139. }
  140. }
  141. // rtctime.adjust_delta (usec)
  142. static int rtctime_adjust_delta (lua_State *L)
  143. {
  144. uint32_t us = luaL_checkinteger (L, 1);
  145. lua_pushinteger(L, rtc_time_adjust_delta_by_rate(us));
  146. return 1;
  147. }
  148. // rtctime.dsleep (usec, option)
  149. static int rtctime_dsleep (lua_State *L)
  150. {
  151. uint32_t us = luaL_checkinteger (L, 1);
  152. do_sleep_opt (L, 2);
  153. rtctime_deep_sleep_us (us); // does not return
  154. return 0;
  155. }
  156. // rtctime.dsleep_aligned (aligned_usec, min_usec, option)
  157. static int rtctime_dsleep_aligned (lua_State *L)
  158. {
  159. if (!rtctime_have_time ())
  160. return luaL_error (L, "time not available, unable to align");
  161. uint32_t align_us = luaL_checkinteger (L, 1);
  162. uint32_t min_us = luaL_checkinteger (L, 2);
  163. do_sleep_opt (L, 3);
  164. rtctime_deep_sleep_until_aligned_us (align_us, min_us); // does not return
  165. return 0;
  166. }
  167. #define ADD_TABLE_ITEM(L, key, val) \
  168. lua_pushinteger (L, val); \
  169. lua_setfield (L, -2, key);
  170. // rtctime.epoch2cal (stamp)
  171. static int rtctime_epoch2cal (lua_State *L)
  172. {
  173. struct rtc_tm date;
  174. int32_t stamp = luaL_checkint (L, 1);
  175. luaL_argcheck (L, stamp >= 0, 1, "wrong arg range");
  176. rtctime_gmtime (stamp, &date);
  177. /* construct Lua table */
  178. lua_createtable (L, 0, 8);
  179. ADD_TABLE_ITEM (L, "yday", date.tm_yday + 1);
  180. ADD_TABLE_ITEM (L, "wday", date.tm_wday + 1);
  181. ADD_TABLE_ITEM (L, "year", date.tm_year + 1900);
  182. ADD_TABLE_ITEM (L, "mon", date.tm_mon + 1);
  183. ADD_TABLE_ITEM (L, "day", date.tm_mday);
  184. ADD_TABLE_ITEM (L, "hour", date.tm_hour);
  185. ADD_TABLE_ITEM (L, "min", date.tm_min);
  186. ADD_TABLE_ITEM (L, "sec", date.tm_sec);
  187. return 1;
  188. }
  189. // Module function map
  190. LROT_BEGIN(rtctime, NULL, 0)
  191. LROT_FUNCENTRY( set, rtctime_set )
  192. LROT_FUNCENTRY( get, rtctime_get )
  193. LROT_FUNCENTRY( adjust_delta, rtctime_adjust_delta )
  194. LROT_FUNCENTRY( dsleep, rtctime_dsleep )
  195. LROT_FUNCENTRY( dsleep_aligned, rtctime_dsleep_aligned )
  196. LROT_FUNCENTRY( epoch2cal, rtctime_epoch2cal )
  197. LROT_END(rtctime, NULL, 0)
  198. NODEMCU_MODULE(RTCTIME, "rtctime", rtctime, NULL);