root / proj / src / rtc.c @ 246
History | View | Annotate | Download (2.74 KB)
1 |
#include "rtc.h" |
---|---|
2 |
#include "rtc_macros.h" |
3 |
|
4 |
#include <lcom/lcf.h> |
5 |
|
6 |
#include "utils.h" |
7 |
#include "errors.h" |
8 |
|
9 |
int (subscribe_rtc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
10 |
if (interrupt_id == NULL) return NULL_PTR; |
11 |
*interrupt_id = interrupt_bit; |
12 |
if (sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR; |
13 |
return SUCCESS;
|
14 |
} |
15 |
|
16 |
int (rtc_read_register)(uint32_t reg, uint8_t *data) {
|
17 |
if (sys_outb(RTC_ADDR_REG, reg)) return WRITE_ERROR; |
18 |
|
19 |
if (util_sys_inb(RTC_DATA_REG, data)) return READ_ERROR; |
20 |
|
21 |
return SUCCESS;
|
22 |
} |
23 |
|
24 |
int (rtc_write_register)(uint32_t reg, uint8_t data) {
|
25 |
if (sys_outb(RTC_ADDR_REG, reg)) return WRITE_ERROR; |
26 |
|
27 |
if (sys_outb(RTC_DATA_REG, data)) return WRITE_ERROR; |
28 |
|
29 |
return SUCCESS;
|
30 |
} |
31 |
|
32 |
int (rtc_check_update)(void) { |
33 |
uint8_t data; |
34 |
int r;
|
35 |
|
36 |
if ((r = rtc_read_register(RTC_REG_A, &data))) return r; |
37 |
|
38 |
return (data & UIP) != 0; |
39 |
} |
40 |
|
41 |
int (rtc_set_updates)(int on) { |
42 |
uint8_t data; |
43 |
int r;
|
44 |
if (on) {
|
45 |
if ((r = rtc_read_register(RTC_REG_B, &data))) return r; |
46 |
|
47 |
data &= ~SET; |
48 |
|
49 |
if ((r = rtc_write_register(RTC_REG_B, data))) return r; |
50 |
|
51 |
} else {
|
52 |
while (rtc_check_update());
|
53 |
|
54 |
if ((r = rtc_read_register(RTC_REG_B, &data))) return r; |
55 |
|
56 |
data |= SET; |
57 |
|
58 |
if ((r = rtc_write_register(RTC_REG_B, data))) return r; |
59 |
} |
60 |
return SUCCESS;
|
61 |
} |
62 |
|
63 |
int (rtc_read_time)(uint8_t *time) {
|
64 |
|
65 |
int r;
|
66 |
//if ((r = rtc_set_updates(false))) return r;
|
67 |
while (rtc_check_update());
|
68 |
|
69 |
uint8_t hour, min, sec; |
70 |
|
71 |
if ((r = rtc_read_register(RTC_SEC, &sec))) return r; |
72 |
if ((r = rtc_read_register(RTC_MIN, &min))) return r; |
73 |
if ((r = rtc_read_register(RTC_HOUR, &hour))) return r; |
74 |
|
75 |
//if ((r = rtc_set_updates(true))) return r;
|
76 |
|
77 |
time[0] = BCD_FIRST(sec)*10 + BCD_SECOND(sec); |
78 |
time[1] = BCD_FIRST(min)*10 + BCD_SECOND(min); |
79 |
time[2] = BCD_FIRST(hour)*10 + BCD_SECOND(hour); |
80 |
|
81 |
return SUCCESS;
|
82 |
} |
83 |
|
84 |
int (rtc_read_date)(uint8_t *date) {
|
85 |
int r;
|
86 |
//if ((r = rtc_set_updates(false))) return r;
|
87 |
while (rtc_check_update());
|
88 |
|
89 |
uint8_t year, month, day, weekday; |
90 |
|
91 |
if ((r = rtc_read_register(RTC_WEEK_DAY, &weekday))) return r; |
92 |
if ((r = rtc_read_register(RTC_MONTH_DAY, &day))) return r; |
93 |
if ((r = rtc_read_register(RTC_MONTH, &month))) return r; |
94 |
if ((r = rtc_read_register(RTC_YEAR, &year))) return r; |
95 |
|
96 |
//if ((r = rtc_set_updates(true))) return r;
|
97 |
|
98 |
date[0] = weekday;
|
99 |
date[1] = BCD_FIRST(day)*10 + BCD_SECOND(day); |
100 |
date[2] = BCD_FIRST(month)*10 + BCD_SECOND(month); |
101 |
date[3] = BCD_FIRST(year)*10 + BCD_SECOND(year); |
102 |
|
103 |
return SUCCESS;
|
104 |
} |