systime.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Get time according to system (RTC or whatever we have...)
  3. * This is similar to localtime() in the standard C library, except
  4. * only the current time is supported and some of the derived fields
  5. * are not produced.
  6. */
  7. #ifndef SYSTIME_H
  8. #define SYSTIME_H
  9. #include <stdint.h>
  10. #include "irq.h"
  11. struct tms_tick_reg {
  12. union {
  13. uint16_t ticks;
  14. struct {
  15. unsigned int tm_tick : 15;
  16. unsigned int tm_1sec : 1;
  17. };
  18. };
  19. } __attribute__((aligned(2)));
  20. struct tms {
  21. union {
  22. uint32_t words[2];
  23. struct {
  24. unsigned int tm_2sec : 5;
  25. unsigned int tm_min : 6;
  26. unsigned int tm_hour : 5;
  27. unsigned int tm_mday : 5;
  28. unsigned int tm_mon : 4;
  29. unsigned int tm_year : 7;
  30. struct tms_tick_reg hold;
  31. struct tms_tick_reg now;
  32. };
  33. };
  34. } __attribute__((aligned(4)));
  35. static inline struct tms get_systime(void)
  36. {
  37. struct tms tms;
  38. irqmask_t mask = mask_irq(ESP_IRQ);
  39. tms.words[0] = SYSCLOCK_DATETIME;
  40. tms.words[1] = SYSCLOCK_TICK;
  41. restore_irqs(mask);
  42. return tms;
  43. }
  44. static inline void set_systime(struct tms tms)
  45. {
  46. irqmask_t mask = mask_irq(ESP_IRQ);
  47. SYSCLOCK_TICK_HOLD = tms.hold.ticks;
  48. SYSCLOCK_DATETIME = tms.words[0];
  49. restore_irqs(mask);
  50. }
  51. static inline unsigned int tms_sec(struct tms tms)
  52. {
  53. return (tms.tm_2sec << 1) | tms.hold.tm_1sec;
  54. }
  55. #endif /* SYSTIME_H */