Browse Source

add rtc wip

SBird1337 6 years ago
parent
commit
3c9403bab4
4 changed files with 136 additions and 1 deletions
  1. 4
    0
      bpre.sym
  2. 18
    0
      src/include/rtc.h
  3. 4
    1
      src/overworld/dns/dns.c
  4. 110
    0
      src/rtc/rtc.c

+ 4
- 0
bpre.sym View File

203
 
203
 
204
 current_oe_state = 0x020386E0;
204
 current_oe_state = 0x020386E0;
205
 
205
 
206
+rtc_data = 0x080000C4;
207
+rtc_direction = 0x080000C6;
208
+rtc_control = 0x080000C8;
209
+
206
 /*__aeabi_idiv = 0x081E4018|1;
210
 /*__aeabi_idiv = 0x081E4018|1;
207
 __aeabi_idivmod = 0x081E40F4|1;
211
 __aeabi_idivmod = 0x081E40F4|1;
208
 __aeabi_uidiv = 0x081E460C|1;
212
 __aeabi_uidiv = 0x081E460C|1;

+ 18
- 0
src/include/rtc.h View File

1
+#ifndef RTC_H_
2
+#define RTC_H_
3
+
4
+#include <pokeagb/pokeagb.h>
5
+
6
+struct RtcTimestamp {
7
+    u8 year;
8
+    u8 month;
9
+    u8 day;
10
+    u8 day_of_week;
11
+    u8 hour;
12
+    u8 minute;
13
+    u8 second;
14
+};
15
+
16
+void rtc_get_time(struct RtcTimestamp* out);
17
+
18
+#endif

+ 4
- 1
src/overworld/dns/dns.c View File

1
 #include <pokeagb/pokeagb.h>
1
 #include <pokeagb/pokeagb.h>
2
 #include <agb_debug.h>
2
 #include <agb_debug.h>
3
 #include <dns.h>
3
 #include <dns.h>
4
+#include <rtc.h>
4
 
5
 
5
 #define DNS_BUF_SIZE 256
6
 #define DNS_BUF_SIZE 256
6
 #define DNS_SEC_PAL_START 224
7
 #define DNS_SEC_PAL_START 224
62
 }
63
 }
63
 
64
 
64
 void dns_oec01_load_pal_impl(u32 *oe_script) {
65
 void dns_oec01_load_pal_impl(u32 *oe_script) {
66
+    struct RtcTimestamp stamp;
67
+    rtc_get_time(&stamp);
68
+    dprintf("rtc data was read: %x.%x.%x\n", stamp.day, stamp.month, stamp.year);
65
     struct SpritePalette *pal = (struct SpritePalette *)oe_read_word(oe_script);
69
     struct SpritePalette *pal = (struct SpritePalette *)oe_read_word(oe_script);
66
     struct SpritePalette palToApply = {
70
     struct SpritePalette palToApply = {
67
         .data = NULL,
71
         .data = NULL,
115
     memcpy(buffer, ow_pal_table[npcPalIdx].data,32);
119
     memcpy(buffer, ow_pal_table[npcPalIdx].data,32);
116
     dns_modify_palette(buffer,16);
120
     dns_modify_palette(buffer,16);
117
     gpu_pal_apply(buffer, 256 + 16*idx, 32);
121
     gpu_pal_apply(buffer, 256 + 16*idx, 32);
118
-
119
     free(buffer);
122
     free(buffer);
120
     tint_palette_switch(idx);
123
     tint_palette_switch(idx);
121
 }
124
 }

+ 110
- 0
src/rtc/rtc.c View File

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
+}