|
@@ -0,0 +1,110 @@
|
|
1
|
+#include <pokeagb/pokeagb.h>
|
|
2
|
+
|
|
3
|
+#define HIGH 1
|
|
4
|
+#define LOW 0
|
|
5
|
+#define IN 0
|
|
6
|
+#define OUT 1
|
|
7
|
+
|
|
8
|
+struct RtcTimestamp {
|
|
9
|
+ u8 year;
|
|
10
|
+ u8 month;
|
|
11
|
+ u8 day;
|
|
12
|
+ u8 day_of_week;
|
|
13
|
+ u8 hour;
|
|
14
|
+ u8 minute;
|
|
15
|
+ u8 second;
|
|
16
|
+};
|
|
17
|
+
|
|
18
|
+struct RtcGpioData {
|
|
19
|
+ u16 sck : 1;
|
|
20
|
+ u16 sio : 1;
|
|
21
|
+ u16 cs : 1;
|
|
22
|
+ u16 unused : 1;
|
|
23
|
+ u16 free : 12;
|
|
24
|
+};
|
|
25
|
+
|
|
26
|
+ASSERT_SIZEOF(struct RtcGpioData, 2);
|
|
27
|
+
|
|
28
|
+struct RtcGpioDirection {
|
|
29
|
+ u16 sck : 1;
|
|
30
|
+ u16 sio : 1;
|
|
31
|
+ u16 cs : 1;
|
|
32
|
+ u16 unused : 1;
|
|
33
|
+ u16 free : 12;
|
|
34
|
+};
|
|
35
|
+
|
|
36
|
+ASSERT_SIZEOF(struct RtcGpioDirection, 2);
|
|
37
|
+
|
|
38
|
+extern struct RtcGpioData rtc_data;
|
|
39
|
+extern struct RtcGpioDirection rtc_direction;
|
|
40
|
+extern u16 rtc_control;
|
|
41
|
+
|
|
42
|
+void rtc_wait_cycles(volatile u16 cycles);
|
|
43
|
+void rtc_write(u8 b);
|
|
44
|
+u8 rtc_read(void);
|
|
45
|
+
|
|
46
|
+void rtc_get_time(struct RtcTimestamp* out)
|
|
47
|
+{
|
|
48
|
+ rtc_control = 1;
|
|
49
|
+
|
|
50
|
+ rtc_direction.sck = OUT;
|
|
51
|
+ rtc_direction.cs = OUT;
|
|
52
|
+ rtc_data.cs = LOW;
|
|
53
|
+ rtc_data.sck = HIGH;
|
|
54
|
+
|
|
55
|
+ rtc_wait_cycles(20);
|
|
56
|
+
|
|
57
|
+ rtc_data.cs = HIGH;
|
|
58
|
+ rtc_wait_cycles(20);
|
|
59
|
+ rtc_direction.sck = OUT;
|
|
60
|
+ rtc_direction.cs = OUT;
|
|
61
|
+ rtc_write(0x65);
|
|
62
|
+
|
|
63
|
+ out->year = rtc_read();
|
|
64
|
+ out->month = rtc_read();
|
|
65
|
+ out->day = rtc_read();
|
|
66
|
+ out->day_of_week = rtc_read();
|
|
67
|
+ out->hour = rtc_read();
|
|
68
|
+ out->minute = rtc_read();
|
|
69
|
+ out->second = rtc_read();
|
|
70
|
+
|
|
71
|
+ rtc_data.sck = LOW;
|
|
72
|
+ rtc_data.sio = LOW;
|
|
73
|
+ rtc_data.cs = LOW;
|
|
74
|
+}
|
|
75
|
+
|
|
76
|
+void rtc_write(u8 b){
|
|
77
|
+ rtc_direction.sio = OUT;
|
|
78
|
+ rtc_direction.sck = OUT;
|
|
79
|
+ for(u8 i = 0; i < 8; ++i) {
|
|
80
|
+ rtc_data.sck = LOW;
|
|
81
|
+ rtc_data.sio = (b & 0x80) >> 7;
|
|
82
|
+ b <<= 1;
|
|
83
|
+ rtc_wait_cycles(100);
|
|
84
|
+ rtc_data.sck = HIGH;
|
|
85
|
+ rtc_wait_cycles(100);
|
|
86
|
+ }
|
|
87
|
+}
|
|
88
|
+
|
|
89
|
+u8 rtc_read(void){
|
|
90
|
+ u8 val = 0;
|
|
91
|
+ rtc_direction.sio = IN;
|
|
92
|
+ rtc_direction.sck = OUT;
|
|
93
|
+ for(u8 i = 0; i < 8; ++i) {
|
|
94
|
+ rtc_data.sck = LOW;
|
|
95
|
+ rtc_wait_cycles(100);
|
|
96
|
+ rtc_data.sck = HIGH;
|
|
97
|
+ val |= (rtc_data.sio << i);
|
|
98
|
+ rtc_wait_cycles(100);
|
|
99
|
+ }
|
|
100
|
+ return val;
|
|
101
|
+}
|
|
102
|
+
|
|
103
|
+//this is a blocking wait, because fk IRQ
|
|
104
|
+void rtc_wait_cycles(volatile u16 cycles)
|
|
105
|
+{
|
|
106
|
+ while(cycles > 0)
|
|
107
|
+ {
|
|
108
|
+ cycles--;
|
|
109
|
+ }
|
|
110
|
+}
|