|
| 1 | +#pragma once |
1 | 2 | /** |
2 | 3 | * @file hwclock.h |
3 | 4 | * |
4 | 5 | */ |
5 | | -/* Copyright (C) 2020-2023 by Arjan van Vught mailto:info@gd32-dmx.org |
| 6 | +/* Copyright (C) 2020-2025 by Arjan van Vught mailto:info@gd32-dmx.org |
6 | 7 | * |
7 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | 9 | * of this software and associated documentation files (the "Software"), to deal |
|
23 | 24 | * THE SOFTWARE. |
24 | 25 | */ |
25 | 26 |
|
26 | | -#ifndef HWCLOCK_H_ |
27 | | -#define HWCLOCK_H_ |
28 | | - |
29 | 27 | #include <cstdint> |
30 | 28 | #include <time.h> |
31 | 29 | #include <sys/time.h> |
32 | 30 |
|
33 | | -namespace rtc { |
34 | | -enum class Type : uint8_t { |
35 | | - MCP7941X, DS3231, PCF8563, SOC_INTERNAL, UNKNOWN |
36 | | -}; |
37 | | -} // namespace rtc |
38 | | - |
39 | | -class HwClock { |
40 | | -public: |
41 | | - HwClock(); |
42 | | - void RtcProbe(); |
43 | | - |
44 | | - void HcToSys(); // Set the System Clock from the Hardware Clock |
45 | | - void SysToHc(); // Set the Hardware Clock from the System Clock |
46 | | - |
47 | | - bool Set(const struct tm *pTime); |
48 | | - bool Get(struct tm *pTime) { |
49 | | - return RtcGet(pTime); |
50 | | - } |
51 | | - |
52 | | - bool AlarmSet(const struct tm *pTime) { |
53 | | - return RtcSetAlarm(pTime); |
54 | | - } |
55 | | - bool AlarmGet(struct tm *pTime) { |
56 | | - return RtcGetAlarm(pTime); |
57 | | - } |
58 | | - void AlarmEnable(const bool bEnable) { |
59 | | - m_bRtcAlarmEnabled = bEnable; |
60 | | - } |
61 | | - bool AlarmIsEnabled() const { |
62 | | - return m_bRtcAlarmEnabled; |
63 | | - } |
64 | | - |
65 | | - bool IsConnected() const { |
66 | | - return is_connected_; |
67 | | - } |
68 | | - |
69 | | - void Run(const bool bDoRun) { |
70 | | - if (!bDoRun || !is_connected_) { |
71 | | - return; |
72 | | - } |
73 | | - Process(); |
74 | | - } |
75 | | - |
76 | | - void Print(); |
77 | | - |
78 | | - static HwClock *Get() { |
79 | | - return s_this; |
80 | | - } |
81 | | - |
82 | | -private: |
83 | | - void Process(); |
84 | | - bool RtcSet(const struct tm *pime); |
85 | | - bool RtcGet(struct tm *pTime); |
86 | | - bool RtcSetAlarm(const struct tm *pTime); |
87 | | - bool RtcGetAlarm(struct tm *pTime); |
88 | | - int MCP794xxAlarmWeekday(struct tm *pTime); |
89 | | - void PCF8563GetAlarmMode(); |
90 | | - void PCF8563SetAlarmMode(); |
91 | | - |
92 | | -private: |
93 | | - uint32_t m_nSetDelayMicros { 0 }; |
94 | | - uint32_t m_nLastHcToSysMillis { 0 }; |
95 | | - uint8_t address_ { 0 }; |
96 | | - rtc::Type m_Type { rtc::Type::UNKNOWN }; |
97 | | - bool is_connected_ { false }; |
98 | | - bool m_bRtcAlarmEnabled { false }; |
99 | | - bool m_bRtcAlarmPending { false }; |
100 | | - |
101 | | - static inline HwClock *s_this; |
| 31 | +namespace rtc |
| 32 | +{ |
| 33 | +enum class Type : uint8_t |
| 34 | +{ |
| 35 | + kMcP7941X, |
| 36 | + kDS3231, |
| 37 | + kPcF8563, |
| 38 | + kSocInternal, |
| 39 | + kUnknown |
102 | 40 | }; |
| 41 | +} // namespace rtc |
| 42 | + |
| 43 | +class HwClock |
| 44 | +{ |
| 45 | + public: |
| 46 | + HwClock(); |
| 47 | + void RtcProbe(); |
| 48 | + |
| 49 | + void HcToSys(); // Set the System Clock from the Hardware Clock |
| 50 | + void SysToHc(); // Set the Hardware Clock from the System Clock |
| 51 | + |
| 52 | + bool Set(const struct tm* time); |
| 53 | + bool Get(struct tm* time) { return RtcGet(time); } |
| 54 | + |
| 55 | + bool AlarmSet(const struct tm* time) { return RtcSetAlarm(time); } |
| 56 | + |
| 57 | + bool AlarmGet(struct tm* time) { return RtcGetAlarm(time); } |
| 58 | + |
| 59 | + void AlarmEnable(bool enable) { m_bRtcAlarmEnabled = enable; } |
103 | 60 |
|
104 | | -#endif /* HWCLOCK_H_ */ |
| 61 | + bool AlarmIsEnabled() const { return m_bRtcAlarmEnabled; } |
| 62 | + |
| 63 | + bool IsConnected() const { return is_connected_; } |
| 64 | + |
| 65 | + void Run(bool do_run) |
| 66 | + { |
| 67 | + if (!do_run || !is_connected_) |
| 68 | + { |
| 69 | + return; |
| 70 | + } |
| 71 | + Process(); |
| 72 | + } |
| 73 | + |
| 74 | + void Print(); |
| 75 | + |
| 76 | + static HwClock* Get() { return s_this; } |
| 77 | + |
| 78 | + private: |
| 79 | + void Process(); |
| 80 | + bool RtcSet(const struct tm* time); |
| 81 | + bool RtcGet(struct tm* time); |
| 82 | + bool RtcSetAlarm(const struct tm* time); |
| 83 | + bool RtcGetAlarm(struct tm* time); |
| 84 | + int MCP794xxAlarmWeekday(struct tm* time); |
| 85 | + void PCF8563GetAlarmMode(); |
| 86 | + void PCF8563SetAlarmMode(); |
| 87 | + |
| 88 | + private: |
| 89 | + uint32_t m_nSetDelayMicros{0}; |
| 90 | + uint32_t m_nLastHcToSysMillis{0}; |
| 91 | + uint8_t address_{0}; |
| 92 | + rtc::Type m_Type{rtc::Type::kUnknown}; |
| 93 | + bool is_connected_{false}; |
| 94 | + bool m_bRtcAlarmEnabled{false}; |
| 95 | + bool m_bRtcAlarmPending{false}; |
| 96 | + |
| 97 | + static inline HwClock* s_this; |
| 98 | +}; |
0 commit comments