-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtc.c
More file actions
38 lines (31 loc) · 774 Bytes
/
Copy pathrtc.c
File metadata and controls
38 lines (31 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "rtc.h"
#include "ports.h"
#define CMOS_ADDR 0x70
#define CMOS_DATA 0x71
static int bcd_mode;
static uint8_t cmos_read(uint8_t reg) {
outb(CMOS_ADDR, reg);
io_wait();
return inb(CMOS_DATA);
}
static int from_bcd(uint8_t val) {
if (bcd_mode)
return (val & 0x0F) + ((val >> 4) * 10);
return val;
}
void rtc_init(void) {
uint8_t reg_b = cmos_read(0x0B);
bcd_mode = !(reg_b & 0x04);
}
void rtc_read_time(rtc_time_t *t) {
uint8_t sec, min, hour;
do {
while (cmos_read(0x0A) & 0x80);
sec = cmos_read(0x00);
min = cmos_read(0x02);
hour = cmos_read(0x04);
} while (cmos_read(0x0A) & 0x80);
t->seconds = from_bcd(sec);
t->minutes = from_bcd(min);
t->hours = from_bcd(hour);
}