fw_rtc.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ___FW_RTC_H___
  15. #define ___FW_RTC_H___
  16. #include "fw_conf.h"
  17. #include "fw_types.h"
  18. typedef enum
  19. {
  20. RTC_ClockSource_External = 0x00,
  21. RTC_ClockSource_Internal = 0x01,
  22. } RTC_ClockSource_t;
  23. #define RTC_SetRunState(__STATE__) SFRX_ASSIGN(RTCCR, 0, (__STATE__))
  24. #define RTC_SetClockSource(__SOURCE__) SFRX_ASSIGN(RTCCFG, 1, (__SOURCE__))
  25. #define RTC_ConfigClockApply() SFRX_SET(RTCCFG, 0)
  26. /**
  27. * Call SFRX_ON(); before calling following macros
  28. */
  29. #define RTC_ClearAllInterrupts() (RTCIF = 0x00)
  30. #define RTC_IsAlarmInterrupt() (RTCIF & 0x80)
  31. #define RTC_ClearAlarmInterrupt() (RTCIF &= ~0x80)
  32. #define RTC_IsDayInterrupt() (RTCIF & 0x40)
  33. #define RTC_ClearDayInterrupt() (RTCIF &= ~0x40)
  34. #define RTC_IsHourInterrupt() (RTCIF & 0x20)
  35. #define RTC_ClearHourInterrupt() (RTCIF &= ~0x20)
  36. #define RTC_IsMinuteInterrupt() (RTCIF & 0x10)
  37. #define RTC_ClearMinuteInterrupt() (RTCIF &= ~0x10)
  38. #define RTC_IsSecondInterrupt() (RTCIF & 0x08)
  39. #define RTC_ClearSecondInterrupt() (RTCIF &= ~0x08)
  40. #define RTC_IsSecondDiv2Interrupt() (RTCIF & 0x04)
  41. #define RTC_ClearSecondDiv2Interrupt() (RTCIF &= ~0x04)
  42. #define RTC_IsSecondDiv8Interrupt() (RTCIF & 0x02)
  43. #define RTC_ClearSecondDiv8Interrupt() (RTCIF &= ~0x02)
  44. #define RTC_IsSecondDiv32Interrupt() (RTCIF & 0x01)
  45. #define RTC_ClearSecondDiv32Interrupt() (RTCIF &= ~0x01)
  46. /**
  47. * Set Alarm
  48. */
  49. #define RTC_ConfigAlarm(__HOUR__, __MIN__, __SEC__, __SSEC__) do { \
  50. SFRX_ON(); \
  51. ALAHOUR = (__HOUR__ & 0x1F); \
  52. ALAMIN = (__MIN__ & 0x3F); \
  53. ALASEC = (__SEC__ & 0x3F); \
  54. ALASSEC = (__SSEC__ & 0x7F); \
  55. SFRX_OFF(); \
  56. } while(0)
  57. /**
  58. * Set clock initial value
  59. */
  60. #define RTC_ConfigClock(__YEAR__, __MON__, __DAY__, __HOUR__, __MIN__, __SEC__, __SSEC__) do { \
  61. SFRX_ON(); \
  62. INIYEAR = (__YEAR__ & 0x7F); \
  63. INIMONTH = (__MON__ & 0x0F); \
  64. INIDAY = (__DAY__ & 0x1F); \
  65. INIHOUR = (__HOUR__ & 0x1F); \
  66. INIMIN = (__MIN__ & 0x3F); \
  67. INISEC = (__SEC__ & 0x3F); \
  68. INISSEC = (__SSEC__ & 0x7F); \
  69. SFRX_OFF(); \
  70. } while(0)
  71. #endif