]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/io/wallclock/v2_0/src/wallclock.cxx
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / io / wallclock / v2_0 / src / wallclock.cxx
1 //==========================================================================
2 //
3 //      devs/wallclock/wallclock.cxx
4 //
5 //      Wallclock base
6 //
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.
12 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):     jskov
44 // Contributors:  jskov
45 // Date:          2000-03-28
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).
50 //
51 //                The driver can be configured to run in one of two modes:
52 //                  init-get mode:
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
59 //                    clocks.
60 //
61 //                  set-get mode:
62 //                    Requires both set and get functions for the hardware
63 //                    device. While bigger, it allows support of 
64 //                    battery-backed up clocks.
65 //
66 //
67 //####DESCRIPTIONEND####
68 //
69 //==========================================================================
70
71 #include <pkgconf/wallclock.h>          // Wallclock device config
72
73 #include <cyg/infra/cyg_type.h>         // Common type definitions and support
74
75 #include <cyg/hal/drv_api.h>            // Driver API
76
77 #include <cyg/io/wallclock.hxx>         // The WallClock API
78
79
80 //-----------------------------------------------------------------------------
81 // Local static variables
82
83 static Cyg_WallClock wallclock_instance CYGBLD_ATTRIB_INIT_AFTER( CYG_INIT_CLOCK );
84
85 #ifndef CYGSEM_WALLCLOCK_SET_GET_MODE
86 static cyg_uint32 epoch_ticks;
87 static cyg_uint32 epoch_time_stamp;
88 #endif
89
90 static cyg_drv_mutex_t wallclock_lock;
91
92 Cyg_WallClock *Cyg_WallClock::wallclock;
93
94 //-----------------------------------------------------------------------------
95 // Constructor
96
97 Cyg_WallClock::Cyg_WallClock()
98 {
99     // install instance pointer
100     wallclock = &wallclock_instance;
101
102     // Initialize lock used for mutually exclusive access to hardware
103     cyg_drv_mutex_init(&wallclock_lock);
104
105     // Always allow low-level driver to initialize clock, even though it
106     // may not be necessary for set-get mode.
107     init_hw_seconds();
108 }
109
110 //-----------------------------------------------------------------------------
111 // Returns the number of seconds elapsed since 1970-01-01 00:00:00.
112 // This may involve reading the hardware, so it may take anything up to
113 // a second to complete.
114
115 cyg_uint32 Cyg_WallClock::get_current_time()
116 {
117     cyg_uint32 res;
118
119     while (!cyg_drv_mutex_lock(&wallclock_lock));
120
121 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
122     res = get_hw_seconds();
123 #else
124     res = epoch_time_stamp + get_hw_seconds() - epoch_ticks;
125 #endif
126
127     cyg_drv_mutex_unlock(&wallclock_lock);
128
129     return res;
130 }
131
132 //-----------------------------------------------------------------------------
133 // Sets the clock. Argument is seconds elapsed since 1970-01-01 00:00:00.
134 // This may involve reading or writing to the hardware, so it may take
135 // anything up to a second to complete.
136 void Cyg_WallClock::set_current_time( cyg_uint32 time_stamp )
137 {
138     while (!cyg_drv_mutex_lock(&wallclock_lock));
139
140 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
141     set_hw_seconds(time_stamp);
142 #else
143     epoch_time_stamp    = time_stamp;
144     epoch_ticks         = get_hw_seconds();
145 #endif
146
147     cyg_drv_mutex_unlock(&wallclock_lock);
148 }
149
150 //-----------------------------------------------------------------------------
151 // End of devs/wallclock/wallclock.cxx