wd_initialize.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. * sched/wdog/wd_initialize.c
  3. *
  4. * Copyright (C) 2007, 2009, 2014 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <queue.h>
  40. #include "wdog/wdog.h"
  41. /****************************************************************************
  42. * Public Data
  43. ****************************************************************************/
  44. /* The g_wdfreelist data structure is a singly linked list of watchdogs
  45. * available to the system for delayed function use.
  46. */
  47. sq_queue_t g_wdfreelist;
  48. /* The g_wdactivelist data structure is a singly linked list ordered by
  49. * watchdog expiration time. When watchdog timers expire,the functions on
  50. * this linked list are removed and the function is called.
  51. */
  52. sq_queue_t g_wdactivelist;
  53. /* This is the number of free, pre-allocated watchdog structures in the
  54. * g_wdfreelist. This value is used to enforce a reserve for interrupt
  55. * handlers.
  56. */
  57. uint16_t g_wdnfree;
  58. /* This is wdog tickbase, for wd_gettime() may called many times
  59. * between 2 times of wd_timer(), we use it to update wd_gettime().
  60. */
  61. #ifdef CONFIG_SCHED_TICKLESS
  62. clock_t g_wdtickbase;
  63. #endif
  64. /****************************************************************************
  65. * Private Data
  66. ****************************************************************************/
  67. /* g_wdpool is a list of pre-allocated watchdogs. The number of watchdogs
  68. * in the pool is a configuration item.
  69. */
  70. static struct wdog_s g_wdpool[CONFIG_PREALLOC_WDOGS];
  71. /****************************************************************************
  72. * Public Functions
  73. ****************************************************************************/
  74. /****************************************************************************
  75. * Name: wd_initialize
  76. *
  77. * Description:
  78. * This function initializes the watchdog data structures
  79. *
  80. * Input Parameters:
  81. * None
  82. *
  83. * Returned Value:
  84. * None
  85. *
  86. * Assumptions:
  87. * This function must be called early in the initialization sequence
  88. * before the timer interrupt is attached and before any watchdog
  89. * services are used.
  90. *
  91. ****************************************************************************/
  92. void wd_initialize(void)
  93. {
  94. FAR struct wdog_s *wdog = g_wdpool;
  95. int i;
  96. /* Initialize watchdog lists */
  97. sq_init(&g_wdfreelist);
  98. sq_init(&g_wdactivelist);
  99. /* The g_wdfreelist must be loaded at initialization time to hold the
  100. * configured number of watchdogs.
  101. */
  102. for (i = 0; i < CONFIG_PREALLOC_WDOGS; i++)
  103. {
  104. sq_addlast((FAR sq_entry_t *)wdog++, &g_wdfreelist);
  105. }
  106. /* All watchdogs are free */
  107. g_wdnfree = CONFIG_PREALLOC_WDOGS;
  108. }