Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSTestFrameworkImpl
0007  *
0008  * @brief This source file contains the implementation of
0009  *   T_get_one_clock_tick_busy().
0010  */
0011 
0012 /*
0013  * Copyright (C) 2014, 2020 embedded brains GmbH & Co. KG
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #include <rtems/test.h>
0042 
0043 #include <rtems.h>
0044 
0045 static uint_fast32_t
0046 T_estimate_busy_loop_maximum(void)
0047 {
0048     uint_fast32_t initial;
0049     uint_fast32_t units;
0050 
0051     initial = rtems_clock_get_ticks_since_boot();
0052     units = 0;
0053 
0054     while (initial == rtems_clock_get_ticks_since_boot()) {
0055         ++units;
0056     }
0057 
0058     return units;
0059 }
0060 
0061 static uint_fast32_t
0062 T_wait_for_tick_change(void)
0063 {
0064     uint_fast32_t initial;
0065     uint_fast32_t now;
0066 
0067     initial = rtems_clock_get_ticks_since_boot();
0068 
0069     do {
0070         now = rtems_clock_get_ticks_since_boot();
0071     } while (now == initial);
0072 
0073     return now;
0074 }
0075 
0076 uint_fast32_t
0077 T_get_one_clock_tick_busy(void)
0078 {
0079     uint_fast32_t last;
0080     uint_fast32_t now;
0081     uint_fast32_t a;
0082     uint_fast32_t b;
0083     uint_fast32_t m;
0084 
0085     /* Choose a lower bound */
0086     a = 1;
0087 
0088     /* Estimate an upper bound */
0089 
0090     T_wait_for_tick_change();
0091     b = 2 * T_estimate_busy_loop_maximum();
0092 
0093     while (true) {
0094         last = T_wait_for_tick_change();
0095         T_busy(b);
0096         now = rtems_clock_get_ticks_since_boot();
0097 
0098         if (now != last) {
0099             break;
0100         }
0101 
0102         b *= 2;
0103     }
0104 
0105     /* Find a good value */
0106     do {
0107         m = (a + b) / 2;
0108 
0109         last = T_wait_for_tick_change();
0110         T_busy(m);
0111         now = rtems_clock_get_ticks_since_boot();
0112 
0113         if (now != last) {
0114             b = m;
0115         } else {
0116             a = m;
0117         }
0118     } while (b - a > 1);
0119 
0120     return m;
0121 }