uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_wdt.c
Go to the documentation of this file.
1 /***********************************************************************/
20 /* Peripheral group ----------------------------------------------------------- */
25 /* Includes ------------------------------------------------------------------- */
26 #include "lpc17xx_wdt.h"
27 #include "lpc17xx_clkpwr.h"
28 #include "lpc17xx_pinsel.h"
29 
30 
31 /* If this source file built with example, the LPC17xx FW library configuration
32  * file in each example directory ("lpc17xx_libcfg.h") must be included,
33  * otherwise the default FW library configuration file must be included instead
34  */
35 #ifdef __BUILD_WITH_EXAMPLE__
36 #include "lpc17xx_libcfg.h"
37 #else
38 #include "lpc17xx_libcfg_default.h"
39 #endif /* __BUILD_WITH_EXAMPLE__ */
40 
41 
42 #ifdef _WDT
43 
44 /* Private Functions ---------------------------------------------------------- */
45 
46 static uint8_t WDT_SetTimeOut (uint8_t clk_source, uint32_t timeout);
47 
48 /********************************************************************/
54 static uint8_t WDT_SetTimeOut (uint8_t clk_source, uint32_t timeout)
55 {
56 
57  uint32_t pclk_wdt = 0;
58  uint32_t tempval = 0;
59 
60  switch ((WDT_CLK_OPT) clk_source)
61  {
62  case WDT_CLKSRC_IRC:
63  pclk_wdt = 4000000;
64  // Calculate TC in WDT
65  tempval = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
66  // Check if it valid
67  if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
68  {
69  LPC_WDT->WDTC = tempval;
70  return SUCCESS;
71  }
72 
73  break;
74 
75  case WDT_CLKSRC_PCLK:
76 
77  // Get WDT clock with CCLK divider = 4
78  pclk_wdt = SystemCoreClock / 4;
79  // Calculate TC in WDT
80  tempval = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
81 
82  if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
83  {
85  LPC_WDT->WDTC = (uint32_t) tempval;
86  return SUCCESS;
87  }
88 
89  // Get WDT clock with CCLK divider = 2
90  pclk_wdt = SystemCoreClock / 2;
91  // Calculate TC in WDT
92  tempval = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
93 
94  if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
95  {
97  LPC_WDT->WDTC = (uint32_t) tempval;
98  return SUCCESS;
99  }
100 
101  // Get WDT clock with CCLK divider = 1
102  pclk_wdt = SystemCoreClock;
103  // Calculate TC in WDT
104  tempval = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
105 
106  if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
107  {
109  LPC_WDT->WDTC = (uint32_t) tempval;
110  return SUCCESS;
111  }
112  break ;
113 
114 
115  case WDT_CLKSRC_RTC:
116  pclk_wdt = 32768;
117  // Calculate TC in WDT
118  tempval = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
119  // Check if it valid
120  if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
121  {
122  LPC_WDT->WDTC = (uint32_t) tempval;
123  return SUCCESS;
124  }
125 
126  break;
127 
128 // Error parameter
129  default:
130  break;
131 }
132 
133  return ERROR;
134 }
135 
136 /* End of Private Functions --------------------------------------------------- */
137 
138 
139 /* Public Functions ----------------------------------------------------------- */
145 /*********************************************************************/
157 void WDT_Init (WDT_CLK_OPT ClkSrc, WDT_MODE_OPT WDTMode)
158 {
162 
163  //Set clock source
164  LPC_WDT->WDCLKSEL &= ~WDT_WDCLKSEL_MASK;
165  LPC_WDT->WDCLKSEL |= ClkSrc;
166  //Set WDT mode
167  if (WDTMode == WDT_MODE_RESET){
168  LPC_WDT->WDMOD |= WDT_WDMOD(WDTMode);
169  }
170 }
171 
172 /*********************************************************************/
177 void WDT_Start(uint32_t TimeOut)
178 {
179  uint32_t ClkSrc;
180 
181  ClkSrc = LPC_WDT->WDCLKSEL;
182  ClkSrc &=WDT_WDCLKSEL_MASK;
183  WDT_SetTimeOut(ClkSrc,TimeOut);
184  //enable watchdog
185  LPC_WDT->WDMOD |= WDT_WDMOD_WDEN;
186  WDT_Feed();
187 }
188 
189 /********************************************************************/
195 {
196  return ((FlagStatus)((LPC_WDT->WDMOD & WDT_WDMOD_WDTOF) >>2));
197 }
198 
199 /********************************************************************/
204 void WDT_ClrTimeOutFlag (void)
205 {
206  LPC_WDT->WDMOD &=~WDT_WDMOD_WDTOF;
207 }
208 
209 /********************************************************************/
214 void WDT_UpdateTimeOut ( uint32_t TimeOut)
215 {
216  uint32_t ClkSrc;
217  ClkSrc = LPC_WDT->WDCLKSEL;
218  ClkSrc &=WDT_WDCLKSEL_MASK;
219  WDT_SetTimeOut(ClkSrc,TimeOut);
220  WDT_Feed();
221 }
222 
223 
224 /********************************************************************/
231 void WDT_Feed (void)
232 {
233  // Disable irq interrupt
234  __disable_irq();
235  LPC_WDT->WDFEED = 0xAA;
236  LPC_WDT->WDFEED = 0x55;
237  // Then enable irq interrupt
238  __enable_irq();
239 }
240 
241 /********************************************************************/
246 uint32_t WDT_GetCurrentCount(void)
247 {
248  return LPC_WDT->WDTV;
249 }
250 
255 #endif /* _WDT */
256 
261 /* --------------------------------- End Of File ------------------------------ */