xiaoxiang781216 commented on code in PR #20782: URL: https://github.com/apache/nuttx/pull/20782#discussion_r4307926189
########## 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: change prefix from TASKWDT_ to WATCHDOG_TASKMONITOR_ ########## 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: why not reuse https://github.com/apache/nuttx/blob/master/drivers/timers/timer_wdog.c#L42 ########## drivers/timers/Kconfig: ########## @@ -579,6 +579,62 @@ endif # !WATCHDOG_AUTOMONITOR_BY_CAPTURE && !WATCHDOG_AUTOMONITOR_BY_IDLE endif # WATCHDOG_AUTOMONITOR +menuconfig TASKWDT Review Comment: WATCHDOG_TASKMONITOR ########## drivers/timers/taskwdt.c: ########## @@ -0,0 +1,436 @@ +/**************************************************************************** + * drivers/timers/taskwdt.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 <sys/types.h> +#include <stdint.h> +#include <stdbool.h> +#include <string.h> +#include <fcntl.h> +#include <assert.h> +#include <errno.h> +#include <nuttx/debug.h> + +#include <nuttx/clock.h> +#include <nuttx/fs/fs.h> +#include <nuttx/kmalloc.h> +#include <nuttx/list.h> +#include <nuttx/mutex.h> +#include <nuttx/sched.h> +#include <nuttx/wqueue.h> +#include <nuttx/timers/taskwdt.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define TASKWDT_INTERVAL_TICK MSEC2TICK(CONFIG_TASKWDT_INTERVAL) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* A channel is the state behind one open() of the device */ + +struct taskwdt_chan_s +{ + struct list_node node; /* Entry in taskwdt_s.chans */ + pid_t pid; /* Task that opened it */ + char name[CONFIG_TASK_NAME_SIZE + 1]; /* Its name */ + clock_t timeout; /* Timeout in ticks */ + clock_t deadline; /* Tick the channel expires */ + bool armed; /* Is the channel supervised */ +}; + +struct taskwdt_s +{ + struct file wdt; /* The hardware watchdog */ + struct work_s work; /* Checks the channels, feeds the hardware */ + struct list_node chans; /* All the channels (struct taskwdt_chan_s) */ + mutex_t lock; /* Protects everything above and below */ + bool started; /* The hardware watchdog is armed */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int taskwdt_open(FAR struct file *filep); Review Comment: but why not integrate task wdt into https://github.com/apache/nuttx/blob/master/drivers/timers/watchdog.c#L471? we have a similar patch implement in this way: https://github.com/xiaoxiang781216/nuttx/commit/a2c01567a8332b638ef5116d6a040915e2642329 could you cherry-pick it and continue enhance it? -- 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]
