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)
78 # error "Need to know base of DS12887 part"
80 # define DS_READ(offset, data) DS_READ_UINT8(DS_BASE + ((offset) << DS_STEP), (data))
81 # define DS_WRITE(offset, data) DS_WRITE_UINT8(DS_BASE + ((offset) << DS_STEP), (data))
83 # if !defined(DS_ADDR) || !defined(DS_DATA)
84 # error "Need to know addr/data locations of DS12887 part"
86 # define DS_READ(offset, data) \
88 DS_WRITE_UINT8(DS_ADDR, (offset)); \
89 DS_READ_UINT8(DS_DATA, (data)); \
91 # define DS_WRITE(offset, data) \
93 DS_WRITE_UINT8(DS_ADDR, (offset)); \
94 DS_WRITE_UINT8(DS_DATA, (data)); \
99 #define DS_SECONDS 0x00
100 #define DS_SECONDS_ALARM 0x01
101 #define DS_MINUTES 0x02
102 #define DS_MINUTES_ALARM 0x03
103 #define DS_HOURS 0x04
104 #define DS_HOURS_ALARM 0x05
107 #define DS_MONTH 0x08
109 #define DS_CENTURY 0x32
111 #define DS_REG_A 0x0a
112 #define DS_REG_B 0x0b
113 #define DS_REG_C 0x0c
114 #define DS_REG_D 0x0d
117 #define DS_REG_A_UIP 0x80
118 #define DS_REG_A_ENABLE 0x20
120 #define DS_REG_B_SET 0x80
121 #define DS_REG_B_DM 0x04
122 #define DS_REG_B_24H 0x02
125 //----------------------------------------------------------------------------
126 // Accessor functions
128 init_ds_hwclock(void)
130 cyg_uint8 _regb, _tmp;
133 DS_WRITE(DS_REG_B, DS_REG_B_24H);
135 DS_WRITE(DS_REG_A, DS_REG_A_ENABLE);
137 // Verify that there are reasonable default settings - otherwise
141 DS_READ(DS_REG_B, _regb);
142 _regb |= DS_REG_B_SET;
143 DS_WRITE(DS_REG_B, _regb);
145 DS_READ(DS_CENTURY, _tmp);
147 DS_WRITE(DS_CENTURY, TO_BCD(20));
149 DS_READ(DS_MONTH, _tmp);
151 DS_WRITE(DS_MONTH, TO_BCD(1));
153 DS_READ(DS_DOM, _tmp);
155 DS_WRITE(DS_DOM, TO_BCD(1));
157 DS_READ(DS_DOM, _tmp);
159 DS_WRITE(DS_DOM, TO_BCD(1));
162 _regb &= ~DS_REG_B_SET;
163 DS_WRITE(DS_REG_B, _regb);
168 set_ds_hwclock(cyg_uint32 year, cyg_uint32 month, cyg_uint32 mday,
169 cyg_uint32 hour, cyg_uint32 minute, cyg_uint32 second)
173 DS_READ(DS_REG_B, _regb);
174 _regb |= DS_REG_B_SET;
175 DS_WRITE(DS_REG_B, _regb);
177 DS_WRITE(DS_CENTURY, TO_BCD((cyg_uint8)(year / 100)));
178 DS_WRITE(DS_YEAR, TO_BCD((cyg_uint8)(year % 100)));
179 DS_WRITE(DS_MONTH, TO_BCD((cyg_uint8)month));
180 DS_WRITE(DS_DOM, TO_BCD((cyg_uint8)mday));
181 DS_WRITE(DS_HOURS, TO_BCD((cyg_uint8)hour));
182 DS_WRITE(DS_MINUTES, TO_BCD((cyg_uint8)minute));
183 DS_WRITE(DS_SECONDS, TO_BCD((cyg_uint8)second));
186 _regb &= ~DS_REG_B_SET;
187 DS_WRITE(DS_REG_B, _regb);
190 // This will cause the test to eventually fail due to these printouts
191 // causing timer interrupts to be lost...
192 diag_printf("Set -------------\n");
193 diag_printf("year %02d\n", year);
194 diag_printf("month %02d\n", month);
195 diag_printf("mday %02d\n", mday);
196 diag_printf("hour %02d\n", hour);
197 diag_printf("minute %02d\n", minute);
198 diag_printf("second %02d\n", second);
203 get_ds_hwclock(cyg_uint32* year, cyg_uint32* month, cyg_uint32* mday,
204 cyg_uint32* hour, cyg_uint32* minute, cyg_uint32* second)
206 cyg_uint8 _reg, _t1, _t2;
209 // Wait for update flag clears
211 DS_READ(DS_REG_A, _reg);
212 } while (_reg & DS_REG_A_UIP);
214 // Disable interrupts while reading to ensure it doesn't take more
216 HAL_DISABLE_INTERRUPTS(_old);
218 DS_READ(DS_CENTURY, _t1);
219 DS_READ(DS_YEAR, _t2);
220 *year = (cyg_uint32)TO_DEC(_t1)*100 + (cyg_uint32)TO_DEC(_t2);
222 DS_READ(DS_MONTH, _t1);
223 *month = (cyg_uint32)TO_DEC(_t1);
225 DS_READ(DS_DOM, _t1);
226 *mday = (cyg_uint32)TO_DEC(_t1);
228 DS_READ(DS_HOURS, _t1);
229 *hour = (cyg_uint32)TO_DEC(_t1);
231 DS_READ(DS_MINUTES, _t1);
232 *minute = (cyg_uint32)TO_DEC(_t1);
234 DS_READ(DS_SECONDS, _t1);
235 *second = (cyg_uint32)TO_DEC(_t1);
237 // Reenable interrupts
238 HAL_RESTORE_INTERRUPTS(_old);
241 // This will cause the test to eventually fail due to these printouts
242 // causing timer interrupts to be lost...
243 diag_printf("year %02d\n", *year);
244 diag_printf("month %02d\n", *month);
245 diag_printf("mday %02d\n", *mday);
246 diag_printf("hour %02d\n", *hour);
247 diag_printf("minute %02d\n", *minute);
248 diag_printf("second %02d\n", *second);
252 //-----------------------------------------------------------------------------
253 // Functions required for the hardware-driver API.
255 // Returns the number of seconds elapsed since 1970-01-01 00:00:00.
257 Cyg_WallClock::get_hw_seconds(void)
259 cyg_uint32 year, month, mday, hour, minute, second;
261 get_ds_hwclock(&year, &month, &mday, &hour, &minute, &second);
263 cyg_uint32 now = _simple_mktime(year, month, mday, hour, minute, second);
267 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
269 // Sets the clock. Argument is seconds elapsed since 1970-01-01 00:00:00.
271 Cyg_WallClock::set_hw_seconds( cyg_uint32 secs )
273 cyg_uint32 year, month, mday, hour, minute, second;
275 _simple_mkdate(secs, &year, &month, &mday, &hour, &minute, &second);
277 set_ds_hwclock(year, month, mday, hour, minute, second);
283 Cyg_WallClock::init_hw_seconds(void)
285 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
288 // This is our base: 1970-01-01 00:00:00
289 // Set the HW clock - if for nothing else, just to be sure it's in a
290 // legal range. Any arbitrary base could be used.
291 // After this the hardware clock is only read.
292 set_ds_hwclock(1970,1,1,0,0,0);
296 //-----------------------------------------------------------------------------
297 // End of devs/wallclock/ds12887.inl