clock_systime_ticks.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /****************************************************************************
  2. * sched/clock/clock_systime_ticks.c
  3. *
  4. * Copyright (C) 2011, 2014-2016, 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <nuttx/irq.h>
  41. #include <nuttx/arch.h>
  42. #include <nuttx/clock.h>
  43. #include "clock/clock.h"
  44. /****************************************************************************
  45. * Pre-processor Definitions
  46. ****************************************************************************/
  47. /* See nuttx/clock.h */
  48. #undef clock_systime_ticks
  49. /* 32-bit mask for 64-bit timer values */
  50. #define TIMER_MASK32 0x00000000ffffffff
  51. /****************************************************************************
  52. * Public Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: clock_systime_ticks
  56. *
  57. * Description:
  58. * Return the current value of the 32/64-bit system timer counter.
  59. *
  60. * Indirect access to the system timer counter is required through this
  61. * function if the execution environment does not have direct access to
  62. * kernel global data.
  63. *
  64. * Use of this function is also required to assure atomic access to the
  65. * 64-bit system timer.
  66. *
  67. * NOTE: This is an internal OS interface and should not be called from
  68. * application code. Rather, the functionally equivalent, standard
  69. * interface clock() should be used.
  70. *
  71. * Input Parameters:
  72. * None
  73. *
  74. * Returned Value:
  75. * The current value of the system timer counter
  76. *
  77. ****************************************************************************/
  78. clock_t clock_systime_ticks(void)
  79. {
  80. #ifdef CONFIG_SCHED_TICKLESS
  81. # ifdef CONFIG_SYSTEM_TIME64
  82. struct timespec ts;
  83. /* Get the time from the platform specific hardware */
  84. clock_systime_timespec(&ts);
  85. /* Convert to a 64-bit value in microseconds, then in clock tick units */
  86. return USEC2TICK(1000000 * (uint64_t)ts.tv_sec + ts.tv_nsec / 1000);
  87. # else /* CONFIG_SYSTEM_TIME64 */
  88. struct timespec ts;
  89. uint64_t tmp;
  90. /* Get the time from the platform specific hardware */
  91. clock_systime_timespec(&ts);
  92. /* Convert to a 64- then a 32-bit value */
  93. tmp = USEC2TICK(1000000 * (uint64_t)ts.tv_sec + ts.tv_nsec / 1000);
  94. return (clock_t)(tmp & TIMER_MASK32);
  95. # endif /* CONFIG_SYSTEM_TIME64 */
  96. #else /* CONFIG_SCHED_TICKLESS */
  97. # ifdef CONFIG_SYSTEM_TIME64
  98. clock_t sample;
  99. clock_t verify;
  100. /* 64-bit accesses are not atomic on most architectures. The following
  101. * loop samples the 64-bit timer twice and loops in the rare event that
  102. * there was 32-bit rollover between samples.
  103. *
  104. * If there is no 32-bit rollover, then:
  105. *
  106. * - The MS 32-bits of each sample will be the same, and
  107. * - The LS 32-bits of the second sample will be greater than or equal to
  108. * the LS 32-bits for the first sample.
  109. */
  110. do
  111. {
  112. verify = g_system_timer;
  113. sample = g_system_timer;
  114. }
  115. while ((sample & TIMER_MASK32) < (verify & TIMER_MASK32) ||
  116. (sample & ~TIMER_MASK32) != (verify & ~TIMER_MASK32));
  117. return sample;
  118. # else /* CONFIG_SYSTEM_TIME64 */
  119. /* Return the current system time */
  120. return g_system_timer;
  121. # endif /* CONFIG_SYSTEM_TIME64 */
  122. #endif /* CONFIG_SCHED_TICKLESS */
  123. }