source: rtems/c/src/lib/libbsp/i386/pc386/clock/rtc.c @ 6f9c75c3

4.104.114.84.95
Last change on this file since 6f9c75c3 was 6f9c75c3, checked in by Joel Sherrill <joel.sherrill@…>, on 01/16/98 at 16:56:48

Ralf Corsepius reported a number of missing CVS Id's:

RTEMS is under CVS control and has been since rtems 3.1.16 which was
around May 1995. So I just to add the $Id$. If you notice other files
with missing $Id$'s let me know. I try to keep w\up with it.

Now that you have asked -- I'll attach a list of files lacking an RCS-Id to
this mail. This list has been generated by a little sh-script I'll also
enclose.

  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*-------------------------------------------------------------------------+
2| rtc.c v1.1 - PC386 BSP - 1997/08/07
3+--------------------------------------------------------------------------+
4| This file contains the real time clock manipulation package for the
5| PC386 board.
6+--------------------------------------------------------------------------+
7| (C) Copyright 1997 -
8| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
9|
10| http://pandora.ist.utl.pt
11|
12| Instituto Superior Tecnico * Lisboa * PORTUGAL
13+--------------------------------------------------------------------------+
14| Disclaimer:
15|
16| This file is provided "AS IS" without warranty of any kind, either
17| expressed or implied.
18+--------------------------------------------------------------------------+
19| This code is based on:
20|   rtc.c,v 1.4 1995/12/19 20:07:15 joel Exp - go32 BSP
21| With the following copyright notice:
22| **************************************************************************
23| *  COPYRIGHT (c) 1989-1997.
24| *  On-Line Applications Research Corporation (OAR).
25| *  Copyright assigned to U.S. Government, 1994.
26| *
27| *  The license and distribution terms for this file may be
28| *  found in found in the file LICENSE in this distribution or at
29| *  http://www.OARcorp.com/rtems/license.html.
30| **************************************************************************
31|
32|  $Id$
33+--------------------------------------------------------------------------*/
34
35
36#include <string.h>
37
38#include <bsp.h>
39
40/*-------------------------------------------------------------------------+
41| Constants
42+--------------------------------------------------------------------------*/
43#define IO_RTC         0x70  /* RTC                                    */
44
45#define RTC_SEC        0x00  /* seconds                                */
46#define RTC_SECALRM    0x01  /* seconds alarm                          */
47#define RTC_MIN        0x02  /* minutes                                */
48#define RTC_MINALRM    0x03  /* minutes alarm                          */
49#define RTC_HRS        0x04  /* hours                                  */
50#define RTC_HRSALRM    0x05  /* hours alarm                            */
51#define RTC_WDAY       0x06  /* week day                               */
52#define RTC_DAY        0x07  /* day of month                           */
53#define RTC_MONTH      0x08  /* month of year                          */
54#define RTC_YEAR       0x09  /* month of year                          */
55#define RTC_STATUSA    0x0a  /* status register A                      */
56#define  RTCSA_TUP     0x80  /* time update, don't look now            */
57
58#define RTC_STATUSB    0x0b  /* status register B                      */
59
60#define RTC_INTR       0x0c  /* status register C (R) interrupt source */
61#define  RTCIR_UPDATE  0x10  /* update intr                            */
62#define  RTCIR_ALARM   0x20  /* alarm intr                             */
63#define  RTCIR_PERIOD  0x40  /* periodic intr                          */
64#define  RTCIR_INT     0x80  /* interrupt output signal                */
65
66#define RTC_STATUSD    0x0d  /* status register D (R) Lost Power       */
67#define  RTCSD_PWR     0x80  /* clock lost power                       */
68
69#define RTC_DIAG       0x0e  /* status register E - bios diagnostic    */
70#define RTCDG_BITS     "\020\010clock_battery\007ROM_cksum\006config_unit\005memory_size\004fixed_disk\003invalid_time"
71
72#define RTC_CENTURY    0x32  /* current century - increment in Dec99   */
73
74
75/*-------------------------------------------------------------------------+
76| Auxiliary Functions
77+--------------------------------------------------------------------------*/
78/*-------------------------------------------------------------------------+
79|         Function: bcd
80|      Description: Convert 2 digit number to its BCD representation.
81| Global Variables: None.
82|        Arguments: i - Number to convert.
83|          Returns: BCD representation of number.
84+--------------------------------------------------------------------------*/
85static inline rtems_unsigned8
86bcd(rtems_unsigned8 i)
87{
88  return ((i / 16) * 10 + (i % 16));
89} /* bcd */
90
91#define QUICK_READ  /* Quick read of the RTC: don't return number of seconds. */
92
93#ifndef QUICK_READ
94
95#define SECS_PER_DAY      (24 * 60 * 60)
96#define SECS_PER_REG_YEAR (365 * SECS_PER_DAY)
97
98/*-------------------------------------------------------------------------+
99|         Function: ytos
100|      Description: Convert years to seconds (since 1970).
101| Global Variables: None.
102|        Arguments: y - year to convert (1970 <= y <= 2100).
103|          Returns: number of seconds since 1970.
104+--------------------------------------------------------------------------*/
105static inline rtems_unsigned32
106ytos(rtems_unsigned16 y)
107{                                       /* v NUM LEAP YEARS v */
108  return ((y - 1970) * SECS_PER_REG_YEAR + (y - 1970 + 1) / 4 * SECS_PER_DAY);
109} /* ytos */
110
111
112/*-------------------------------------------------------------------------+
113|         Function: mtos
114|      Description: Convert months to seconds since January.
115| Global Variables: None.
116|        Arguments: m - month to convert, leap - is this a month of a leap year.
117|          Returns: number of seconds since January.
118+--------------------------------------------------------------------------*/
119static inline rtems_unsigned32
120mtos(rtems_unsigned8 m, rtems_boolean leap)
121{
122  static rtems_unsigned16 daysMonth[] = { 0, 0, 31,  59,  90, 120, 151, 181,
123                                               212, 243, 273, 304, 334, 365 };
124    /* Days since beginning of year until beginning of month. */
125
126  return ((daysMonth[m] + (leap ? 1 : 0)) * SECS_PER_DAY);
127} /* mtos */
128
129#endif /* QUICK_READ */
130
131/*-------------------------------------------------------------------------+
132|         Function: rtcin
133|      Description: Perform action on RTC and return its result.
134| Global Variables: None.
135|        Arguments: what - what to write to RTC port (what to do).
136|          Returns: result received from RTC port after action performed.
137+--------------------------------------------------------------------------*/
138static inline rtems_unsigned8
139rtcin(rtems_unsigned8 what)
140{
141    rtems_unsigned8 r;
142
143    outport_byte(IO_RTC,   what);
144    inport_byte (IO_RTC+1, r);
145    return r;
146} /* rtcin */
147
148
149/*-------------------------------------------------------------------------+
150| Functions
151+--------------------------------------------------------------------------*/
152/*-------------------------------------------------------------------------+
153|         Function: init_rtc
154|      Description: Initialize real-time clock (RTC).
155| Global Variables: None.
156|        Arguments: None.
157|          Returns: Nothing.
158+--------------------------------------------------------------------------*/
159void
160init_rtc(void)
161{
162  rtems_unsigned8 s;
163
164  /* initialize brain-dead battery powered clock */
165  outport_byte(IO_RTC,   RTC_STATUSA);
166  outport_byte(IO_RTC+1, 0x26);
167  outport_byte(IO_RTC,   RTC_STATUSB);
168  outport_byte(IO_RTC+1, 2);
169
170  outport_byte(IO_RTC,   RTC_DIAG);
171  inport_byte (IO_RTC+1, s);
172  if (s)
173    printk("RTC BIOS diagnostic error %b\n", s);
174
175  /* FIXME: This was last line's original version. How was it supposed to work?
176       printf("RTC BIOS diagnostic error %b\n", s, RTCDG_BITS); */
177} /* init_rtc */
178
179
180/*-------------------------------------------------------------------------+
181|         Function: rtc_read
182|      Description: Read present time from RTC and return it.
183| Global Variables: None.
184|        Arguments: tod - to return present time in 'rtems_time_of_day' format.
185|          Returns: number of seconds from 1970/01/01 corresponding to 'tod'.
186+--------------------------------------------------------------------------*/
187long int
188rtc_read(rtems_time_of_day *tod)
189{
190  rtems_unsigned8  sa;
191  rtems_unsigned32 sec = 0;
192
193  memset(tod, 0, sizeof *tod); /* zero tod structure */
194
195  /* do we have a realtime clock present? (otherwise we loop below) */
196  sa = rtcin(RTC_STATUSA);
197  if (sa == 0xff || sa == 0)
198    return -1;
199
200  /* ready for a read? */
201  while ((sa&RTCSA_TUP) == RTCSA_TUP)
202    sa = rtcin(RTC_STATUSA);
203
204  tod->year     = bcd(rtcin(RTC_YEAR)) + 1900;  /* year    */
205  if (tod->year < 1970) tod->year += 100;       
206  tod->month    = bcd(rtcin(RTC_MONTH));        /* month   */
207  tod->day      = bcd(rtcin(RTC_DAY));          /* day     */
208  (void)          bcd(rtcin(RTC_WDAY));         /* weekday */
209  tod->hour     = bcd(rtcin(RTC_HRS));          /* hour    */
210  tod->minute   = bcd(rtcin(RTC_MIN));          /* minutes */
211  tod->second   = bcd(rtcin(RTC_SEC));          /* seconds */
212  tod->ticks    = 0;
213
214#ifndef QUICK_READ  /* Quick read of the RTC: don't return number of seconds. */
215  sec =  ytos(tod->year);
216  sec += mtos(tod->month, (tod->year % 4) == 0);
217  sec += tod->day * SECS_PER_DAY;
218  sec += tod->hour * 60 * 60;                     /* hour    */
219  sec += tod->minute * 60;                        /* minutes */
220  sec += tod->second;                             /* seconds */
221#endif /* QUICK_READ */
222
223  return (long int)sec;
224} /* rtc_read */
225
226
Note: See TracBrowser for help on using the repository browser.