source: rtems/c/src/lib/libbsp/powerpc/dmv177/tod/tod.c @ dc104a4

4.104.114.84.95
Last change on this file since dc104a4 was dc104a4, checked in by Joel Sherrill <joel.sherrill@…>, on May 30, 1998 at 11:46:21 AM

Updated to current source and removed warnings.

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Real Time Clock (Harris ICM7170) for RTEMS
3 *
4 *  This part is found on the second generation of this board.
5 *
6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
8 *  http://www.OARcorp.com/rtems/license.html.
9 *
10 *  $Id$
11 */ 
12
13#include <rtems.h>
14#include <tod.h>
15#include <bsp.h>
16
17/*
18 * These values are programed into a register and must not be changed.
19 */
20
21#define ICM1770_CRYSTAL_FREQ_32K      0x00
22#define ICM1770_CRYSTAL_FREQ_1M       0x01
23#define ICM1770_CRYSTAL_FREQ_2M       0x02
24#define ICM1770_CRYSTAL_FREQ_4M       0x03
25
26void ICM7170_GetTOD(
27  volatile unsigned char *imc1770_regs,
28  rtems_unsigned8         icm1770_freq,
29  rtems_time_of_day      *rtc_tod
30);
31void ICM7170_SetTOD(
32  volatile unsigned char *imc1770_regs,
33  rtems_unsigned8         icm1770_freq,
34  rtems_time_of_day      *rtc_tod
35);
36
37/*
38 *  This code is dependent on the boards use of the ICM7170 RTC/NVRAM
39 *  and should remain in this file.
40 */
41
42/*  PAGE
43 *
44 *  This routine copies the time from the real time clock to RTEMS
45 *
46 *  Input parameters:  NONE
47 *
48 *  Output parameters:  NONE
49 *
50 *  Return values: NONE
51 */
52
53void setRealTimeToRTEMS()
54{
55  rtems_time_of_day rtc_tod;
56
57  ICM7170_GetTOD( DMV170_RTC_ADDRESS, DMV170_RTC_FREQUENCY, &rtc_tod );
58  rtems_clock_set( &rtc_tod );
59}
60
61/*  PAGE
62 *
63 *  setRealTimeFromRTEMS
64 *
65 *  This routine copies the time from RTEMS to the real time clock
66 *
67 *  Input parameters:  NONE
68 *
69 *  Output parameters:  NONE
70 *
71 *  Return values: NONE
72 */
73
74void setRealTimeFromRTEMS()
75{
76  rtems_time_of_day rtems_tod;
77
78  rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod );
79  ICM7170_SetTOD( DMV170_RTC_ADDRESS, DMV170_RTC_FREQUENCY, &rtems_tod );
80}
81
82/*  PAGE
83 *
84 *  checkRealTime
85 *
86 *  This routine reads the returns the variance betweent the real time and
87 *  rtems time.
88 *
89 *  Input parameters: NONE
90 *
91 *  Output parameters:  NONE
92 *
93 *  Return values:
94 *    int   The differance between the real time clock and rtems time or
95 *          9999 in the event of an error.
96 */
97
98int checkRealTime()
99{
100  rtems_time_of_day rtems_tod;
101  rtems_time_of_day rtc_tod;
102
103  ICM7170_GetTOD( DMV170_RTC_ADDRESS, DMV170_RTC_FREQUENCY, &rtc_tod );
104  rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod );
105
106  if( rtems_tod.year == rtc_tod.year &&
107      rtems_tod.month == rtc_tod.month &&
108      rtems_tod.day == rtc_tod.day ) {
109     return ((rtems_tod.hour   - rtc_tod.hour) * 3600) +
110            ((rtems_tod.minute - rtc_tod.minute) * 60) +
111             (rtems_tod.second - rtc_tod.second);
112  }
113  return 9999;
114}
115
116/*
117 *  These routines are ICM7170 should be in
118 *  a separate support library. When the chiplib
119 *  is created for RTEMS these routines will be moved.
120 */
121
122/*  PAGE
123 *
124 *  ICM7170_GetField
125 *
126 *  This routine gets a field
127 *
128 *  Input parameters:
129 *    imc1770_regs - pointer to the register base address.
130 *    reg          - register id
131 *
132 *  Output parameters: NONE
133 *
134 *  Return values: value of register
135 */
136
137static int ICM7170_GetField(
138  volatile unsigned char *imc1770_regs,
139  int                     reg
140)
141{
142  unsigned char x;
143
144  x = imc1770_regs[reg*4];
145
146  return x;
147}
148
149/*  PAGE
150 *
151 *  ICM7170_SetField
152 *
153 *  This routine sets a field
154 *
155 *  Input parameters:
156 *    imc1770_regs - pointer to the register base address.
157 *    reg          - register id
158 *    d            - data to write
159 *
160 *  Output parameters:  NONE
161 *
162 *  Return values: NONE
163 */
164
165static void ICM7170_SetField(
166  volatile unsigned char *imc1770_regs,
167  int                     reg,
168  unsigned char             d
169)
170{
171  imc1770_regs[reg*4] = d;
172}
173
174/*  PAGE
175 *
176 *  ICM7170_GetTOD
177 *
178 *  This routine gets the real time clock time of day in rtems
179 *  time of day format.
180 *
181 *  Input parameters:
182 *    imc1770_regs - pointer to the register base address.
183 *    icm1770_freq - oscillator frequency
184 *
185 *  Output parameters: 
186 *    rtc_tod - time of day by the real time clock     
187 *
188 *  Return values: NONE
189 */
190
191void ICM7170_GetTOD(
192  volatile unsigned char *imc1770_regs,
193  rtems_unsigned8        icm1770_freq,
194  rtems_time_of_day      *rtc_tod
195)
196{
197  int year;
198  int usec;
199  static rtems_boolean init = TRUE;
200
201  /* Initialize the clock at once prior to reading */
202  if (init ) {
203    ICM7170_SetField( imc1770_regs,  0x11, (0x0c | icm1770_freq) );
204    init = FALSE;
205  } 
206
207  usec = ICM7170_GetField( imc1770_regs, 0x00 );
208   
209  year = ICM7170_GetField( imc1770_regs, 0x06 );
210  if ( year >= 88 )
211    year += 1900;
212  else
213    year += 2000;
214
215  rtc_tod->year   = year;
216  rtc_tod->month  = ICM7170_GetField( imc1770_regs, 0x04 );
217  rtc_tod->day    = ICM7170_GetField( imc1770_regs, 0x05 );
218  rtc_tod->hour   = ICM7170_GetField( imc1770_regs, 0x01 );
219  rtc_tod->minute = ICM7170_GetField( imc1770_regs, 0x02 );
220  rtc_tod->second = ICM7170_GetField( imc1770_regs, 0x03 );
221  rtc_tod->ticks  = ICM7170_GetField( imc1770_regs, 0x00 );
222}
223
224/*  PAGE
225 *
226 *  ICM7170_SetTOD
227 *
228 *  This routine sets the real time clock
229 *
230 *  Input parameters:
231 *    imc1770_regs - pointer to the register base address.
232 *    icm1770_freq - oscillator frequency
233 *    rtc_tod      - time of day to set     
234 *
235 *  Output parameters: 
236 *
237 *  Return values:
238 */
239
240void ICM7170_SetTOD(
241  volatile unsigned char *imc1770_regs,
242  rtems_unsigned8        icm1770_freq,
243  rtems_time_of_day      *rtc_tod
244)
245{
246  int year;
247
248  year = rtc_tod->year;
249  if ( year >= 2088 )        /* plan ahead :) */
250    rtems_fatal_error_occurred( 0xBAD0BAD0 );
251
252  if ( year >= 2000 )
253    year -= 2000;
254  else
255    year -= 1900;
256 
257  ICM7170_SetField( imc1770_regs, 0x11, (0x04 |icm1770_freq ) );
258
259  ICM7170_SetField( imc1770_regs, 0x06, year );
260  ICM7170_SetField( imc1770_regs, 0x04, rtc_tod->month );
261  ICM7170_SetField( imc1770_regs, 0x05, rtc_tod->day );
262  ICM7170_SetField( imc1770_regs, 0x01, rtc_tod->hour );
263  ICM7170_SetField( imc1770_regs, 0x02, rtc_tod->minute );
264  ICM7170_SetField( imc1770_regs, 0x03, rtc_tod->second );
265
266  /*
267   * I don't know which day of week is
268   * 
269   */
270  ICM7170_SetField( imc1770_regs, 0x07, 1 );
271
272  ICM7170_SetField( imc1770_regs,  0x11, (0x0c | icm1770_freq) );
273}
274
Note: See TracBrowser for help on using the repository browser.