diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2021-04-12 07:53:48 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2021-07-12 14:01:24 +0200 |
commit | f4c60bd1950b47aac0f7f3b38fe4965d1e679120 (patch) | |
tree | 57d8aa0f274a7eaf63187cb44da85af727519607 | |
parent | ea4b0ff97428553ef89b02632d36bc3f582b214b (diff) |
validation: Add software timecounter support
-rw-r--r-- | spec/build/testsuites/validation/libvalidation.yml | 1 | ||||
-rw-r--r-- | testsuites/validation/tx-support.h | 33 | ||||
-rw-r--r-- | testsuites/validation/tx-timecounter.c | 115 |
3 files changed, 149 insertions, 0 deletions
diff --git a/spec/build/testsuites/validation/libvalidation.yml b/spec/build/testsuites/validation/libvalidation.yml index cd661e16e0..47651d4284 100644 --- a/spec/build/testsuites/validation/libvalidation.yml +++ b/spec/build/testsuites/validation/libvalidation.yml @@ -15,6 +15,7 @@ source: - testsuites/validation/tx-interrupt.c - testsuites/validation/tx-memory-alloc.c - testsuites/validation/tx-support.c +- testsuites/validation/tx-timecounter.c - testsuites/validation/tx-thread-queue.c target: validation type: build diff --git a/testsuites/validation/tx-support.h b/testsuites/validation/tx-support.h index 32f02564ba..7b76ffc023 100644 --- a/testsuites/validation/tx-support.h +++ b/testsuites/validation/tx-support.h @@ -107,6 +107,39 @@ struct _Thread_Control *GetThread( rtems_id id ); void ClockTick( void ); +typedef uint32_t ( *GetTimecountHandler )( void ); + +/** + * @brief Sets the get timecount handler. + * + * Using this function will replace the timecounter of the clock driver. + * + * @return Returns the previous get timecount handler. + */ +GetTimecountHandler SetGetTimecountHandler( GetTimecountHandler handler ); + +/** + * @brief This constant represents the fake frequency of the software + * timecounter. + */ +#define SOFTWARE_TIMECOUNTER_FREQUENCY 1000000 + +/** + * @brief Gets the software timecount counter value. + * + * @return Returns the current software timecounter counter value. + */ +uint32_t GetTimecountCounter( void ); + +/** + * @brief Sets and gets the software timecount counter value. + * + * @param counter is the new software timecounter counter value. + * + * @return Returns the previous software timecounter counter value. + */ +uint32_t SetTimecountCounter( uint32_t counter ); + /** * @brief Fails a dynamic memory allocation when the counter reaches zero. * diff --git a/testsuites/validation/tx-timecounter.c b/testsuites/validation/tx-timecounter.c new file mode 100644 index 0000000000..cecbddf0f0 --- /dev/null +++ b/testsuites/validation/tx-timecounter.c @@ -0,0 +1,115 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +/** + * @file + * + * @ingroup RTEMSTestSuites + * + * @brief This source file contains the definition of SetGetTimecountHandler(), + * GetTimecountCounter(), and SetTimecountCounter(). + */ + +/* + * Copyright (C) 2021 embedded brains GmbH (http://www.embedded-brains.de) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "tx-support.h" + +#include <rtems/sysinit.h> +#include <rtems/timecounter.h> +#include <rtems/score/atomic.h> + +typedef struct { + struct timecounter base; + GetTimecountHandler handler; + Atomic_Ulong counter; +} TimecounterControl; + +static TimecounterControl TimecounterInstance; + +GetTimecountHandler SetGetTimecountHandler( GetTimecountHandler handler ) +{ + GetTimecountHandler previous; + + previous = TimecounterInstance.handler; + TimecounterInstance.handler = handler; + return previous; +} + +uint32_t GetTimecountCounter( void ) +{ + return (uint32_t) _Atomic_Load_ulong( + &TimecounterInstance.counter, + ATOMIC_ORDER_RELAXED + ); +} + +uint32_t SetTimecountCounter( uint32_t counter ) +{ + return (uint32_t) _Atomic_Exchange_ulong( + &TimecounterInstance.counter, + counter, + ATOMIC_ORDER_RELAXED + ); +} + +static uint32_t GetTimecountSoftware( void ) +{ + return (uint32_t) _Atomic_Fetch_add_ulong( + &TimecounterInstance.counter, + 1, + ATOMIC_ORDER_RELAXED + ); +} + +static uint32_t GetTimecountWrapper( struct timecounter *tc ) +{ + TimecounterControl *self; + + self = (TimecounterControl *) tc; + return ( *self->handler )(); +} + +static void InstallTimecounter( void ) +{ + TimecounterControl *self; + + self = &TimecounterInstance; + self->handler = GetTimecountSoftware; + self->base.tc_get_timecount = GetTimecountWrapper; + self->base.tc_counter_mask = 0xffffffff; + self->base.tc_frequency = SOFTWARE_TIMECOUNTER_FREQUENCY; + self->base.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER + 1; + rtems_timecounter_install( &self->base ); +} + +RTEMS_SYSINIT_ITEM( + InstallTimecounter, + RTEMS_SYSINIT_DEVICE_DRIVERS, + RTEMS_SYSINIT_ORDER_LAST +); |