You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/02/15 13:07:45 UTC

[incubator-nuttx] 03/03: arch/sim: Implement arch rtc interface on top of rtc driver

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr274
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 3332ec3fdf8b7cb2ba3fe6f2f83245912e28f98c
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Thu Feb 13 01:53:05 2020 +0800

    arch/sim: Implement arch rtc interface on top of rtc driver
    
    Change-Id: Icd35945e0c63fe46dd38d0d432007d0d2320a56a
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 arch/Kconfig                 |   1 +
 arch/sim/src/Makefile        |   8 ++-
 arch/sim/src/nuttx-names.dat |   1 +
 arch/sim/src/sim/up_rtc.c    | 127 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 134 insertions(+), 3 deletions(-)

diff --git a/arch/Kconfig b/arch/Kconfig
index ce5bac9..7f97c83 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 5204949..69781b0 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 cd89d17..d359e45 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 0000000..838d205
--- /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 <nuttx/config.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/timers/rtc.h>
+#include <nuttx/timers/arch_rtc.h>
+
+#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);
+}