1 //==========================================================================
3 // devs/wallclock/wallclock.cxx
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 base driver
47 // Description: This provides a generic wallclock base driver which
48 // relies on sub-drivers to provide the interface functions
49 // to the hardware device (see wallclock.hxx for details).
51 // The driver can be configured to run in one of two modes:
53 // In this mode, the hardware driver only needs to
54 // implement a function to read the hardware clock
55 // and a (possibly empty) init function which makes
56 // sure the hardware clock is properly initialized.
57 // While the driver in this mode has a smaller memory
58 // foot-print, it does not support battery-backed up
62 // Requires both set and get functions for the hardware
63 // device. While bigger, it allows support of
64 // battery-backed up clocks.
67 //####DESCRIPTIONEND####
69 //==========================================================================
71 #include <pkgconf/wallclock.h> // Wallclock device config
73 #include <cyg/infra/cyg_type.h> // Common type definitions and support
75 #include <cyg/hal/drv_api.h> // Driver API
77 #include <cyg/io/wallclock.hxx> // The WallClock API
80 //-----------------------------------------------------------------------------
81 // Local static variables
83 static Cyg_WallClock wallclock_instance CYGBLD_ATTRIB_INIT_AFTER( CYG_INIT_CLOCK );
85 #ifndef CYGSEM_WALLCLOCK_SET_GET_MODE
86 static cyg_uint32 epoch_ticks;
87 static cyg_uint32 epoch_time_stamp;
90 Cyg_WallClock *Cyg_WallClock::wallclock;
92 //-----------------------------------------------------------------------------
95 Cyg_WallClock::Cyg_WallClock()
97 // install instance pointer
98 wallclock = &wallclock_instance;
100 // Always allow low-level driver to initialize clock, even though it
101 // may not be necessary for set-get mode.
105 //-----------------------------------------------------------------------------
106 // Returns the number of seconds elapsed since 1970-01-01 00:00:00.
107 // This may involve reading the hardware, so it may take anything up to
108 // a second to complete.
110 cyg_uint32 Cyg_WallClock::get_current_time()
116 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
117 res = get_hw_seconds();
119 res = epoch_time_stamp + get_hw_seconds() - epoch_ticks;
122 cyg_drv_dsr_unlock();
127 //-----------------------------------------------------------------------------
128 // Sets the clock. Argument is seconds elapsed since 1970-01-01 00:00:00.
129 // This may involve reading or writing to the hardware, so it may take
130 // anything up to a second to complete.
131 void Cyg_WallClock::set_current_time( cyg_uint32 time_stamp )
135 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
136 set_hw_seconds(time_stamp);
138 epoch_time_stamp = time_stamp;
139 epoch_ticks = get_hw_seconds();
142 cyg_drv_dsr_unlock();
145 //-----------------------------------------------------------------------------
146 // End of devs/wallclock/wallclock.cxx