daniel-p-carvalho commented on code in PR #20782: URL: https://github.com/apache/nuttx/pull/20782#discussion_r4354813668
########## 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: timer_wdog.c is a timer lower half (timer_lowerhalf_s, exposed as /dev/timerN) built on the kernel wd_start(), which is the same primitive sim_watchdog.c already uses directly. sim_watchdog could sit on top of timer_wdog, but the behavior does not match: timer_wdog's stop() clears the callback (a start() after a stop() returns -EINVAL), settimeout() does not rearm, and the callback is periodic (it restarts with wd_start_next). So I would need an adapter that sets the callback again on every start and returns false to be one-shot, plus a dependency on CONFIG_TIMER and a /dev/timerN registered only for this. That is more code than the ten or so lines of wd_start()/wd_cancel() it has today, so I kept wd_start() directly. If you prefer the adapter, I can do it. ########## drivers/timers/Kconfig: ########## @@ -579,6 +579,62 @@ endif # !WATCHDOG_AUTOMONITOR_BY_CAPTURE && !WATCHDOG_AUTOMONITOR_BY_IDLE endif # WATCHDOG_AUTOMONITOR +menuconfig TASKWDT + bool "Task watchdog supervisor" + depends on !WATCHDOG_AUTOMONITOR && SCHED_LPWORK + ---help--- + Multiplex one hardware watchdog among any number of tasks. Each task + opens CONFIG_TASKWDT_DEVPATH to get its own channel with its own + timeout and feeds it with write() or WDIOC_KEEPALIVE, as on the + Linux watchdog API. The hardware watchdog is fed by the supervisor + only while all the armed channels are within their timeouts. + See include/nuttx/timers/taskwdt.h. + + This replaces the Auto-monitor, which feeds the watchdog + unconditionally. + +if TASKWDT + +config TASKWDT_DEVPATH Review Comment: I have no objection to changing the prefix from TASKWDT_ to WATCHDOG_TASKMONITOR_ (Kconfig, driver, header, defconfig and docs), but it would also require a nuttx-apps PR: the test app under apps/testing/drivers/taskwdt, already merged in nuttx-apps#3874, uses CONFIG_TASKWDT and CONFIG_TASKWDT_DEVPATH. Both would have to land together so sim:taskwdt is not broken in between. If you want the change, I will do both PRs, together with the other changes of this 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]
