| 1 |
786 |
skrzyp |
//==========================================================================
|
| 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, 2009 Free Software Foundation, 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
|
| 16 |
|
|
// version.
|
| 17 |
|
|
//
|
| 18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
| 19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 21 |
|
|
// for more details.
|
| 22 |
|
|
//
|
| 23 |
|
|
// You should have received a copy of the GNU General Public License
|
| 24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
| 25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
| 26 |
|
|
//
|
| 27 |
|
|
// As a special exception, if other files instantiate templates or use
|
| 28 |
|
|
// macros or inline functions from this file, or you compile this file
|
| 29 |
|
|
// and link it with other works to produce a work based on this file,
|
| 30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
| 31 |
|
|
// the GNU General Public License. However the source code for this file
|
| 32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
| 33 |
|
|
// General Public License v2.
|
| 34 |
|
|
//
|
| 35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
| 36 |
|
|
// on this file might be covered by the GNU General Public License.
|
| 37 |
|
|
// -------------------------------------------
|
| 38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
| 39 |
|
|
//==========================================================================
|
| 40 |
|
|
//#####DESCRIPTIONBEGIN####
|
| 41 |
|
|
//
|
| 42 |
|
|
// Author(s): jskov
|
| 43 |
|
|
// Contributors: jskov
|
| 44 |
|
|
// Date: 2000-03-28
|
| 45 |
|
|
// Purpose: Wallclock base driver
|
| 46 |
|
|
// Description: This provides a generic wallclock base driver which
|
| 47 |
|
|
// relies on sub-drivers to provide the interface functions
|
| 48 |
|
|
// to the hardware device (see wallclock.hxx for details).
|
| 49 |
|
|
//
|
| 50 |
|
|
// The driver can be configured to run in one of two modes:
|
| 51 |
|
|
// init-get mode:
|
| 52 |
|
|
// In this mode, the hardware driver only needs to
|
| 53 |
|
|
// implement a function to read the hardware clock
|
| 54 |
|
|
// and a (possibly empty) init function which makes
|
| 55 |
|
|
// sure the hardware clock is properly initialized.
|
| 56 |
|
|
// While the driver in this mode has a smaller memory
|
| 57 |
|
|
// foot-print, it does not support battery-backed up
|
| 58 |
|
|
// clocks.
|
| 59 |
|
|
//
|
| 60 |
|
|
// set-get mode:
|
| 61 |
|
|
// Requires both set and get functions for the hardware
|
| 62 |
|
|
// device. While bigger, it allows support of
|
| 63 |
|
|
// battery-backed up clocks.
|
| 64 |
|
|
//
|
| 65 |
|
|
//
|
| 66 |
|
|
//####DESCRIPTIONEND####
|
| 67 |
|
|
//
|
| 68 |
|
|
//==========================================================================
|
| 69 |
|
|
|
| 70 |
|
|
#include <pkgconf/wallclock.h> // Wallclock device config
|
| 71 |
|
|
|
| 72 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions and support
|
| 73 |
|
|
|
| 74 |
|
|
#include <cyg/hal/drv_api.h> // Driver API
|
| 75 |
|
|
|
| 76 |
|
|
#include <cyg/io/wallclock.hxx> // The WallClock API
|
| 77 |
|
|
|
| 78 |
|
|
|
| 79 |
|
|
//-----------------------------------------------------------------------------
|
| 80 |
|
|
// Local static variables
|
| 81 |
|
|
|
| 82 |
|
|
static Cyg_WallClock wallclock_instance CYGBLD_ATTRIB_INIT_PRI( CYG_INIT_DEV_WALLCLOCK );
|
| 83 |
|
|
|
| 84 |
|
|
#ifndef CYGSEM_WALLCLOCK_SET_GET_MODE
|
| 85 |
|
|
static cyg_uint32 epoch_ticks;
|
| 86 |
|
|
static cyg_uint32 epoch_time_stamp;
|
| 87 |
|
|
#endif
|
| 88 |
|
|
|
| 89 |
|
|
static cyg_drv_mutex_t wallclock_lock;
|
| 90 |
|
|
|
| 91 |
|
|
Cyg_WallClock *Cyg_WallClock::wallclock;
|
| 92 |
|
|
|
| 93 |
|
|
//-----------------------------------------------------------------------------
|
| 94 |
|
|
// Constructor
|
| 95 |
|
|
|
| 96 |
|
|
Cyg_WallClock::Cyg_WallClock()
|
| 97 |
|
|
{
|
| 98 |
|
|
// install instance pointer
|
| 99 |
|
|
wallclock = &wallclock_instance;
|
| 100 |
|
|
|
| 101 |
|
|
// Initialize lock used for mutually exclusive access to hardware
|
| 102 |
|
|
cyg_drv_mutex_init(&wallclock_lock);
|
| 103 |
|
|
|
| 104 |
|
|
// Always allow low-level driver to initialize clock, even though it
|
| 105 |
|
|
// may not be necessary for set-get mode.
|
| 106 |
|
|
init_hw_seconds();
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
//-----------------------------------------------------------------------------
|
| 110 |
|
|
// Returns the number of seconds elapsed since 1970-01-01 00:00:00.
|
| 111 |
|
|
// This may involve reading the hardware, so it may take anything up to
|
| 112 |
|
|
// a second to complete.
|
| 113 |
|
|
|
| 114 |
|
|
cyg_uint32 Cyg_WallClock::get_current_time()
|
| 115 |
|
|
{
|
| 116 |
|
|
cyg_uint32 res;
|
| 117 |
|
|
|
| 118 |
|
|
while (!cyg_drv_mutex_lock(&wallclock_lock));
|
| 119 |
|
|
|
| 120 |
|
|
#ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
|
| 121 |
|
|
res = get_hw_seconds();
|
| 122 |
|
|
#else
|
| 123 |
|
|
res = epoch_time_stamp + get_hw_seconds() - epoch_ticks;
|
| 124 |
|
|
#endif
|
| 125 |
|
|
|
| 126 |
|
|
cyg_drv_mutex_unlock(&wallclock_lock);
|
| 127 |
|
|
|
| 128 |
|
|
return res;
|
| 129 |
|
|
}
|
| 130 |
|
|
|
| 131 |
|
|
//-----------------------------------------------------------------------------
|
| 132 |
|
|
// Sets the clock. Argument is seconds elapsed since 1970-01-01 00:00:00.
|
| 133 |
|
|
// This may involve reading or writing to the hardware, so it may take
|
| 134 |
|
|
// anything up to a second to complete.
|
| 135 |
|
|
void Cyg_WallClock::set_current_time( cyg_uint32 time_stamp )
|
| 136 |
|
|
{
|
| 137 |
|
|
while (!cyg_drv_mutex_lock(&wallclock_lock));
|
| 138 |
|
|
|
| 139 |
|
|
#ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
|
| 140 |
|
|
set_hw_seconds(time_stamp);
|
| 141 |
|
|
#else
|
| 142 |
|
|
epoch_time_stamp = time_stamp;
|
| 143 |
|
|
epoch_ticks = get_hw_seconds();
|
| 144 |
|
|
#endif
|
| 145 |
|
|
|
| 146 |
|
|
cyg_drv_mutex_unlock(&wallclock_lock);
|
| 147 |
|
|
}
|
| 148 |
|
|
|
| 149 |
|
|
//-----------------------------------------------------------------------------
|
| 150 |
|
|
// End of devs/wallclock/wallclock.cxx
|