summaryrefslogtreecommitdiff
path: root/ecos/packages/kernel/current/tests/clock0.cxx
blob: 42bdbb3fd8a640a9fcfa12aefd91916884ee048d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//==========================================================================
//
//        clock0.cxx
//
//        Clock test 0
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):     dsm
// Contributors:    dsm
// Date:          1998-02-13
// Description:   Tests some basic clock functions.
// Omissions:     Doesn't test likely boundary conditions for
//                CYGNUM_KERNEL_COUNTERS_MULTI_LIST_SIZE
//                Real Time Clock Testing is limited
// Options:
//     CYGIMP_KERNEL_COUNTERS_SINGLE_LIST
//     CYGIMP_KERNEL_COUNTERS_MULTI_LIST
//     CYGVAR_KERNEL_COUNTERS_CLOCK
//     CYGNUM_KERNEL_COUNTERS_MULTI_LIST_SIZE
// Assumptions:   This assumes we have long long support and
//                that counters are 64 bits.
//####DESCRIPTIONEND####

#include <pkgconf/kernel.h>

#include <cyg/kernel/clock.hxx>

#include <cyg/infra/testcase.h>

#include <cyg/kernel/clock.inl>

#include "testaux.hxx"

#ifdef CYGVAR_KERNEL_COUNTERS_CLOCK

cyg_alarm_fn call_me;

bool flash( void )
{
    Cyg_Counter counter0 = Cyg_Counter();
    Cyg_Counter counter1 = Cyg_Counter(723);
    
    CYG_ASSERTCLASSO( counter0, "error" );
    CYG_ASSERTCLASSO( counter1, "error" );

    Cyg_Alarm alarm0 = Cyg_Alarm(&counter0, call_me, 12);

    CYG_ASSERTCLASSO( alarm0, "error" );

    Cyg_Clock::cyg_resolution res = {1,2};

    Cyg_Clock clock0(res);

    CYG_ASSERTCLASSO( clock0, "error" );

    return true;
}

// Testing alarms
//
// call_me is a function that will be called when an alarm is
// triggered.  It updates a global variable called which is CHECKed
// explicitly to see if the approriate alarms have been called.

cyg_uint16 called = 0x0;

void call_me(Cyg_Alarm *alarm, CYG_ADDRWORD data)
{
    called ^= data;
}

void call_me2(Cyg_Alarm *alarm, CYG_ADDRWORD data)
{
    call_me(alarm, data^0x10);
}


