1 | /* |
---|
2 | * Real Time Clock (Harris ICM7170) for RTEMS |
---|
3 | * |
---|
4 | * This part is found on the second generation of this board. |
---|
5 | * |
---|
6 | * COPYRIGHT (c) 1989-2009. |
---|
7 | * On-Line Applications Research Corporation (OAR). |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.rtems.org/license/LICENSE. |
---|
12 | */ |
---|
13 | |
---|
14 | #include <rtems.h> |
---|
15 | #include <tod.h> |
---|
16 | #include <bsp.h> |
---|
17 | |
---|
18 | /* |
---|
19 | * These values are programed into a register and must not be changed. |
---|
20 | */ |
---|
21 | |
---|
22 | #define ICM1770_CRYSTAL_FREQ_32K 0x00 |
---|
23 | #define ICM1770_CRYSTAL_FREQ_1M 0x01 |
---|
24 | #define ICM1770_CRYSTAL_FREQ_2M 0x02 |
---|
25 | #define ICM1770_CRYSTAL_FREQ_4M 0x03 |
---|
26 | |
---|
27 | void ICM7170_GetTOD( |
---|
28 | volatile unsigned char *imc1770_regs, |
---|
29 | uint8_t icm1770_freq, |
---|
30 | rtems_time_of_day *rtc_tod |
---|
31 | ); |
---|
32 | void ICM7170_SetTOD( |
---|
33 | volatile unsigned char *imc1770_regs, |
---|
34 | uint8_t icm1770_freq, |
---|
35 | rtems_time_of_day *rtc_tod |
---|
36 | ); |
---|
37 | |
---|
38 | /* |
---|
39 | * This code is dependent on the Vista 603e's use of the ICM7170 RTC/NVRAM |
---|
40 | * and should remain in this file. |
---|
41 | */ |
---|
42 | |
---|
43 | void setRealTimeToRTEMS() |
---|
44 | { |
---|
45 | rtems_time_of_day rtc_tod; |
---|
46 | |
---|
47 | ICM7170_GetTOD( BSP_RTC_ADDRESS, BSP_RTC_FREQUENCY, &rtc_tod ); |
---|
48 | rtems_clock_set( &rtc_tod ); |
---|
49 | } |
---|
50 | |
---|
51 | void setRealTimeFromRTEMS() |
---|
52 | { |
---|
53 | rtems_time_of_day rtems_tod; |
---|
54 | |
---|
55 | rtems_clock_get_tod( &rtems_tod ); |
---|
56 | ICM7170_SetTOD( BSP_RTC_ADDRESS, BSP_RTC_FREQUENCY, &rtems_tod ); |
---|
57 | } |
---|
58 | |
---|
59 | int checkRealTime() |
---|
60 | { |
---|
61 | rtems_time_of_day rtems_tod; |
---|
62 | rtems_time_of_day rtc_tod; |
---|
63 | |
---|
64 | ICM7170_GetTOD( BSP_RTC_ADDRESS, BSP_RTC_FREQUENCY, &rtc_tod ); |
---|
65 | rtems_clock_get_tod( &rtems_tod ); |
---|
66 | |
---|
67 | if( rtems_tod.year == rtc_tod.year && |
---|
68 | rtems_tod.month == rtc_tod.month && |
---|
69 | rtems_tod.day == rtc_tod.day ) { |
---|
70 | return ((rtems_tod.hour - rtc_tod.hour) * 3600) + |
---|
71 | ((rtems_tod.minute - rtc_tod.minute) * 60) + |
---|
72 | (rtems_tod.second - rtc_tod.second); |
---|
73 | } |
---|
74 | return 9999; |
---|
75 | } |
---|
76 | |
---|
77 | /* |
---|
78 | * These routines are ICM7170 should be in |
---|
79 | * a separate support library. |
---|
80 | * XXX Make static |
---|
81 | */ |
---|
82 | static int ICM7170_GetField( |
---|
83 | volatile unsigned char *imc1770_regs, |
---|
84 | int reg |
---|
85 | ) |
---|
86 | { |
---|
87 | unsigned char x; |
---|
88 | |
---|
89 | x = imc1770_regs[reg*4]; |
---|
90 | |
---|
91 | return x; |
---|
92 | } |
---|
93 | |
---|
94 | static void ICM7170_SetField( |
---|
95 | volatile unsigned char *imc1770_regs, |
---|
96 | int reg, |
---|
97 | unsigned char d |
---|
98 | ) |
---|
99 | { |
---|
100 | imc1770_regs[reg*4] = d; |
---|
101 | } |
---|
102 | |
---|
103 | void ICM7170_GetTOD( |
---|
104 | volatile unsigned char *imc1770_regs, |
---|
105 | uint8_t icm1770_freq, |
---|
106 | rtems_time_of_day *rtc_tod |
---|
107 | ) |
---|
108 | { |
---|
109 | int year; |
---|
110 | int usec; |
---|
111 | static bool init = true; |
---|
112 | |
---|
113 | /* Initialize the clock at once prior to reading */ |
---|
114 | if (init ) { |
---|
115 | ICM7170_SetField( imc1770_regs, 0x11, (0x0c | icm1770_freq) ); |
---|
116 | init = false; |
---|
117 | } |
---|
118 | |
---|
119 | /* Latch times */ |
---|
120 | /* rtc_tod->ticks = */ |
---|
121 | |
---|
122 | usec = ICM7170_GetField( imc1770_regs, 0x00 ); |
---|
123 | |
---|
124 | year = ICM7170_GetField( imc1770_regs, 0x06 ); |
---|
125 | if ( year >= 88 ) |
---|
126 | year += 1900; |
---|
127 | else |
---|
128 | year += 2000; |
---|
129 | |
---|
130 | rtc_tod->year = year; |
---|
131 | rtc_tod->month = ICM7170_GetField( imc1770_regs, 0x04 ); |
---|
132 | rtc_tod->day = ICM7170_GetField( imc1770_regs, 0x05 ); |
---|
133 | rtc_tod->hour = ICM7170_GetField( imc1770_regs, 0x01 ); |
---|
134 | rtc_tod->minute = ICM7170_GetField( imc1770_regs, 0x02 ); |
---|
135 | rtc_tod->second = ICM7170_GetField( imc1770_regs, 0x03 ); |
---|
136 | rtc_tod->ticks = ICM7170_GetField( imc1770_regs, 0x00 ); |
---|
137 | } |
---|
138 | |
---|
139 | void ICM7170_SetTOD( |
---|
140 | volatile unsigned char *imc1770_regs, |
---|
141 | uint8_t icm1770_freq, |
---|
142 | rtems_time_of_day *rtc_tod |
---|
143 | ) |
---|
144 | { |
---|
145 | int year; |
---|
146 | |
---|
147 | year = rtc_tod->year; |
---|
148 | if ( year >= 2088 ) /* plan ahead :) */ |
---|
149 | rtems_fatal_error_occurred( 0xBAD0BAD0 ); |
---|
150 | |
---|
151 | if ( year >= 2000 ) |
---|
152 | year -= 2000; |
---|
153 | else |
---|
154 | year -= 1900; |
---|
155 | |
---|
156 | ICM7170_SetField( imc1770_regs, 0x11, (0x04 |icm1770_freq ) ); |
---|
157 | |
---|
158 | ICM7170_SetField( imc1770_regs, 0x06, year ); |
---|
159 | ICM7170_SetField( imc1770_regs, 0x04, rtc_tod->month ); |
---|
160 | ICM7170_SetField( imc1770_regs, 0x05, rtc_tod->day ); |
---|
161 | ICM7170_SetField( imc1770_regs, 0x01, rtc_tod->hour ); |
---|
162 | ICM7170_SetField( imc1770_regs, 0x02, rtc_tod->minute ); |
---|
163 | ICM7170_SetField( imc1770_regs, 0x03, rtc_tod->second ); |
---|
164 | |
---|
165 | /* |
---|
166 | * I don't know which day of week is |
---|
167 | * |
---|
168 | */ |
---|
169 | ICM7170_SetField( imc1770_regs, 0x07, 1 ); |
---|
170 | |
---|
171 | ICM7170_SetField( imc1770_regs, 0x11, (0x0c | icm1770_freq) ); |
---|
172 | } |
---|