1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 2016 embedded brains GmbH |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <libchip/chip.h> |
---|
29 | #include <libchip/rtc.h> |
---|
30 | |
---|
31 | #include <bsp.h> |
---|
32 | |
---|
33 | #define ARBITRARY_DAY_OF_WEEK 1 |
---|
34 | |
---|
35 | void atsam_rtc_get_time(rtems_time_of_day *tod) |
---|
36 | { |
---|
37 | Rtc *rtc = RTC; |
---|
38 | uint8_t hour; |
---|
39 | uint8_t minute; |
---|
40 | uint8_t second; |
---|
41 | uint16_t year; |
---|
42 | uint8_t month; |
---|
43 | uint8_t day; |
---|
44 | uint8_t week; |
---|
45 | |
---|
46 | RTC_GetDate(rtc, &year, &month, &day, &week); |
---|
47 | RTC_GetTime(rtc, &hour, &minute, &second); |
---|
48 | |
---|
49 | tod->ticks = 0; |
---|
50 | tod->second = second; |
---|
51 | tod->minute = minute; |
---|
52 | tod->hour = hour; |
---|
53 | tod->day = day; |
---|
54 | tod->month = month; |
---|
55 | tod->year = year; |
---|
56 | } |
---|
57 | |
---|
58 | static void atsam_rtc_device_initialize(int minor) |
---|
59 | { |
---|
60 | Rtc *rtc = RTC; |
---|
61 | |
---|
62 | RTC_DisableIt(rtc, 0x1F); |
---|
63 | } |
---|
64 | |
---|
65 | static int atsam_rtc_device_get_time(int minor, rtems_time_of_day *tod) |
---|
66 | { |
---|
67 | atsam_rtc_get_time(tod); |
---|
68 | |
---|
69 | return 0; |
---|
70 | } |
---|
71 | |
---|
72 | static int atsam_rtc_device_set_time(int minor, const rtems_time_of_day *tod) |
---|
73 | { |
---|
74 | Rtc *rtc = RTC; |
---|
75 | uint8_t hour; |
---|
76 | uint8_t minute; |
---|
77 | uint8_t second; |
---|
78 | uint16_t year; |
---|
79 | uint8_t month; |
---|
80 | uint8_t day; |
---|
81 | uint8_t week; |
---|
82 | |
---|
83 | second = (uint8_t) tod->second; |
---|
84 | minute = (uint8_t) tod->minute; |
---|
85 | hour = (uint8_t) tod->hour; |
---|
86 | day = (uint8_t) tod->day; |
---|
87 | month = (uint8_t) tod->month; |
---|
88 | week = ARBITRARY_DAY_OF_WEEK; |
---|
89 | year = (uint16_t) tod->year; |
---|
90 | |
---|
91 | RTC_SetDate(rtc, year, month, day, week); |
---|
92 | RTC_SetTime(rtc, hour, minute, second); |
---|
93 | |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | static bool atsam_rtc_device_probe(int minor) |
---|
98 | { |
---|
99 | return true; |
---|
100 | } |
---|
101 | |
---|
102 | const rtc_fns atsam_rtc_device_ops = { |
---|
103 | .deviceInitialize = atsam_rtc_device_initialize, |
---|
104 | .deviceGetTime = atsam_rtc_device_get_time, |
---|
105 | .deviceSetTime = atsam_rtc_device_set_time |
---|
106 | }; |
---|
107 | |
---|
108 | rtc_tbl RTC_Table[] = { |
---|
109 | { |
---|
110 | .sDeviceName = "/dev/rtc", |
---|
111 | .deviceType = RTC_CUSTOM, |
---|
112 | .pDeviceFns = &atsam_rtc_device_ops, |
---|
113 | .deviceProbe = atsam_rtc_device_probe |
---|
114 | } |
---|
115 | }; |
---|
116 | |
---|
117 | size_t RTC_Count = RTEMS_ARRAY_SIZE(RTC_Table); |
---|