xiaoxiang781216 commented on code in PR #20915: URL: https://github.com/apache/nuttx/pull/20915#discussion_r4359831216
########## drivers/timers/watchdog_wdog.c: ########## @@ -0,0 +1,211 @@ +/**************************************************************************** + * drivers/timers/watchdog_wdog.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 <assert.h> +#include <errno.h> +#include <stdint.h> +#include <stdbool.h> + +#include <nuttx/board.h> +#include <nuttx/clock.h> +#include <nuttx/debug.h> +#include <nuttx/kmalloc.h> +#include <nuttx/wdog.h> +#include <nuttx/timers/watchdog.h> +#include <nuttx/timers/watchdog_wdog.h> + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct watchdog_wdog_s +{ + struct watchdog_lowerhalf_s lower; /* Must be first */ + struct wdog_s wdog; /* The timer that expires */ + uint32_t timeout; /* Timeout in milliseconds */ + bool started; /* Is the watchdog running */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int watchdog_wdog_start(FAR struct watchdog_lowerhalf_s *lower); +static int watchdog_wdog_stop(FAR struct watchdog_lowerhalf_s *lower); +static int watchdog_wdog_keepalive(FAR struct watchdog_lowerhalf_s *lower); +static int watchdog_wdog_getstatus(FAR struct watchdog_lowerhalf_s *lower, + FAR struct watchdog_status_s *status); +static int watchdog_wdog_settimeout(FAR struct watchdog_lowerhalf_s *lower, + uint32_t timeout); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct watchdog_ops_s g_watchdog_wdog_ops = +{ + .start = watchdog_wdog_start, + .stop = watchdog_wdog_stop, + .keepalive = watchdog_wdog_keepalive, + .getstatus = watchdog_wdog_getstatus, + .settimeout = watchdog_wdog_settimeout, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: watchdog_wdog_expire + * + * Description: + * The wdog handler: the watchdog was not fed in time. + * + ****************************************************************************/ + +static void watchdog_wdog_expire(wdparm_t arg) +{ + _alert("Watchdog expired\n"); + +#if defined(CONFIG_WATCHDOG_WDOG_REBOOT) + board_reset(CONFIG_WATCHDOG_WDOG_STATUS); +#elif defined(CONFIG_WATCHDOG_WDOG_POWEROFF) + board_power_off(CONFIG_WATCHDOG_WDOG_STATUS); +#else + PANIC(); +#endif +} + +/**************************************************************************** + * Name: watchdog_wdog_arm + ****************************************************************************/ + +static int watchdog_wdog_arm(FAR struct watchdog_wdog_s *priv) +{ + return wd_start(&priv->wdog, MSEC2TICK(priv->timeout), + watchdog_wdog_expire, (wdparm_t)priv); +} + +static int watchdog_wdog_start(FAR struct watchdog_lowerhalf_s *lower) +{ + FAR struct watchdog_wdog_s *priv = (FAR struct watchdog_wdog_s *)lower; + int ret; + + ret = watchdog_wdog_arm(priv); + if (ret >= 0) + { + priv->started = true; + } + + return ret; +} + +static int watchdog_wdog_stop(FAR struct watchdog_lowerhalf_s *lower) +{ + FAR struct watchdog_wdog_s *priv = (FAR struct watchdog_wdog_s *)lower; + + priv->started = false; + wd_cancel(&priv->wdog); + return OK; +} + +static int watchdog_wdog_keepalive(FAR struct watchdog_lowerhalf_s *lower) +{ + FAR struct watchdog_wdog_s *priv = (FAR struct watchdog_wdog_s *)lower; + + return priv->started ? watchdog_wdog_arm(priv) : OK; +} + +static int watchdog_wdog_getstatus(FAR struct watchdog_lowerhalf_s *lower, + FAR struct watchdog_status_s *status) +{ + FAR struct watchdog_wdog_s *priv = (FAR struct watchdog_wdog_s *)lower; + + status->flags = priv->started ? WDFLAGS_ACTIVE | WDFLAGS_RESET : 0; + status->timeout = priv->timeout; + status->timeleft = 0; + + if (priv->started) + { + /* wd_start() adds a tick, do not report more than the timeout */ + + status->timeleft = TICK2MSEC(wd_gettime(&priv->wdog)); + if (status->timeleft > priv->timeout) Review Comment: why need ########## boards/sim/sim/sim/src/sim_bringup.c: ########## @@ -599,6 +603,14 @@ int sim_bringup(void) } #endif +#ifdef CONFIG_WATCHDOG_WDOG + ret = watchdog_wdog_initialize(CONFIG_WATCHDOG_DEVPATH); Review Comment: where we define CONFIG_WATCHDOG_DEVPATH ########## boards/sim/sim/sim/src/CMakeLists.txt: ########## @@ -46,6 +46,10 @@ if(CONFIG_EXAMPLES_GPIO) endif() endif() +if(CONFIG_SIM_WATCHDOG) Review Comment: please merge the first two patch into one -- 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]
