daniel-p-carvalho commented on code in PR #20915: URL: https://github.com/apache/nuttx/pull/20915#discussion_r4356767171
########## drivers/timers/Kconfig: ########## @@ -480,6 +480,20 @@ menuconfig WATCHDOG if WATCHDOG +config WATCHDOG_WDOG + bool "Watchdog implemented by wdog timer" + default n + depends on !WATCHDOG_AUTOMONITOR_BY_ONESHOT && !WATCHDOG_AUTOMONITOR_BY_TIMER Review Comment: With WATCHDOG_AUTOMONITOR_BY_ONESHOT and _BY_TIMER, watchdog_register() takes a third argument (the oneshot or timer lower half), so the call in this driver would not compile, and this driver has nothing to pass. I added the reason to the help text. If you prefer, I can drop the dependency and pass NULL under those two modes, but then the auto-monitor would dereference it, so I kept the dependency. ########## drivers/timers/watchdog_wdog.c: ########## @@ -0,0 +1,215 @@ +/**************************************************************************** + * 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/clock.h> +#include <nuttx/kmalloc.h> +#include <nuttx/wdog.h> +#include <nuttx/timers/watchdog.h> +#include <nuttx/timers/watchdog_wdog.h> + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* expire is called from the wdog timer context when the watchdog expires */ + +struct watchdog_wdog_s +{ + struct watchdog_lowerhalf_s lower; /* Must be first */ + struct wdog_s wdog; /* The timer that expires */ + CODE void (*expire)(void); + 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) +{ + FAR struct watchdog_wdog_s *priv = (FAR struct watchdog_wdog_s *)arg; + + if (priv->expire != NULL) + { + priv->expire(); + } + else + { + PANIC(); + } +} + +/**************************************************************************** + * 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); Review Comment: I can not find wd_cancel_sync() in master: include/nuttx/wdog.h only has wd_cancel(), and the name only appears in the 12.10 release notes (#16342). Is it in another branch or PR? If it lands I will use it in stop(), so that the handler can not be running when stop() returns. ########## drivers/timers/watchdog_wdog.c: ########## @@ -0,0 +1,215 @@ +/**************************************************************************** + * 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/clock.h> +#include <nuttx/kmalloc.h> +#include <nuttx/wdog.h> +#include <nuttx/timers/watchdog.h> +#include <nuttx/timers/watchdog_wdog.h> + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* expire is called from the wdog timer context when the watchdog expires */ + +struct watchdog_wdog_s +{ + struct watchdog_lowerhalf_s lower; /* Must be first */ + struct wdog_s wdog; /* The timer that expires */ + CODE void (*expire)(void); + 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) +{ + FAR struct watchdog_wdog_s *priv = (FAR struct watchdog_wdog_s *)arg; + + if (priv->expire != NULL) + { + priv->expire(); + } + else + { + PANIC(); Review Comment: Good idea, done. CONFIG_WATCHDOG_WDOG_ACTION now selects panic, reboot (board_reset()) or power off (board_power_off()), and CONFIG_WATCHDOG_WDOG_STATUS is the status given to the board. This removed the expire callback from watchdog_wdog_initialize() (it only takes the path now, the timeout is CONFIG_WATCHDOG_WDOG_TIMEOUT) and the board file sim_watchdog.c: sim_bringup() calls it when CONFIG_WATCHDOG_WDOG is set, like timer_wdog, and sim:watchdog selects power off with status 77. -- 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]
