-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtc.cpp
174 lines (151 loc) · 7.25 KB
/
rtc.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Copyright 2019-2020 Hydr8gon
This file is part of NooDS.
NooDS 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.
NooDS 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 NooDS. If not, see <https://www.gnu.org/licenses/>.
*/
#include <ctime>
#include "rtc.h"
#include "core.h"
// I find GBATEK's RTC documentation to be lacking, so here's a quick summary of how the I/O works
//
// Bits 2 and 6 are connected to the CS pinout
// Bit 6 should always be set, so setting bit 2 to 1 or 0 causes CS to be high or low, respectively
//
// Bits 1 and 5 are connected to the SCK pinout
// Bit 5 should always be set, so setting bit 1 to 1 or 0 causes SCK to be high or low, respectively
//
// Bits 0 and 4 are connected to the SIO pinout
// Bit 4 indicates data direction; 0 is read, and 1 is write
// Bit 0 is where data sent from the RTC is read, and where data sent to the RTC is written
//
// To start a transfer, switch CS from low to high
// To end a transfer, switch CS from high to low
//
// To transfer a bit, set SCK to low and then high (it should be high when the transfer starts)
// When writing a bit to the RTC, you should set bit 0 at the same time as setting SCK to low
// When reading a bit from the RTC, you should read bit 0 after setting SCK to low (or high?)
void Rtc::writeRtc(uint8_t value)
{
if (value & BIT(2)) // CS high
{
/*if ((rtc & BIT(1)) && !(value & BIT(1))) // SCK high to low
{
if (writeCount < 8)
{
// Write the first 8 bits to the command register, in reverse order
command |= (value & BIT(0)) << (7 - writeCount);
writeCount++;
}
else
{
if (!(value & BIT(4))) // Read
{
value &= ~BIT(0);
// Read a bit from an RTC register
switch ((command & 0x0E) >> 1) // Register select
{
case 0: // Status register 1
{
value |= ((status1 >> (writeCount - 8)) & BIT(0));
break;
}
case 2: // Date and time
{
// Update the date and time
if (writeCount == 8)
{
// Get the local time
std::time_t t = std::time(nullptr);
std::tm *time = std::localtime(&t);
time->tm_year %= 100; // The DS only counts years 2000-2099
time->tm_mon++; // The DS starts month values at 1, not 0
// Convert to 12-hour mode if enabled
uint8_t hour = time->tm_hour;
if (!(status1 & BIT(1))) time->tm_hour %= 12;
// Save to the date and time registers in BCD format
// Index 3 contains the day of the week, but most things don't care
dateTime[0] = ((time->tm_year / 10) << 4) | (time->tm_year % 10);
dateTime[1] = ((time->tm_mon / 10) << 4) | (time->tm_mon % 10);
dateTime[2] = ((time->tm_mday / 10) << 4) | (time->tm_mday % 10);
dateTime[4] = ((time->tm_hour / 10) << 4) | (time->tm_hour % 10);
dateTime[5] = ((time->tm_min / 10) << 4) | (time->tm_min % 10);
dateTime[6] = ((time->tm_sec / 10) << 4) | (time->tm_sec % 10);
// Set the AM/PM bit
if (hour >= 12) dateTime[4] |= BIT(6);
}
value |= ((dateTime[(writeCount / 8) - 1] >> (writeCount % 8)) & BIT(0));
break;
}
case 3: // Time
{
// Update the time
if (writeCount == 8)
{
// Get the local time
std::time_t t = std::time(nullptr);
std::tm *time = std::localtime(&t);
// Convert to 12-hour mode if enabled
uint8_t hour = time->tm_hour;
if (!(status1 & BIT(1))) time->tm_hour %= 12;
// Save to the date and time registers in BCD format
dateTime[4] = ((time->tm_hour / 10) << 4) | (time->tm_hour % 10);
dateTime[5] = ((time->tm_min / 10) << 4) | (time->tm_min % 10);
dateTime[6] = ((time->tm_sec / 10) << 4) | (time->tm_sec % 10);
// Set the AM/PM bit
if (hour >= 12) dateTime[4] |= BIT(6);
}
value |= ((dateTime[(writeCount / 8) - 5] >> (writeCount % 8)) & BIT(0));
break;
}
default:
{
printf("Read from unknown RTC registers: %d\n", (command & 0x0E) >> 1);
break;
}
}
}
else // Write
{
// Write a bit to an RTC register
switch ((command & 0x0E) >> 1) // Register select
{
case 0: // Status register 1
{
// Only write to the writable bits 1 through 3
if (writeCount - 8 >= 1 && writeCount - 8 <= 3)
status1 = (status1 & ~BIT(writeCount - 8)) | ((value & BIT(0)) << (writeCount - 8));
break;
}
default:
{
printf("Write to unknown RTC registers: %d\n", (command & 0x0E) >> 1);
break;
}
}
}
writeCount++;
}
}
else if (!(rtc & BIT(1)) && (value & BIT(1)) && !(value & BIT(4))) // SCK low to high, read
{
// Read bits can still be read after switching SCK to high, so keep the previous bit value
value = (value & ~BIT(0)) | (rtc & BIT(0));
}*/
}
else // CS low
{
// Reset the transfer
writeCount = 0;
command = 0;
}
rtc = value;
}