void clock0_main(void)
{
    CYG_TEST_INIT();

    CHECK(flash());
    CHECK(flash());

    const cyg_uint32 big_number = 3333222111u;
    Cyg_Counter counter0 = Cyg_Counter();

    CHECK( 0 == counter0.current_value() );
    CHECK( 0 == counter0.current_value_lo() );
    CHECK( 0 == counter0.current_value_hi() );

    counter0.tick();

    CHECK( 1 == counter0.current_value() );
    CHECK( 1 == counter0.current_value_lo() );
    CHECK( 0 == counter0.current_value_hi() );

    counter0.tick(6);

    CHECK( 7 == counter0.current_value() );
    CHECK( 7 == counter0.current_value_lo() );
    CHECK( 0 == counter0.current_value_hi() );

    counter0.set_value( 0xfffffffc );

    CHECK( 0xfffffffc == counter0.current_value() );
    CHECK( 0xfffffffc == counter0.current_value_lo() );
    CHECK( 0 == counter0.current_value_hi() );

    counter0.tick( 0x13 );      // Overflows 32 bits
    
    CHECK( 0x10000000fULL == counter0.current_value() );
    CHECK( 0xf == counter0.current_value_lo() );
    CHECK( 0x1 == counter0.current_value_hi() );

    
    Cyg_Counter counter1 = Cyg_Counter(big_number);

    CHECK( 0 == counter1.current_value() );
    CHECK( 0 == counter1.current_value_lo() );
    CHECK( 0 == counter1.current_value_hi() );

    counter1.tick(2);
    
    CHECK( 2ll*big_number == counter1.current_value() );
    CHECK( ((2ll*big_number) & 0xffffffff) ==
                counter1.current_value_lo() );
    CHECK( ((2ll*big_number) >> 32) == counter1.current_value_hi() );

    counter1.tick();
    
    CHECK( 3ll*big_number == counter1.current_value() );
    CHECK( ((3ll*big_number) & 0xffffffff) ==
                counter1.current_value_lo() );
    CHECK( ((3ll*big_number) >> 32) == counter1.current_value_hi() );

    counter1.tick();
    
    CHECK( 4ll*big_number == counter1.current_value() );
    CHECK( ((4ll*big_number) & 0xffffffff) ==
                counter1.current_value_lo() );
    CHECK( ((4ll*big_number) >> 32) == counter1.current_value_hi() );

    counter1.set_value(1222333444555ll);
    CHECK( 1222333444555ll == counter1.current_value() );

    counter0.set_value(11);
    CHECK( 11 == counter0.current_value() );
    
    // the call_me functions cause the "called" bits to toggle
    // CHECKing the value of called TEST_CHECKs the parity of # of calls
    // made by each alarm.

    Cyg_Alarm alarm0 = Cyg_Alarm(&counter0, call_me, 0x1);
    Cyg_Alarm alarm1 = Cyg_Alarm(&counter0, call_me, 0x2);
    Cyg_Alarm alarm2 = Cyg_Alarm(&counter0, call_me2, 0x4);
    
    CHECK( 0x00 == called );
    alarm0.initialize(12,3);
    alarm2.initialize(21,2);
    CHECK( 0x00 == called );

    counter0.tick();            // 12 a0
    CHECK( 0x01 == called );

    alarm1.initialize(13,0);
    counter0.tick();            // 13 a1
    CHECK( 0x03 == called );

    alarm1.initialize(17,0);
    counter0.tick();            // 14
    CHECK( 0x03 == called );

    counter0.tick();            // 15 a0
    CHECK( 0x02 == called );

    counter0.tick(2);           // 17 a1
    CHECK( 0x00 == called );

    counter0.tick();            // 18 a0
    CHECK( 0x01 == called );

    counter0.tick(3);           // 21 a0 a2
    CHECK( 0x14 == called );

    counter0.tick(2);           // 23 a2
    CHECK( 0x00 == called );
    
    alarm2.disable();  

    counter0.tick(2);           // 25 a0(24)
    CHECK( 0x01 == called );
    
    alarm2.enable();            // a2 (enabled at 25)
    CHECK( 0x15 == called );

    counter0.tick();            // 26
    CHECK( 0x15 == called );
    
    counter0.tick(2);           // 28 a0(27) a2(27)
    CHECK( 0x00 == called );
    
    counter0.tick(3);           // 31 a0(30) a2(29 31)
    CHECK( 0x01 == called );

    Cyg_Clock::cyg_resolution res0;

    res0.dividend = 100;
    res0.divisor = 3;

    Cyg_Clock::cyg_resolution res1;

    Cyg_Clock clock0 = Cyg_Clock(res0);

    res1 = clock0.get_resolution();
    CHECK( res0.dividend == res1.dividend );
    CHECK( res0.divisor == res1.divisor );

    res1.dividend = 12;
    res1.divisor = 25;

    clock0.set_resolution(res1);
    res0 = clock0.get_resolution();
    CHECK( res0.dividend == res1.dividend );
    CHECK( res0.divisor == res1.divisor );

    res0 = Cyg_Clock::real_time_clock->get_resolution();

    CYG_TEST_PASS_FINISH("Clock 0 OK");
}

externC void
cyg_start( void )
{
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
    cyg_hal_invoke_constructors();
#endif
    clock0_main();
}

#else // def CYGVAR_KERNEL_COUNTERS_CLOCK

externC void
cyg_start( void )
{
    CYG_TEST_INIT();
    CYG_TEST_NA( "Kernel real-time clock disabled");
}

#endif // def CYGVAR_KERNEL_COUNTERS_CLOCK
// EOF clock0.cxx