rtc_systime_service.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*!
  2. * \file systime.h
  3. *
  4. * \brief System time functions implementation.
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2018 Semtech - STMicroelectronics
  16. *
  17. * \endcode
  18. *
  19. * \author Miguel Luis ( Semtech )
  20. *
  21. * \author Gregory Cristian ( Semtech )
  22. *
  23. * \author MCD Application Team ( STMicroelectronics International )
  24. *
  25. * \author forest-rain
  26. */
  27. #ifndef __SYS_TIME_H__
  28. #define __SYS_TIME_H__
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33. #include <stdint.h>
  34. #include "time.h"
  35. /*!
  36. * \brief Days, Hours, Minutes and seconds of systime.h
  37. */
  38. #define TM_DAYS_IN_LEAP_YEAR ( ( uint32_t ) 366U )
  39. #define TM_DAYS_IN_YEAR ( ( uint32_t ) 365U )
  40. #define TM_SECONDS_IN_1DAY ( ( uint32_t )86400U )
  41. #define TM_SECONDS_IN_1HOUR ( ( uint32_t ) 3600U )
  42. #define TM_SECONDS_IN_1MINUTE ( ( uint32_t ) 60U )
  43. #define TM_MINUTES_IN_1HOUR ( ( uint32_t ) 60U )
  44. #define TM_HOURS_IN_1DAY ( ( uint32_t ) 24U )
  45. /*!
  46. * \brief Months of systime.h
  47. */
  48. #define TM_MONTH_JANUARY ( ( uint8_t ) 0U )
  49. #define TM_MONTH_FEBRUARY ( ( uint8_t ) 1U )
  50. #define TM_MONTH_MARCH ( ( uint8_t ) 2U )
  51. #define TM_MONTH_APRIL ( ( uint8_t ) 3U )
  52. #define TM_MONTH_MAY ( ( uint8_t ) 4U )
  53. #define TM_MONTH_JUNE ( ( uint8_t ) 5U )
  54. #define TM_MONTH_JULY ( ( uint8_t ) 6U )
  55. #define TM_MONTH_AUGUST ( ( uint8_t ) 7U )
  56. #define TM_MONTH_SEPTEMBER ( ( uint8_t ) 8U )
  57. #define TM_MONTH_OCTOBER ( ( uint8_t ) 9U )
  58. #define TM_MONTH_NOVEMBER ( ( uint8_t )10U )
  59. #define TM_MONTH_DECEMBER ( ( uint8_t )11U )
  60. /*!
  61. * \brief Week days of systime.h
  62. */
  63. #define TM_WEEKDAY_SUNDAY ( ( uint8_t )0U )
  64. #define TM_WEEKDAY_MONDAY ( ( uint8_t )1U )
  65. #define TM_WEEKDAY_TUESDAY ( ( uint8_t )2U )
  66. #define TM_WEEKDAY_WEDNESDAY ( ( uint8_t )3U )
  67. #define TM_WEEKDAY_THURSDAY ( ( uint8_t )4U )
  68. #define TM_WEEKDAY_FRIDAY ( ( uint8_t )5U )
  69. #define TM_WEEKDAY_SATURDAY ( ( uint8_t )6U )
  70. /*!
  71. * \brief Number of seconds elapsed between Unix and GPS epoch
  72. */
  73. #define UNIX_GPS_EPOCH_OFFSET 315964800
  74. /*!
  75. * \brief Structure holding the system time in seconds and milliseconds.
  76. */
  77. typedef struct SysTime_s
  78. {
  79. uint32_t Seconds;
  80. int16_t SubSeconds;
  81. }SysTime_t;
  82. /*!
  83. * \brief Adds 2 SysTime_t values
  84. *
  85. * \param a Value
  86. * \param b Value to added
  87. *
  88. * \retval result Addition result (SysTime_t value)
  89. */
  90. SysTime_t SysTimeAdd( SysTime_t a, SysTime_t b );
  91. /*!
  92. * \brief Subtracts 2 SysTime_t values
  93. *
  94. * \param a Value
  95. * \param b Value to be subtracted
  96. *
  97. * \retval result Subtraction result (SysTime_t value)
  98. */
  99. SysTime_t SysTimeSub( SysTime_t a, SysTime_t b );
  100. /*!
  101. * \brief Sets new system time
  102. *
  103. * \param sysTime New seconds/sub-seconds since UNIX epoch origin
  104. */
  105. void SysTimeSet( SysTime_t sysTime );
  106. /*!
  107. * \brief Gets current system time
  108. *
  109. * \retval sysTime Current seconds/sub-seconds since UNIX epoch origin
  110. */
  111. SysTime_t SysTimeGet( void );
  112. /*!
  113. * \brief Gets current MCU system time
  114. *
  115. * \retval sysTime Current seconds/sub-seconds since Mcu started
  116. */
  117. SysTime_t SysTimeGetMcuTime( void );
  118. /*!
  119. * Converts the given SysTime to the equivalent RTC value in milliseconds
  120. *
  121. * \param [IN] sysTime System time to be converted
  122. *
  123. * \retval timeMs The RTC converted time value in ms
  124. */
  125. uint32_t SysTimeToMs( SysTime_t sysTime );
  126. /*!
  127. * Converts the given RTC value in milliseconds to the equivalent SysTime
  128. *
  129. * \param [IN] timeMs The RTC time value in ms to be converted
  130. *
  131. * \retval sysTime Converted system time
  132. */
  133. SysTime_t SysTimeFromMs( uint32_t timeMs );
  134. /*!
  135. * \brief Convert a calendar time into time since UNIX epoch as a uint32_t.
  136. *
  137. * \param [IN] localtime Pointer to the object containing the calendar time
  138. * \retval timestamp The calendar time as seconds since UNIX epoch.
  139. */
  140. uint32_t SysTimeMkTime( const struct tm* localtime );
  141. /*!
  142. * \brief Converts a given time in seconds since UNIX epoch into calendar time.
  143. *
  144. * \param [IN] timestamp The time since UNIX epoch to convert into calendar time.
  145. * \param [OUT] localtime Pointer to the calendar time object which will contain
  146. the result of the conversion.
  147. */
  148. void SysTimeLocalTime( const uint32_t timestamp, struct tm *localtime );
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. #endif // __SYS_TIME_H__