luat_rtc_idf5.c 1001 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "luat_base.h"
  2. #include "luat_rtc.h"
  3. #include "sys/time.h"
  4. #include "esp_sleep.h"
  5. #include "luat_log.h"
  6. #define LUAT_LOG_TAG "rtc"
  7. void sntp_set_system_time(uint32_t sec, uint32_t us);
  8. extern int Base_year;
  9. int luat_rtc_set(struct tm *tblock){
  10. time_t timeSinceEpoch = mktime(tblock);
  11. struct timeval now = {0};
  12. now.tv_sec = timeSinceEpoch;
  13. settimeofday(&now, NULL);
  14. return 0;
  15. }
  16. int luat_rtc_get(struct tm *tblock){
  17. time_t now = {0};
  18. time(&now);
  19. gmtime_r(&now, tblock);
  20. return 0;
  21. }
  22. int luat_rtc_timer_start(int id, struct tm *tblock){
  23. time_t now = {0};
  24. time(&now);
  25. time_t time_rtc = mktime(tblock);
  26. uint64_t time_us = (time_rtc-now)*1000*1000;
  27. esp_sleep_enable_timer_wakeup(time_us);
  28. return 0;
  29. }
  30. int luat_rtc_timer_stop(int id){
  31. return -1; // 暂不支持
  32. }
  33. int luat_rtc_timezone(int* timezone) {
  34. return 32; // 暂不支持
  35. }
  36. void luat_rtc_set_tamp32(uint32_t tamp) {
  37. sntp_set_system_time(tamp, 0);
  38. return;
  39. }