Browse Source

update rtc

SBird1337 6 years ago
parent
commit
0db5ce8e89
2 changed files with 54 additions and 32 deletions
  1. 3
    3
      src/overworld/dns/dns.c
  2. 51
    29
      src/rtc/rtc.c

+ 3
- 3
src/overworld/dns/dns.c View File

22
 
22
 
23
 
23
 
24
 void dns_blockset_load_palette(struct MapBlockset* blockset, u16 offset, u16 size){
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
     if(blockset != NULL){
28
     if(blockset != NULL){
26
         union Color *dns_buffer = malloc(DNS_BUF_SIZE*2);
29
         union Color *dns_buffer = malloc(DNS_BUF_SIZE*2);
27
         if(blockset->secondary > 0)
30
         if(blockset->secondary > 0)
63
 }
66
 }
64
 
67
 
65
 void dns_oec01_load_pal_impl(u32 *oe_script) {
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
     struct SpritePalette *pal = (struct SpritePalette *)oe_read_word(oe_script);
69
     struct SpritePalette *pal = (struct SpritePalette *)oe_read_word(oe_script);
70
     struct SpritePalette palToApply = {
70
     struct SpritePalette palToApply = {
71
         .data = NULL,
71
         .data = NULL,

+ 51
- 29
src/rtc/rtc.c View File

6
 #define IN 0
6
 #define IN 0
7
 #define OUT 1
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
     u16 sck : 1;
14
     u16 sck : 1;
11
     u16 sio : 1;
15
     u16 sio : 1;
12
     u16 cs : 1;
16
     u16 cs : 1;
14
     u16 free : 12;
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
     u16 sck : 1;
29
     u16 sck : 1;
21
     u16 sio : 1;
30
     u16 sio : 1;
22
     u16 cs : 1;
31
     u16 cs : 1;
24
     u16 free : 12;
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
 extern u16 rtc_control;
45
 extern u16 rtc_control;
32
 
46
 
33
 void rtc_wait_cycles(volatile u16 cycles);
47
 void rtc_wait_cycles(volatile u16 cycles);
38
 {
52
 {
39
     rtc_control = 1;
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
     rtc_wait_cycles(20);
62
     rtc_wait_cycles(20);
47
 
63
 
48
-    rtc_data.cs = HIGH;
64
+    rtc_data.ports.cs = HIGH;
49
     rtc_wait_cycles(20);
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
     rtc_write(0x65);
69
     rtc_write(0x65);
53
 
70
 
54
     out->year = rtc_read();
71
     out->year = rtc_read();
59
     out->minute = rtc_read();
76
     out->minute = rtc_read();
60
     out->second = rtc_read();
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
 void rtc_write(u8 b){
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
     for(u8 i = 0; i < 8; ++i) {
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
         rtc_wait_cycles(100);
92
         rtc_wait_cycles(100);
75
-        rtc_data.sck = HIGH;
93
+        rtc_data.packed = ( (HIGH << SCK) | (bit << SIO) | (LOW << CS));
76
         rtc_wait_cycles(100);
94
         rtc_wait_cycles(100);
95
+        b <<= 1;
77
     }
96
     }
78
 }
97
 }
79
 
98
 
80
 u8 rtc_read(void){
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
     for(u8 i = 0; i < 8; ++i) {
104
     for(u8 i = 0; i < 8; ++i) {
85
-        rtc_data.sck = LOW;
105
+        rtc_data.packed = 0;
106
+        //rtc_data.sck = LOW;
86
         rtc_wait_cycles(100);
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
         rtc_wait_cycles(100);
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
 //this is a blocking wait, because fk IRQ
116
 //this is a blocking wait, because fk IRQ