-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtc.c
More file actions
66 lines (50 loc) · 1.25 KB
/
rtc.c
File metadata and controls
66 lines (50 loc) · 1.25 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <avr/io.h>
#include <stdlib.h>
#include "groom/i2c.h"
#include "groom/rtc.h"
#define RTC_DEVICE_ADDRESS 0xd0
void rtc_init(void)
{
/* nothing */
}
uint8_t rtc_get_time(struct rtc_time *time_buf)
{
uint8_t abuf[1];
uint8_t buf[7];
abuf[0] = 0x00;
int res = i2c_io(RTC_DEVICE_ADDRESS, abuf, sizeof(abuf), NULL, 0, buf, sizeof(buf));
if (res) {
return res;
}
/* YAY, BCD IS GREAT! */
time_buf->seconds = buf[0] & 0x0f;
time_buf->seconds += ((buf[0] & 0x70) >> 4) * 10;
time_buf->minutes = buf[1] & 0x0f;
time_buf->minutes += ((buf[1] & 0x70) >> 4) * 10;
time_buf->hours = buf[2] & 0x0f;
time_buf->hours += ((buf[2] & 0x30) >> 4) * 10;
time_buf->day = buf[3] & 0x07;
time_buf->date = buf[4] & 0x0f;
time_buf->date += ((buf[4] & 0x30) >> 4) * 10;
time_buf->month = buf[5] & 0x0f;
time_buf->month += ((buf[5] & 0x10) >> 4) * 10;
time_buf->year = buf[6] & 0x0f;
time_buf->year += ((buf[6] & 0xf0) >> 4) * 10;
return 0;
}
uint8_t rtc_run(void)
{
uint8_t buf[] = {
0x30, // seconds
0x55, // minutes
0x18, // hours
0x06, // day
0x25, // date
0x04, // month
0x13 // year
};
uint8_t abuf[] = {
0x00
};
return i2c_io(RTC_DEVICE_ADDRESS, abuf, sizeof(abuf), buf, sizeof(buf), NULL, 0);
}