summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/sptimecounter01/init.c
blob: 9e396de054f6cb6cb61bcd04f67d4afd6ac22c78 (plain) (blame)
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
/*
 * Copyright (c) 2015 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif

#define TESTS_USE_PRINTK
#include "tmacros.h"

#include <assert.h>

#include <bsp/bootcard.h>

#include <rtems/score/timecounterimpl.h>
#include <rtems/score/todimpl.h>
#include <rtems/timecounter.h>
#include <rtems/bsd.h>

const char rtems_test_name[] = "SPTIMECOUNTER 1";

typedef struct {
  struct timecounter tc_soft;
  u_int tc_soft_counter;
} test_context;

static test_context test_instance;

static uint32_t test_get_timecount_soft(struct timecounter *tc)
{
  test_context *ctx = tc->tc_priv;

  ++ctx->tc_soft_counter;

  return ctx->tc_soft_counter;
}

void boot_card(const char *cmdline)
{
  test_context *ctx = &test_instance;
  struct timecounter *tc_soft = &ctx->tc_soft;
  uint64_t soft_freq = 1000000;
  struct bintime bt;
  struct timeval tv;
  struct timespec ts;

  TEST_BEGIN();

  assert(time(NULL) == TOD_SECONDS_1970_THROUGH_1988);

  rtems_bsd_bintime(&bt);
  assert(bt.sec == TOD_SECONDS_1970_THROUGH_1988);
  assert(bt.frac == 0);

  rtems_bsd_getbintime(&bt);
  assert(bt.sec == TOD_SECONDS_1970_THROUGH_1988);
  assert(bt.frac == 0);

  rtems_bsd_microtime(&tv);
  assert(tv.tv_sec == TOD_SECONDS_1970_THROUGH_1988);
  assert(tv.tv_usec == 0);

  rtems_bsd_getmicrotime(&tv);
  assert(tv.tv_sec == TOD_SECONDS_1970_THROUGH_1988);
  assert(tv.tv_usec == 0);

  rtems_bsd_nanotime(&ts);
  assert(ts.tv_sec == TOD_SECONDS_1970_THROUGH_1988);
  assert(ts.tv_nsec == 0);

  rtems_bsd_getnanotime(&ts);
  assert(ts.tv_sec == TOD_SECONDS_1970_THROUGH_1988);
  assert(ts.tv_nsec == 0);

  assert(rtems_clock_get_uptime_seconds() == 0);
  assert(rtems_clock_get_uptime_nanoseconds() == 0);

  rtems_bsd_binuptime(&bt);
  assert(bt.sec == 1);
  assert(bt.frac == 0);

  rtems_bsd_getbinuptime(&bt);
  assert(bt.sec == 1);
  assert(bt.frac == 0);

  rtems_bsd_microuptime(&tv);
  assert(tv.tv_sec == 1);
  assert(tv.tv_usec == 0);

  rtems_bsd_getmicrouptime(&tv);
  assert(tv.tv_sec == 1);
  assert(tv.tv_usec == 0);

  rtems_bsd_nanouptime(&ts);
  assert(ts.tv_sec == 1);
  assert(ts.tv_nsec == 0);

  rtems_bsd_getnanouptime(&ts);
  assert(ts.tv_sec == 1);
  assert(ts.tv_nsec == 0);

  /* On RTEMS time does not advance using the dummy timecounter */
  rtems_bsd_binuptime(&bt);
  assert(bt.sec == 1);
  assert(bt.frac == 0);

  rtems_timecounter_tick();
  rtems_bsd_binuptime(&bt);
  assert(bt.sec == 1);
  assert(bt.frac == 0);

  ctx->tc_soft_counter = 0;
  tc_soft->tc_get_timecount = test_get_timecount_soft;
  tc_soft->tc_counter_mask = 0x0fffffff;
  tc_soft->tc_frequency = soft_freq;
  tc_soft->tc_quality = 1234;
  tc_soft->tc_priv = ctx;
  _Timecounter_Install(tc_soft);
  assert(ctx->tc_soft_counter == 3);

  rtems_bsd_binuptime(&bt);
  assert(ctx->tc_soft_counter == 4);

  assert(bt.sec == 1);
  assert(bt.frac == 18446744073708);

  ctx->tc_soft_counter = 0xf0000000 | 3;
  rtems_bsd_binuptime(&bt);
  assert(ctx->tc_soft_counter == (0xf0000000 | 4));

  assert(bt.sec == 1);
  assert(bt.frac == 18446744073708);

  /* Ensure that the fraction overflows and the second remains constant */
  ctx->tc_soft_counter = (0xf0000000 | 3) + soft_freq;
  rtems_bsd_binuptime(&bt);
  assert(ctx->tc_soft_counter == (0xf0000000 | 4) + soft_freq);
  assert(bt.sec == 1);
  assert(bt.frac == 18446742522092);

  TEST_END();

  _Terminate(RTEMS_FATAL_SOURCE_EXIT, false, 0);
}

#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER

#define CONFIGURE_APPLICATION_DISABLE_FILESYSTEM

#define CONFIGURE_DISABLE_NEWLIB_REENTRANCY

#define CONFIGURE_SCHEDULER_USER

#define CONFIGURE_SCHEDULER_CONTEXT

#define CONFIGURE_SCHEDULER_CONTROLS { }

#define CONFIGURE_MEMORY_PER_TASK_FOR_SCHEDULER 0

#define CONFIGURE_TASK_STACK_ALLOCATOR NULL

#define CONFIGURE_TASK_STACK_DEALLOCATOR NULL

#define CONFIGURE_IDLE_TASK_INITIALIZES_APPLICATION

#define CONFIGURE_IDLE_TASK_BODY NULL

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_INIT

#include <rtems/confdefs.h>