comparison packages/devs/wallclock/sh3/current/src/wallclock_sh3.cxx @ 80:2085233a121a ecos-sw-2000-04-07

Merge from eCos master repository on 2000-04-07-07:52:29-BST
author jlarmour
date Fri, 07 Apr 2000 07:45:04 +0000
parents
children 4642a6d35749
comparison
equal deleted inserted replaced
79:5b39a5a40ccd 80:2085233a121a
1 //==========================================================================
2 //
3 // devs/wallclock/sh3.cxx
4 //
5 // Wallclock emulation implementation
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): jskov
35 // Contributors: jskov
36 // Date: 2000-03-17
37 // Purpose: Wallclock driver for SH3 CPU RTC module
38 //
39 //####DESCRIPTIONEND####
40 //
41 //==========================================================================
42
43 #include <pkgconf/kernel.h> // Kernel config
44 #include <pkgconf/wallclock.h> // Wallclock device config
45
46 #include <cyg/infra/cyg_type.h> // Common type definitions and support
47
48 #define __CYGBLD_DRIVER_PRIVATE
49 #include <cyg/io/wallclock.hxx> // The WallClock API
50
51 #include <cyg/hal/sh_regs.h> // RTC register definitions
52
53 #include <cyg/hal/hal_io.h> // IO macros
54
55 #include <cyg/infra/diag.h> // For debugging
56
57
58
59 //-----------------------------------------------------------------------------
60 // Functions for setting and getting the hardware clock counters
61
62 // Year must be last two digits of "western calendar year". Leap year when
63 // divisible by four.
64 static void
65 set_sh3_hwclock(cyg_uint32 year, cyg_uint32 month, cyg_uint32 mday,
66 cyg_uint32 hour, cyg_uint32 minute, cyg_uint32 second)
67 {
68 // Stop RTC
69 HAL_WRITE_UINT8(CYGARC_REG_RCR2, CYGARC_REG_RCR2_RESET);
70
71 // Program it
72 HAL_WRITE_UINT8(CYGARC_REG_RYRCNT, TO_BCD(year));
73 HAL_WRITE_UINT8(CYGARC_REG_RMONCNT, TO_BCD(month));
74 HAL_WRITE_UINT8(CYGARC_REG_RDAYCNT, TO_BCD(mday));
75 HAL_WRITE_UINT8(CYGARC_REG_RHRCNT, TO_BCD(hour));
76 HAL_WRITE_UINT8(CYGARC_REG_RMINCNT, TO_BCD(minute));
77 HAL_WRITE_UINT8(CYGARC_REG_RSECCNT, TO_BCD(second));
78
79 // Start RTC
80 HAL_WRITE_UINT8(CYGARC_REG_RCR1, CYGARC_REG_RCR1_CIE);
81 HAL_WRITE_UINT8(CYGARC_REG_RCR2,
82 CYGARC_REG_RCR2_RTCEN | CYGARC_REG_RCR2_START);
83
84 }
85
86 static void
87 get_sh3_hwclock(cyg_uint32* year, cyg_uint32* month, cyg_uint32* mday,
88 cyg_uint32* hour, cyg_uint32* minute, cyg_uint32* second)
89 {
90 cyg_uint8 tmp;
91
92 do {
93 // Clear carry flag
94 HAL_WRITE_UINT8(CYGARC_REG_RCR1, 0);
95
96 // Read time
97 HAL_READ_UINT8(CYGARC_REG_RYRCNT, tmp);
98 *year = TO_DEC(tmp);
99 HAL_READ_UINT8(CYGARC_REG_RMONCNT, tmp);
100 *month = TO_DEC(tmp);
101 HAL_READ_UINT8(CYGARC_REG_RDAYCNT, tmp);
102 *mday = TO_DEC(tmp);
103 HAL_READ_UINT8(CYGARC_REG_RHRCNT, tmp);
104 *hour = TO_DEC(tmp);
105 HAL_READ_UINT8(CYGARC_REG_RMINCNT, tmp);
106 *minute = TO_DEC(tmp);
107 HAL_READ_UINT8(CYGARC_REG_RSECCNT, tmp);
108 *second = TO_DEC(tmp);
109
110 // Read carry flag
111 HAL_READ_UINT8(CYGARC_REG_RCR1, tmp);
112 } while (CYGARC_REG_RCR1_CF & tmp); // loop if carry set
113 }
114
115 //-----------------------------------------------------------------------------
116 // Functions required for the hardware-driver API.
117
118 // Returns the number of seconds elapsed since 1970-01-01 00:00:00.
119 cyg_uint32
120 Cyg_WallClock::get_hw_seconds(void)
121 {
122 cyg_uint32 year, month, mday, hour, minute, second;
123
124 get_sh3_hwclock(&year, &month, &mday, &hour, &minute, &second);
125
126 #if 0
127 // This will cause the test to eventually fail due to these printouts
128 // causing timer interrupts to be lost...
129 diag_printf("year %02d\n", year);
130 diag_printf("month %02d\n", month);
131 diag_printf("mday %02d\n", mday);
132 diag_printf("hour %02d\n", hour);
133 diag_printf("minute %02d\n", minute);
134 diag_printf("second %02d\n", second);
135 #endif
136
137 #ifndef CYGSEM_WALLCLOCK_SET_GET_MODE
138 // We know what we initialized the hardware for : 1970, so by doing this
139 // the returned time should be OK for 30 years uptime.
140 year += 1900;
141 #else
142 // Need to use sliding window or similar to figure out what the
143 // century should be... Patent issue is unclear, and since there's
144 // no battery backup of the clock, there's little point in
145 // investigating.
146 # error "Need some magic here to figure out century counter"
147 #endif
148
149 cyg_uint32 now = _simple_mktime(year, month, mday, hour, minute, second);
150 return now;
151 }
152
153 #ifndef CYGSEM_WALLCLOCK_SET_GET_MODE
154
155 void
156 Cyg_WallClock::init_hw_seconds(void)
157 {
158 // This is our base: 1970-01-01 00:00:00
159 // Set the HW clock - if for nothing else, just to be sure it's in a
160 // legal range. Any arbitrary base could be used.
161 // After this the hardware clock is only read.
162 set_sh3_hwclock(70,1,1,0,0,0);
163 }
164
165 #endif // CYGSEM_WALLCLOCK_SET_GET_MODE
166
167 //-----------------------------------------------------------------------------
168 // End of devs/wallclock/sh3.cxx