xiaoxiang781216 commented on code in PR #20782:
URL: https://github.com/apache/nuttx/pull/20782#discussion_r4355696852


##########
boards/sim/sim/sim/src/sim_watchdog.c:
##########
@@ -0,0 +1,180 @@
+/****************************************************************************
+ * boards/sim/sim/sim/src/sim_watchdog.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <stdbool.h>
+#include <errno.h>
+#include <nuttx/debug.h>
+
+#include <nuttx/board.h>
+#include <nuttx/clock.h>
+#include <nuttx/wdog.h>
+#include <nuttx/timers/watchdog.h>
+
+#include "sim.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_wdt_s
+{
+  struct watchdog_lowerhalf_s lower;
+  struct wdog_s wdog;
+  uint32_t      timeout;  /* Timeout in milliseconds */
+  bool          started;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_wdt_start(struct watchdog_lowerhalf_s *lower);
+static int sim_wdt_stop(struct watchdog_lowerhalf_s *lower);
+static int sim_wdt_keepalive(struct watchdog_lowerhalf_s *lower);
+static int sim_wdt_getstatus(struct watchdog_lowerhalf_s *lower,
+                             struct watchdog_status_s *status);
+static int sim_wdt_settimeout(struct watchdog_lowerhalf_s *lower,
+                              uint32_t timeout);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct watchdog_ops_s g_sim_wdt_ops =
+{
+  .start      = sim_wdt_start,
+  .stop       = sim_wdt_stop,
+  .keepalive  = sim_wdt_keepalive,
+  .getstatus  = sim_wdt_getstatus,
+  .settimeout = sim_wdt_settimeout,
+};
+
+static struct sim_wdt_s g_sim_wdt =
+{
+  .lower.ops = &g_sim_wdt_ops,
+  .timeout   = CONFIG_SIM_WATCHDOG_TIMEOUT,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sim_wdt_expire
+ *
+ * Description:
+ *   The simulator has no reset, so a watchdog timeout ends it with
+ *   CONFIG_SIM_WATCHDOG_EXITCODE.
+ *
+ ****************************************************************************/
+
+static void sim_wdt_expire(wdparm_t arg)
+{
+  syslog(LOG_EMERG, "Simulated watchdog expired, exit %d\n",
+         CONFIG_SIM_WATCHDOG_EXITCODE);
+  board_power_off(CONFIG_SIM_WATCHDOG_EXITCODE);
+}
+
+static void sim_wdt_arm(struct sim_wdt_s *priv)
+{
+  wd_start(&priv->wdog, MSEC2TICK(priv->timeout), sim_wdt_expire,
+           (wdparm_t)priv);
+}
+
+static int sim_wdt_start(struct watchdog_lowerhalf_s *lower)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  priv->started = true;
+  sim_wdt_arm(priv);
+  return OK;
+}
+
+static int sim_wdt_stop(struct watchdog_lowerhalf_s *lower)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  priv->started = false;
+  wd_cancel(&priv->wdog);
+  return OK;
+}
+
+static int sim_wdt_keepalive(struct watchdog_lowerhalf_s *lower)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  if (priv->started)
+    {
+      sim_wdt_arm(priv);
+    }
+
+  return OK;
+}
+
+static int sim_wdt_getstatus(struct watchdog_lowerhalf_s *lower,
+                             struct watchdog_status_s *status)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  status->flags    = priv->started ? WDFLAGS_ACTIVE | WDFLAGS_RESET : 0;
+  status->timeout  = priv->timeout;
+  status->timeleft = TICK2MSEC(wd_gettime(&priv->wdog));
+  return OK;
+}
+
+static int sim_wdt_settimeout(struct watchdog_lowerhalf_s *lower,
+                              uint32_t timeout)
+{
+  struct sim_wdt_s *priv = (struct sim_wdt_s *)lower;
+
+  if (timeout == 0)
+    {
+      return -EINVAL;
+    }
+
+  priv->timeout = timeout;
+  return sim_wdt_keepalive(lower);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sim_watchdog_initialize
+ *
+ * Description:
+ *   Register the simulated watchdog at CONFIG_WATCHDOG_DEVPATH.
+ *
+ ****************************************************************************/
+
+int sim_watchdog_initialize(void)

Review Comment:
   yes, you are right. how about move sim watchdog to the common place like 
timer_wdog.c?



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