2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2014-08-08 22:21:48 +02:00
|
|
|
* sched/wdog/wd_start.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2014-08-07 19:39:16 +02:00
|
|
|
* Copyright (C) 2007-2009, 2012, 2014 Gregory Nutt. All rights reserved.
|
2012-05-02 17:36:19 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-01-31 18:59:22 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <stdint.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2007-02-21 22:55:16 +01:00
|
|
|
#include <stdarg.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <wdog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
2014-08-07 19:39:16 +02:00
|
|
|
#include <assert.h>
|
2007-02-21 22:55:16 +01:00
|
|
|
#include <errno.h>
|
2009-06-17 00:08:59 +02:00
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 22:21:48 +02:00
|
|
|
#include "wdog/wdog.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2009-12-14 22:15:18 +01:00
|
|
|
* Pre-processor Definitions
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
# define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Type Declarations
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
typedef void (*wdentry0_t)(int argc);
|
|
|
|
#if CONFIG_MAX_WDOGPARMS > 0
|
2009-12-14 19:39:29 +01:00
|
|
|
typedef void (*wdentry1_t)(int argc, uint32_t arg1);
|
2007-02-21 22:55:16 +01:00
|
|
|
#endif
|
|
|
|
#if CONFIG_MAX_WDOGPARMS > 1
|
2009-12-14 19:39:29 +01:00
|
|
|
typedef void (*wdentry2_t)(int argc, uint32_t arg1, uint32_t arg2);
|
2007-02-21 22:55:16 +01:00
|
|
|
#endif
|
|
|
|
#if CONFIG_MAX_WDOGPARMS > 2
|
2009-12-14 19:39:29 +01:00
|
|
|
typedef void (*wdentry3_t)(int argc, uint32_t arg1, uint32_t arg2,
|
|
|
|
uint32_t arg3);
|
2007-02-21 22:55:16 +01:00
|
|
|
#endif
|
|
|
|
#if CONFIG_MAX_WDOGPARMS > 3
|
2009-12-14 19:39:29 +01:00
|
|
|
typedef void (*wdentry4_t)(int argc, uint32_t arg1, uint32_t arg2,
|
|
|
|
uint32_t arg3, uint32_t arg4);
|
2007-02-21 22:55:16 +01:00
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Global Variables
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Variables
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Functions
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2014-08-07 19:39:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: wd_expiration
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the timer for the watchdog at the head of list is ready to
|
|
|
|
* run. If so, remove the watchdog from the list and execute it.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void wd_expiration(void)
|
|
|
|
{
|
|
|
|
FAR wdog_t *wdog;
|
|
|
|
|
|
|
|
/* Check if the watchdog at the head of the list is ready to run */
|
|
|
|
|
|
|
|
if (((FAR wdog_t*)g_wdactivelist.head)->lag <= 0)
|
|
|
|
{
|
|
|
|
/* Process the watchdog at the head of the list as well as any
|
|
|
|
* other watchdogs that became ready to run at this time
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (g_wdactivelist.head &&
|
|
|
|
((FAR wdog_t*)g_wdactivelist.head)->lag <= 0)
|
|
|
|
{
|
|
|
|
/* Remove the watchdog from the head of the list */
|
|
|
|
|
|
|
|
wdog = (FAR wdog_t*)sq_remfirst(&g_wdactivelist);
|
|
|
|
|
|
|
|
/* If there is another watchdog behind this one, update its
|
|
|
|
* its lag (this shouldn't be necessary).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_wdactivelist.head)
|
|
|
|
{
|
|
|
|
((FAR wdog_t*)g_wdactivelist.head)->lag += wdog->lag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indicate that the watchdog is no longer active. */
|
|
|
|
|
|
|
|
wdog->active = false;
|
|
|
|
|
|
|
|
/* Execute the watchdog function */
|
|
|
|
|
|
|
|
up_setpicbase(wdog->picbase);
|
|
|
|
switch (wdog->argc)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
DEBUGPANIC();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
(*((wdentry0_t)(wdog->func)))(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if CONFIG_MAX_WDOGPARMS > 0
|
|
|
|
case 1:
|
|
|
|
(*((wdentry1_t)(wdog->func)))(1, wdog->parm[0]);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_MAX_WDOGPARMS > 1
|
|
|
|
case 2:
|
|
|
|
(*((wdentry2_t)(wdog->func)))(2,
|
|
|
|
wdog->parm[0], wdog->parm[1]);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_MAX_WDOGPARMS > 2
|
|
|
|
case 3:
|
|
|
|
(*((wdentry3_t)(wdog->func)))(3,
|
|
|
|
wdog->parm[0], wdog->parm[1],
|
|
|
|
wdog->parm[2]);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_MAX_WDOGPARMS > 3
|
|
|
|
case 4:
|
|
|
|
(*((wdentry4_t)(wdog->func)))(4,
|
|
|
|
wdog->parm[0], wdog->parm[1],
|
|
|
|
wdog->parm[2] ,wdog->parm[3]);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: wd_start
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function adds a watchdog to the timer queue. The specified
|
|
|
|
* watchdog function will be called from the interrupt level after the
|
|
|
|
* specified number of ticks has elapsed. Watchdog timers may be started
|
|
|
|
* from the interrupt level.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2014-08-07 19:39:16 +02:00
|
|
|
* Watchdog timers execute in the address environment that was in effect
|
2012-07-14 21:30:31 +02:00
|
|
|
* when wd_start() is called.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Watchdog timers execute only once.
|
|
|
|
*
|
2012-07-14 21:30:31 +02:00
|
|
|
* To replace either the timeout delay or the function to be executed,
|
|
|
|
* call wd_start again with the same wdog; only the most recent wdStart()
|
|
|
|
* on a given watchdog ID has any effect.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
2008-01-31 18:59:22 +01:00
|
|
|
* wdog = watchdog ID
|
|
|
|
* delay = Delay count in clock ticks
|
|
|
|
* wdentry = function to call on timeout
|
2007-02-18 00:21:28 +01:00
|
|
|
* parm1..4 = parameters to pass to wdentry
|
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* OK or ERROR
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2012-07-14 21:30:31 +02:00
|
|
|
* The watchdog routine runs in the context of the timer interrupt handler
|
|
|
|
* and is subject to all ISR restrictions.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-13 19:01:46 +01:00
|
|
|
int wd_start(WDOG_ID wdog, int delay, wdentry_t wdentry, int argc, ...)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-21 22:55:16 +01:00
|
|
|
va_list ap;
|
2008-01-31 18:59:22 +01:00
|
|
|
FAR wdog_t *curr;
|
|
|
|
FAR wdog_t *prev;
|
|
|
|
FAR wdog_t *next;
|
2009-12-14 19:39:29 +01:00
|
|
|
int32_t now;
|
2007-02-21 22:55:16 +01:00
|
|
|
irqstate_t saved_state;
|
2014-08-07 19:39:16 +02:00
|
|
|
#ifdef CONFIG_SCHED_TICKLESS
|
|
|
|
bool reassess = false;
|
|
|
|
#endif
|
2007-02-21 22:55:16 +01:00
|
|
|
int i;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
/* Verify the wdog */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
if (!wdog || argc > CONFIG_MAX_WDOGPARMS || delay < 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-07-14 21:30:31 +02:00
|
|
|
set_errno(EINVAL);
|
2007-02-18 00:21:28 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the watchdog has been started. If so, stop it.
|
|
|
|
* NOTE: There is a race condition here... the caller may receive
|
|
|
|
* the watchdog between the time that wd_start is called and
|
|
|
|
* the critical section is established.
|
|
|
|
*/
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
2007-02-21 22:55:16 +01:00
|
|
|
if (wdog->active)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-21 22:55:16 +01:00
|
|
|
wd_cancel(wdog);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the data in the watchdog structure */
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
wdog->func = wdentry; /* Function to execute when delay expires */
|
2009-06-17 23:15:31 +02:00
|
|
|
up_getpicbase(&wdog->picbase);
|
2008-01-31 18:59:22 +01:00
|
|
|
wdog->argc = argc;
|
2007-02-21 22:55:16 +01:00
|
|
|
|
|
|
|
va_start(ap, argc);
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
2009-12-14 19:39:29 +01:00
|
|
|
wdog->parm[i] = va_arg(ap, uint32_t);
|
2007-02-21 22:55:16 +01:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
for (; i < CONFIG_MAX_WDOGPARMS; i++)
|
|
|
|
{
|
|
|
|
wdog->parm[i] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
va_end(ap);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Calculate delay+1, forcing the delay into a range that we can handle */
|
|
|
|
|
|
|
|
if (delay <= 0)
|
|
|
|
{
|
|
|
|
delay = 1;
|
|
|
|
}
|
|
|
|
else if (++delay <= 0)
|
|
|
|
{
|
|
|
|
delay--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the easy case first -- when the watchdog timer queue is empty. */
|
|
|
|
|
|
|
|
if (g_wdactivelist.head == NULL)
|
|
|
|
{
|
2014-08-07 19:39:16 +02:00
|
|
|
/* Add the watchdog to the head of the queue. */
|
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
sq_addlast((FAR sq_entry_t*)wdog,&g_wdactivelist);
|
2014-08-07 19:39:16 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_TICKLESS
|
|
|
|
/* Whenever the watchdog at the head of the queue changes, then we
|
|
|
|
* need to reassess the interval timer setting.
|
|
|
|
*/
|
|
|
|
|
|
|
|
reassess = true;
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* There are other active watchdogs in the timer queue */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
now = 0;
|
2008-01-31 23:25:31 +01:00
|
|
|
prev = curr = (FAR wdog_t*)g_wdactivelist.head;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Advance to positive time */
|
|
|
|
|
|
|
|
while ((now += curr->lag) < 0 && curr->next)
|
|
|
|
{
|
|
|
|
prev = curr;
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance past shorter delays */
|
|
|
|
|
|
|
|
while (now <= delay && curr->next)
|
|
|
|
{
|
|
|
|
prev = curr;
|
|
|
|
curr = curr->next;
|
|
|
|
now += curr->lag;
|
|
|
|
}
|
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
/* Check if the new wdog must be inserted before the curr. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
if (delay < now)
|
|
|
|
{
|
|
|
|
/* The relative delay time is smaller or equal to the current delay
|
|
|
|
* time, so decrement the current delay time by the new relative
|
|
|
|
* delay time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
delay -= (now - curr->lag);
|
|
|
|
curr->lag -= delay;
|
|
|
|
|
|
|
|
/* Insert the new watchdog in the list */
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
if (curr == (FAR wdog_t*)g_wdactivelist.head)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-08-07 19:39:16 +02:00
|
|
|
/* Insert the watchdog in mid- or end-of-queue */
|
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
sq_addfirst((FAR sq_entry_t*)wdog, &g_wdactivelist);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-07 19:39:16 +02:00
|
|
|
/* Insert the watchdog in mid- or end-of-queue */
|
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
sq_addafter((FAR sq_entry_t*)prev, (FAR sq_entry_t*)wdog,
|
2007-02-18 00:21:28 +01:00
|
|
|
&g_wdactivelist);
|
2014-08-07 19:39:16 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_TICKLESS
|
|
|
|
/* If the watchdog at the head of the queue changes, then we
|
|
|
|
* need to reassess the interval timer setting.
|
|
|
|
*/
|
|
|
|
|
|
|
|
reassess = true;
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The new watchdog delay time is greater than the curr delay time,
|
2007-02-21 22:55:16 +01:00
|
|
|
* so the new wdog must be inserted after the curr. This only occurs
|
|
|
|
* if the wdog is to be added to the end of the list.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delay -= now;
|
|
|
|
if (!curr->next)
|
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
sq_addlast((FAR sq_entry_t*)wdog, &g_wdactivelist);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next = curr->next;
|
|
|
|
next->lag -= delay;
|
2007-02-27 22:17:21 +01:00
|
|
|
sq_addafter((FAR sq_entry_t*)curr, (FAR sq_entry_t*)wdog,
|
2007-02-18 00:21:28 +01:00
|
|
|
&g_wdactivelist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put the lag into the watchdog structure and mark it as active. */
|
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
wdog->lag = delay;
|
2009-12-14 22:15:18 +01:00
|
|
|
wdog->active = true;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
#ifdef CONFIG_SCHED_TICKLESS
|
|
|
|
/* Reassess the interval timer that will generate the next interval event.
|
|
|
|
* In many cases, this will be unnecessary: This is really only necessary
|
|
|
|
* when the watchdog timer at the head of the queue is change. If the
|
|
|
|
* timer is inserted later in the queue then the timer at the head is
|
|
|
|
* unchanged.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (reassess)
|
|
|
|
{
|
|
|
|
sched_timer_reassess();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
irqrestore(saved_state);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: wd_timer
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2008-01-31 18:59:22 +01:00
|
|
|
* This function is called from the timer interrupt handler to determine
|
|
|
|
* if it is time to execute a watchdog function. If so, the watchdog
|
2014-08-07 19:39:16 +02:00
|
|
|
* function will be executed in the context of the timer interrupt
|
|
|
|
* handler.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
2014-08-07 19:39:16 +02:00
|
|
|
* ticks - If CONFIG_SCHED_TICKLESS is defined then the number of ticks
|
|
|
|
* in the the interval that just expired is provided. Otherwise,
|
|
|
|
* this function is called on each timer interrupt and a value of one
|
|
|
|
* is implicit.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Return Value:
|
2014-08-07 19:39:16 +02:00
|
|
|
* If CONFIG_SCHED_TICKLESS is defined then the number of ticks for the
|
|
|
|
* next delay is provided (zero if no delay). Otherwise, this function
|
|
|
|
* has no returned value.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2014-08-07 19:39:16 +02:00
|
|
|
* Called from interrupt handler logic with interrupts disabled.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
#ifdef CONFIG_SCHED_TICKLESS
|
|
|
|
unsigned int wd_timer(int ticks)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2008-01-31 18:59:22 +01:00
|
|
|
FAR wdog_t *wdog;
|
2014-08-07 19:39:16 +02:00
|
|
|
int decr;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Check if there are any active watchdogs to process */
|
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
while (g_wdactivelist.head && ticks > 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-08-07 19:39:16 +02:00
|
|
|
/* Get the watchdog at the head of the list */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
wdog = (FAR wdog_t*)g_wdactivelist.head;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
/* Decrement the lag for this watchdog.
|
|
|
|
*
|
|
|
|
* There is logic to handle the case where ticks is greater than
|
|
|
|
* the watchdog lag, but if the scheduling is working properly
|
|
|
|
* that should never happen.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
DEBUGASSERT(ticks <= wdog->lag);
|
|
|
|
decr = MIN(wdog->lag, ticks);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
/* There are. Decrement the lag counter */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
wdog->lag -= decr;
|
|
|
|
ticks -= ticks;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
/* Check if the watchdog at the head of the list is ready to run */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
wd_expiration();
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
/* Return the delay for the next watchdog to expire */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
return g_wdactivelist.head ?
|
|
|
|
((FAR wdog_t*)g_wdactivelist.head)->lag : 0;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
#else
|
|
|
|
void wd_timer(void)
|
|
|
|
{
|
|
|
|
/* Check if there are any active watchdogs to process */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
if (g_wdactivelist.head)
|
|
|
|
{
|
|
|
|
/* There are. Decrement the lag counter */
|
2007-02-21 22:55:16 +01:00
|
|
|
|
2014-08-07 19:39:16 +02:00
|
|
|
--(((FAR wdog_t*)g_wdactivelist.head)->lag);
|
|
|
|
|
|
|
|
/* Check if the watchdog at the head of the list is ready to run */
|
|
|
|
|
|
|
|
wd_expiration();
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-07 19:39:16 +02:00
|
|
|
#endif /* CONFIG_SCHED_TICKLESS */
|