summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/spcpucounter01/init.c
blob: d1c5489ad462ad2a9be56f5ac9e6c55cca735980 (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
/*
 * Copyright (c) 2014 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

#include <stdio.h>
#include <inttypes.h>

#include <rtems.h>
#include <rtems/counter.h>

#define TESTS_USE_PRINTF
#include "tmacros.h"

#define NS_PER_TICK 1000000

static rtems_interval sync_with_clock_tick(void)
{
  rtems_interval start = rtems_clock_get_ticks_since_boot();
  rtems_interval current;

  do {
    current = rtems_clock_get_ticks_since_boot();
  } while (current == start);

  return current;
}

static void test_converter(void)
{
  CPU_Counter_ticks frequency = rtems_counter_nanoseconds_to_ticks(1000000000);
  uint64_t ns = rtems_counter_ticks_to_nanoseconds(frequency);

  printf("CPU counter frequency: %" PRIu32 "Hz\n", frequency);
  printf("nanoseconds for frequency count ticks: %" PRIu64 "\n", ns);

  rtems_test_assert(ns == 1000000000);
}

static void test_delay_nanoseconds(void)
{
  rtems_counter_ticks start;
  rtems_counter_ticks end;
  rtems_counter_ticks delta;
  double ns_per_tick = NS_PER_TICK;
  uint64_t ns_delta;
  rtems_interval tick;
  int n = 10;
  int i;

  printf("test delay nanoseconds (%i times)\n", n);

  for (i = 0; i < n; ++i) {
    tick = sync_with_clock_tick();

    start = rtems_counter_read();
    rtems_counter_delay_nanoseconds(NS_PER_TICK);
    end = rtems_counter_read();

    rtems_test_assert(tick < rtems_clock_get_ticks_since_boot());

    delta = rtems_counter_difference(end, start);
    ns_delta = rtems_counter_ticks_to_nanoseconds(delta);

    /* Special case for CPU counters using the clock driver counter */
    if (ns_delta < rtems_configuration_get_nanoseconds_per_tick()) {
      printf(
        "warning: the RTEMS counter seems to be unable to\n"
        "  measure intervals greater than the clock tick interval\n"
      );

      ns_delta += rtems_configuration_get_nanoseconds_per_tick();
    }

    printf(
      "busy wait duration: %" PRIu64 "ns\n"
      "busy wait relative to clock tick: %f\n",
      ns_delta,
      (ns_delta - ns_per_tick) / ns_per_tick
    );
  }
}

static void Init(rtems_task_argument arg)
{
  puts("\n\n*** TEST SPCPUCOUNTER 1 ***");

  test_converter();
  test_delay_nanoseconds();

  puts("*** END OF TEST SPCPUCOUNTER 1 ***");

  rtems_test_exit(0);
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_MICROSECONDS_PER_TICK (NS_PER_TICK / 1000)

#define CONFIGURE_MAXIMUM_TASKS 1

#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>