Revision 380
fixed rtc
proj/src/data/scoreboard.txt | ||
---|---|---|
1 |
06/01/20 18:01:51 - 8 - 45 |
|
2 |
06/01/20 15:01:27 - 6 - 38 |
|
3 |
06/01/20 19:01:08 - 5 - 25 |
|
1 |
06/01/20 20:43:27 - 1 - 2 |
|
2 |
06/01/20 20:01:58 - 0 - 15 |
|
3 |
06/01/20 34:01:31 - 0 - 11 |
proj/src/libs/rtc/include/rtc.h | ||
---|---|---|
11 | 11 |
|
12 | 12 |
#include <stdint.h> |
13 | 13 |
|
14 |
// RTC IRQ Line |
|
15 |
|
|
16 |
#define RTC_IRQ 8 /* @brief RTC IRQ Line */ |
|
17 |
|
|
14 | 18 |
/** |
15 | 19 |
* @brief Subscribes RTC Interrupts |
16 | 20 |
* @param interrupt_bit Bit of Interrupt Vector that will be set when RTC Interrupt is pending |
... | ... | |
38 | 42 |
int (rtc_write_register)(uint32_t reg, uint8_t data); |
39 | 43 |
|
40 | 44 |
/** |
45 |
* @brief Reads seconds register. |
|
46 |
* |
|
47 |
* @param sec Address to store value |
|
48 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
49 |
* @see {_ERRORS_H_::errors} |
|
50 |
*/ |
|
51 |
int rtc_read_sec(uint8_t *sec); |
|
52 |
|
|
53 |
/** |
|
54 |
* @brief Reads minutes register. |
|
55 |
* |
|
56 |
* @param min Address to store value |
|
57 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
58 |
* @see {_ERRORS_H_::errors} |
|
59 |
*/ |
|
60 |
int rtc_read_min(uint8_t *min); |
|
61 |
|
|
62 |
/** |
|
63 |
* @brief Reads hours register. |
|
64 |
* |
|
65 |
* @param hour Address to store value |
|
66 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
67 |
* @see {_ERRORS_H_::errors} |
|
68 |
*/ |
|
69 |
int rtc_read_hour(uint8_t *hour); |
|
70 |
/** |
|
71 |
* @brief Reads weekday register. |
|
72 |
* |
|
73 |
* @param hour Address to store value |
|
74 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
75 |
* @see {_ERRORS_H_::errors} |
|
76 |
*/ |
|
77 |
int (rtc_read_weekday)(uint8_t *weekday); |
|
78 |
/** |
|
79 |
* @brief Reads day register. |
|
80 |
* |
|
81 |
* @param hour Address to store value |
|
82 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
83 |
* @see {_ERRORS_H_::errors} |
|
84 |
*/ |
|
85 |
int (rtc_read_day)(uint8_t *day); |
|
86 |
/** |
|
87 |
* @brief Reads month register. |
|
88 |
* |
|
89 |
* @param hour Address to store value |
|
90 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
91 |
* @see {_ERRORS_H_::errors} |
|
92 |
*/ |
|
93 |
int (rtc_read_month)(uint8_t *month); |
|
94 |
/** |
|
95 |
* @brief Reads year register. |
|
96 |
* |
|
97 |
* @param hour Address to store value |
|
98 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
99 |
* @see {_ERRORS_H_::errors} |
|
100 |
*/ |
|
101 |
int (rtc_read_year)(uint8_t *year); |
|
102 |
|
|
103 |
/** |
|
41 | 104 |
* @brief Checks if there's an update in progress. |
42 | 105 |
* @return The value of the flag UIP, or ERROR_CODE if error occurs. |
43 | 106 |
*/ |
44 | 107 |
int (rtc_check_update)(void); |
45 | 108 |
|
46 | 109 |
/** |
47 |
* @brief Enables/Disables updates of time/date registers.
|
|
110 |
* @brief Enables/Disables interrupts on update.
|
|
48 | 111 |
* @param on zero to disable, any other value to enable |
49 | 112 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
50 | 113 |
*/ |
51 |
int (rtc_set_updates)(int on); |
|
114 |
int (rtc_set_updates_int)(int on);
|
|
52 | 115 |
|
53 | 116 |
/** |
54 | 117 |
* @brief Reads time from RTC. |
... | ... | |
63 | 126 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
64 | 127 |
*/ |
65 | 128 |
int (rtc_read_date)(uint8_t *date); |
129 |
/// @brief RTC Interrupt Handler |
|
130 |
void rtc_ih(); |
|
66 | 131 |
|
67 | 132 |
/** |
68 | 133 |
* @} |
proj/src/libs/rtc/src/rtc.c | ||
---|---|---|
5 | 5 |
#include "utils.h" |
6 | 6 |
#include "errors.h" |
7 | 7 |
|
8 |
// RTC IRQ Line |
|
9 |
|
|
10 |
#define KBC_IRQ 8 /* @brief KBC IRQ Line */ |
|
11 |
|
|
12 | 8 |
// RTC Ports |
13 | 9 |
|
14 | 10 |
#define RTC_ADDR_REG 0x70 |
... | ... | |
61 | 57 |
|
62 | 58 |
#define VRT BIT(7) /** @brief Valid RAM/time - If set to 0 RTC reading aren't valid */ |
63 | 59 |
|
60 |
static uint8_t s = 0, m = 0, h = 0; |
|
61 |
|
|
64 | 62 |
int (subscribe_rtc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
65 | 63 |
if (interrupt_id == NULL) return NULL_PTR; |
66 | 64 |
*interrupt_id = interrupt_bit; |
67 |
if (sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
|
|
65 |
if (sys_irqsetpolicy(RTC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
|
|
68 | 66 |
return SUCCESS; |
69 | 67 |
} |
70 | 68 |
|
... | ... | |
93 | 91 |
return (data & UIP) != 0; |
94 | 92 |
} |
95 | 93 |
|
96 |
int (rtc_set_updates)(int on) { |
|
94 |
int (rtc_set_updates_int)(int on) {
|
|
97 | 95 |
uint8_t data; |
98 | 96 |
int r; |
99 | 97 |
if (on) { |
100 | 98 |
if ((r = rtc_read_register(RTC_REG_B, &data))) return r; |
101 | 99 |
|
102 |
data &= ~SET;
|
|
100 |
data |= UIE;
|
|
103 | 101 |
|
104 | 102 |
if ((r = rtc_write_register(RTC_REG_B, data))) return r; |
105 | 103 |
|
106 | 104 |
} else { |
107 |
while (rtc_check_update()); |
|
108 |
|
|
109 | 105 |
if ((r = rtc_read_register(RTC_REG_B, &data))) return r; |
110 | 106 |
|
111 |
data |= SET;
|
|
107 |
data &= ~UIE;
|
|
112 | 108 |
|
113 | 109 |
if ((r = rtc_write_register(RTC_REG_B, data))) return r; |
114 | 110 |
} |
115 | 111 |
return SUCCESS; |
116 | 112 |
} |
117 | 113 |
|
118 |
int (rtc_read_time)(uint8_t *time) { |
|
114 |
int (rtc_read_sec)(uint8_t *sec) { |
|
115 |
return rtc_read_register(RTC_SEC, sec); |
|
116 |
} |
|
119 | 117 |
|
120 |
int r;
|
|
121 |
//if ((r = rtc_set_updates(false))) return r;
|
|
122 |
while (rtc_check_update());
|
|
118 |
int (rtc_read_min)(uint8_t *min) {
|
|
119 |
return rtc_read_register(RTC_MIN, min);
|
|
120 |
}
|
|
123 | 121 |
|
124 |
uint8_t hour, min, sec; |
|
122 |
int (rtc_read_hour)(uint8_t *hour) { |
|
123 |
return rtc_read_register(RTC_HOUR, hour); |
|
124 |
} |
|
125 | 125 |
|
126 |
if ((r = rtc_read_register(RTC_SEC, &sec))) return r;
|
|
127 |
if ((r = rtc_read_register(RTC_MIN, &min))) return r;
|
|
128 |
if ((r = rtc_read_register(RTC_HOUR, &hour))) return r;
|
|
126 |
int (rtc_read_weekday)(uint8_t *weekday) {
|
|
127 |
return rtc_read_register(RTC_WEEK_DAY, weekday);
|
|
128 |
}
|
|
129 | 129 |
|
130 |
//if ((r = rtc_set_updates(true))) return r; |
|
130 |
int (rtc_read_day)(uint8_t *day) { |
|
131 |
return rtc_read_register(RTC_MONTH_DAY, day); |
|
132 |
} |
|
131 | 133 |
|
132 |
time[0] = BCD_FIRST(sec)*10 + BCD_SECOND(sec);
|
|
133 |
time[1] = BCD_FIRST(min)*10 + BCD_SECOND(min);
|
|
134 |
time[2] = BCD_FIRST(hour)*10 + BCD_SECOND(hour);
|
|
134 |
int (rtc_read_month)(uint8_t *month) {
|
|
135 |
return rtc_read_register(RTC_MONTH, month);
|
|
136 |
}
|
|
135 | 137 |
|
138 |
int (rtc_read_year)(uint8_t *year) { |
|
139 |
return rtc_read_register(RTC_YEAR, year); |
|
140 |
} |
|
141 |
|
|
142 |
int (rtc_read_time)(uint8_t *time) { |
|
143 |
|
|
144 |
time[0] = BCD_FIRST(s)*10 + BCD_SECOND(s); |
|
145 |
time[1] = BCD_FIRST(m)*10 + BCD_SECOND(m); |
|
146 |
time[2] = BCD_FIRST(h)*10 + BCD_SECOND(h); |
|
147 |
|
|
136 | 148 |
return SUCCESS; |
137 | 149 |
} |
138 | 150 |
|
139 | 151 |
int (rtc_read_date)(uint8_t *date) { |
140 | 152 |
int r; |
141 |
//if ((r = rtc_set_updates(false))) return r; |
|
142 |
while (rtc_check_update()); |
|
143 | 153 |
|
144 |
uint8_t year, month, day, weekday; |
|
154 |
uint8_t year=0, month=0, day=0, weekday=0; |
|
155 |
uint8_t year2=0, month2=0, day2=0, weekday2=0; |
|
145 | 156 |
|
146 |
if ((r = rtc_read_register(RTC_WEEK_DAY, &weekday))) return r; |
|
147 |
if ((r = rtc_read_register(RTC_MONTH_DAY, &day))) return r; |
|
148 |
if ((r = rtc_read_register(RTC_MONTH, &month))) return r; |
|
149 |
if ((r = rtc_read_register(RTC_YEAR, &year))) return r; |
|
157 |
do { |
|
158 |
year2 = year; |
|
159 |
month2 = month; |
|
160 |
day2 = day; |
|
161 |
weekday2 = weekday; |
|
150 | 162 |
|
151 |
//if ((r = rtc_set_updates(true))) return r; |
|
163 |
if ((r = rtc_read_weekday(&weekday))) return r; |
|
164 |
if ((r = rtc_read_day(&day))) return r; |
|
165 |
if ((r = rtc_read_month(&month))) return r; |
|
166 |
if ((r = rtc_read_year(&year))) return r; |
|
152 | 167 |
|
168 |
} while (year != year2 || month != month2 || day != day2 || weekday != weekday2); |
|
169 |
|
|
153 | 170 |
date[0] = weekday; |
154 | 171 |
date[1] = BCD_FIRST(day)*10 + BCD_SECOND(day); |
155 | 172 |
date[2] = BCD_FIRST(month)*10 + BCD_SECOND(month); |
... | ... | |
157 | 174 |
|
158 | 175 |
return SUCCESS; |
159 | 176 |
} |
177 |
|
|
178 |
void rtc_ih() { |
|
179 |
uint8_t data; |
|
180 |
|
|
181 |
if (rtc_read_register(RTC_REG_C, &data)) return; |
|
182 |
|
|
183 |
if (data & UF) { |
|
184 |
printf("REACH\n"); |
|
185 |
if (rtc_read_sec(&s)) return; |
|
186 |
if (rtc_read_min(&m)) return; |
|
187 |
if (rtc_read_hour(&h)) return; |
|
188 |
printf("%x %x %x", h, m, s); |
|
189 |
} |
|
190 |
} |
proj/src/project/src/interrupts_func.c | ||
---|---|---|
7 | 7 |
#include "hltp.h" |
8 | 8 |
#include "utils.h" |
9 | 9 |
#include "errors.h" |
10 |
#include "rtc.h" |
|
10 | 11 |
|
11 | 12 |
static int timer_subscribed = 0, timer_id; |
12 | 13 |
|
... | ... | |
16 | 17 |
|
17 | 18 |
static int uart_subscribed = 0, uart_id; |
18 | 19 |
|
20 |
static int rtc_subscribed = 0, rtc_id; |
|
21 |
|
|
19 | 22 |
static void (*const ih[])(void) = { timer_int_handler, |
20 | 23 |
kbc_ih, |
21 | 24 |
NULL, |
... | ... | |
24 | 27 |
NULL, |
25 | 28 |
NULL, |
26 | 29 |
NULL, |
30 |
rtc_ih, |
|
27 | 31 |
NULL, |
28 | 32 |
NULL, |
29 | 33 |
NULL, |
30 |
NULL, |
|
31 | 34 |
mouse_ih, |
32 | 35 |
NULL, |
33 | 36 |
NULL, |
... | ... | |
110 | 113 |
return SBCR_ERROR; |
111 | 114 |
} |
112 | 115 |
|
116 |
if (subscribe_rtc_interrupt(RTC_IRQ, &rtc_id)){ |
|
117 |
printf("%s: failed to enable rtc interrupts.\n", __func__); |
|
118 |
if (unsubscribe_all()) |
|
119 |
printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__); |
|
120 |
return SBCR_ERROR; |
|
121 |
} |
|
122 |
rtc_subscribed = 1; |
|
123 |
rtc_set_updates_int(1); |
|
124 |
|
|
125 |
|
|
126 |
|
|
113 | 127 |
/// UART interrupt handling |
114 | 128 |
uart_id = 0; |
115 | 129 |
uart_set_bits_per_character(COM1_ADDR, 8); |
... | ... | |
170 | 184 |
mouse_subscribed = 0; |
171 | 185 |
} |
172 | 186 |
|
187 |
if (rtc_subscribed) { |
|
188 |
if (unsubscribe_interrupt(&rtc_id)) { |
|
189 |
printf("%s: failed to unsubcribe RTC interrupts.\n", __func__); |
|
190 |
r = UNSBCR_ERROR; |
|
191 |
} |
|
192 |
rtc_subscribed = 0; |
|
193 |
} |
|
194 |
|
|
173 | 195 |
// Unsubscribe UART interrupts |
174 | 196 |
if (uart_subscribed) { |
175 | 197 |
if (unsubscribe_interrupt(&uart_id)) { |
proj/src/project/src/scoreboards.c | ||
---|---|---|
184 | 184 |
score_info_t *score = p->highscores[i]; |
185 | 185 |
sprintf(buffer, "%02d/%02d/%02d %02d:%02d:%02d", |
186 | 186 |
(score->day), (score->month), (score->year), |
187 |
(score->hour), (score->month), (score->sec));
|
|
187 |
(score->hour), (score->min), (score->sec));
|
|
188 | 188 |
|
189 | 189 |
|
190 | 190 |
text_set_string(p->t[MAX_HIGHSCORES + i*3], buffer); |
... | ... | |
254 | 254 |
score_info_t *score = p->highscores[i]; |
255 | 255 |
fprintf(f, "%02d/%02d/%02d %02d:%02d:%02d - %d - %d\n", |
256 | 256 |
(score->day), (score->month), (score->year), |
257 |
(score->hour), (score->month), (score->sec),
|
|
257 |
(score->hour), (score->min), (score->sec),
|
|
258 | 258 |
(score->score), (score->time_played)); |
259 | 259 |
} |
260 | 260 |
fclose(f); |
Also available in: Unified diff