source: rtems/c/src/libchip/rtc/m48t08.c @ 602e395

4.115
Last change on this file since 602e395 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 *  This file interfaces with the real-time clock found in
3 *  a Mostek M48T08 or M48T18 or compatibles.
4 *
5 *  Year 2K Notes:
6 *
7 *  This chip only uses a two digit field to store the year.  This
8 *  code uses the RTEMS Epoch as a pivot year.  This lets us map the
9 *  two digit year field as follows:
10 *
11 *    + two digit years 0-87 are mapped to 2000-2087.
12 *    + two digit years 88-99 are mapped to 1988-1999.
13 *
14 *  This is less than the time span supported by RTEMS.
15 *
16 *  COPYRIGHT (c) 1989-1999.
17 *  On-Line Applications Research Corporation (OAR).
18 *
19 *  The license and distribution terms for this file may be
20 *  found in the file LICENSE in this distribution or at
21 *  http://www.rtems.org/license/LICENSE.
22 */
23
24#include <rtems.h>
25#include <libchip/rtc.h>
26#include <libchip/m48t08.h>
27
28/*
29 *  Control register bits
30 */
31
32#define M48T08_CONTROL_WRITE  0x80
33#define M48T08_CONTROL_READ   0x40
34#define M48T08_CONTROL_SIGN   0x20
35
36/*
37 *  m48t08_initialize
38 */
39
40static void m48t08_initialize(
41  int minor
42)
43{
44}
45
46/*
47 *  m48t08_get_time
48 */
49
50#define From_BCD( _x ) ((((_x) >> 4) * 10) + ((_x) & 0x0F))
51#define To_BCD( _x )   ((((_x) / 10) << 4) + ((_x) % 10))
52
53static int m48t08_get_time(
54  int                minor,
55  rtems_time_of_day *time
56)
57{
58  uint32_t       m48t08;
59  getRegister_f  getReg;
60  setRegister_f  setReg;
61  uint8_t        controlReg;
62  uint32_t       value1;
63  uint32_t       value2;
64
65  m48t08 = RTC_Table[ minor ].ulCtrlPort1;
66  getReg = RTC_Table[ minor ].getRegister;
67  setReg = RTC_Table[ minor ].setRegister;
68
69  /*
70   *  Put the RTC into read mode
71   */
72
73  controlReg = (*getReg)( m48t08, M48T08_CONTROL );
74  (*setReg)( m48t08, M48T08_CONTROL, controlReg | M48T08_CONTROL_READ );
75
76  value1 = (*getReg)( m48t08, M48T08_YEAR );
77  value2 = From_BCD( value1 );
78  if ( value2 < 88 )
79    time->year = 2000 + value2;
80  else
81    time->year = 1900 + value2;
82
83  value1 = (*getReg)( m48t08, M48T08_MONTH );
84  time->month = From_BCD( value1 );
85
86  value1 = (*getReg)( m48t08, M48T08_DATE );
87  time->day = From_BCD( value1 );
88
89  value1 = (*getReg)( m48t08, M48T08_HOUR );
90  time->hour = From_BCD( value1 );
91
92  value1 = (*getReg)( m48t08, M48T08_MINUTE );
93  time->minute = From_BCD( value1 );
94
95  value1 = (*getReg)( m48t08, M48T08_SECOND );
96  time->second = From_BCD( value1 );
97
98  time->ticks  = 0;
99
100  /*
101   *  Put the RTC back into normal mode.
102   */
103
104  (*setReg)( m48t08, M48T08_CONTROL, controlReg );
105
106  return 0;
107}
108
109/*
110 *  m48t08_set_time
111 */
112
113static int m48t08_set_time(
114  int                minor,
115  const rtems_time_of_day *time
116)
117{
118  uint32_t       m48t08;
119  getRegister_f  getReg;
120  setRegister_f  setReg;
121  uint8_t        controlReg;
122
123  m48t08 = RTC_Table[ minor ].ulCtrlPort1;
124  getReg = RTC_Table[ minor ].getRegister;
125  setReg = RTC_Table[ minor ].setRegister;
126
127  /*
128   *  Put the RTC into read mode
129   */
130
131  controlReg = (*getReg)( m48t08, M48T08_CONTROL );
132  (*setReg)( m48t08, M48T08_CONTROL, controlReg | M48T08_CONTROL_WRITE );
133
134  if ( time->year >= 2088 )
135    rtems_fatal_error_occurred( RTEMS_INVALID_NUMBER );
136
137  (*setReg)( m48t08, M48T08_YEAR,    To_BCD(time->year % 100) );
138  (*setReg)( m48t08, M48T08_MONTH,   To_BCD(time->month) );
139  (*setReg)( m48t08, M48T08_DATE,    To_BCD(time->day) );
140  (*setReg)( m48t08, M48T08_HOUR,    To_BCD(time->hour) );
141  (*setReg)( m48t08, M48T08_MINUTE,  To_BCD(time->minute) );
142  (*setReg)( m48t08, M48T08_SECOND,  To_BCD(time->second) );
143
144  /*
145   *  Put the RTC back into normal mode.
146   */
147
148  (*setReg)( m48t08, M48T08_CONTROL, controlReg );
149
150  return 0;
151}
152
153/*
154 *  Driver function table
155 */
156
157rtc_fns m48t08_fns = {
158  m48t08_initialize,
159  m48t08_get_time,
160  m48t08_set_time
161};
Note: See TracBrowser for help on using the repository browser.