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

4.104.114.84.95
Last change on this file since a38b9f8 was c932d85, checked in by Joel Sherrill <joel.sherrill@…>, on 05/30/98 at 10:09:14

New files -- from rtems-LM-980406 which was based on an RTEMS from 12/97.
This was called the dmv170 BSP in that source tree but since the DMV171
is now obsolete, we have transitioned to the DMV177 and have no intention
of checking compatibility with any other models.

  • 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 ticks;
247  int year;
248
249  year = rtc_tod->year;
250  if ( year >= 2088 )        /* plan ahead :) */
251    rtems_fatal_error_occurred( 0xBAD0BAD0 );
252
253  if ( year >= 2000 )
254    year -= 2000;
255  else
256    year -= 1900;
257 
258  ICM7170_SetField( imc1770_regs, 0x11, (0x04 |icm1770_freq ) );
259
260  ICM7170_SetField( imc1770_regs, 0x06, year );
261  ICM7170_SetField( imc1770_regs, 0x04, rtc_tod->month );
262  ICM7170_SetField( imc1770_regs, 0x05, rtc_tod->day );
263  ICM7170_SetField( imc1770_regs, 0x01, rtc_tod->hour );
264  ICM7170_SetField( imc1770_regs, 0x02, rtc_tod->minute );
265  ICM7170_SetField( imc1770_regs, 0x03, rtc_tod->second );
266
267  /*
268   * I don't know which day of week is
269   * 
270   */
271  ICM7170_SetField( imc1770_regs, 0x07, 1 );
272
273  ICM7170_SetField( imc1770_regs,  0x11, (0x0c | icm1770_freq) );
274}
275
276
277
278
279
280
281
282
Note: See TracBrowser for help on using the repository browser.