Переглянути джерело

more rtc failures (im crying rnow)

SBird1337 6 роки тому
джерело
коміт
259214ea3b
1 змінених файлів з 38 додано та 20 видалено
  1. 38
    20
      src/rtc/rtc.c

+ 38
- 20
src/rtc/rtc.c Переглянути файл

@@ -46,6 +46,9 @@ extern u16 rtc_control;
46 46
 
47 47
 void rtc_wait_cycles(volatile u16 cycles);
48 48
 void rtc_write(u8 b);
49
+void rtc_block_wait(void);
50
+void rtc_gpio_set_data(bool sck, bool sio, bool cs);
51
+
49 52
 u8 rtc_read(void);
50 53
 
51 54
 void rtc_get_time(struct RtcTimestamp* out)
@@ -54,15 +57,16 @@ void rtc_get_time(struct RtcTimestamp* out)
54 57
 
55 58
     //rtc_direction.sck = OUT;
56 59
     //rtc_direction.cs = OUT;
57
-    rtc_direction.packed = ( (OUT << SCK) | (OUT << CS) );
58
-    rtc_data.packed = HIGH << SCK;
60
+    rtc_direction.packed = 5;
61
+    rtc_control = 1;
62
+    rtc_direction.packed = 7;
63
+    rtc_gpio_set_data(HIGH, LOW, LOW);
59 64
     //rtc_data.cs = LOW;
60 65
     //rtc_data.sck = HIGH;
66
+    rtc_block_wait();
61 67
 
62
-    rtc_wait_cycles(20);
63
-
64
-    rtc_data.ports.cs = HIGH;
65
-    rtc_wait_cycles(20);
68
+    rtc_gpio_set_data(HIGH,LOW,HIGH);
69
+    rtc_block_wait();
66 70
     //rtc_direction.packed = ( (OUT << SCK) | (OUT << CS) | (OUT << SIO));
67 71
     //rtc_direction.sck = OUT;
68 72
     //rtc_direction.cs = OUT;
@@ -84,35 +88,44 @@ void rtc_get_time(struct RtcTimestamp* out)
84 88
 
85 89
 void rtc_write(u8 b){
86 90
     //all out
87
-    rtc_direction.packed = ( (OUT << SCK) | (OUT << CS) | (OUT << SIO));
88
-    for(u8 i = 0; i < 8; ++i) {
91
+    rtc_direction.packed = 7;
92
+    for(u8 i = 8; i > 0; ++i) {
89 93
         u8 bit = (u8) ((b & 0x80) >> 7);
90
-        rtc_data.packed = ( (LOW << SCK) | (bit << SIO) | (LOW << CS));
91
-        
92
-        rtc_wait_cycles(100);
93
-        rtc_data.packed = ( (HIGH << SCK) | (bit << SIO) | (LOW << CS));
94
-        rtc_wait_cycles(100);
95 94
         b <<= 1;
95
+        rtc_gpio_set_data(LOW,bit,LOW);
96
+        rtc_block_wait();
97
+        rtc_gpio_set_data(HIGH,bit,LOW);
98
+        rtc_block_wait();
96 99
     }
97 100
 }
98 101
 
99 102
 u8 rtc_read(void){
100 103
     u16 val = 0;
101
-    rtc_direction.packed = ((SIO << IN) | (SCK << OUT) | (CS << OUT));
104
+    rtc_direction.packed = 5;
102 105
     //rtc_direction.sio = IN;
103 106
     //rtc_direction.sck = OUT;
104 107
     for(u8 i = 0; i < 8; ++i) {
105
-        rtc_data.packed = 0;
108
+        rtc_gpio_set_data(LOW,LOW,LOW);
106 109
         //rtc_data.sck = LOW;
107
-        rtc_wait_cycles(100);
108
-        rtc_data.packed = ((HIGH << SCK) | (LOW << SIO) | (LOW << CS));
110
+        rtc_block_wait();
111
+        rtc_gpio_set_data(HIGH,LOW,LOW);
109 112
         //rtc_data.sck = HIGH;
110
-        rtc_wait_cycles(100);
111
-        val |= (rtc_data.ports.sio << i);
113
+        rtc_block_wait();
114
+        val |= ((rtc_data.ports.sio) << i);
112 115
     }
113 116
     return (u8)(val >> 1);
114 117
 }
115 118
 
119
+void rtc_block_wait(void) {
120
+    u8 i = 100;
121
+    while (i--) {
122
+        __asm__ __volatile__
123
+        (
124
+            "nop\n\r"
125
+        );
126
+    }
127
+}
128
+
116 129
 //this is a blocking wait, because fk IRQ
117 130
 void rtc_wait_cycles(volatile u16 cycles)
118 131
 {
@@ -120,4 +133,9 @@ void rtc_wait_cycles(volatile u16 cycles)
120 133
     {
121 134
         cycles--;
122 135
     }
123
-}
136
+}
137
+
138
+void rtc_gpio_set_data(bool sck, bool sio, bool cs) {
139
+    u16 value = (u16) (sck | (sio << 1) | (cs << 2));
140
+    rtc_data.packed = value;
141
+}