2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2014-08-08 22:21:48 +02:00
|
|
|
* sched/wdog/wd_create.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2016-02-14 15:17:46 +01:00
|
|
|
* Copyright (C) 2007-2009, 2014, 2016 Gregory Nutt. All rights reserved.
|
2012-07-14 21:30:31 +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
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <queue.h>
|
2013-02-06 16:43:28 +01:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/wdog.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/wdog.h>
|
2014-08-21 16:44:29 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
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
|
|
|
/****************************************************************************
|
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_create
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2014-04-13 22:32:20 +02:00
|
|
|
* Description:
|
2017-02-24 17:58:37 +01:00
|
|
|
* The wd_create function will create a watchdog timer by allocating one
|
|
|
|
* from the list of free watchdog timers.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2008-01-31 18:59:22 +01:00
|
|
|
* Pointer to watchdog (i.e., the watchdog ID), or NULL if insufficient
|
|
|
|
* watchdogs are available.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
WDOG_ID wd_create (void)
|
|
|
|
{
|
2014-08-22 16:46:34 +02:00
|
|
|
FAR struct wdog_s *wdog;
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-21 16:44:29 +02:00
|
|
|
/* These actions must be atomic with respect to other tasks and also with
|
|
|
|
* respect to interrupt handlers that may be allocating or freeing watchdog
|
|
|
|
* timers.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-21 16:44:29 +02:00
|
|
|
/* If we are in an interrupt handler -OR- if the number of pre-allocated
|
2017-05-11 21:35:56 +02:00
|
|
|
* timer structures exceeds the reserve, then take the next timer from
|
2014-08-21 16:44:29 +02:00
|
|
|
* the head of the free list.
|
|
|
|
*/
|
|
|
|
|
2014-11-11 01:15:26 +01:00
|
|
|
if (g_wdnfree > CONFIG_WDOG_INTRESERVE || up_interrupt_context())
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2015-08-21 19:30:22 +02:00
|
|
|
/* Remove the watchdog timer from the free list */
|
2014-08-21 16:44:29 +02:00
|
|
|
|
2014-08-22 16:46:34 +02:00
|
|
|
wdog = (FAR struct wdog_s *)sq_remfirst(&g_wdfreelist);
|
2014-08-21 16:44:29 +02:00
|
|
|
|
|
|
|
/* Did we get one? */
|
|
|
|
|
2015-08-21 20:33:14 +02:00
|
|
|
if (wdog != NULL)
|
2014-08-21 16:44:29 +02:00
|
|
|
{
|
2015-08-21 19:30:22 +02:00
|
|
|
/* Yes.. decrement the count of free, pre-allocated timers (all
|
|
|
|
* with interrupts disabled).
|
|
|
|
*/
|
|
|
|
|
2015-08-21 20:33:14 +02:00
|
|
|
DEBUGASSERT(g_wdnfree > 0);
|
2015-08-21 19:30:22 +02:00
|
|
|
g_wdnfree--;
|
|
|
|
|
|
|
|
/* Clear the forward link and all flags */
|
2014-08-21 16:44:29 +02:00
|
|
|
|
2015-08-21 20:33:14 +02:00
|
|
|
wdog->next = NULL;
|
2014-08-21 16:44:29 +02:00
|
|
|
wdog->flags = 0;
|
|
|
|
}
|
2015-08-21 19:30:22 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We didn't get one... The count should then be exactly zero */
|
|
|
|
|
|
|
|
DEBUGASSERT(g_wdnfree == 0);
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2014-08-21 16:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We are in a normal tasking context AND there are not enough unreserved,
|
|
|
|
* pre-allocated watchdog timers. We need to allocate one from the kernel
|
|
|
|
* heap.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We do not require that interrupts be disabled to do this. */
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2014-09-01 01:26:36 +02:00
|
|
|
wdog = (FAR struct wdog_s *)kmm_malloc(sizeof(struct wdog_s));
|
2014-08-21 16:44:29 +02:00
|
|
|
|
|
|
|
/* Did we get one? */
|
|
|
|
|
|
|
|
if (wdog)
|
|
|
|
{
|
|
|
|
/* Yes.. Clear the forward link and set the allocated flag */
|
|
|
|
|
|
|
|
wdog->next = NULL;
|
|
|
|
wdog->flags = WDOGF_ALLOCED;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
return (WDOG_ID)wdog;
|
|
|
|
}
|