Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:49

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (C) 2015, 2019 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include "tmacros.h"
0033 
0034 #include <assert.h>
0035 
0036 #include <bsp/bootcard.h>
0037 
0038 #include <rtems/score/timecounterimpl.h>
0039 #include <rtems/score/percpu.h>
0040 #include <rtems/score/todimpl.h>
0041 #include <rtems/timecounter.h>
0042 #include <rtems/bsd.h>
0043 
0044 const char rtems_test_name[] = "SPTIMECOUNTER 1";
0045 
0046 #define TEST_QUAL 1234
0047 
0048 #define TEST_FREQ 1000000
0049 
0050 typedef struct {
0051   struct timecounter tc;
0052   uint32_t counter;
0053   struct timecounter tc_2;
0054   uint32_t counter_2;
0055 } test_context;
0056 
0057 static test_context test_instance;
0058 
0059 static Thread_Control executing;
0060 
0061 static uint32_t test_get_timecount(struct timecounter *tc)
0062 {
0063   test_context *ctx;
0064 
0065   ctx = RTEMS_CONTAINER_OF(tc, test_context, tc);
0066   ++ctx->counter;
0067 
0068   return ctx->counter;
0069 }
0070 
0071 static uint32_t test_get_timecount_2(struct timecounter *tc)
0072 {
0073   test_context *ctx;
0074 
0075   ctx = RTEMS_CONTAINER_OF(tc, test_context, tc_2);
0076   ++ctx->counter_2;
0077 
0078   return ctx->counter_2;
0079 }
0080 
0081 static void test_install(test_context *ctx)
0082 {
0083   struct timecounter *tc;
0084   struct timecounter *tc_2;
0085   uint32_t c;
0086   uint32_t c_2;
0087 
0088   tc = &ctx->tc;
0089   tc_2 = &ctx->tc_2;
0090   c = ctx->counter;
0091   c_2 = ctx->counter_2;
0092 
0093   tc_2->tc_get_timecount = test_get_timecount_2;
0094   tc_2->tc_counter_mask = 0x0fffffff;
0095   tc_2->tc_frequency = TEST_FREQ - 1;
0096   tc_2->tc_quality = TEST_QUAL;
0097   _Timecounter_Install(tc_2);
0098   assert(ctx->counter == c);
0099   assert(ctx->counter_2 == c_2);
0100 
0101   tc_2->tc_get_timecount = test_get_timecount_2;
0102   tc_2->tc_counter_mask = 0x0fffffff;
0103   tc_2->tc_frequency = TEST_FREQ - 1;
0104   tc_2->tc_quality = TEST_QUAL + 1;
0105   _Timecounter_Install(tc_2);
0106   assert(ctx->counter == c + 1);
0107   assert(ctx->counter_2 == c_2 + 1);
0108 
0109   tc->tc_get_timecount = test_get_timecount;
0110   tc->tc_counter_mask = 0x0fffffff;
0111   tc->tc_frequency = TEST_FREQ;
0112   tc->tc_quality = TEST_QUAL + 1;
0113   _Timecounter_Install(tc);
0114   assert(ctx->counter == c + 2);
0115   assert(ctx->counter_2 == c_2 + 2);
0116 }
0117 
0118 void boot_card(const char *cmdline)
0119 {
0120   test_context *ctx;
0121   struct timecounter *tc;
0122   struct bintime bt;
0123   struct timeval tv;
0124   struct timespec ts;
0125   Per_CPU_Control *cpu_self;
0126 
0127   ctx = &test_instance;
0128   tc = &ctx->tc;
0129 
0130   TEST_BEGIN();
0131 
0132   cpu_self = _Per_CPU_Get();
0133   cpu_self->executing = &executing;
0134   cpu_self->heir = &executing;
0135 
0136   assert(time(NULL) == TOD_SECONDS_1970_THROUGH_1988);
0137 
0138   rtems_bsd_bintime(&bt);
0139   assert(bt.sec == TOD_SECONDS_1970_THROUGH_1988);
0140   assert(bt.frac == 0);
0141 
0142   rtems_bsd_getbintime(&bt);
0143   assert(bt.sec == TOD_SECONDS_1970_THROUGH_1988);
0144   assert(bt.frac == 0);
0145 
0146   rtems_bsd_microtime(&tv);
0147   assert(tv.tv_sec == TOD_SECONDS_1970_THROUGH_1988);
0148   assert(tv.tv_usec == 0);
0149 
0150   rtems_bsd_getmicrotime(&tv);
0151   assert(tv.tv_sec == TOD_SECONDS_1970_THROUGH_1988);
0152   assert(tv.tv_usec == 0);
0153 
0154   rtems_bsd_nanotime(&ts);
0155   assert(ts.tv_sec == TOD_SECONDS_1970_THROUGH_1988);
0156   assert(ts.tv_nsec == 0);
0157 
0158   rtems_bsd_getnanotime(&ts);
0159   assert(ts.tv_sec == TOD_SECONDS_1970_THROUGH_1988);
0160   assert(ts.tv_nsec == 0);
0161 
0162   assert(rtems_clock_get_uptime_seconds() == 0);
0163   assert(rtems_clock_get_uptime_nanoseconds() == 0);
0164 
0165   rtems_bsd_binuptime(&bt);
0166   assert(bt.sec == 1);
0167   assert(bt.frac == 0);
0168 
0169   rtems_bsd_getbinuptime(&bt);
0170   assert(bt.sec == 1);
0171   assert(bt.frac == 0);
0172 
0173   rtems_bsd_microuptime(&tv);
0174   assert(tv.tv_sec == 1);
0175   assert(tv.tv_usec == 0);
0176 
0177   rtems_bsd_getmicrouptime(&tv);
0178   assert(tv.tv_sec == 1);
0179   assert(tv.tv_usec == 0);
0180 
0181   rtems_bsd_nanouptime(&ts);
0182   assert(ts.tv_sec == 1);
0183   assert(ts.tv_nsec == 0);
0184 
0185   rtems_bsd_getnanouptime(&ts);
0186   assert(ts.tv_sec == 1);
0187   assert(ts.tv_nsec == 0);
0188 
0189   /* On RTEMS time does not advance using the dummy timecounter */
0190   rtems_bsd_binuptime(&bt);
0191   assert(bt.sec == 1);
0192   assert(bt.frac == 0);
0193 
0194   rtems_timecounter_tick();
0195   rtems_bsd_binuptime(&bt);
0196   assert(bt.sec == 1);
0197   assert(bt.frac == 0);
0198 
0199   ctx->counter = 0;
0200   tc->tc_get_timecount = test_get_timecount;
0201   tc->tc_counter_mask = 0x0fffffff;
0202   tc->tc_frequency = TEST_FREQ;
0203   tc->tc_quality = TEST_QUAL;
0204   _Timecounter_Install(tc);
0205   assert(ctx->counter == 1);
0206 
0207   rtems_bsd_binuptime(&bt);
0208   assert(ctx->counter == 2);
0209 
0210   assert(bt.sec == 1);
0211   assert(bt.frac == 18446744073708);
0212 
0213   ctx->counter = 0xf0000000 | 1;
0214   rtems_bsd_binuptime(&bt);
0215   assert(ctx->counter == (0xf0000000 | 2));
0216 
0217   assert(bt.sec == 1);
0218   assert(bt.frac == 18446744073708);
0219 
0220   /* Check that a large delta yields a correct time */
0221   ctx->counter = (0xf0000000 | 1) + TEST_FREQ;
0222   rtems_bsd_binuptime(&bt);
0223   assert(ctx->counter == (0xf0000000 | 2) + TEST_FREQ);
0224   assert(bt.sec == 2);
0225   assert(bt.frac == 18446742522092);
0226 
0227   test_install(ctx);
0228 
0229   TEST_END();
0230 
0231   _Terminate(RTEMS_FATAL_SOURCE_EXIT, 0);
0232 }
0233 
0234 static void *stack_allocate(size_t size)
0235 {
0236   (void) size;
0237   return NULL;
0238 }
0239 
0240 static void stack_free(void *ptr)
0241 {
0242   (void) ptr;
0243 }
0244 
0245 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0246 
0247 #define CONFIGURE_APPLICATION_DISABLE_FILESYSTEM
0248 
0249 #define CONFIGURE_DISABLE_NEWLIB_REENTRANCY
0250 
0251 #define CONFIGURE_SCHEDULER_USER
0252 
0253 #define CONFIGURE_SCHEDULER
0254 
0255 #define CONFIGURE_SCHEDULER_TABLE_ENTRIES { }
0256 
0257 #define CONFIGURE_TASK_STACK_ALLOCATOR stack_allocate
0258 
0259 #define CONFIGURE_TASK_STACK_DEALLOCATOR stack_free
0260 
0261 #define CONFIGURE_IDLE_TASK_INITIALIZES_APPLICATION
0262 
0263 #define CONFIGURE_IDLE_TASK_BODY NULL
0264 
0265 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0266 
0267 #define CONFIGURE_INIT
0268 
0269 #include <rtems/confdefs.h>