rtctime.c 5.6 KB

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