2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2014-08-09 04:43:02 +08:00
|
|
|
* sched/clock/clock_settime.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2009-12-15 02:39:29 +08:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <time.h>
|
2011-10-03 01:53:17 +08:00
|
|
|
#include <assert.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2011-10-02 06:09:00 +08:00
|
|
|
|
2015-02-13 22:41:34 +08:00
|
|
|
#include <nuttx/arch.h>
|
2016-02-14 22:17:46 +08:00
|
|
|
#include <nuttx/irq.h>
|
2023-11-14 19:52:53 +08:00
|
|
|
#include <sys/time.h>
|
2015-02-13 22:41:34 +08:00
|
|
|
|
2014-08-09 04:43:02 +08:00
|
|
|
#include "clock/clock.h"
|
2016-07-11 20:54:02 +08:00
|
|
|
#ifdef CONFIG_CLOCK_TIMEKEEPING
|
2016-07-11 06:14:25 +08:00
|
|
|
# include "clock/clock_timekeeping.h"
|
|
|
|
#endif
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Public Functions
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2024-06-05 19:59:23 +08:00
|
|
|
* Name: nxclock_settime
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clock Functions based on POSIX APIs
|
|
|
|
*
|
2024-06-05 19:59:23 +08:00
|
|
|
* CLOCK_REALTIME - POSIX demands this to be present. This is the wall
|
|
|
|
* time clock.
|
|
|
|
*
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
void nxclock_settime(clockid_t clock_id, FAR const struct timespec *tp)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2021-04-11 07:58:15 +08:00
|
|
|
#ifndef CONFIG_CLOCK_TIMEKEEPING
|
2014-09-11 06:36:25 +08:00
|
|
|
struct timespec bias;
|
2011-10-02 22:16:30 +08:00
|
|
|
irqstate_t flags;
|
2024-06-05 19:59:23 +08:00
|
|
|
# ifdef CONFIG_CLOCK_ADJTIME
|
2023-11-14 19:52:53 +08:00
|
|
|
const struct timeval zerodelta = {
|
|
|
|
0, 0
|
|
|
|
};
|
2024-06-05 19:59:23 +08:00
|
|
|
# endif
|
2023-11-14 19:52:53 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
/* Interrupts are disabled here so that the in-memory time
|
|
|
|
* representation and the RTC setting will be as close as
|
|
|
|
* possible.
|
2011-05-07 05:10:00 +08:00
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
flags = enter_critical_section();
|
2014-09-11 06:36:25 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
/* Get the elapsed time since power up (in milliseconds). This is a
|
|
|
|
* bias value that we need to use to correct the base time.
|
|
|
|
*/
|
2014-09-11 06:36:25 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
clock_systime_timespec(&bias);
|
|
|
|
clock_timespec_subtract(tp, &bias, &g_basetime);
|
2014-09-11 06:36:25 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
leave_critical_section(flags);
|
2011-05-07 05:10:00 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
/* Setup the RTC (lo- or high-res) */
|
2011-05-07 05:10:00 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
# ifdef CONFIG_RTC
|
|
|
|
if (g_rtc_enabled)
|
|
|
|
{
|
|
|
|
up_rtc_settime(tp);
|
|
|
|
}
|
|
|
|
# endif
|
2019-10-25 00:49:28 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
# ifdef CONFIG_CLOCK_ADJTIME
|
|
|
|
/* Cancel any ongoing adjustment */
|
2023-11-14 19:52:53 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
adjtime(&zerodelta, NULL);
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
clock_timekeeping_set_wall_time(tp);
|
2023-04-24 19:45:56 +08:00
|
|
|
#endif
|
2024-06-05 19:59:23 +08:00
|
|
|
}
|
2023-04-24 19:45:56 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: clock_settime
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clock Functions based on POSIX APIs
|
|
|
|
*
|
|
|
|
* CLOCK_REALTIME - POSIX demands this to be present. This is the wall
|
|
|
|
* time clock.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
int clock_settime(clockid_t clock_id, FAR const struct timespec *tp)
|
|
|
|
{
|
|
|
|
if (clock_id != CLOCK_REALTIME || tp == NULL ||
|
|
|
|
tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000)
|
2011-05-07 05:10:00 +08:00
|
|
|
{
|
2011-09-12 01:48:52 +08:00
|
|
|
set_errno(EINVAL);
|
2024-06-05 19:59:23 +08:00
|
|
|
return ERROR;
|
2011-05-07 05:10:00 +08:00
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2024-06-05 19:59:23 +08:00
|
|
|
nxclock_settime(clock_id, tp);
|
|
|
|
return OK;
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|