cwespressif commented on a change in pull request #3794:
URL: https://github.com/apache/incubator-nuttx/pull/3794#discussion_r646258113



##########
File path: arch/risc-v/src/esp32c3/esp32c3_rtc_lowerhalf.c
##########
@@ -0,0 +1,575 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_rtc_lowerhalf.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/spinlock.h>
+
+#include <sys/types.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/timers/rtc.h>
+
+#include "esp32c3_rtc.h"
+#include "hardware/esp32c3_tim.h"
+
+#ifdef CONFIG_RTC_DRIVER
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_ALARM
+struct esp32c3_cbinfo_s
+{
+  volatile rtc_alarm_callback_t cb;  /* Callback when the alarm expires */
+  volatile FAR void *priv;           /* Private argurment to accompany 
callback */
+};
+#endif
+
+/* This is the private type for the RTC state.  It must be cast compatible
+ * with struct rtc_lowerhalf_s.
+ */
+
+struct esp32c3_lowerhalf_s
+{
+  /* This is the contained reference to the read-only, lower-half
+   * operations vtable (which may lie in FLASH or ROM)
+   */
+
+  FAR const struct rtc_ops_s *ops;
+#ifdef CONFIG_RTC_ALARM
+  /* Alarm callback information */
+
+  struct esp32c3_cbinfo_s cbinfo[RTC_ALARM_LAST];
+#endif
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Prototypes for static methods in struct rtc_ops_s */
+
+static int rtc_rdtime(FAR struct rtc_lowerhalf_s *lower,
+                      FAR struct rtc_time *rtctime);
+static int rtc_settime(FAR struct rtc_lowerhalf_s *lower,
+                       FAR const struct rtc_time *rtctime);
+static bool rtc_havesettime(FAR struct rtc_lowerhalf_s *lower);
+
+#ifdef CONFIG_RTC_ALARM
+static void rtc_alarm_callback(FAR void *arg, unsigned int alarmid);
+static int rtc_setalarm(FAR struct rtc_lowerhalf_s *lower,
+                        FAR const struct lower_setalarm_s *alarminfo);
+static int rtc_setrelative(FAR struct rtc_lowerhalf_s *lower,
+                           FAR const struct lower_setrelative_s *alarminfo);
+static int rtc_cancelalarm(FAR struct rtc_lowerhalf_s *lower,
+                           int alarmid);
+static int rtc_rdalarm(FAR struct rtc_lowerhalf_s *lower,
+                       FAR struct lower_rdalarm_s *alarminfo);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* ESP32-C3 RTC driver operations */
+
+static const struct rtc_ops_s g_rtc_ops =
+{
+  .rdtime      = rtc_rdtime,
+  .settime     = rtc_settime,
+  .havesettime = rtc_havesettime,
+#ifdef CONFIG_RTC_ALARM
+  .setalarm    = rtc_setalarm,
+  .setrelative = rtc_setrelative,
+  .cancelalarm = rtc_cancelalarm,
+  .rdalarm     = rtc_rdalarm,
+#endif
+#ifdef CONFIG_RTC_PERIODIC
+  .setperiodic    = NULL,
+  .cancelperiodic = NULL,
+#endif
+#ifdef CONFIG_RTC_IOCTL
+  .ioctl       = NULL,
+#endif
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  .destroy     = NULL,
+#endif
+};
+
+/* ESP32-C3 RTC device state */
+
+static struct esp32c3_lowerhalf_s g_rtc_lowerhalf =
+{
+  .ops        = &g_rtc_ops,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: rtc_alarm_callback
+ *
+ * Description:
+ *   This is the function that is called from the RTC driver when the alarm
+ *   goes off. It just invokes the upper half drivers callback.
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_ALARM
+static void rtc_alarm_callback(FAR void *arg, unsigned int alarmid)
+{
+  FAR struct esp32c3_lowerhalf_s *lower;
+  FAR struct esp32c3_cbinfo_s *cbinfo;
+  rtc_alarm_callback_t cb;
+  FAR void *priv;
+
+  DEBUGASSERT((RTC_ALARM0 <= alarmid) && (alarmid < RTC_ALARM_LAST));
+
+  lower        = (struct esp32c3_lowerhalf_s *)arg;
+  cbinfo       = &lower->cbinfo[alarmid];
+
+  /* Sample and clear the callback information to minimize the window in
+   * time in which race conditions can occur.
+   */
+
+  cb           = (rtc_alarm_callback_t)cbinfo->cb;
+  priv         = (FAR void *)cbinfo->priv;
+
+  cbinfo->cb   = NULL;
+  cbinfo->priv = NULL;
+
+  /* Perform the callback */
+
+  if (cb != NULL)
+    {
+      cb(priv, alarmid);
+    }
+}
+#endif /* CONFIG_RTC_ALARM */
+
+/****************************************************************************
+ * Name: rtc_rdtime
+ *
+ * Description:
+ *   Implements the rdtime() method of the RTC driver interface
+ *
+ * Input Parameters:
+ *   lower   - A reference to RTC lower half driver state structure
+ *   rcttime - The location in which to return the current RTC time.
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; a negated errno value is returned
+ *   on any failure.
+ *
+ ****************************************************************************/
+
+static int rtc_rdtime(FAR struct rtc_lowerhalf_s *lower,
+                      FAR struct rtc_time *rtctime)
+{
+#if defined(CONFIG_RTC_DATETIME)
+  /* This operation depends on the fact that struct rtc_time is cast
+   * compatible with struct tm.
+   */
+
+  return up_rtc_getdatetime((FAR struct tm *)rtctime);
+
+#elif defined(CONFIG_RTC_HIRES)
+  FAR struct timespec ts;
+  int ret;
+
+  /* Get the higher resolution time */
+
+  ret = up_rtc_gettime(&ts);
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  /* Convert the one second epoch time to a struct tm.  This operation
+   * depends on the fact that struct rtc_time and struct tm are cast
+   * compatible.
+   */
+
+  if (!gmtime_r(&ts.tv_sec, (FAR struct tm *)rtctime))
+    {
+      ret = -get_errno();
+      goto errout;
+    }
+
+  return OK;
+
+errout:
+  DEBUGASSERT(ret < 0);
+  return ret;
+
+#else
+  time_t timer;
+
+  /* The resolution of time is only 1 second */
+
+  timer = up_rtc_time();
+
+  /* Convert the one second epoch time to a struct tm */
+
+  if (gmtime_r(&timer, (FAR struct tm *)rtctime) == 0)
+    {
+      int errcode = get_errno();
+      DEBUGASSERT(errcode > 0);
+
+      rtcerr("ERROR: gmtime_r failed: %d\n", errcode);
+      return -errcode;
+    }
+
+  return OK;
+#endif
+}
+
+/****************************************************************************
+ * Name: rtc_settime
+ *
+ * Description:
+ *   Implements the settime() method of the RTC driver interface
+ *
+ * Input Parameters:
+ *   lower   - A reference to RTC lower half driver state structure
+ *   rcttime - The new time to set
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; a negated errno value is returned
+ *   on any failure.
+ *
+ ****************************************************************************/
+
+static int rtc_settime(FAR struct rtc_lowerhalf_s *lower,
+                       FAR const struct rtc_time *rtctime)
+{
+  struct timespec ts;
+
+  /* Convert the struct rtc_time to a time_t.  Here we assume that struct
+   * rtc_time is cast compatible with struct tm.
+   */
+
+  ts.tv_sec  = mktime((FAR struct tm *)rtctime);
+  ts.tv_nsec = 0;
+
+  /* Now set the time (to one second accuracy) */
+
+  return up_rtc_settime(&ts);
+}
+
+/****************************************************************************
+ * Name: rtc_havesettime
+ *
+ * Description:
+ *   Implements the havesettime() method of the RTC driver interface
+ *
+ * Input Parameters:
+ *   lower - A reference to RTC lower half driver state structure
+ *
+ * Returned Value:
+ *   Returns true if RTC date-time have been previously set.
+ *
+ ****************************************************************************/
+
+static bool rtc_havesettime(FAR struct rtc_lowerhalf_s *lower)
+{
+  if (esp32c3_rtc_get_boot_time() == 0)
+    {
+      return false;
+    }
+
+  return true;
+}
+
+/****************************************************************************
+ * Name: rtc_setalarm
+ *
+ * Description:
+ *   Set a new alarm. This function implements the setalarm() method of the
+ *   RTC driver interface
+ *
+ * Input Parameters:
+ *   lower - A reference to RTC lower half driver state structure
+ *   alarminfo - Provided information needed to set the alarm
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; a negated errno value is returned
+ *   on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_ALARM
+static int rtc_setalarm(FAR struct rtc_lowerhalf_s *lower,
+                        FAR const struct lower_setalarm_s *alarminfo)
+{
+  FAR struct esp32c3_lowerhalf_s *priv;
+  FAR struct esp32c3_cbinfo_s *cbinfo;
+  struct alm_setalarm_s lowerinfo;
+  int ret = -EINVAL;

Review comment:
       Done

##########
File path: arch/risc-v/src/esp32c3/esp32c3_rtc_lowerhalf.c
##########
@@ -0,0 +1,575 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_rtc_lowerhalf.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/spinlock.h>
+
+#include <sys/types.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/timers/rtc.h>
+
+#include "esp32c3_rtc.h"
+#include "hardware/esp32c3_tim.h"
+
+#ifdef CONFIG_RTC_DRIVER
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_ALARM
+struct esp32c3_cbinfo_s
+{
+  volatile rtc_alarm_callback_t cb;  /* Callback when the alarm expires */
+  volatile FAR void *priv;           /* Private argurment to accompany 
callback */
+};
+#endif
+
+/* This is the private type for the RTC state.  It must be cast compatible
+ * with struct rtc_lowerhalf_s.
+ */
+
+struct esp32c3_lowerhalf_s
+{
+  /* This is the contained reference to the read-only, lower-half
+   * operations vtable (which may lie in FLASH or ROM)
+   */
+
+  FAR const struct rtc_ops_s *ops;
+#ifdef CONFIG_RTC_ALARM
+  /* Alarm callback information */
+
+  struct esp32c3_cbinfo_s cbinfo[RTC_ALARM_LAST];
+#endif
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Prototypes for static methods in struct rtc_ops_s */
+
+static int rtc_rdtime(FAR struct rtc_lowerhalf_s *lower,
+                      FAR struct rtc_time *rtctime);
+static int rtc_settime(FAR struct rtc_lowerhalf_s *lower,
+                       FAR const struct rtc_time *rtctime);
+static bool rtc_havesettime(FAR struct rtc_lowerhalf_s *lower);
+
+#ifdef CONFIG_RTC_ALARM
+static void rtc_alarm_callback(FAR void *arg, unsigned int alarmid);
+static int rtc_setalarm(FAR struct rtc_lowerhalf_s *lower,
+                        FAR const struct lower_setalarm_s *alarminfo);
+static int rtc_setrelative(FAR struct rtc_lowerhalf_s *lower,
+                           FAR const struct lower_setrelative_s *alarminfo);
+static int rtc_cancelalarm(FAR struct rtc_lowerhalf_s *lower,
+                           int alarmid);
+static int rtc_rdalarm(FAR struct rtc_lowerhalf_s *lower,
+                       FAR struct lower_rdalarm_s *alarminfo);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* ESP32-C3 RTC driver operations */
+
+static const struct rtc_ops_s g_rtc_ops =
+{
+  .rdtime      = rtc_rdtime,
+  .settime     = rtc_settime,
+  .havesettime = rtc_havesettime,
+#ifdef CONFIG_RTC_ALARM
+  .setalarm    = rtc_setalarm,
+  .setrelative = rtc_setrelative,
+  .cancelalarm = rtc_cancelalarm,
+  .rdalarm     = rtc_rdalarm,
+#endif
+#ifdef CONFIG_RTC_PERIODIC
+  .setperiodic    = NULL,
+  .cancelperiodic = NULL,
+#endif
+#ifdef CONFIG_RTC_IOCTL
+  .ioctl       = NULL,
+#endif
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  .destroy     = NULL,
+#endif
+};
+
+/* ESP32-C3 RTC device state */
+
+static struct esp32c3_lowerhalf_s g_rtc_lowerhalf =
+{
+  .ops        = &g_rtc_ops,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: rtc_alarm_callback
+ *
+ * Description:
+ *   This is the function that is called from the RTC driver when the alarm
+ *   goes off. It just invokes the upper half drivers callback.
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_ALARM
+static void rtc_alarm_callback(FAR void *arg, unsigned int alarmid)
+{
+  FAR struct esp32c3_lowerhalf_s *lower;
+  FAR struct esp32c3_cbinfo_s *cbinfo;
+  rtc_alarm_callback_t cb;
+  FAR void *priv;
+
+  DEBUGASSERT((RTC_ALARM0 <= alarmid) && (alarmid < RTC_ALARM_LAST));
+
+  lower        = (struct esp32c3_lowerhalf_s *)arg;
+  cbinfo       = &lower->cbinfo[alarmid];
+
+  /* Sample and clear the callback information to minimize the window in
+   * time in which race conditions can occur.
+   */
+
+  cb           = (rtc_alarm_callback_t)cbinfo->cb;
+  priv         = (FAR void *)cbinfo->priv;
+
+  cbinfo->cb   = NULL;
+  cbinfo->priv = NULL;
+
+  /* Perform the callback */
+
+  if (cb != NULL)
+    {
+      cb(priv, alarmid);
+    }
+}
+#endif /* CONFIG_RTC_ALARM */
+
+/****************************************************************************
+ * Name: rtc_rdtime
+ *
+ * Description:
+ *   Implements the rdtime() method of the RTC driver interface
+ *
+ * Input Parameters:
+ *   lower   - A reference to RTC lower half driver state structure
+ *   rcttime - The location in which to return the current RTC time.
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; a negated errno value is returned
+ *   on any failure.
+ *
+ ****************************************************************************/
+
+static int rtc_rdtime(FAR struct rtc_lowerhalf_s *lower,
+                      FAR struct rtc_time *rtctime)
+{
+#if defined(CONFIG_RTC_DATETIME)
+  /* This operation depends on the fact that struct rtc_time is cast
+   * compatible with struct tm.
+   */
+
+  return up_rtc_getdatetime((FAR struct tm *)rtctime);
+
+#elif defined(CONFIG_RTC_HIRES)
+  FAR struct timespec ts;
+  int ret;
+
+  /* Get the higher resolution time */
+
+  ret = up_rtc_gettime(&ts);
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  /* Convert the one second epoch time to a struct tm.  This operation
+   * depends on the fact that struct rtc_time and struct tm are cast
+   * compatible.
+   */
+
+  if (!gmtime_r(&ts.tv_sec, (FAR struct tm *)rtctime))
+    {
+      ret = -get_errno();
+      goto errout;
+    }
+
+  return OK;
+
+errout:
+  DEBUGASSERT(ret < 0);

Review comment:
       Done

##########
File path: arch/risc-v/src/esp32c3/esp32c3_rtc.c
##########
@@ -2135,3 +2246,415 @@ void IRAM_ATTR 
esp32c3_rtc_sleep_set_wakeup_time(uint64_t t)
   modifyreg32(RTC_CNTL_INT_CLR_REG, 0, RTC_CNTL_MAIN_TIMER_INT_CLR_M);
   modifyreg32(RTC_CNTL_SLP_TIMER1_REG, 0, RTC_CNTL_MAIN_TIMER_ALARM_EN_M);
 }
+
+/****************************************************************************
+ * Name: esp32c3_rtc_set_boot_time
+ *
+ * Description:
+ *   Set time to RTC register to replace the original boot time.
+ *
+ * Input Parameters:
+ *   time_us - set time in microseconds.
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void IRAM_ATTR esp32c3_rtc_set_boot_time(uint64_t time_us)
+{
+  putreg32((uint32_t)(time_us & 0xffffffff), RTC_BOOT_TIME_LOW_REG);
+  putreg32((uint32_t)(time_us >> 32), RTC_BOOT_TIME_HIGH_REG);
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_get_boot_time
+ *
+ * Description:
+ *   Get time of RTC register to indicate the original boot time.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   time_us - get time in microseconds.
+ *
+ ****************************************************************************/
+
+uint64_t IRAM_ATTR esp32c3_rtc_get_boot_time(void)
+{
+  return ((uint64_t)getreg32(RTC_BOOT_TIME_LOW_REG))
+        + (((uint64_t)getreg32(RTC_BOOT_TIME_HIGH_REG)) << 32);
+}
+
+#ifdef CONFIG_RTC_DRIVER
+
+/****************************************************************************
+ * Name: up_rtc_time
+ *
+ * Description:
+ *   Get the current time in seconds.  This is similar to the standard time()
+ *   function.  This interface is only required if the low-resolution
+ *   RTC/counter hardware implementation selected.  It is only used by the
+ *   RTOS during initialization to set up the system time when CONFIG_RTC is
+ *   set but neither CONFIG_RTC_HIRES nor CONFIG_RTC_DATETIME are set.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   The current time in seconds
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_RTC_HIRES
+time_t up_rtc_time(void)
+{
+  uint64_t time_us;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(NULL);
+
+  if (g_rt_timer_enabled == true)
+    {
+      time_us = rt_timer_time_us() + g_rtc_save->offset +
+                              esp32c3_rtc_get_boot_time();
+    }
+  else
+    {
+      time_us = esp32c3_rtc_get_time_us() +
+                  esp32c3_rtc_get_boot_time();
+    }
+
+  spin_unlock_irqrestore(NULL, flags);
+
+  return (time_t)(time_us / USEC_PER_SEC);
+}
+#endif /* CONFIG_RTC_HIRES */
+
+/****************************************************************************
+ * Name: up_rtc_settime
+ *
+ * Description:
+ *   Set the RTC to the provided time. All RTC implementations must be
+ *   able to set their time based on a standard timespec.
+ *
+ * Input Parameters:
+ *   tp - the time to use
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno on failure
+ *
+ ****************************************************************************/
+
+int up_rtc_settime(FAR const struct timespec *ts)
+{
+  irqstate_t flags;
+  uint64_t now_us;
+  uint64_t rtc_offset_us;
+
+  DEBUGASSERT(ts != NULL && ts->tv_nsec < NSEC_PER_SEC);
+  flags = spin_lock_irqsave(NULL);
+
+  now_us = ((uint64_t) ts->tv_sec) * USEC_PER_SEC +
+          ts->tv_nsec / NSEC_PER_USEC;
+  if (g_rt_timer_enabled == true)
+    {
+      rtc_offset_us = now_us - rt_timer_time_us();
+    }
+  else
+    {
+      rtc_offset_us = now_us - esp32c3_rtc_get_time_us();
+    }
+
+  g_rtc_save->offset = 0;
+  esp32c3_rtc_set_boot_time(rtc_offset_us);
+
+  spin_unlock_irqrestore(NULL, flags);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: up_rtc_initialize
+ *
+ * Description:
+ *   Initialize the hardware RTC per the selected configuration.
+ *   This function is called once during the OS initialization sequence
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno on failure
+ *
+ ****************************************************************************/
+
+int up_rtc_initialize(void)
+{
+#ifndef CONFIG_PM
+  /* Initialize RTC controller parameters */
+
+  esp32c3_rtc_init();
+  esp32c3_rtc_clk_set();
+#endif
+
+  g_rtc_save = &rtc_saved_data;
+
+  /* If saved data is invalid, clear offset information */
+
+  if (g_rtc_save->magic != MAGIC_RTC_SAVE)
+    {
+      g_rtc_save->magic = MAGIC_RTC_SAVE;
+      g_rtc_save->offset = 0;
+      esp32c3_rtc_set_boot_time(0);
+    }
+
+#ifdef CONFIG_RTC_HIRES
+  /* Synchronize the base time to the RTC time */
+
+  up_rtc_gettime(&g_basetime);
+#endif
+
+  g_rtc_enabled = true;
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: up_rtc_gettime
+ *
+ * Description:
+ *   Get the current time from the high resolution RTC time or RT-Timer. This
+ *   interface is only supported by the high-resolution RTC/counter hardware
+ *   implementation. It is used to replace the system timer.
+ *
+ * Input Parameters:
+ *   tp - The location to return the RTC time or RT-Timer value.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno on failure
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_HIRES
+int up_rtc_gettime(FAR struct timespec *tp)
+{
+  irqstate_t flags;
+  uint64_t time_us;
+
+  flags = spin_lock_irqsave(NULL);
+
+  if (g_rt_timer_enabled == true)
+    {
+      time_us = rt_timer_time_us() + g_rtc_save->offset +
+                              esp32c3_rtc_get_boot_time();
+    }
+  else
+    {
+      time_us = = esp32c3_rtc_get_time_us() +
+                    esp32c3_rtc_get_boot_time();
+    }
+
+  tp->tv_sec  = time_us / USEC_PER_SEC;
+  tp->tv_nsec = (time_us % USEC_PER_SEC) * NSEC_PER_USEC;
+
+  spin_unlock_irqrestore(NULL, flags);
+
+  return OK;
+}
+#endif /* CONFIG_RTC_HIRES */
+
+#ifdef CONFIG_RTC_ALARM
+
+/****************************************************************************
+ * Name: up_rtc_setalarm
+ *
+ * Description:
+ *   Set up an alarm.
+ *
+ * Input Parameters:
+ *   alminfo - Information about the alarm configuration.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno on failure
+ *
+ ****************************************************************************/
+
+int up_rtc_setalarm(FAR struct alm_setalarm_s *alminfo)
+{
+  struct rt_timer_args_s rt_timer_args;
+  FAR struct alm_cbinfo_s *cbinfo;
+  irqstate_t flags;
+  int ret = -EBUSY;
+  int id;
+
+  DEBUGASSERT(alminfo != NULL);
+  DEBUGASSERT((RTC_ALARM0 <= alminfo->as_id) &&
+              (alminfo->as_id < RTC_ALARM_LAST));
+
+  /* Set the alarm in RT-Timer */
+
+  id = alminfo->as_id;
+  cbinfo = &g_alarmcb[id];
+
+  if (cbinfo->ac_cb == NULL)
+    {
+      /* Create the RT-Timer alarm */
+
+      flags = spin_lock_irqsave(NULL);
+
+      if (cbinfo->alarm_hdl == NULL)
+        {
+          cbinfo->index = id;
+          rt_timer_args.arg = cbinfo;
+          rt_timer_args.callback = esp32c3_rt_cb_handler;
+          ret = rt_timer_create(&rt_timer_args, &cbinfo->alarm_hdl);
+          if (ret)
+            {
+              rtcerr("ERROR: Failed to create rt_timer error=%d\n", ret);
+              spin_unlock_irqrestore(NULL, flags);
+              return ret;
+            }
+        }
+
+      cbinfo->ac_cb  = alminfo->as_cb;
+      cbinfo->ac_arg = alminfo->as_arg;
+      cbinfo->deadline_us = alminfo->as_time.tv_sec * USEC_PER_SEC +
+                            alminfo->as_time.tv_nsec / NSEC_PER_USEC;
+
+      if (cbinfo->alarm_hdl == NULL)
+        {
+          rtcerr("ERROR: failed to creat alarm timer\n");
+        }
+      else
+        {
+          rtcinfo("Start RTC alarm.\n");
+          rt_timer_start(cbinfo->alarm_hdl, cbinfo->deadline_us, false);
+          ret = OK;
+        }
+
+      spin_unlock_irqrestore(NULL, flags);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: up_rtc_cancelalarm
+ *
+ * Description:
+ *   Cancel an alarm.
+ *
+ * Input Parameters:
+ *   alarmid - Identifies the alarm to be cancelled
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno on failure
+ *
+ ****************************************************************************/
+
+int up_rtc_cancelalarm(enum alm_id_e alarmid)
+{
+  FAR struct alm_cbinfo_s *cbinfo;
+  irqstate_t flags;
+  int ret = -ENODATA;
+
+  DEBUGASSERT((RTC_ALARM0 <= alarmid) &&
+              (alarmid < RTC_ALARM_LAST));
+
+  /* Set the alarm in hardware and enable interrupts */
+
+  cbinfo = &g_alarmcb[alarmid];
+
+  if (cbinfo->ac_cb != NULL)
+    {
+      flags = spin_lock_irqsave(NULL);
+
+      /* Stop and delete the alarm */
+
+      rtcinfo("Cancel RTC alarm.\n");
+      rt_timer_stop(cbinfo->alarm_hdl);
+      rt_timer_delete(cbinfo->alarm_hdl);
+      cbinfo->ac_cb = NULL;
+      cbinfo->deadline_us = 0;
+      cbinfo->alarm_hdl = NULL;
+
+      spin_unlock_irqrestore(NULL, flags);
+
+      ret = OK;
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: up_rtc_rdalarm
+ *
+ * Description:
+ *   Query an alarm configured in hardware.
+ *
+ * Input Parameters:
+ *   tp      - Location to return the timer match register.
+ *   alarmid - Identifies the alarm to be cancelled
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno on failure
+ *
+ ****************************************************************************/
+
+int up_rtc_rdalarm(FAR struct timespec *tp, uint32_t alarmid)
+{
+  irqstate_t flags;
+  FAR struct alm_cbinfo_s *cbinfo;
+  DEBUGASSERT(tp != NULL);
+  DEBUGASSERT((RTC_ALARM0 <= alarmid) &&
+              (alarmid < RTC_ALARM_LAST));
+
+  flags = spin_lock_irqsave(NULL);
+
+  /* Set the alarm in hardware and enable interrupts */

Review comment:
       Done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to