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
|
|
|
*
|
2020-03-11 20:51:28 +01:00
|
|
|
* 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
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-11 20:51:28 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-11 20:51:28 +01:00
|
|
|
* 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.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
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
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* 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;
|
|
|
|
}
|