henryrov commented on code in PR #12771:
URL: https://github.com/apache/nuttx/pull/12771#discussion_r1692413587


##########
arch/risc-v/src/bl808/bl808_wdt.c:
##########
@@ -0,0 +1,385 @@
+/****************************************************************************
+ * arch/risc-v/src/bl808/bl808_wdt.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 <sys/types.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/irq.h>
+#include <nuttx/timers/watchdog.h>
+#include <nuttx/fs/ioctl.h>
+
+#include "hardware/bl808_timer.h"
+#include "riscv_internal.h"
+#include "chip.h"
+#include "bl808_wdt.h"
+
+#ifdef CONFIG_BL808_WDT
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define WDT_CLK_SRC_1K 2
+#define WDT_CLK_SRC_NONE 5
+
+#define BL808_UNLOCK_WDT(n) \
+  ({ \
+    putreg32(0xbaba, BL808_WDT_KEY1(n)); \
+    putreg32(0xeb10, BL808_WDT_KEY2(n)); \
+  })
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct bl808_wdt_s
+{
+  const struct watchdog_ops_s *ops;
+  int idx;
+  xcpt_t callback;
+  bool started;
+  uint32_t timeout;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+int bl808_wdt_start(FAR struct watchdog_lowerhalf_s *lower);
+int bl808_wdt_stop(FAR struct watchdog_lowerhalf_s *lower);
+int bl808_wdt_keepalive(FAR struct watchdog_lowerhalf_s *lower);
+int bl808_wdt_getstatus(FAR struct watchdog_lowerhalf_s *lower,
+                          FAR struct watchdog_status_s *status);
+int bl808_wdt_settimeout(FAR struct watchdog_lowerhalf_s *lower,
+                           uint32_t timeout);
+xcpt_t bl808_wdt_capture(FAR struct watchdog_lowerhalf_s *lower,
+                       CODE xcpt_t callback);
+int bl808_wdt_ioctl(FAR struct watchdog_lowerhalf_s *lower,
+                      int cmd, unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct watchdog_ops_s bl808_wdt_ops =
+  {
+    .start = bl808_wdt_start,
+    .stop = bl808_wdt_stop,
+    .keepalive = bl808_wdt_keepalive,
+    .getstatus = bl808_wdt_getstatus,
+    .settimeout = bl808_wdt_settimeout,
+    .capture = bl808_wdt_capture,
+    .ioctl = bl808_wdt_ioctl,
+  };
+
+static struct bl808_wdt_s wdt0 =
+  {
+    .ops = &bl808_wdt_ops,
+    .idx = 0,
+    .callback = NULL,
+    .started = false,
+    .timeout = 0
+  };
+
+static struct bl808_wdt_s wdt1 =
+  {
+    .ops = &bl808_wdt_ops,
+    .idx = 1,
+    .callback = NULL,
+    .started = false,
+    .timeout = 0
+  };
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: wdt_interrupt
+ *
+ * Description:
+ *   Watchdog interrupt handler. Clears the interrupt and
+ *   Calls the attached callback if there is one.
+ *
+ ****************************************************************************/
+
+static int __wdt_interrupt(int irq, void *context, void *arg)
+{
+  struct bl808_wdt_s *priv = (struct bl808_wdt_s *)arg;
+
+  /* Clear IRQ */
+
+  BL808_UNLOCK_WDT(priv->idx);
+  modifyreg32(BL808_WDT_ICLR(priv->idx), 0, WDT_CLEAR_IRQ);
+
+  if (priv->callback != NULL)
+    {
+      priv->callback(irq, context, arg);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: bl808_wdt_start
+ *
+ * Description:
+ *   Reset the time to the current timeout and start the watchdog.
+ *
+ ****************************************************************************/
+
+int bl808_wdt_start(FAR struct watchdog_lowerhalf_s *lower)
+{
+  struct bl808_wdt_s *priv = (struct bl808_wdt_s *)lower;
+
+  /* Enable clock */
+
+  modifyreg32(BL808_TIMER_TCCR(priv->idx), WDT_CLKSEL_MASK,
+              WDT_CLK_SRC_1K << WDT_CLKSEL_SHIFT);
+
+  /* Clear counter */
+
+  BL808_UNLOCK_WDT(priv->idx);
+  modifyreg32(BL808_WDT_COUNT_CLEAR(priv->idx), 0, WDT_CLEAR_COUNT);
+  while (getreg32(BL808_WDT_COUNTER(priv->idx)) != 0);
+  BL808_UNLOCK_WDT(priv->idx);
+  modifyreg32(BL808_WDT_COUNT_CLEAR(priv->idx), WDT_CLEAR_COUNT, 0);
+
+  priv->started = true;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: bl808_timer_stop
+ *
+ * Description:
+ *   Stop the watchdog.
+ *
+ ****************************************************************************/
+
+int bl808_wdt_stop(FAR struct watchdog_lowerhalf_s *lower)
+{
+  struct bl808_wdt_s *priv = (struct bl808_wdt_s *)lower;
+
+  /* WDTs are stopped by setting the input clock to NONE.
+   * This is done to allow calling watchdog_stop and then get
+   * the time left to timeout afterwards. If we used the
+   * watchdog enable bits, the counter would reset to 0 when stopped.
+   */
+
+  modifyreg32(BL808_TIMER_TCCR(priv->idx), WDT_CLKSEL_MASK,
+              WDT_CLK_SRC_NONE << WDT_CLKSEL_SHIFT);
+
+  priv->started = false;
+  return OK;
+}
+
+int bl808_wdt_keepalive(FAR struct watchdog_lowerhalf_s *lower)
+{
+  struct bl808_wdt_s *priv = (struct bl808_wdt_s *)lower;
+
+  /* Check that the watchdog is running */
+
+  if (priv->started == false)
+    {
+      return -EIO;
+    }
+
+  BL808_UNLOCK_WDT(priv->idx);
+  modifyreg32(BL808_WDT_COUNT_CLEAR(priv->idx), 0, WDT_CLEAR_COUNT);
+  while (getreg32(BL808_WDT_COUNTER(priv->idx)) != 0);
+  BL808_UNLOCK_WDT(priv->idx);
+  modifyreg32(BL808_WDT_COUNT_CLEAR(priv->idx), WDT_CLEAR_COUNT, 0);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: bl808_wdt_getstatus
+ *
+ * Description:
+ *   Get current watchdog status. Returns to status parameter.

Review Comment:
   I'll add the inputs and return descriptions for these.
   
   For getstatus and settimeout, these functions don't really have an error 
condition, so I don't see a situation where they would return an error. The 
prototypes in nuttx/watchdog.h are defined to return an error number, so I made 
these return OK to match those, which is also done by other watchdog drivers 
(eg bl602 or esp32c3-legacy). Is it necessary to change this?



-- 
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: [email protected]

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

Reply via email to