rtctime.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Module for RTC time keeping
  2. #include "lauxlib.h"
  3. #include "rtc/rtctime_internal.h"
  4. #include "rtc/rtctime.h"
  5. // ******* C API functions *************
  6. void rtctime_early_startup (void)
  7. {
  8. Cache_Read_Enable (0, 0, 1);
  9. rtc_time_register_bootup ();
  10. rtc_time_switch_clocks ();
  11. Cache_Read_Disable ();
  12. }
  13. void rtctime_late_startup (void)
  14. {
  15. rtc_time_switch_system ();
  16. }
  17. void rtctime_gettimeofday (struct rtc_timeval *tv)
  18. {
  19. rtc_time_gettimeofday (tv);
  20. }
  21. void rtctime_settimeofday (const struct rtc_timeval *tv)
  22. {
  23. if (!rtc_time_check_magic ())
  24. rtc_time_prepare ();
  25. rtc_time_settimeofday (tv);
  26. }
  27. bool rtctime_have_time (void)
  28. {
  29. return rtc_time_have_time ();
  30. }
  31. void rtctime_deep_sleep_us (uint32_t us)
  32. {
  33. rtc_time_deep_sleep_us (us);
  34. }
  35. void rtctime_deep_sleep_until_aligned_us (uint32_t align_us, uint32_t min_us)
  36. {
  37. rtc_time_deep_sleep_until_aligned (align_us, min_us);
  38. }
  39. // ******* Lua API functions *************
  40. // rtctime.set (sec, usec)
  41. static int rtctime_set (lua_State *L)
  42. {
  43. if (!rtc_time_check_magic ())
  44. rtc_time_prepare ();
  45. uint32_t sec = luaL_checknumber (L, 1);
  46. uint32_t usec = 0;
  47. if (lua_isnumber (L, 2))
  48. usec = lua_tonumber (L, 2);
  49. struct rtc_timeval tv = { sec, usec };
  50. rtctime_settimeofday (&tv);
  51. return 0;
  52. }
  53. // sec, usec = rtctime.get ()
  54. static int rtctime_get (lua_State *L)
  55. {
  56. struct rtc_timeval tv;
  57. rtctime_gettimeofday (&tv);
  58. lua_pushnumber (L, tv.tv_sec);
  59. lua_pushnumber (L, tv.tv_usec);
  60. return 2;
  61. }
  62. static void do_sleep_opt (lua_State *L, int idx)
  63. {
  64. if (lua_isnumber (L, idx))
  65. {
  66. uint32_t opt = lua_tonumber (L, idx);
  67. if (opt < 0 || opt > 4)
  68. luaL_error (L, "unknown sleep option");
  69. deep_sleep_set_option (opt);
  70. }
  71. }
  72. // rtctime.dsleep (usec, option)
  73. static int rtctime_dsleep (lua_State *L)
  74. {
  75. uint32_t us = luaL_checknumber (L, 1);
  76. do_sleep_opt (L, 2);
  77. rtctime_deep_sleep_us (us); // does not return
  78. return 0;
  79. }
  80. // rtctime.dsleep_aligned (aligned_usec, min_usec, option)
  81. static int rtctime_dsleep_aligned (lua_State *L)
  82. {
  83. if (!rtctime_have_time ())
  84. return luaL_error (L, "time not available, unable to align");
  85. uint32_t align_us = luaL_checknumber (L, 1);
  86. uint32_t min_us = luaL_checknumber (L, 2);
  87. do_sleep_opt (L, 3);
  88. rtctime_deep_sleep_until_aligned_us (align_us, min_us); // does not return
  89. return 0;
  90. }
  91. // Module function map
  92. #define MIN_OPT_LEVEL 2
  93. #include "lrodefs.h"
  94. const LUA_REG_TYPE rtctime_map[] =
  95. {
  96. { LSTRKEY("set"), LFUNCVAL(rtctime_set) },
  97. { LSTRKEY("get"), LFUNCVAL(rtctime_get) },
  98. { LSTRKEY("dsleep"), LFUNCVAL(rtctime_dsleep) },
  99. { LSTRKEY("dsleep_aligned"), LFUNCVAL(rtctime_dsleep_aligned) },
  100. { LNILKEY, LNILVAL }
  101. };
  102. LUALIB_API int luaopen_rtctime (lua_State *L)
  103. {
  104. #if LUA_OPTIMIZE_MEMORY > 0
  105. return 0;
  106. #else
  107. luaL_register (L, AUXLIB_RTCTIME, rtctime_map);
  108. return 1;
  109. #endif
  110. }