2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:43:02 +02:00
|
|
|
* sched/clock/clock_settime.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 16:33:58 +01: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 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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 00:21:28 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <time.h>
|
2011-10-02 19:53:17 +02:00
|
|
|
#include <assert.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2011-10-02 00:09:00 +02:00
|
|
|
|
2015-02-13 15:41:34 +01:00
|
|
|
#include <nuttx/arch.h>
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2023-11-14 12:52:53 +01:00
|
|
|
#include <sys/time.h>
|
2015-02-13 15:41:34 +01:00
|
|
|
|
2014-08-08 22:43:02 +02:00
|
|
|
#include "clock/clock.h"
|
2016-07-11 14:54:02 +02:00
|
|
|
#ifdef CONFIG_CLOCK_TIMEKEEPING
|
2016-07-11 00:14:25 +02:00
|
|
|
# include "clock/clock_timekeeping.h"
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2024-06-05 13:59:23 +02:00
|
|
|
* Name: nxclock_settime
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clock Functions based on POSIX APIs
|
|
|
|
*
|
2024-06-05 13:59:23 +02:00
|
|
|
* CLOCK_REALTIME - POSIX demands this to be present. This is the wall
|
|
|
|
* time clock.
|
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
void nxclock_settime(clockid_t clock_id, FAR const struct timespec *tp)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2021-04-11 01:58:15 +02:00
|
|
|
#ifndef CONFIG_CLOCK_TIMEKEEPING
|
2014-09-11 00:36:25 +02:00
|
|
|
struct timespec bias;
|
2011-10-02 16:16:30 +02:00
|
|
|
irqstate_t flags;
|
2024-06-05 13:59:23 +02:00
|
|
|
# ifdef CONFIG_CLOCK_ADJTIME
|
2023-11-14 12:52:53 +01:00
|
|
|
const struct timeval zerodelta = {
|
|
|
|
0, 0
|
|
|
|
};
|
2024-06-05 13:59:23 +02:00
|
|
|
# endif
|
2023-11-14 12:52:53 +01:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
/* Interrupts are disabled here so that the in-memory time
|
|
|
|
* representation and the RTC setting will be as close as
|
|
|
|
* possible.
|
2011-05-06 23:10:00 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
flags = enter_critical_section();
|
2014-09-11 00:36:25 +02:00
|
|
|
|
2024-06-05 13:59:23 +02: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 00:36:25 +02:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
clock_systime_timespec(&bias);
|
|
|
|
clock_timespec_subtract(tp, &bias, &g_basetime);
|
2014-09-11 00:36:25 +02:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
leave_critical_section(flags);
|
2011-05-06 23:10:00 +02:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
/* Setup the RTC (lo- or high-res) */
|
2011-05-06 23:10:00 +02:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
# ifdef CONFIG_RTC
|
|
|
|
if (g_rtc_enabled)
|
|
|
|
{
|
|
|
|
up_rtc_settime(tp);
|
|
|
|
}
|
|
|
|
# endif
|
2019-10-24 18:49:28 +02:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
# ifdef CONFIG_CLOCK_ADJTIME
|
|
|
|
/* Cancel any ongoing adjustment */
|
2023-11-14 12:52:53 +01:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
adjtime(&zerodelta, NULL);
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
clock_timekeeping_set_wall_time(tp);
|
2023-04-24 13:45:56 +02:00
|
|
|
#endif
|
2024-06-05 13:59:23 +02:00
|
|
|
}
|
2023-04-24 13:45:56 +02:00
|
|
|
|
2024-06-05 13:59:23 +02: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 00:21:28 +01:00
|
|
|
|
2024-06-05 13:59:23 +02: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-06 23:10:00 +02:00
|
|
|
{
|
2011-09-11 19:48:52 +02:00
|
|
|
set_errno(EINVAL);
|
2024-06-05 13:59:23 +02:00
|
|
|
return ERROR;
|
2011-05-06 23:10:00 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-06-05 13:59:23 +02:00
|
|
|
nxclock_settime(clock_id, tp);
|
|
|
|
return OK;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|