rtcmem.c 1015 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Module for RTC user memory access
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "rtc/rtcaccess.h"
  5. static int rtcmem_read32 (lua_State *L)
  6. {
  7. int idx = luaL_checkinteger (L, 1);
  8. int n = (lua_gettop(L) < 2) ? 1 : lua_tointeger (L, 2);
  9. if (n == 0 || !lua_checkstack (L, n)) {
  10. return 0;
  11. }
  12. int ret = 0;
  13. while (n > 0 && idx >= 0 && idx < RTC_USER_MEM_NUM_DWORDS)
  14. {
  15. lua_pushinteger (L, rtc_mem_read (idx++));
  16. --n;
  17. ++ret;
  18. }
  19. return ret;
  20. }
  21. static int rtcmem_write32 (lua_State *L)
  22. {
  23. int idx = luaL_checkinteger (L, 1);
  24. int n = lua_gettop (L) - 1;
  25. luaL_argcheck (
  26. L, idx + n <= RTC_USER_MEM_NUM_DWORDS, 1, "RTC mem would overrun");
  27. int src = 2;
  28. while (n-- > 0)
  29. {
  30. rtc_mem_write (idx++, (uint32_t) lua_tointeger (L, src++));
  31. }
  32. return 0;
  33. }
  34. // Module function map
  35. LROT_BEGIN(rtcmem, NULL, 0)
  36. LROT_FUNCENTRY( read32, rtcmem_read32 )
  37. LROT_FUNCENTRY( write32, rtcmem_write32 )
  38. LROT_END(rtcmem, NULL, 0)
  39. NODEMCU_MODULE(RTCMEM, "rtcmem", rtcmem, NULL);