1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup lpc32xx |
---|
5 | * |
---|
6 | * @brief RTC configuration. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009 |
---|
11 | * embedded brains GmbH |
---|
12 | * Obere Lagerstr. 30 |
---|
13 | * D-82178 Puchheim |
---|
14 | * Germany |
---|
15 | * <rtems@embedded-brains.de> |
---|
16 | * |
---|
17 | * The license and distribution terms for this file may be |
---|
18 | * found in the file LICENSE in this distribution or at |
---|
19 | * http://www.rtems.com/license/LICENSE. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <libchip/rtc.h> |
---|
23 | |
---|
24 | #include <bsp.h> |
---|
25 | #include <bsp/lpc32xx.h> |
---|
26 | |
---|
27 | #define LPC32XX_RTC_COUNT 1U |
---|
28 | |
---|
29 | #define LPC32XX_RTC_COUNTER_DELTA 0xfffffffeU |
---|
30 | |
---|
31 | #define LPC32XX_RTC_KEY 0xb5c13f27U |
---|
32 | |
---|
33 | #define LPC32XX_RTC_CTRL_FORCE_ONSW (1U << 7) |
---|
34 | #define LPC32XX_RTC_CTRL_STOP (1U << 6) |
---|
35 | #define LPC32XX_RTC_CTRL_RESET (1U << 4) |
---|
36 | #define LPC32XX_RTC_CTRL_MATCH_1_ONSW (1U << 3) |
---|
37 | #define LPC32XX_RTC_CTRL_MATCH_0_ONSW (1U << 2) |
---|
38 | #define LPC32XX_RTC_CTRL_MATCH_1_INTR (1U << 1) |
---|
39 | #define LPC32XX_RTC_CTRL_MATCH_0_INTR (1U << 0) |
---|
40 | |
---|
41 | static void lpc32xx_rtc_set(uint32_t val) |
---|
42 | { |
---|
43 | unsigned i = LPC32XX_ARM_CLK / LPC32XX_OSCILLATOR_RTC; |
---|
44 | |
---|
45 | lpc32xx.rtc.ctrl |= LPC32XX_RTC_CTRL_STOP; |
---|
46 | lpc32xx.rtc.ucount = val; |
---|
47 | lpc32xx.rtc.dcount = LPC32XX_RTC_COUNTER_DELTA - val; |
---|
48 | lpc32xx.rtc.ctrl &= ~LPC32XX_RTC_CTRL_STOP; |
---|
49 | |
---|
50 | /* It needs some time before we can read the values back */ |
---|
51 | while (i != 0) { |
---|
52 | asm volatile ("nop"); |
---|
53 | --i; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | static void lpc32xx_rtc_reset(void) |
---|
58 | { |
---|
59 | lpc32xx.rtc.ctrl = LPC32XX_RTC_CTRL_RESET; |
---|
60 | lpc32xx.rtc.ctrl = 0; |
---|
61 | lpc32xx.rtc.key = LPC32XX_RTC_KEY; |
---|
62 | lpc32xx_rtc_set(0); |
---|
63 | } |
---|
64 | |
---|
65 | static void lpc32xx_rtc_initialize(int minor) |
---|
66 | { |
---|
67 | uint32_t up_first = 0; |
---|
68 | uint32_t up_second = 0; |
---|
69 | uint32_t down_first = 0; |
---|
70 | uint32_t down_second = 0; |
---|
71 | |
---|
72 | if (lpc32xx.rtc.key != LPC32XX_RTC_KEY) { |
---|
73 | lpc32xx_rtc_reset(); |
---|
74 | } |
---|
75 | |
---|
76 | do { |
---|
77 | up_first = lpc32xx.rtc.ucount; |
---|
78 | down_first = lpc32xx.rtc.dcount; |
---|
79 | up_second = lpc32xx.rtc.ucount; |
---|
80 | down_second = lpc32xx.rtc.dcount; |
---|
81 | } while (up_first != up_second || down_first != down_second); |
---|
82 | |
---|
83 | if (up_first + down_first != LPC32XX_RTC_COUNTER_DELTA) { |
---|
84 | lpc32xx_rtc_reset(); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | static int lpc32xx_rtc_get_time(int minor, rtems_time_of_day *tod) |
---|
89 | { |
---|
90 | struct timeval now = { |
---|
91 | .tv_sec = lpc32xx.rtc.ucount, |
---|
92 | .tv_usec = 0 |
---|
93 | }; |
---|
94 | struct tm time; |
---|
95 | |
---|
96 | gmtime_r(&now.tv_sec, &time); |
---|
97 | |
---|
98 | tod->year = time.tm_year + 1900; |
---|
99 | tod->month = time.tm_mon + 1; |
---|
100 | tod->day = time.tm_mday; |
---|
101 | tod->hour = time.tm_hour; |
---|
102 | tod->minute = time.tm_min; |
---|
103 | tod->second = time.tm_sec; |
---|
104 | tod->ticks = 0; |
---|
105 | |
---|
106 | return RTEMS_SUCCESSFUL; |
---|
107 | } |
---|
108 | |
---|
109 | static int lpc32xx_rtc_set_time(int minor, const rtems_time_of_day *tod) |
---|
110 | { |
---|
111 | lpc32xx_rtc_set(_TOD_To_seconds(tod)); |
---|
112 | |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | static bool lpc32xx_rtc_probe(int minor) |
---|
117 | { |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | const rtc_fns lpc32xx_rtc_ops = { |
---|
122 | .deviceInitialize = lpc32xx_rtc_initialize, |
---|
123 | .deviceGetTime = lpc32xx_rtc_get_time, |
---|
124 | .deviceSetTime = lpc32xx_rtc_set_time |
---|
125 | }; |
---|
126 | |
---|
127 | unsigned long RTC_Count = LPC32XX_RTC_COUNT; |
---|
128 | |
---|
129 | rtems_device_minor_number RTC_Minor = 0; |
---|
130 | |
---|
131 | rtc_tbl RTC_Table [LPC32XX_RTC_COUNT] = { |
---|
132 | { |
---|
133 | .sDeviceName = "/dev/rtc", |
---|
134 | .deviceType = RTC_CUSTOM, |
---|
135 | .pDeviceFns = &lpc32xx_rtc_ops, |
---|
136 | .deviceProbe = lpc32xx_rtc_probe, |
---|
137 | .pDeviceParams = NULL, |
---|
138 | .ulCtrlPort1 = 0, |
---|
139 | .ulDataPort = 0, |
---|
140 | .getRegister = NULL, |
---|
141 | .setRegister = NULL |
---|
142 | } |
---|
143 | }; |
---|