SBird1337 vor 6 Jahren
Ursprung
Commit
0db5ce8e89
2 geänderte Dateien mit 54 neuen und 32 gelöschten Zeilen
  1. 3
    3
      src/overworld/dns/dns.c
  2. 51
    29
      src/rtc/rtc.c

+ 3
- 3
src/overworld/dns/dns.c Datei anzeigen

@@ -22,6 +22,9 @@ union Color dns_color_multiply(union Color a, union Color b);
22 22
 
23 23
 
24 24
 void dns_blockset_load_palette(struct MapBlockset* blockset, u16 offset, u16 size){
25
+    struct RtcTimestamp stamp;
26
+    rtc_get_time(&stamp);
27
+    dprintf("rtc data was read: %x.%x.%x\n", stamp.day, stamp.month, stamp.year);
25 28
     if(blockset != NULL){
26 29
         union Color *dns_buffer = malloc(DNS_BUF_SIZE*2);
27 30
         if(blockset->secondary > 0)
@@ -63,9 +66,6 @@ void dns_blockset_load_palette(struct MapBlockset* blockset, u16 offset, u16 siz
63 66
 }
64 67
 
65 68
 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);
69 69
     struct SpritePalette *pal = (struct SpritePalette *)oe_read_word(oe_script);
70 70
     struct SpritePalette palToApply = {
71 71
         .data = NULL,

+ 51
- 29
src/rtc/rtc.c Datei anzeigen

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