wdt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * $Log: wdt.c,v $
  36. *
  37. */
  38. #include <sys/timer.h>
  39. #include <dev/watchdog.h>
  40. #include <arch/avr32.h>
  41. #include <avr32/io.h>
  42. // AP7000 includes lack this define, even though they are the same as UC30512
  43. #if !defined(AVR32_WDT_KEY_VALUE)
  44. #define AVR32_WDT_KEY_VALUE 0x00000055
  45. #endif
  46. /*!
  47. * \addtogroup xgNutArchAvr32DevWatchDog
  48. */
  49. /*@{*/
  50. static ureg_t nested;
  51. static void wdt_set_ctrl(unsigned long ctrl)
  52. {
  53. AVR32_WDT.ctrl = ctrl | (AVR32_WDT_KEY_VALUE << AVR32_WDT_CTRL_KEY_OFFSET);
  54. AVR32_WDT.ctrl = ctrl | ((~AVR32_WDT_KEY_VALUE << AVR32_WDT_CTRL_KEY_OFFSET) & AVR32_WDT_CTRL_KEY_MASK);
  55. }
  56. static long long wdt_get_us_timeout_period(void)
  57. {
  58. unsigned int slowclock = NutArchClockGet(NUT_HWCLK_SLOW_CLOCK);
  59. // Read CTRL.PSEL and translate it into us.
  60. return (AVR32_WDT.ctrl & AVR32_WDT_CTRL_EN_MASK) ?
  61. ((1ULL << (((AVR32_WDT.ctrl & AVR32_WDT_CTRL_PSEL_MASK) >> AVR32_WDT_CTRL_PSEL_OFFSET) + 1)) *
  62. 1000000 + slowclock / 2) / slowclock : -1;
  63. }
  64. /*!
  65. * \brief Start the AVR32 hardware watch dog timer.
  66. *
  67. * For portability, applications should use the platform independent
  68. * \ref xgWatchDog "Watchdog Driver API".
  69. */
  70. uint32_t Avr32WatchDogStart(uint32_t ms)
  71. {
  72. unsigned long long int timeout = ms * 1000;
  73. unsigned int slowclock = NutArchClockGet(NUT_HWCLK_SLOW_CLOCK);
  74. Avr32WatchDogDisable();
  75. #define MIN_US_TIMEOUT_PERIOD \
  76. (((1ULL << 1 ) * 1000000 + slowclock / 2) / slowclock)
  77. #define MAX_US_TIMEOUT_PERIOD \
  78. (((1ULL << (1 << AVR32_WDT_CTRL_PSEL_SIZE)) * 1000000 + slowclock / 2) / slowclock)
  79. // Set the CTRL.EN bit and translate the us timeout to fit in CTRL.PSEL using
  80. // the formula wdt = 2pow(PSEL+1) / fRCosc in useconds.
  81. if (timeout < MIN_US_TIMEOUT_PERIOD)
  82. timeout = MIN_US_TIMEOUT_PERIOD;
  83. else if (timeout > MAX_US_TIMEOUT_PERIOD)
  84. timeout = MAX_US_TIMEOUT_PERIOD;
  85. wdt_set_ctrl(AVR32_WDT_CTRL_EN_MASK |
  86. ((32 - __builtin_clz(((((timeout * slowclock + 500000) / 1000000) << 1) - 1) >> 1) -
  87. 1) << AVR32_WDT_CTRL_PSEL_OFFSET));
  88. Avr32WatchDogRestart();
  89. nested = 1;
  90. // Return the actual wdt period in us.
  91. return wdt_get_us_timeout_period() / 1000;
  92. }
  93. /*!
  94. * \brief Re-start the AVR32 hardware watch dog timer.
  95. *
  96. * For portability, applications should use the platform independent
  97. * \ref xgWatchDog "Watchdog Driver API".
  98. */
  99. void Avr32WatchDogRestart(void)
  100. {
  101. wdt_set_ctrl(AVR32_WDT.ctrl | AVR32_WDT_CTRL_EN_MASK);
  102. AVR32_WDT.clr = 0;
  103. }
  104. /*!
  105. * \brief Disable the AVR32 hardware watch dog timer.
  106. *
  107. * For portability, applications should use the platform independent
  108. * \ref xgWatchDog "Watchdog Driver API".
  109. */
  110. void Avr32WatchDogDisable(void)
  111. {
  112. if (nested) {
  113. nested++;
  114. }
  115. wdt_set_ctrl(AVR32_WDT.ctrl & ~AVR32_WDT_CTRL_EN_MASK);
  116. }
  117. /*!
  118. * \brief Enable the AT91 hardware watch dog timer.
  119. *
  120. * For portability, applications should use the platform independent
  121. * \ref xgWatchDog "Watchdog Driver API".
  122. */
  123. void Avr32WatchDogEnable(void)
  124. {
  125. if (nested > 1 && --nested == 1) {
  126. Avr32WatchDogRestart();
  127. }
  128. }
  129. /*@}*/