1 //==========================================================================
3 // devs/wallclock/ds12887.inl
5 // Wallclock implementation for Dallas 12887
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
44 // Contributors: jskov
46 // Purpose: Wallclock driver for Dallas 12887
48 //####DESCRIPTIONEND####
50 //==========================================================================
52 #include <pkgconf/wallclock.h> // Wallclock device config
54 #include <cyg/hal/hal_io.h> // IO macros
55 #include <cyg/hal/hal_intr.h> // interrupt enable/disable
56 #include <cyg/infra/cyg_type.h> // Common type definitions and support
58 #include <cyg/io/wallclock.hxx> // The WallClock API
59 #include <cyg/io/wallclock/wallclock.inl> // Helpers
61 #include <cyg/infra/diag.h>
66 #include CYGDAT_DEVS_WALLCLOCK_DALLAS_12887_INL
69 # define DS_READ_UINT8(x,y) HAL_READ_UINT8(x,y)
70 # define DS_WRITE_UINT8(x,y) HAL_WRITE_UINT8(x,y)
73 #if !defined(DS_READ) && !defined(DS_WRITE) // Allow for INL to define this
79 # error "Need to know base of DS12887 part"
81 #define DS_READ( offset, data) \
82 DS_READ_UINT8( DS_BASE + ((offset) << DS_STEP), (data))
83 #define DS_WRITE(offset, data) \
84 DS_WRITE_UINT8(DS_BASE + ((offset) << DS_STEP), (data))
86 # if !defined(DS_ADDR) || !defined(DS_DATA)
87 # error "Need to know addr/data locations of DS12887 part"
89 # define DS_READ(offset, data) \
91 DS_WRITE_UINT8(DS_ADDR, (offset)); \
92 DS_READ_UINT8(DS_DATA, (data)); \
94 # define DS_WRITE(offset, data) \
96 DS_WRITE_UINT8(DS_ADDR, (offset)); \
97 DS_WRITE_UINT8(DS_DATA, (data)); \
100 #endif // ! DS_READ && ! DS_WRITE
103 #define DS_SECONDS 0x00
104 #define DS_SECONDS_ALARM 0x01
105 #define DS_MINUTES 0x02
106 #define DS_MINUTES_ALARM 0x03
107 #define DS_HOURS 0x04
108 #define DS_HOURS_ALARM 0x05
111 #define DS_MONTH 0x08
113 #define DS_CENTURY 0x32
115 #define DS_REG_A 0x0a
116 #define DS_REG_B 0x0b
117 #define DS_REG_C 0x0c
118 #define DS_REG_D 0x0d
121 #define DS_REG_A_UIP 0x80
122 #define DS_REG_A_ENABLE 0x20
124 #define DS_REG_B_SET 0x80
125 #define DS_REG_B_DM 0x04
126 #define DS_REG_B_24H 0x02
129 //----------------------------------------------------------------------------
130 // Accessor functions
132 init_ds_hwclock(void)
134 cyg_uint8 _regb, _tmp;
137 DS_WRITE(DS_REG_B, DS_REG_B_24H);
139 DS_WRITE(DS_REG_A, DS_REG_A_ENABLE);
141 // Verify that there are reasonable default settings - otherwise
145 DS_READ(DS_REG_B, _regb);
146 _regb |= DS_REG_B_SET;
147 DS_WRITE(DS_REG_B, _regb);
149 DS_READ(DS_CENTURY, _tmp);
151 DS_WRITE(DS_CENTURY, TO_BCD(20));
153 DS_READ(DS_MONTH, _tmp);
155 DS_WRITE(DS_MONTH, TO_BCD(1));
157 DS_READ(DS_DOM, _tmp);
159 DS_WRITE(DS_DOM, TO_BCD(1));
161 DS_READ(DS_DOM, _tmp);
163 DS_WRITE(DS_DOM, TO_BCD(1));
166 _regb &= ~DS_REG_B_SET;
167 DS_WRITE(DS_REG_B, _regb);
172 set_ds_hwclock(cyg_uint32 year, cyg_uint32 month, cyg_uint32 mday,
173 cyg_uint32 hour, cyg_uint32 minute, cyg_uint32 second)
177 DS_READ(DS_REG_B, _regb);
178 _regb |= DS_REG_B_SET;
179 DS_WRITE(DS_REG_B, _regb);
181 DS_WRITE(DS_CENTURY, TO_BCD((cyg_uint8)(year / 100)));
182 DS_WRITE(DS_YEAR, TO_BCD((cyg_uint8)(year % 100)));
183 DS_WRITE(DS_MONTH, TO_BCD((cyg_uint8)month));
184 DS_WRITE(DS_DOM, TO_BCD((cyg_uint8)mday));
185 DS_WRITE(DS_HOURS, TO_BCD((cyg_uint8)hour));
186 DS_WRITE(DS_MINUTES, TO_BCD((cyg_uint8)minute));
187 DS_WRITE(DS_SECONDS, TO_BCD((cyg_uint8)second));
190 _regb &= ~DS_REG_B_SET;
191 DS_WRITE(DS_REG_B, _regb);
194 // This will cause the test to eventually fail due to these printouts
195 // causing timer interrupts to be lost...
196 diag_printf("Set -------------\n");
197 diag_printf("year %02d\n", year);
198 diag_printf("month %02d\n", month);
199 diag_printf("mday %02d\n", mday);
200 diag_printf("hour %02d\n", hour);
201 diag_printf("minute %02d\n", minute);
202 diag_printf("second %02d\n", second);
207 get_ds_hwclock(cyg_uint32* year, cyg_uint32* month, cyg_uint32* mday,
208 cyg_uint32* hour, cyg_uint32* minute, cyg_uint32* second)
210 cyg_uint8 _reg, _t1, _t2;
213 // Wait for update flag clears
215 DS_READ(DS_REG_A, _reg);
216 } while (_reg & DS_REG_A_UIP);
218 // Disable interrupts while reading to ensure it doesn't take more
220 HAL_DISABLE_INTERRUPTS(_old);
222 DS_READ(DS_CENTURY, _t1);
223 DS_READ(DS_YEAR, _t2);
224 *year = (cyg_uint32)TO_DEC(_t1)*100 + (cyg_uint32)TO_DEC(_t2);
226 DS_READ(DS_MONTH, _t1);
227 *month = (cyg_uint32)TO_DEC(_t1);
229 DS_READ(DS_DOM, _t1);
230 *mday = (cyg_uint32)TO_DEC(_t1);
232 DS_READ(DS_HOURS, _t1);
233 *hour = (cyg_uint32)TO_DEC(_t1);
235 DS_READ(DS_MINUTES, _t1);
236 *minute = (cyg_uint32)TO_DEC(_t1);
238 DS_READ(DS_SECONDS, _t1);
239 *second = (cyg_uint32)TO_DEC(_t1);
241 // Reenable interrupts
242 HAL_RESTORE_INTERRUPTS(_old);
245 // This will cause the test to eventually fail due to these printouts
246 // causing timer interrupts to be lost...
247 diag_printf("year %02d\n", *year);
248 diag_printf("month %02d\n", *month);
249 diag_printf("mday %02d\n", *mday);
250 diag_printf("hour %02d\n", *hour);
251 diag_printf("minute %02d\n", *minute);
252 diag_printf("second %02d\n", *second);
256 //-----------------------------------------------------------------------------
257 // Functions required for the hardware-driver API.
259 // Returns the number of seconds elapsed since 1970-01-01 00:00:00.
261 Cyg_WallClock::get_hw_seconds(void)
263 cyg_uint32 year, month, mday, hour, minute, second;
265 get_ds_hwclock(&year, &month, &mday, &hour, &minute, &second);
267 cyg_uint32 now = _simple_mktime(year, month, mday, hour, minute, second);
271 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
273 // Sets the clock. Argument is seconds elapsed since 1970-01-01 00:00:00.
275 Cyg_WallClock::set_hw_seconds( cyg_uint32 secs )
277 cyg_uint32 year, month, mday, hour, minute, second;
279 _simple_mkdate(secs, &year, &month, &mday, &hour, &minute, &second);
281 set_ds_hwclock(year, month, mday, hour, minute, second);
287 Cyg_WallClock::init_hw_seconds(void)
289 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
292 // This is our base: 1970-01-01 00:00:00
293 // Set the HW clock - if for nothing else, just to be sure it's in a
294 // legal range. Any arbitrary base could be used.
295 // After this the hardware clock is only read.
296 set_ds_hwclock(1970,1,1,0,0,0);
300 //-----------------------------------------------------------------------------
301 // End of devs/wallclock/ds12887.inl