pkarashchenko commented on code in PR #8980:
URL: https://github.com/apache/nuttx/pull/8980#discussion_r1160621569


##########
arch/risc-v/src/espressif/esp_hr_timer.c:
##########
@@ -648,20 +648,31 @@ void IRAM_ATTR esp_hr_timer_calibration(uint64_t time_us)
 
 int esp_hr_timer_init(void)
 {
-  struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
+  static bool g_hr_timer_initialized = false;

Review Comment:
   ```suggestion
     static bool g_hr_timer_initialized;
   ```



##########
arch/risc-v/src/espressif/Kconfig:
##########
@@ -217,8 +217,8 @@ config ESPRESSIF_UART1
        select ARCH_HAVE_SERIAL_TERMIOS
 
 config ESPRESSIF_HR_TIMER
-       bool "High-Resolution Timer"
-       default n
+       bool
+       default y if RTC_DRIVER

Review Comment:
   ```suggestion
        default RTC_DRIVER
   ```



##########
arch/risc-v/src/espressif/esp_hr_timer.c:
##########
@@ -648,20 +648,31 @@ void IRAM_ATTR esp_hr_timer_calibration(uint64_t time_us)
 
 int esp_hr_timer_init(void)
 {
-  struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
+  static bool g_hr_timer_initialized = false;
+  struct esp_hr_timer_context_s *priv;
+  int pid;
+
+  if (g_hr_timer_initialized)
+    {
+      tmrinfo("HR Timer already initialized, skipping...\n");
+
+      return OK;
+    }
 
-  int pid = kthread_create(CONFIG_ESPRESSIF_HR_TIMER_TASK_NAME,
-                           CONFIG_ESPRESSIF_HR_TIMER_TASK_PRIORITY,
-                           CONFIG_ESPRESSIF_HR_TIMER_TASK_STACK_SIZE,
-                           esp_hr_timer_thread,
-                           NULL);
+  pid  = kthread_create(CONFIG_ESPRESSIF_HR_TIMER_TASK_NAME,
+                        CONFIG_ESPRESSIF_HR_TIMER_TASK_PRIORITY,
+                        CONFIG_ESPRESSIF_HR_TIMER_TASK_STACK_SIZE,
+                        esp_hr_timer_thread,
+                        NULL);

Review Comment:
   ```suggestion
     pid = kthread_create(CONFIG_ESPRESSIF_HR_TIMER_TASK_NAME,
                          CONFIG_ESPRESSIF_HR_TIMER_TASK_PRIORITY,
                          CONFIG_ESPRESSIF_HR_TIMER_TASK_STACK_SIZE,
                          esp_hr_timer_thread,
                          NULL);
   ```



##########
arch/risc-v/src/espressif/esp_rtc.c:
##########
@@ -0,0 +1,878 @@
+/****************************************************************************
+ * arch/risc-v/src/espressif/esp_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 <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/timers/rtc.h>
+
+#include "clock/clock.h"
+
+#include "esp_hr_timer.h"
+#include "esp_rtc.h"
+#include "riscv_internal.h"
+
+#include "esp_attr.h"
+#include "soc/rtc.h"
+
+/* Chip-dependent headers from esp-hal-3rdparty */
+
+#ifdef CONFIG_ESPRESSIF_ESP32C3
+#include "esp32c3/rtc.h"
+#include "esp32c3/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32C6)
+#include "esp32c6/rtc.h"
+#include "esp32c6/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32H2)
+#include "esp32h2/rtc.h"
+#include "esp32h2/rom/rtc.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The magic data for the struct esp_rtc_backup_s that is in RTC slow
+ * memory.
+ */
+
+#define MAGIC_RTC_SAVE (UINT64_C(0x11223344556677))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_DRIVER
+
+#ifdef CONFIG_RTC_ALARM
+struct alm_cbinfo_s
+{
+  struct esp_hr_timer_s  *alarm_hdl;    /* Timer id point to here */
+  rtc_alarm_callback_t    ac_cb;        /* Client callback function */
+  volatile void          *ac_arg;       /* Argument to pass with the callback 
function */
+  uint64_t                deadline_us;
+  uint8_t                 index;
+};
+#endif /* CONFIG_RTC_ALARM */
+
+/* This is the private type for the RTC state. It must be cast compatible
+ * with struct rtc_lowerhalf_s.
+ */
+
+struct esp_rtc_lowerhalf_s
+{
+  /* This is the contained reference to the read-only, lower-half
+   * operations vtable (which may lie in FLASH or ROM)
+   */
+
+  const struct rtc_ops_s *ops;
+#ifdef CONFIG_RTC_ALARM
+  /* Alarm callback information */
+
+  struct alm_cbinfo_s     alarmcb[CONFIG_RTC_NALARMS];
+#endif /* CONFIG_RTC_ALARM */
+};
+
+#endif/* CONFIG_RTC_DRIVER */
+
+struct esp_rtc_backup_s
+{
+  uint64_t magic;
+  int64_t  offset;           /* Offset time from RTC HW value */
+  int64_t  reserved0;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Prototypes for static methods in struct rtc_ops_s */
+
+#ifdef CONFIG_RTC_DRIVER
+static int esp_rtc_rdtime(struct rtc_lowerhalf_s *lower,
+                          struct rtc_time *rtctime);
+static int esp_rtc_settime(struct rtc_lowerhalf_s *lower,
+                           const struct rtc_time *rtctime);
+static bool esp_rtc_havesettime(struct rtc_lowerhalf_s *lower);
+
+#ifdef CONFIG_RTC_ALARM
+static void IRAM_ATTR rtc_hr_timer_cb(void *arg);
+static int esp_rtc_setalarm(struct rtc_lowerhalf_s *lower,
+                            const struct lower_setalarm_s *alarminfo);
+static int esp_rtc_setrelative(struct rtc_lowerhalf_s *lower,
+                               const struct lower_setrelative_s *alarminfo);
+static int esp_rtc_cancelalarm(struct rtc_lowerhalf_s *lower,
+                               int alarmid);
+static int esp_rtc_rdalarm(struct rtc_lowerhalf_s *lower,
+                           struct lower_rdalarm_s *alarminfo);
+#endif /* CONFIG_RTC_ALARM */
+#endif /* CONFIG_RTC_DRIVER */
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_DRIVER
+/* RTC driver operations */
+
+static const struct rtc_ops_s g_rtc_ops =
+{
+  .rdtime      = esp_rtc_rdtime,
+  .settime     = esp_rtc_settime,
+  .havesettime = esp_rtc_havesettime,
+#ifdef CONFIG_RTC_ALARM
+  .setalarm    = esp_rtc_setalarm,
+  .setrelative = esp_rtc_setrelative,
+  .cancelalarm = esp_rtc_cancelalarm,
+  .rdalarm     = esp_rtc_rdalarm,
+#endif /* CONFIG_RTC_ALARM */
+};
+
+/* RTC device state */
+
+static struct esp_rtc_lowerhalf_s g_rtc_lowerhalf =
+{
+  .ops = &g_rtc_ops
+};
+
+/* Flag for tracking HR Timer enable status */
+
+static bool g_hr_timer_enabled = false;

Review Comment:
   ```suggestion
   static bool g_hr_timer_enabled;
   ```



##########
arch/risc-v/src/espressif/esp_rtc.c:
##########
@@ -0,0 +1,878 @@
+/****************************************************************************
+ * arch/risc-v/src/espressif/esp_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 <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/timers/rtc.h>
+
+#include "clock/clock.h"
+
+#include "esp_hr_timer.h"
+#include "esp_rtc.h"
+#include "riscv_internal.h"
+
+#include "esp_attr.h"
+#include "soc/rtc.h"
+
+/* Chip-dependent headers from esp-hal-3rdparty */
+
+#ifdef CONFIG_ESPRESSIF_ESP32C3
+#include "esp32c3/rtc.h"
+#include "esp32c3/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32C6)
+#include "esp32c6/rtc.h"
+#include "esp32c6/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32H2)
+#include "esp32h2/rtc.h"
+#include "esp32h2/rom/rtc.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The magic data for the struct esp_rtc_backup_s that is in RTC slow
+ * memory.
+ */
+
+#define MAGIC_RTC_SAVE (UINT64_C(0x11223344556677))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_DRIVER
+
+#ifdef CONFIG_RTC_ALARM
+struct alm_cbinfo_s
+{
+  struct esp_hr_timer_s  *alarm_hdl;    /* Timer id point to here */
+  rtc_alarm_callback_t    ac_cb;        /* Client callback function */
+  volatile void          *ac_arg;       /* Argument to pass with the callback 
function */
+  uint64_t                deadline_us;
+  uint8_t                 index;
+};
+#endif /* CONFIG_RTC_ALARM */
+
+/* This is the private type for the RTC state. It must be cast compatible
+ * with struct rtc_lowerhalf_s.
+ */
+
+struct esp_rtc_lowerhalf_s
+{
+  /* This is the contained reference to the read-only, lower-half
+   * operations vtable (which may lie in FLASH or ROM)
+   */
+
+  const struct rtc_ops_s *ops;
+#ifdef CONFIG_RTC_ALARM
+  /* Alarm callback information */
+
+  struct alm_cbinfo_s     alarmcb[CONFIG_RTC_NALARMS];
+#endif /* CONFIG_RTC_ALARM */
+};
+
+#endif/* CONFIG_RTC_DRIVER */
+
+struct esp_rtc_backup_s
+{
+  uint64_t magic;
+  int64_t  offset;           /* Offset time from RTC HW value */
+  int64_t  reserved0;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Prototypes for static methods in struct rtc_ops_s */
+
+#ifdef CONFIG_RTC_DRIVER
+static int esp_rtc_rdtime(struct rtc_lowerhalf_s *lower,
+                          struct rtc_time *rtctime);
+static int esp_rtc_settime(struct rtc_lowerhalf_s *lower,
+                           const struct rtc_time *rtctime);
+static bool esp_rtc_havesettime(struct rtc_lowerhalf_s *lower);
+
+#ifdef CONFIG_RTC_ALARM
+static void IRAM_ATTR rtc_hr_timer_cb(void *arg);
+static int esp_rtc_setalarm(struct rtc_lowerhalf_s *lower,
+                            const struct lower_setalarm_s *alarminfo);
+static int esp_rtc_setrelative(struct rtc_lowerhalf_s *lower,
+                               const struct lower_setrelative_s *alarminfo);
+static int esp_rtc_cancelalarm(struct rtc_lowerhalf_s *lower,
+                               int alarmid);
+static int esp_rtc_rdalarm(struct rtc_lowerhalf_s *lower,
+                           struct lower_rdalarm_s *alarminfo);
+#endif /* CONFIG_RTC_ALARM */
+#endif /* CONFIG_RTC_DRIVER */
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_DRIVER
+/* RTC driver operations */
+
+static const struct rtc_ops_s g_rtc_ops =
+{
+  .rdtime      = esp_rtc_rdtime,
+  .settime     = esp_rtc_settime,
+  .havesettime = esp_rtc_havesettime,
+#ifdef CONFIG_RTC_ALARM
+  .setalarm    = esp_rtc_setalarm,
+  .setrelative = esp_rtc_setrelative,
+  .cancelalarm = esp_rtc_cancelalarm,
+  .rdalarm     = esp_rtc_rdalarm,
+#endif /* CONFIG_RTC_ALARM */
+};
+
+/* RTC device state */
+
+static struct esp_rtc_lowerhalf_s g_rtc_lowerhalf =
+{
+  .ops = &g_rtc_ops
+};
+
+/* Flag for tracking HR Timer enable status */
+
+static bool g_hr_timer_enabled = false;
+
+#endif /* CONFIG_RTC_DRIVER */
+
+/* Saved data for persistent RTC time */
+
+static RTC_DATA_ATTR struct esp_rtc_backup_s g_rtc_saved_data;
+static struct esp_rtc_backup_s *g_rtc_save;
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+volatile bool g_rtc_enabled = false;

Review Comment:
   ```suggestion
   volatile bool g_rtc_enabled;
   ```



##########
arch/risc-v/src/espressif/esp_rtc.c:
##########
@@ -0,0 +1,878 @@
+/****************************************************************************
+ * arch/risc-v/src/espressif/esp_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 <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/timers/rtc.h>
+
+#include "clock/clock.h"
+
+#include "esp_hr_timer.h"
+#include "esp_rtc.h"
+#include "riscv_internal.h"
+
+#include "esp_attr.h"
+#include "soc/rtc.h"
+
+/* Chip-dependent headers from esp-hal-3rdparty */
+
+#ifdef CONFIG_ESPRESSIF_ESP32C3
+#include "esp32c3/rtc.h"
+#include "esp32c3/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32C6)
+#include "esp32c6/rtc.h"
+#include "esp32c6/rom/rtc.h"

Review Comment:
   ```suggestion
   #  include "esp32c6/rtc.h"
   #  include "esp32c6/rom/rtc.h"
   ```



##########
arch/risc-v/src/espressif/esp_rtc.c:
##########
@@ -0,0 +1,878 @@
+/****************************************************************************
+ * arch/risc-v/src/espressif/esp_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 <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/timers/rtc.h>
+
+#include "clock/clock.h"
+
+#include "esp_hr_timer.h"
+#include "esp_rtc.h"
+#include "riscv_internal.h"
+
+#include "esp_attr.h"
+#include "soc/rtc.h"
+
+/* Chip-dependent headers from esp-hal-3rdparty */
+
+#ifdef CONFIG_ESPRESSIF_ESP32C3
+#include "esp32c3/rtc.h"
+#include "esp32c3/rom/rtc.h"

Review Comment:
   ```suggestion
   #  include "esp32c3/rtc.h"
   #  include "esp32c3/rom/rtc.h"
   ```



##########
arch/risc-v/src/espressif/esp_rtc.c:
##########
@@ -0,0 +1,878 @@
+/****************************************************************************
+ * arch/risc-v/src/espressif/esp_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 <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/timers/rtc.h>
+
+#include "clock/clock.h"
+
+#include "esp_hr_timer.h"
+#include "esp_rtc.h"
+#include "riscv_internal.h"
+
+#include "esp_attr.h"
+#include "soc/rtc.h"
+
+/* Chip-dependent headers from esp-hal-3rdparty */
+
+#ifdef CONFIG_ESPRESSIF_ESP32C3
+#include "esp32c3/rtc.h"
+#include "esp32c3/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32C6)
+#include "esp32c6/rtc.h"
+#include "esp32c6/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32H2)
+#include "esp32h2/rtc.h"
+#include "esp32h2/rom/rtc.h"

Review Comment:
   ```suggestion
   #  include "esp32h2/rtc.h"
   #  include "esp32h2/rom/rtc.h"
   ```



##########
arch/risc-v/src/espressif/esp_rtc.c:
##########
@@ -0,0 +1,878 @@
+/****************************************************************************
+ * arch/risc-v/src/espressif/esp_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 <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/timers/rtc.h>
+
+#include "clock/clock.h"
+
+#include "esp_hr_timer.h"
+#include "esp_rtc.h"
+#include "riscv_internal.h"
+
+#include "esp_attr.h"
+#include "soc/rtc.h"
+
+/* Chip-dependent headers from esp-hal-3rdparty */
+
+#ifdef CONFIG_ESPRESSIF_ESP32C3
+#include "esp32c3/rtc.h"
+#include "esp32c3/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32C6)
+#include "esp32c6/rtc.h"
+#include "esp32c6/rom/rtc.h"
+#elif defined(CONFIG_ESPRESSIF_ESP32H2)
+#include "esp32h2/rtc.h"
+#include "esp32h2/rom/rtc.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The magic data for the struct esp_rtc_backup_s that is in RTC slow
+ * memory.
+ */
+
+#define MAGIC_RTC_SAVE (UINT64_C(0x11223344556677))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_DRIVER
+
+#ifdef CONFIG_RTC_ALARM
+struct alm_cbinfo_s
+{
+  struct esp_hr_timer_s  *alarm_hdl;    /* Timer id point to here */
+  rtc_alarm_callback_t    ac_cb;        /* Client callback function */
+  volatile void          *ac_arg;       /* Argument to pass with the callback 
function */
+  uint64_t                deadline_us;
+  uint8_t                 index;
+};
+#endif /* CONFIG_RTC_ALARM */
+
+/* This is the private type for the RTC state. It must be cast compatible
+ * with struct rtc_lowerhalf_s.
+ */
+
+struct esp_rtc_lowerhalf_s
+{
+  /* This is the contained reference to the read-only, lower-half
+   * operations vtable (which may lie in FLASH or ROM)
+   */
+
+  const struct rtc_ops_s *ops;
+#ifdef CONFIG_RTC_ALARM
+  /* Alarm callback information */
+
+  struct alm_cbinfo_s     alarmcb[CONFIG_RTC_NALARMS];
+#endif /* CONFIG_RTC_ALARM */
+};
+
+#endif/* CONFIG_RTC_DRIVER */
+
+struct esp_rtc_backup_s
+{
+  uint64_t magic;
+  int64_t  offset;           /* Offset time from RTC HW value */
+  int64_t  reserved0;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Prototypes for static methods in struct rtc_ops_s */
+
+#ifdef CONFIG_RTC_DRIVER
+static int esp_rtc_rdtime(struct rtc_lowerhalf_s *lower,
+                          struct rtc_time *rtctime);
+static int esp_rtc_settime(struct rtc_lowerhalf_s *lower,
+                           const struct rtc_time *rtctime);
+static bool esp_rtc_havesettime(struct rtc_lowerhalf_s *lower);
+
+#ifdef CONFIG_RTC_ALARM
+static void IRAM_ATTR rtc_hr_timer_cb(void *arg);
+static int esp_rtc_setalarm(struct rtc_lowerhalf_s *lower,
+                            const struct lower_setalarm_s *alarminfo);
+static int esp_rtc_setrelative(struct rtc_lowerhalf_s *lower,
+                               const struct lower_setrelative_s *alarminfo);
+static int esp_rtc_cancelalarm(struct rtc_lowerhalf_s *lower,
+                               int alarmid);
+static int esp_rtc_rdalarm(struct rtc_lowerhalf_s *lower,
+                           struct lower_rdalarm_s *alarminfo);
+#endif /* CONFIG_RTC_ALARM */
+#endif /* CONFIG_RTC_DRIVER */
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_DRIVER
+/* RTC driver operations */
+
+static const struct rtc_ops_s g_rtc_ops =
+{
+  .rdtime      = esp_rtc_rdtime,
+  .settime     = esp_rtc_settime,
+  .havesettime = esp_rtc_havesettime,
+#ifdef CONFIG_RTC_ALARM
+  .setalarm    = esp_rtc_setalarm,
+  .setrelative = esp_rtc_setrelative,
+  .cancelalarm = esp_rtc_cancelalarm,
+  .rdalarm     = esp_rtc_rdalarm,
+#endif /* CONFIG_RTC_ALARM */
+};
+
+/* RTC device state */
+
+static struct esp_rtc_lowerhalf_s g_rtc_lowerhalf =
+{
+  .ops = &g_rtc_ops
+};
+
+/* Flag for tracking HR Timer enable status */
+
+static bool g_hr_timer_enabled = false;
+
+#endif /* CONFIG_RTC_DRIVER */
+
+/* Saved data for persistent RTC time */
+
+static RTC_DATA_ATTR struct esp_rtc_backup_s g_rtc_saved_data;
+static struct esp_rtc_backup_s *g_rtc_save;
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+volatile bool g_rtc_enabled = false;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_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.
+ *
+ ****************************************************************************/
+
+static void IRAM_ATTR esp_rtc_set_boot_time(uint64_t time_us)
+{
+  putreg32((uint32_t)(time_us & UINT32_MAX), RTC_BOOT_TIME_LOW_REG);
+  putreg32((uint32_t)(time_us >> 32), RTC_BOOT_TIME_HIGH_REG);
+}
+
+/****************************************************************************
+ * Name: esp_rtc_get_boot_time
+ *
+ * Description:
+ *   Get time of RTC register to indicate the original boot time.
+ *
+ * Input Parameters:
+ *   None.
+ *
+ * Returned Value:
+ *   Boot time in microseconds.
+ *
+ ****************************************************************************/
+
+static uint64_t IRAM_ATTR esp_rtc_get_boot_time(void)
+{
+  return ((uint64_t)getreg32(RTC_BOOT_TIME_LOW_REG))
+        + (((uint64_t)getreg32(RTC_BOOT_TIME_HIGH_REG)) << 32);
+}
+
+/****************************************************************************
+ * Name: rtc_hr_timer_cb
+ *
+ * Description:
+ *   Callback to be called upon HR-Timer expiration.
+ *
+ * Input Parameters:
+ *   arg           - Information about the HR-Timer configuration.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_RTC_DRIVER) && defined(CONFIG_RTC_ALARM)
+static void IRAM_ATTR rtc_hr_timer_cb(void *arg)
+{
+  struct alm_cbinfo_s *cbinfo = (struct alm_cbinfo_s *)arg;
+  rtc_alarm_callback_t cb;
+  void *cb_arg;
+  int alminfo_id;
+
+  DEBUGASSERT(cbinfo != NULL);
+
+  alminfo_id = cbinfo->index;
+
+  if (cbinfo->ac_cb != NULL)
+    {
+      /* Alarm callback */
+
+      cb = cbinfo->ac_cb;
+      cb_arg = (void *)cbinfo->ac_arg;
+      cbinfo->ac_cb  = NULL;
+      cbinfo->ac_arg = NULL;
+      cbinfo->deadline_us = 0;
+      cb(cb_arg, alminfo_id);
+    }
+}
+#endif /* CONFIG_RTC_DRIVER */
+
+/****************************************************************************
+ * Name: esp_rtc_rdtime
+ *
+ * Description:
+ *   Return the current RTC time.
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_DRIVER
+
+#ifdef CONFIG_RTC_HIRES
+static int esp_rtc_rdtime(struct rtc_lowerhalf_s *lower,
+                          struct rtc_time *rtctime)
+{
+  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 tmThis operation
+   * depends on the fact that struct rtc_time and struct tm are cast
+   * compatible.
+   */
+
+  if (!gmtime_r(&ts.tv_sec, (struct tm *)rtctime))
+    {
+      ret = -get_errno();
+      goto errout;
+    }
+
+  return OK;
+
+errout:
+  rtcerr("Failed to get RTC time: %d\n", ret);
+  return ret;
+}
+
+#else /* !CONFIG_RTC_HIRES */
+
+static int esp_rtc_rdtime(struct rtc_lowerhalf_s *lower,
+                          struct rtc_time *rtctime)
+{
+  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, (struct tm *)rtctime) == 0)
+    {
+      int errcode = get_errno();
+      DEBUGASSERT(errcode > 0);
+
+      rtcerr("gmtime_r failed: %d\n", errcode);
+      return -errcode;
+    }
+
+  return OK;
+}
+#endif /* CONFIG_RTC_HIRES */
+
+#endif /* CONFIG_RTC_DRIVER */
+
+/****************************************************************************
+ * Name: esp_rtc_settime
+ *
+ * Description:
+ *   Implement 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_RTC_DRIVER
+static int esp_rtc_settime(struct rtc_lowerhalf_s *lower,
+                           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((struct tm *)rtctime);

Review Comment:
   `mktime` can normalise it's parameter, so dropping `const` here is not safe



-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to