-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathfirmwarertc.cpp
118 lines (102 loc) · 2.84 KB
/
firmwarertc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// firmwarertc.cpp
//
// Circle - A C++ bare metal environment for Raspberry Pi
// Copyright (C) 2024 R. Stange <rsta2@o2online.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <rtc/firmwarertc.h>
#include <circle/bcmpropertytags.h>
#include <circle/machineinfo.h>
#include <circle/logger.h>
#include <assert.h>
LOGMODULE ("fwrtc");
boolean CFirmwareRTC::Initialize (void)
{
const CDeviceTreeBlob *pDTB = CMachineInfo::Get ()->GetDTB ();
const TDeviceTreeNode *pRTCNode;
const TDeviceTreeProperty *pChargeMicroVolt;
if ( !pDTB
|| !(pRTCNode = pDTB->FindNode ("/soc/rpi_rtc"))
|| !(pChargeMicroVolt = pDTB->FindProperty (pRTCNode, "trickle-charge-microvolt"))
|| pDTB->GetPropertyValueLength (pChargeMicroVolt) != 4)
{
return FALSE;
}
u32 nChargeMicroVolt = pDTB->GetPropertyValueWord (pChargeMicroVolt, 0);
CBcmPropertyTags Tags;
TPropertyTagRTCRegister RTCReg;
RTCReg.nRegNum = RTC_REGISTER_BBAT_CHG_VOLTS;
RTCReg.nValue = nChargeMicroVolt;
if (!Tags.GetTag (PROPTAG_SET_RTC_REG, &RTCReg, sizeof RTCReg, 8))
{
return FALSE;
}
if (nChargeMicroVolt)
{
LOGNOTE ("Charging enabled at %.1fV", nChargeMicroVolt / 1000000.0);
}
#ifndef NDEBUG
//DumpStatus ();
#endif
return TRUE;
}
boolean CFirmwareRTC::Get (CTime *pUTCTime)
{
assert (pUTCTime);
CBcmPropertyTags Tags;
TPropertyTagRTCRegister RTCReg;
RTCReg.nRegNum = RTC_REGISTER_TIME;
if (!Tags.GetTag (PROPTAG_GET_RTC_REG, &RTCReg, sizeof RTCReg, 4))
{
return FALSE;
}
pUTCTime->Set (RTCReg.nValue);
return TRUE;
}
boolean CFirmwareRTC::Set (const CTime &UTCTime)
{
CBcmPropertyTags Tags;
TPropertyTagRTCRegister RTCReg;
RTCReg.nRegNum = RTC_REGISTER_TIME;
RTCReg.nValue = UTCTime.Get ();
return Tags.GetTag (PROPTAG_SET_RTC_REG, &RTCReg, sizeof RTCReg, 8);
}
#ifndef NDEBUG
void CFirmwareRTC::DumpStatus (void)
{
static const char *Name[] =
{
"TIME",
"ALARM",
"ALARM_PENDING",
"ALARM_ENABLE",
"BBAT_CHG_VOLTS",
"BBAT_CHG_VOLTS_MIN",
"BBAT_CHG_VOLTS_MAX",
"BBAT_VOLTS"
};
for (unsigned i = 0; i < RTC_REGISTER_COUNT; i++)
{
CBcmPropertyTags Tags;
TPropertyTagRTCRegister RTCReg;
RTCReg.nRegNum = i;
if (Tags.GetTag (PROPTAG_GET_RTC_REG, &RTCReg, sizeof RTCReg, 4))
{
LOGNOTE ("%10u %s", RTCReg.nValue, Name[i]);
}
}
}
#endif