lpc17xx_wdt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  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. * \verbatim
  36. * $Id:$
  37. * \endverbatim
  38. */
  39. #include <sys/timer.h>
  40. #include <dev/watchdog.h>
  41. #if defined(MCU_LPC176x)
  42. #include <arch/cm3/nxp/lpc176x.h>
  43. #include <arch/cm3/nxp/lpc176x_clk.h>
  44. #include <arch/cm3/nxp/lpc176x_wdt.h>
  45. #elif defined(MCU_LPC177x_8x)
  46. #include <arch/cm3/nxp/lpc177x_8x.h>
  47. #include <arch/cm3/nxp/lpc177x_8x_clk.h>
  48. #include <arch/cm3/nxp/lpc177x_8x_wwdt.h>
  49. #elif defined(MCU_LPC407x_8x)
  50. #include <arch/cm3/nxp/lpc407x_8x.h>
  51. #include <arch/cm3/nxp/lpc407x_8x_clk.h>
  52. #include <arch/cm3/nxp/lpc177x_8x_wwdt.h>
  53. #else
  54. #warning "Unknown LPC familiy"
  55. #endif
  56. /*!
  57. * \addtogroup xgNutArchCm3Lpc17xxDevWatchDog
  58. */
  59. /*@{*/
  60. //static ureg_t nested;
  61. /*!
  62. * \brief Start the LPC17xx hardware watch dog timer.
  63. *
  64. * \param timeout watchdog timeout in µs
  65. * \return 0 on success or -1 in case of an error.
  66. */
  67. static int Lpc17xxWatchDogSetTimeOut(uint32_t timeout)
  68. {
  69. uint32_t val = WDT_GET_FROM_USEC(timeout);
  70. int rc = 0;
  71. if (val < WDT_TIMEOUT_MIN) {
  72. val = WDT_TIMEOUT_MIN;
  73. rc = -1;
  74. } else
  75. if (val > WDT_TIMEOUT_MAX) {
  76. val = WDT_TIMEOUT_MAX;
  77. rc = -1;
  78. }
  79. LPC_WDT->TC = val;
  80. return rc;
  81. }
  82. /*!
  83. * \brief Start the LPC17xx hardware watch dog timer.
  84. *
  85. * For portability, applications should use the platform independent
  86. * \ref xgWatchDog "Watchdog Driver API".
  87. */
  88. uint32_t Lpc17xxWatchDogStart(uint32_t ms, uint32_t xmode)
  89. {
  90. #if defined (MCU_LPC176x)
  91. /* Select Watchdog clock source to IRC Clock */
  92. LPC_WDT->CLKSEL = WDT_WDCLKSEL_RC;
  93. #endif
  94. /* Clear the Watchdog timeout flag */
  95. LPC_WDT->MOD &= ~WDT_WDMOD_WDTOF;
  96. Lpc17xxWatchDogDisable();
  97. Lpc17xxWatchDogSetTimeOut(ms * 1000);
  98. /* Set reset mode to be compatible with other implementations too */
  99. LPC_WDT->MOD |= WDT_WDMOD_WDRESET;
  100. #if defined (MCU_LPC177x_8x) || defined(MCU_LPC407x_8x)
  101. /* Disable watchdog protect mode */
  102. LPC_WDT->MOD &= ~WDT_WDMOD_WDPROTECT;
  103. #endif
  104. /* Enable the watchdog */
  105. LPC_WDT->MOD |= WDT_WDMOD_WDEN;
  106. /* We have to feed the watchdog once after enabling it */
  107. Lpc17xxWatchDogRestart();
  108. //nested = 1;
  109. return ms;
  110. }
  111. /*!
  112. * \brief Re-start the LPC17xx hardware watch dog timer.
  113. *
  114. * For portability, applications should use the platform independent
  115. * \ref xgWatchDog "Watchdog Driver API".
  116. */
  117. void Lpc17xxWatchDogRestart(void)
  118. {
  119. /* Standard watchdog feed sequence */
  120. __disable_irq();
  121. LPC_WDT->FEED = 0xAA;
  122. LPC_WDT->FEED = 0x55;
  123. __enable_irq();
  124. }
  125. /*!
  126. * \brief Disable the LPC17xx hardware watch dog timer.
  127. *
  128. * Att: Disabling the watchdog is not supported by hardware
  129. *
  130. * For portability, applications should use the platform independent
  131. * \ref xgWatchDog "Watchdog Driver API".
  132. */
  133. void Lpc17xxWatchDogDisable(void)
  134. {
  135. /* Clear the Watchdog timeout flag */
  136. LPC_WDT->MOD &= ~WDT_WDMOD_WDTOF;
  137. /* Disabling the watchdog is not supported by hardware
  138. if (nested) {
  139. nested++;
  140. }
  141. LPC_WDT->MOD &= ~WDT_WDMOD_WDEN;
  142. */
  143. return;
  144. }
  145. /*!
  146. * \brief Enable the LPC17xx hardware watch dog timer.
  147. *
  148. * Att: Disabling and re-enabling the watchdog is not supported by hardware
  149. *
  150. * For portability, applications should use the platform independent
  151. * \ref xgWatchDog "Watchdog Driver API".
  152. */
  153. void Lpc17xxWatchDogEnable(void)
  154. {
  155. /* Disabling and re-enabling the watchdog is not supported by hardware
  156. if (nested > 1 && --nested == 1) {
  157. LPC_WDT->MOD |= WDT_WDMOD_WDEN;
  158. // We have to feed the watchdog once after enabling it
  159. Lpc17xxWatchDogRestart();
  160. }
  161. */
  162. }
  163. /*@}*/