1 | /* |
---|
2 | * Copyright (c) 2016 embedded brains GmbH |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #include <libchip/chip.h> |
---|
10 | #include <libchip/rtc.h> |
---|
11 | |
---|
12 | #include <bsp.h> |
---|
13 | |
---|
14 | #define ARBITRARY_DAY_OF_WEEK 1 |
---|
15 | |
---|
16 | void atsam_rtc_get_time(rtems_time_of_day *tod) |
---|
17 | { |
---|
18 | Rtc *rtc = RTC; |
---|
19 | uint8_t hour; |
---|
20 | uint8_t minute; |
---|
21 | uint8_t second; |
---|
22 | uint16_t year; |
---|
23 | uint8_t month; |
---|
24 | uint8_t day; |
---|
25 | uint8_t week; |
---|
26 | |
---|
27 | RTC_GetDate(rtc, &year, &month, &day, &week); |
---|
28 | RTC_GetTime(rtc, &hour, &minute, &second); |
---|
29 | |
---|
30 | tod->ticks = 0; |
---|
31 | tod->second = second; |
---|
32 | tod->minute = minute; |
---|
33 | tod->hour = hour; |
---|
34 | tod->day = day; |
---|
35 | tod->month = month; |
---|
36 | tod->year = year; |
---|
37 | } |
---|
38 | |
---|
39 | static void atsam_rtc_device_initialize(int minor) |
---|
40 | { |
---|
41 | Rtc *rtc = RTC; |
---|
42 | |
---|
43 | RTC_DisableIt(rtc, 0x1F); |
---|
44 | } |
---|
45 | |
---|
46 | static int atsam_rtc_device_get_time(int minor, rtems_time_of_day *tod) |
---|
47 | { |
---|
48 | atsam_rtc_get_time(tod); |
---|
49 | |
---|
50 | return 0; |
---|
51 | } |
---|
52 | |
---|
53 | static int atsam_rtc_device_set_time(int minor, const rtems_time_of_day *tod) |
---|
54 | { |
---|
55 | Rtc *rtc = RTC; |
---|
56 | uint8_t hour; |
---|
57 | uint8_t minute; |
---|
58 | uint8_t second; |
---|
59 | uint16_t year; |
---|
60 | uint8_t month; |
---|
61 | uint8_t day; |
---|
62 | uint8_t week; |
---|
63 | |
---|
64 | second = (uint8_t) tod->second; |
---|
65 | minute = (uint8_t) tod->minute; |
---|
66 | hour = (uint8_t) tod->hour; |
---|
67 | day = (uint8_t) tod->day; |
---|
68 | month = (uint8_t) tod->month; |
---|
69 | week = ARBITRARY_DAY_OF_WEEK; |
---|
70 | year = (uint16_t) tod->year; |
---|
71 | |
---|
72 | RTC_SetDate(rtc, year, month, day, week); |
---|
73 | RTC_SetTime(rtc, hour, minute, second); |
---|
74 | |
---|
75 | return 0; |
---|
76 | } |
---|
77 | |
---|
78 | static bool atsam_rtc_device_probe(int minor) |
---|
79 | { |
---|
80 | return true; |
---|
81 | } |
---|
82 | |
---|
83 | const rtc_fns atsam_rtc_device_ops = { |
---|
84 | .deviceInitialize = atsam_rtc_device_initialize, |
---|
85 | .deviceGetTime = atsam_rtc_device_get_time, |
---|
86 | .deviceSetTime = atsam_rtc_device_set_time |
---|
87 | }; |
---|
88 | |
---|
89 | rtc_tbl RTC_Table[] = { |
---|
90 | { |
---|
91 | .sDeviceName = "/dev/rtc", |
---|
92 | .deviceType = RTC_CUSTOM, |
---|
93 | .pDeviceFns = &atsam_rtc_device_ops, |
---|
94 | .deviceProbe = atsam_rtc_device_probe |
---|
95 | } |
---|
96 | }; |
---|
97 | |
---|
98 | size_t RTC_Count = RTEMS_ARRAY_SIZE(RTC_Table); |
---|