saramonteiro commented on a change in pull request #3794:
URL: https://github.com/apache/incubator-nuttx/pull/3794#discussion_r642439500
##########
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");
Review comment:
```suggestion
rtcerr("ERROR: failed to create alarm timer\n");
```
--
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]