clock_settime.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /****************************************************************************
  2. * sched/clock/clock_settime.c
  3. *
  4. * Copyright (C) 2007, 2009, 2011, 2014 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 <time.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <debug.h>
  43. #include <nuttx/arch.h>
  44. #include <nuttx/irq.h>
  45. #include "clock/clock.h"
  46. #ifdef CONFIG_CLOCK_TIMEKEEPING
  47. # include "clock/clock_timekeeping.h"
  48. #endif
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: clock_settime
  54. *
  55. * Description:
  56. * Clock Functions based on POSIX APIs
  57. *
  58. ****************************************************************************/
  59. int clock_settime(clockid_t clock_id, FAR const struct timespec *tp)
  60. {
  61. struct timespec bias;
  62. irqstate_t flags;
  63. int ret = OK;
  64. sinfo("clock_id=%d\n", clock_id);
  65. DEBUGASSERT(tp != NULL);
  66. /* CLOCK_REALTIME - POSIX demands this to be present. This is the wall
  67. * time clock.
  68. */
  69. if (clock_id == CLOCK_REALTIME)
  70. {
  71. #ifndef CONFIG_CLOCK_TIMEKEEPING
  72. /* Interrupts are disabled here so that the in-memory time
  73. * representation and the RTC setting will be as close as
  74. * possible.
  75. */
  76. flags = enter_critical_section();
  77. /* Get the elapsed time since power up (in milliseconds). This is a
  78. * bias value that we need to use to correct the base time.
  79. */
  80. (void)clock_systimespec(&bias);
  81. /* Save the new base time. */
  82. g_basetime.tv_sec = tp->tv_sec;
  83. g_basetime.tv_nsec = tp->tv_nsec;
  84. /* Subtract that bias from the basetime so that when the system
  85. * timer is again added to the base time, the result is the current
  86. * time relative to basetime.
  87. */
  88. if (g_basetime.tv_nsec < bias.tv_nsec)
  89. {
  90. g_basetime.tv_nsec += NSEC_PER_SEC;
  91. g_basetime.tv_sec--;
  92. }
  93. /* Result could be negative seconds */
  94. g_basetime.tv_nsec -= bias.tv_nsec;
  95. g_basetime.tv_sec -= bias.tv_sec;
  96. /* Setup the RTC (lo- or high-res) */
  97. #ifdef CONFIG_RTC
  98. if (g_rtc_enabled)
  99. {
  100. up_rtc_settime(tp);
  101. }
  102. #endif
  103. leave_critical_section(flags);
  104. sinfo("basetime=(%ld,%lu) bias=(%ld,%lu)\n",
  105. (long)g_basetime.tv_sec, (unsigned long)g_basetime.tv_nsec,
  106. (long)bias.tv_sec, (unsigned long)bias.tv_nsec);
  107. #else
  108. ret = clock_timekeeping_set_wall_time(tp);
  109. #endif
  110. }
  111. else
  112. {
  113. serr("Returning ERROR\n");
  114. set_errno(EINVAL);
  115. ret = ERROR;
  116. }
  117. return ret;
  118. }