|
| 1 | +/**************************************************************************** |
| 2 | + * sched/clock/clock_adjtime.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/config.h> |
| 26 | + |
| 27 | +#ifdef CONFIG_CLOCK_ADJTIME |
| 28 | + |
| 29 | +#include <sys/time.h> |
| 30 | +#include <stdint.h> |
| 31 | +#include <time.h> |
| 32 | +#include <errno.h> |
| 33 | +#include <debug.h> |
| 34 | + |
| 35 | +#include <nuttx/irq.h> |
| 36 | +#include <nuttx/arch.h> |
| 37 | + |
| 38 | +#include "clock/clock.h" |
| 39 | + |
| 40 | +/**************************************************************************** |
| 41 | + * Pre-processor Definitions |
| 42 | + ****************************************************************************/ |
| 43 | + |
| 44 | +/* Current adjtime implementation uses periodic clock tick to adjust clock |
| 45 | + * period. Therefore this implementation will not work when tickless mode |
| 46 | + * is enabled by CONFIG_SCHED_TICKLESS=y |
| 47 | + */ |
| 48 | + |
| 49 | +#ifdef CONFIG_SCHED_TICKLESS |
| 50 | +# error CONFIG_CLOCK_ADJTIME is not supported when CONFIG_SCHED_TICKLESS \ |
| 51 | + is enabled! |
| 52 | +#endif |
| 53 | + |
| 54 | +/* Set slew limit. In real time systems we don't want the time to adjust |
| 55 | + * too quickly. ADJTIME_SLEWLIMIT defines how many seconds can time change |
| 56 | + * during each clock. |
| 57 | + */ |
| 58 | + |
| 59 | +#define ADJTIME_SLEWLIMIT (CONFIG_CLOCK_ADJTIME_SLEWLIMIT * 0.01) |
| 60 | + |
| 61 | +/* Define system clock adjustment period. */ |
| 62 | + |
| 63 | +#define ADJTIME_PERIOD (CONFIG_CLOCK_ADJTIME_PERIOD * 0.01) |
| 64 | + |
| 65 | +/**************************************************************************** |
| 66 | + * Private Data |
| 67 | + ****************************************************************************/ |
| 68 | + |
| 69 | +/**************************************************************************** |
| 70 | + * Private Functions |
| 71 | + ****************************************************************************/ |
| 72 | + |
| 73 | +/**************************************************************************** |
| 74 | + * Public Functions |
| 75 | + ****************************************************************************/ |
| 76 | + |
| 77 | +/**************************************************************************** |
| 78 | + * Name: adjtime |
| 79 | + * |
| 80 | + * Description: |
| 81 | + * The adjtime() function gradually adjusts the system clock (as returned |
| 82 | + * by gettimeofday(2)). The amount of time by which the clock is to be |
| 83 | + * adjusted is specified in the structure pointed to by delta. |
| 84 | + * |
| 85 | + * This structure has the following form: |
| 86 | + * |
| 87 | + * struct timeval |
| 88 | + * { |
| 89 | + * time_t tv_sec; (seconds) |
| 90 | + * long tv_usec; (microseconds) |
| 91 | + * }; |
| 92 | + * |
| 93 | + * If the adjustment in delta is positive, then the system clock is |
| 94 | + * speeded up by some small percentage until the adjustment has been |
| 95 | + * completed. If the adjustment in delta is negative, then the clock is |
| 96 | + * slowed down in a similar fashion. |
| 97 | + * |
| 98 | + * If a clock adjustment from an earlier adjtime() call is already in |
| 99 | + * progress at the time of a later adjtime() call, and delta is not NULL |
| 100 | + * for the later call, then the earlier adjustment is stopped, but any |
| 101 | + * already completed part of that adjustment is not undone. |
| 102 | + * |
| 103 | + * If olddelta is not NULL, then the buffer that it points to is used to |
| 104 | + * return the amount of time remaining from any previous adjustment that |
| 105 | + * has not yet been completed. |
| 106 | + * |
| 107 | + * NOTE: This is not a POSIX interface but derives from 4.3BSD, System V. |
| 108 | + * It is also supported for Linux compatibility. |
| 109 | + * |
| 110 | + ****************************************************************************/ |
| 111 | + |
| 112 | +int adjtime(FAR const struct timeval *delta, FAR struct timeval *olddelta) |
| 113 | +{ |
| 114 | + irqstate_t flags; |
| 115 | + long long adjust_usec; |
| 116 | + long long period_usec; |
| 117 | + long long adjust_usec_old; |
| 118 | + long long count; /* Number of cycles over which |
| 119 | + * we adjust the period |
| 120 | + */ |
| 121 | + long long incr; /* Period increment applied on |
| 122 | + * every cycle. |
| 123 | + */ |
| 124 | + long long count_old; /* Previous number of cycles over which |
| 125 | + * we adjust the period |
| 126 | + */ |
| 127 | + long long incr_old; /* Previous period increment applied on |
| 128 | + * every cycle. |
| 129 | + */ |
| 130 | + long long incr_limit; |
| 131 | + int is_negative; |
| 132 | + |
| 133 | + is_negative = 0; |
| 134 | + |
| 135 | + if (!delta) |
| 136 | + { |
| 137 | + set_errno(EINVAL); |
| 138 | + return -1; |
| 139 | + } |
| 140 | + |
| 141 | + flags = enter_critical_section(); |
| 142 | + |
| 143 | + adjust_usec = (long long)delta->tv_sec * USEC_PER_SEC + delta->tv_usec; |
| 144 | + |
| 145 | + if (adjust_usec < 0) |
| 146 | + { |
| 147 | + adjust_usec = -adjust_usec; |
| 148 | + is_negative = 1; |
| 149 | + } |
| 150 | + |
| 151 | + /* Get period in usec. Target hardware has to provide support for |
| 152 | + * this function call. |
| 153 | + */ |
| 154 | + |
| 155 | + up_get_timer_period(&period_usec); |
| 156 | + |
| 157 | + /* Determine how much we want to adjust timer period and the number |
| 158 | + * of cycles over which we want to do the adjustment. |
| 159 | + */ |
| 160 | + |
| 161 | + count = (USEC_PER_SEC * ADJTIME_PERIOD) / period_usec; |
| 162 | + incr = adjust_usec / count; |
| 163 | + |
| 164 | + /* Compute maximum possible period increase and check |
| 165 | + * whether previously computed increase exceeds the maximum |
| 166 | + * one. |
| 167 | + */ |
| 168 | + |
| 169 | + incr_limit = ADJTIME_SLEWLIMIT * period_usec; |
| 170 | + if (incr > incr_limit) |
| 171 | + { |
| 172 | + /* It does... limit computed increase and increment count. */ |
| 173 | + |
| 174 | + incr = incr_limit; |
| 175 | + count = adjust_usec / incr; |
| 176 | + } |
| 177 | + |
| 178 | + if (is_negative == 1) |
| 179 | + { |
| 180 | + /* Positive or negative? */ |
| 181 | + |
| 182 | + incr = -incr; |
| 183 | + } |
| 184 | + |
| 185 | + /* Ignore small differences. */ |
| 186 | + |
| 187 | + if (incr == 0) |
| 188 | + { |
| 189 | + count = 0; |
| 190 | + } |
| 191 | + |
| 192 | + leave_critical_section(flags); |
| 193 | + |
| 194 | + /* Initialize clock adjustment and get old adjust values. */ |
| 195 | + |
| 196 | + clock_set_adjust(incr, count, &incr_old, &count_old); |
| 197 | + |
| 198 | + adjust_usec_old = count_old * incr_old; |
| 199 | + if (olddelta) |
| 200 | + { |
| 201 | + olddelta->tv_sec = adjust_usec_old / USEC_PER_SEC; |
| 202 | + olddelta->tv_usec = adjust_usec_old; |
| 203 | + } |
| 204 | + |
| 205 | + return OK; |
| 206 | +} |
| 207 | + |
| 208 | +#endif /* CONFIG_CLOCK_ADJTIME */ |
0 commit comments