diff --git a/arch/Kconfig b/arch/Kconfig index ce5bac91524..7f97c835cd2 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -67,6 +67,7 @@ config ARCH_RISCV config ARCH_SIM bool "Simulation" select ARCH_HAVE_MULTICPU + select ARCH_HAVE_RTC_SUBSECONDS select ARCH_HAVE_TLS select ARCH_HAVE_TICKLESS select ARCH_HAVE_POWEROFF diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile index 5204949c8a5..69781b01b8e 100644 --- a/arch/sim/src/Makefile +++ b/arch/sim/src/Makefile @@ -109,6 +109,10 @@ ifeq ($(CONFIG_ONESHOT),y) CSRCS += up_oneshot.c endif +ifeq ($(CONFIG_RTC_DRIVER),y) + CSRCS += up_rtc.c +endif + ifeq ($(CONFIG_NX_LCDDRIVER),y) CSRCS += up_lcd.c else @@ -124,15 +128,13 @@ ifeq ($(CONFIG_SIM_TOUCHSCREEN),y) REQUIREDOBJS += up_touchscreen$(OBJEXT) HOSTCFLAGS += -DCONFIG_SIM_TOUCHSCREEN=1 HOSTSRCS += up_x11eventloop.c -else -ifeq ($(CONFIG_SIM_AJOYSTICK),y) +else ifeq ($(CONFIG_SIM_AJOYSTICK),y) CSRCS += up_ajoystick.c HOSTCFLAGS += -DCONFIG_SIM_AJOYSTICK=1 HOSTSRCS += up_x11eventloop.c endif endif endif -endif ifeq ($(CONFIG_SIM_IOEXPANDER),y) CSRCS += up_ioexpander.c diff --git a/arch/sim/src/nuttx-names.dat b/arch/sim/src/nuttx-names.dat index cd89d1739e9..d359e45b417 100644 --- a/arch/sim/src/nuttx-names.dat +++ b/arch/sim/src/nuttx-names.dat @@ -8,6 +8,7 @@ chdir NXchdir clearenv NXclearenv clock NXclock clock_gettime NXclock_gettime +clock_settime NXclock_settime close NXclose closedir NXclosedir connect NXconnect diff --git a/arch/sim/src/sim/up_rtc.c b/arch/sim/src/sim/up_rtc.c new file mode 100644 index 00000000000..838d2056b79 --- /dev/null +++ b/arch/sim/src/sim/up_rtc.c @@ -0,0 +1,127 @@ +/**************************************************************************** + * arch/sim/src/sim/up_rtc.c + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include "up_internal.h" + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int sim_rtc_rdtime(FAR struct rtc_lowerhalf_s *lower, + FAR struct rtc_time *rtctime); +static int sim_rtc_settime(FAR struct rtc_lowerhalf_s *lower, + FAR const struct rtc_time *rtctime); +static bool sim_rtc_havesettime(FAR struct rtc_lowerhalf_s *lower); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct rtc_ops_s g_sim_rtc_ops = +{ + .rdtime = sim_rtc_rdtime, + .settime = sim_rtc_settime, + .havesettime = sim_rtc_havesettime, +}; + +static struct rtc_lowerhalf_s g_sim_rtc = +{ + .ops = &g_sim_rtc_ops, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int sim_rtc_rdtime(FAR struct rtc_lowerhalf_s *lower, + FAR struct rtc_time *rtctime) +{ + uint64_t nsec; + time_t sec; + + + nsec = host_gettime(true); + sec = nsec / NSEC_PER_SEC; + nsec -= sec * NSEC_PER_SEC; + + gmtime_r(&sec, (FAR struct tm *)rtctime); + rtctime->tm_nsec = nsec; + + return OK; +} + +static int sim_rtc_settime(FAR struct rtc_lowerhalf_s *lower, + FAR const struct rtc_time *rtctime) +{ + uint64_t nsec; + + nsec = mktime((FAR struct tm *)rtctime); + nsec *= NSEC_PER_SEC; + nsec += rtctime->tm_nsec; + host_settime(true, nsec); + + return OK; +} + +static bool sim_rtc_havesettime(FAR struct rtc_lowerhalf_s *lower) +{ + return true; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_rtc_initialize + * + * Description: + * Initialize the builtin, MCU hardware RTC per the selected + * configuration. This function is called once very early in the OS + * initialization sequence. + * + * NOTE that initialization of external RTC hardware that depends on the + * availability of OS resources (such as SPI or I2C) must be deferred + * until the system has fully booted. Other, RTC-specific initialization + * functions are used in that case. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) on success; a negated errno on failure + * + ****************************************************************************/ + +int up_rtc_initialize(void) +{ + up_rtc_set_lowerhalf(&g_sim_rtc); + return rtc_initialize(0, &g_sim_rtc); +}