2
0

systime.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. struct tms_tick_reg {
  11. union {
  12. uint16_t ticks;
  13. struct {
  14. unsigned int tm_tick : 15;
  15. unsigned int tm_1sec : 1;
  16. };
  17. };
  18. } __attribute__((aligned(2)));
  19. struct tms {
  20. union {
  21. uint32_t words[2];
  22. struct {
  23. unsigned int tm_2sec : 5;
  24. unsigned int tm_min : 6;
  25. unsigned int tm_hour : 5;
  26. unsigned int tm_mday : 5;
  27. unsigned int tm_mon : 4;
  28. unsigned int tm_year : 7;
  29. struct tms_tick_reg hold;
  30. struct tms_tick_reg now;
  31. };
  32. };
  33. } __attribute__((aligned(4)));
  34. static inline struct tms get_systime(void)
  35. {
  36. struct tms tms;
  37. tms.words[0] = SYSCLOCK_DATETIME;
  38. tms.words[1] = SYSCLOCK_TICK;
  39. return tms;
  40. }
  41. static inline void set_systime(struct tms tms)
  42. {
  43. SYSCLOCK_TICK_HOLD = tms.hold.ticks;
  44. SYSCLOCK_DATETIME = tms.words[0];
  45. }
  46. static inline unsigned int tms_sec(struct tms tms)
  47. {
  48. return (tms.tm_2sec << 1) | tms.hold.tm_1sec;
  49. }
  50. #endif /* SYSTIME_H */