tmr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*guys, srsly, turn on warnings in the makefile*/
  2. #if defined(__GNUC__)
  3. #pragma GCC diagnostic warning "-Wall"
  4. #pragma GCC diagnostic warning "-Wextra"
  5. #pragma GCC diagnostic ignored "-Wunused-parameter"
  6. #endif
  7. /*-------------------------------------
  8. NEW TIMER API
  9. ---------------------------------------
  10. tmr.wdclr() -- not changed
  11. tmr.now() -- not changed
  12. tmr.time() -- not changed
  13. tmr.delay() -- not changed
  14. tmr.alarm() -- not changed
  15. tmr.stop() -- changed, see below. use tmr.unregister for old functionality
  16. tmr.register(id, interval, mode, function)
  17. bind function with timer and set the interval in ms
  18. the mode can be:
  19. tmr.ALARM_SINGLE for a single run alarm
  20. tmr.ALARM_SEMI for a multiple single run alarm
  21. tmr.ALARM_AUTO for a repating alarm
  22. tmr.register does NOT start the timer
  23. tmr.alarm is a tmr.register & tmr.start macro
  24. tmr.unregister(id)
  25. stop alarm, unbind function and clean up memory
  26. not needed for ALARM_SINGLE, as it unregisters itself
  27. tmr.start(id)
  28. ret: bool
  29. start a alarm, returns true on success
  30. tmr.stop(id)
  31. ret: bool
  32. stops a alarm, returns true on success
  33. this call dose not free any memory, to do so use tmr.unregister
  34. stopped alarms can be started with start
  35. tmr.interval(id, interval)
  36. set alarm interval, running alarm will be restarted
  37. tmr.state(id)
  38. ret: (bool, int) or nil
  39. returns alarm status (true=started/false=stopped) and mode
  40. nil if timer is unregistered
  41. tmr.softwd(int)
  42. set a negative value to stop the timer
  43. any other value starts the timer, when the
  44. countdown reaches zero, the device restarts
  45. the timer units are seconds
  46. */
  47. #include "module.h"
  48. #include "lauxlib.h"
  49. #include "platform.h"
  50. #include "c_types.h"
  51. #include "user_interface.h"
  52. #define TIMER_MODE_OFF 3
  53. #define TIMER_MODE_SINGLE 0
  54. #define TIMER_MODE_SEMI 2
  55. #define TIMER_MODE_AUTO 1
  56. #define TIMER_IDLE_FLAG (1<<7)
  57. #define STRINGIFY_VAL(x) #x
  58. #define STRINGIFY(x) STRINGIFY_VAL(x)
  59. // assuming system_timer_reinit() has *not* been called
  60. #define MAX_TIMEOUT_DEF 6870947 //SDK 1.5.3 limit (0x68D7A3)
  61. static const uint32 MAX_TIMEOUT=MAX_TIMEOUT_DEF;
  62. static const char* MAX_TIMEOUT_ERR_STR = "Range: 1-"STRINGIFY(MAX_TIMEOUT_DEF);
  63. typedef struct{
  64. os_timer_t os;
  65. sint32_t lua_ref;
  66. uint32_t interval;
  67. uint8_t mode;
  68. }timer_struct_t;
  69. typedef timer_struct_t* timer_t;
  70. // The previous implementation extended the rtc counter to 64 bits, and then
  71. // applied rtc2sec with the current calibration value to that 64 bit value.
  72. // This means that *ALL* clock ticks since bootup are counted with the *current*
  73. // clock period. In extreme cases (long uptime, sudden temperature change), this
  74. // could result in tmr.time() going backwards....
  75. // This implementation instead applies rtc2usec to short time intervals only (the
  76. // longest being around 1 second), and then accumulates the resulting microseconds
  77. // in a 64 bit counter. That's guaranteed to be monotonic, and should be a lot closer
  78. // to representing an actual uptime.
  79. static uint32_t rtc_time_cali=0;
  80. static uint32_t last_rtc_time=0;
  81. static uint64_t last_rtc_time_us=0;
  82. static sint32_t soft_watchdog = -1;
  83. static timer_struct_t alarm_timers[NUM_TMR];
  84. static os_timer_t rtc_timer;
  85. static void alarm_timer_common(void* arg){
  86. timer_t tmr = &alarm_timers[(uint32_t)arg];
  87. lua_State* L = lua_getstate();
  88. if(tmr->lua_ref == LUA_NOREF)
  89. return;
  90. lua_rawgeti(L, LUA_REGISTRYINDEX, tmr->lua_ref);
  91. //if the timer was set to single run we clean up after it
  92. if(tmr->mode == TIMER_MODE_SINGLE){
  93. luaL_unref(L, LUA_REGISTRYINDEX, tmr->lua_ref);
  94. tmr->lua_ref = LUA_NOREF;
  95. tmr->mode = TIMER_MODE_OFF;
  96. }else if(tmr->mode == TIMER_MODE_SEMI){
  97. tmr->mode |= TIMER_IDLE_FLAG;
  98. }
  99. lua_call(L, 0, 0);
  100. }
  101. // Lua: tmr.delay( us )
  102. static int tmr_delay( lua_State* L ){
  103. sint32_t us = luaL_checkinteger(L, 1);
  104. if(us <= 0)
  105. return luaL_error(L, "wrong arg range");
  106. while(us >= 1000000){
  107. us -= 1000000;
  108. os_delay_us(1000000);
  109. system_soft_wdt_feed ();
  110. }
  111. if(us>0){
  112. os_delay_us(us);
  113. system_soft_wdt_feed ();
  114. }
  115. return 0;
  116. }
  117. // Lua: tmr.now() , return system timer in us
  118. static int tmr_now(lua_State* L){
  119. uint32_t now = 0x7FFFFFFF & system_get_time();
  120. lua_pushinteger(L, now);
  121. return 1;
  122. }
  123. // Lua: tmr.register( id, interval, mode, function )
  124. static int tmr_register(lua_State* L){
  125. uint32_t id = luaL_checkinteger(L, 1);
  126. uint32_t interval = luaL_checkinteger(L, 2);
  127. uint8_t mode = luaL_checkinteger(L, 3);
  128. //Check if provided parameters are valid
  129. MOD_CHECK_ID(tmr, id);
  130. luaL_argcheck(L, (interval > 0 && interval <= MAX_TIMEOUT), 2, MAX_TIMEOUT_ERR_STR);
  131. luaL_argcheck(L, (mode == TIMER_MODE_SINGLE || mode == TIMER_MODE_SEMI || mode == TIMER_MODE_AUTO), 3, "Invalid mode");
  132. luaL_argcheck(L, (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION), 4, "Must be function");
  133. //get the lua function reference
  134. lua_pushvalue(L, 4);
  135. sint32_t ref = luaL_ref(L, LUA_REGISTRYINDEX);
  136. timer_t tmr = &alarm_timers[id];
  137. if(!(tmr->mode & TIMER_IDLE_FLAG) && tmr->mode != TIMER_MODE_OFF)
  138. ets_timer_disarm(&tmr->os);
  139. //there was a bug in this part, the second part of the following condition was missing
  140. if(tmr->lua_ref != LUA_NOREF && tmr->lua_ref != ref)
  141. luaL_unref(L, LUA_REGISTRYINDEX, tmr->lua_ref);
  142. tmr->lua_ref = ref;
  143. tmr->mode = mode|TIMER_IDLE_FLAG;
  144. tmr->interval = interval;
  145. ets_timer_setfn(&tmr->os, alarm_timer_common, (void*)id);
  146. return 0;
  147. }
  148. // Lua: tmr.start( id )
  149. static int tmr_start(lua_State* L){
  150. uint8_t id = luaL_checkinteger(L, 1);
  151. MOD_CHECK_ID(tmr,id);
  152. timer_t tmr = &alarm_timers[id];
  153. //we return false if the timer is not idle
  154. if(!(tmr->mode&TIMER_IDLE_FLAG)){
  155. lua_pushboolean(L, 0);
  156. }else{
  157. tmr->mode &= ~TIMER_IDLE_FLAG;
  158. ets_timer_arm_new(&tmr->os, tmr->interval, tmr->mode==TIMER_MODE_AUTO, 1);
  159. lua_pushboolean(L, 1);
  160. }
  161. return 1;
  162. }
  163. // Lua: tmr.alarm( id, interval, repeat, function )
  164. static int tmr_alarm(lua_State* L){
  165. tmr_register(L);
  166. return tmr_start(L);
  167. }
  168. // Lua: tmr.stop( id )
  169. static int tmr_stop(lua_State* L){
  170. uint8_t id = luaL_checkinteger(L, 1);
  171. MOD_CHECK_ID(tmr,id);
  172. timer_t tmr = &alarm_timers[id];
  173. //we return false if the timer is idle (of not registered)
  174. if(!(tmr->mode & TIMER_IDLE_FLAG) && tmr->mode != TIMER_MODE_OFF){
  175. tmr->mode |= TIMER_IDLE_FLAG;
  176. ets_timer_disarm(&tmr->os);
  177. lua_pushboolean(L, 1);
  178. }else{
  179. lua_pushboolean(L, 0);
  180. }
  181. return 1;
  182. }
  183. // Lua: tmr.unregister( id )
  184. static int tmr_unregister(lua_State* L){
  185. uint8_t id = luaL_checkinteger(L, 1);
  186. MOD_CHECK_ID(tmr,id);
  187. timer_t tmr = &alarm_timers[id];
  188. if(!(tmr->mode & TIMER_IDLE_FLAG) && tmr->mode != TIMER_MODE_OFF)
  189. ets_timer_disarm(&tmr->os);
  190. if(tmr->lua_ref != LUA_NOREF)
  191. luaL_unref(L, LUA_REGISTRYINDEX, tmr->lua_ref);
  192. tmr->lua_ref = LUA_NOREF;
  193. tmr->mode = TIMER_MODE_OFF;
  194. return 0;
  195. }
  196. // Lua: tmr.interval( id, interval )
  197. static int tmr_interval(lua_State* L){
  198. uint8_t id = luaL_checkinteger(L, 1);
  199. MOD_CHECK_ID(tmr,id);
  200. timer_t tmr = &alarm_timers[id];
  201. uint32_t interval = luaL_checkinteger(L, 2);
  202. luaL_argcheck(L, (interval > 0 && interval <= MAX_TIMEOUT), 2, MAX_TIMEOUT_ERR_STR);
  203. if(tmr->mode != TIMER_MODE_OFF){
  204. tmr->interval = interval;
  205. if(!(tmr->mode&TIMER_IDLE_FLAG)){
  206. ets_timer_disarm(&tmr->os);
  207. ets_timer_arm_new(&tmr->os, tmr->interval, tmr->mode==TIMER_MODE_AUTO, 1);
  208. }
  209. }
  210. return 0;
  211. }
  212. // Lua: tmr.state( id )
  213. static int tmr_state(lua_State* L){
  214. uint8_t id = luaL_checkinteger(L, 1);
  215. MOD_CHECK_ID(tmr,id);
  216. timer_t tmr = &alarm_timers[id];
  217. if(tmr->mode == TIMER_MODE_OFF){
  218. lua_pushnil(L);
  219. return 1;
  220. }
  221. lua_pushboolean(L, (tmr->mode&TIMER_IDLE_FLAG)==0);
  222. lua_pushinteger(L, tmr->mode&(~TIMER_IDLE_FLAG));
  223. return 2;
  224. }
  225. /*I left the led comments 'couse I don't know
  226. why they are here*/
  227. // extern void update_key_led();
  228. // Lua: tmr.wdclr()
  229. static int tmr_wdclr( lua_State* L ){
  230. system_soft_wdt_feed ();
  231. // update_key_led();
  232. return 0;
  233. }
  234. //system_rtc_clock_cali_proc() returns
  235. //a fixed point value (12 bit fraction part)
  236. //it tells how many rtc clock ticks represent 1us.
  237. //the high 64 bits of the uint64_t multiplication
  238. //are unnedded (I did the math)
  239. static uint32_t rtc2usec(uint64_t rtc){
  240. return (rtc*rtc_time_cali)>>12;
  241. }
  242. // This returns the number of microseconds uptime. Note that it relies on the rtc clock,
  243. // which is notoriously temperature dependent
  244. inline static uint64_t rtc_timer_update(bool do_calibration){
  245. if (do_calibration || rtc_time_cali==0)
  246. rtc_time_cali=system_rtc_clock_cali_proc();
  247. uint32_t current = system_get_rtc_time();
  248. uint32_t since_last=current-last_rtc_time; // This will transparently deal with wraparound
  249. uint32_t us_since_last=rtc2usec(since_last);
  250. uint64_t now=last_rtc_time_us+us_since_last;
  251. // Only update if at least 100ms has passed since we last updated.
  252. // This prevents the rounding errors in rtc2usec from accumulating
  253. if (us_since_last>=100000)
  254. {
  255. last_rtc_time=current;
  256. last_rtc_time_us=now;
  257. }
  258. return now;
  259. }
  260. void rtc_callback(void *arg){
  261. rtc_timer_update(true);
  262. if(soft_watchdog > 0){
  263. soft_watchdog--;
  264. if(soft_watchdog == 0)
  265. system_restart();
  266. }
  267. }
  268. // Lua: tmr.time() , return rtc time in second
  269. static int tmr_time( lua_State* L ){
  270. uint64_t us=rtc_timer_update(false);
  271. lua_pushinteger(L, us/1000000);
  272. return 1;
  273. }
  274. // Lua: tmr.softwd( value )
  275. static int tmr_softwd( lua_State* L ){
  276. soft_watchdog = luaL_checkinteger(L, 1);
  277. return 0;
  278. }
  279. // Module function map
  280. static const LUA_REG_TYPE tmr_map[] = {
  281. { LSTRKEY( "delay" ), LFUNCVAL( tmr_delay ) },
  282. { LSTRKEY( "now" ), LFUNCVAL( tmr_now ) },
  283. { LSTRKEY( "wdclr" ), LFUNCVAL( tmr_wdclr ) },
  284. { LSTRKEY( "softwd" ), LFUNCVAL( tmr_softwd ) },
  285. { LSTRKEY( "time" ), LFUNCVAL( tmr_time ) },
  286. { LSTRKEY( "register" ), LFUNCVAL( tmr_register ) },
  287. { LSTRKEY( "alarm" ), LFUNCVAL( tmr_alarm ) },
  288. { LSTRKEY( "start" ), LFUNCVAL( tmr_start ) },
  289. { LSTRKEY( "stop" ), LFUNCVAL( tmr_stop ) },
  290. { LSTRKEY( "unregister" ), LFUNCVAL( tmr_unregister ) },
  291. { LSTRKEY( "state" ), LFUNCVAL( tmr_state ) },
  292. { LSTRKEY( "interval" ), LFUNCVAL( tmr_interval) },
  293. { LSTRKEY( "ALARM_SINGLE" ), LNUMVAL( TIMER_MODE_SINGLE ) },
  294. { LSTRKEY( "ALARM_SEMI" ), LNUMVAL( TIMER_MODE_SEMI ) },
  295. { LSTRKEY( "ALARM_AUTO" ), LNUMVAL( TIMER_MODE_AUTO ) },
  296. { LNILKEY, LNILVAL }
  297. };
  298. int luaopen_tmr( lua_State *L ){
  299. int i;
  300. for(i=0; i<NUM_TMR; i++){
  301. alarm_timers[i].lua_ref = LUA_NOREF;
  302. alarm_timers[i].mode = TIMER_MODE_OFF;
  303. ets_timer_disarm(&alarm_timers[i].os);
  304. }
  305. last_rtc_time=system_get_rtc_time(); // Right now is time 0
  306. last_rtc_time_us=0;
  307. ets_timer_disarm(&rtc_timer);
  308. ets_timer_setfn(&rtc_timer, rtc_callback, NULL);
  309. ets_timer_arm_new(&rtc_timer, 1000, 1, 1);
  310. return 0;
  311. }
  312. NODEMCU_MODULE(TMR, "tmr", tmr_map, luaopen_tmr);