WatchDog Timer library, support for LPC1768 and LPC11U24
Dependents: ECGAFE_copy WatchDog
Fork of WatchDog by
WatchDog.cpp@0:bbc789f8d849, 2012-07-01 (annotated)
- Committer:
- Lerche
- Date:
- Sun Jul 01 22:43:25 2012 +0000
- Revision:
- 0:bbc789f8d849
- Child:
- 2:a6661a79bf7b
Added support for LPC11U24.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lerche | 0:bbc789f8d849 | 1 | /* |
Lerche | 0:bbc789f8d849 | 2 | * Source code is free to use and distribute. |
Lerche | 0:bbc789f8d849 | 3 | * Software originally written by Simon Ford, |
Lerche | 0:bbc789f8d849 | 4 | * altered by Lerche. |
Lerche | 0:bbc789f8d849 | 5 | * |
Lerche | 0:bbc789f8d849 | 6 | * 02-07-2012 - Added LPC11U24 Watchdog functions. Lerche |
Lerche | 0:bbc789f8d849 | 7 | * |
Lerche | 0:bbc789f8d849 | 8 | */ |
Lerche | 0:bbc789f8d849 | 9 | |
Lerche | 0:bbc789f8d849 | 10 | #include "mbed.h" |
Lerche | 0:bbc789f8d849 | 11 | #include "WatchDog.h" |
Lerche | 0:bbc789f8d849 | 12 | |
Lerche | 0:bbc789f8d849 | 13 | |
Lerche | 0:bbc789f8d849 | 14 | WatchDog::WatchDog(float s) { |
Lerche | 0:bbc789f8d849 | 15 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
Lerche | 0:bbc789f8d849 | 16 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
Lerche | 0:bbc789f8d849 | 17 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
Lerche | 0:bbc789f8d849 | 18 | LPC_WDT->WDTC = s * (float)clk; // Load WD Timer Constant with value determined by float s |
Lerche | 0:bbc789f8d849 | 19 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
Lerche | 0:bbc789f8d849 | 20 | feed(); |
Lerche | 0:bbc789f8d849 | 21 | #elif defined(TARGET_LPC11U24) |
Lerche | 0:bbc789f8d849 | 22 | LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000; |
Lerche | 0:bbc789f8d849 | 23 | LPC_SYSCON->PDRUNCFG &= 0xFFBF; |
Lerche | 0:bbc789f8d849 | 24 | LPC_SYSCON->WDTOSCCTRL = 0x40; |
Lerche | 0:bbc789f8d849 | 25 | uint32_t clk = 100000; |
Lerche | 0:bbc789f8d849 | 26 | LPC_WWDT->TC = s * (float)clk; |
Lerche | 0:bbc789f8d849 | 27 | LPC_WWDT->CLKSEL = 0x1; |
Lerche | 0:bbc789f8d849 | 28 | LPC_WWDT->MOD = 0x3; |
Lerche | 0:bbc789f8d849 | 29 | feed(); |
Lerche | 0:bbc789f8d849 | 30 | #endif |
Lerche | 0:bbc789f8d849 | 31 | } |
Lerche | 0:bbc789f8d849 | 32 | |
Lerche | 0:bbc789f8d849 | 33 | WatchDog_ms::WatchDog_ms(int ms) { |
Lerche | 0:bbc789f8d849 | 34 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
Lerche | 0:bbc789f8d849 | 35 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
Lerche | 0:bbc789f8d849 | 36 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
Lerche | 0:bbc789f8d849 | 37 | LPC_WDT->WDTC = ((float)ms * (float)clk) /1000; // Load WD Timer Constant with value determined by int ms |
Lerche | 0:bbc789f8d849 | 38 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
Lerche | 0:bbc789f8d849 | 39 | feed(); |
Lerche | 0:bbc789f8d849 | 40 | #elif defined(TARGET_LPC11U24) |
Lerche | 0:bbc789f8d849 | 41 | LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000; |
Lerche | 0:bbc789f8d849 | 42 | LPC_SYSCON->PDRUNCFG &= 0xFFBF; |
Lerche | 0:bbc789f8d849 | 43 | LPC_SYSCON->WDTOSCCTRL = 0x40; |
Lerche | 0:bbc789f8d849 | 44 | uint32_t clk = 100000; |
Lerche | 0:bbc789f8d849 | 45 | LPC_WWDT->TC = ((float)ms * (float)clk) / 1000; |
Lerche | 0:bbc789f8d849 | 46 | LPC_WWDT->CLKSEL = 0x1; |
Lerche | 0:bbc789f8d849 | 47 | LPC_WWDT->MOD = 0x3; |
Lerche | 0:bbc789f8d849 | 48 | feed(); |
Lerche | 0:bbc789f8d849 | 49 | #endif |
Lerche | 0:bbc789f8d849 | 50 | } |
Lerche | 0:bbc789f8d849 | 51 | |
Lerche | 0:bbc789f8d849 | 52 | WatchDog_us::WatchDog_us(int us) { |
Lerche | 0:bbc789f8d849 | 53 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
Lerche | 0:bbc789f8d849 | 54 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
Lerche | 0:bbc789f8d849 | 55 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
Lerche | 0:bbc789f8d849 | 56 | LPC_WDT->WDTC = ((float)us * (float)clk) /1000000; // Load WD Timer Constant with value determined by int us |
Lerche | 0:bbc789f8d849 | 57 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
Lerche | 0:bbc789f8d849 | 58 | feed(); |
Lerche | 0:bbc789f8d849 | 59 | #elif defined(TARGET_LPC11U24) |
Lerche | 0:bbc789f8d849 | 60 | LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000; |
Lerche | 0:bbc789f8d849 | 61 | LPC_SYSCON->PDRUNCFG &= 0xFFBF; |
Lerche | 0:bbc789f8d849 | 62 | LPC_SYSCON->WDTOSCCTRL = 0x40; |
Lerche | 0:bbc789f8d849 | 63 | uint32_t clk = 100000; |
Lerche | 0:bbc789f8d849 | 64 | LPC_WWDT->TC = ((float)us * (float)clk) / 1000000; |
Lerche | 0:bbc789f8d849 | 65 | LPC_WWDT->CLKSEL = 0x1; |
Lerche | 0:bbc789f8d849 | 66 | LPC_WWDT->MOD = 0x3; |
Lerche | 0:bbc789f8d849 | 67 | feed(); |
Lerche | 0:bbc789f8d849 | 68 | #endif |
Lerche | 0:bbc789f8d849 | 69 | } |
Lerche | 0:bbc789f8d849 | 70 | |
Lerche | 0:bbc789f8d849 | 71 | void WatchDog::feed() { |
Lerche | 0:bbc789f8d849 | 72 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
Lerche | 0:bbc789f8d849 | 73 | LPC_WDT->WDFEED = 0xAA; // Routine to feed the WatchDog |
Lerche | 0:bbc789f8d849 | 74 | LPC_WDT->WDFEED = 0x55; // according to user manual |
Lerche | 0:bbc789f8d849 | 75 | #elif defined(TARGET_LPC11U24) |
Lerche | 0:bbc789f8d849 | 76 | LPC_WWDT->FEED = 0xAA; |
Lerche | 0:bbc789f8d849 | 77 | LPC_WWDT->FEED = 0x55; |
Lerche | 0:bbc789f8d849 | 78 | #endif |
Lerche | 0:bbc789f8d849 | 79 | } |
Lerche | 0:bbc789f8d849 | 80 | void WatchDog_ms::feed() { |
Lerche | 0:bbc789f8d849 | 81 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
Lerche | 0:bbc789f8d849 | 82 | LPC_WDT->WDFEED = 0xAA; // Routine to feed the WatchDog |
Lerche | 0:bbc789f8d849 | 83 | LPC_WDT->WDFEED = 0x55; // according to user manual |
Lerche | 0:bbc789f8d849 | 84 | #elif defined(TARGET_LPC11U24) |
Lerche | 0:bbc789f8d849 | 85 | LPC_WWDT->FEED = 0xAA; |
Lerche | 0:bbc789f8d849 | 86 | LPC_WWDT->FEED = 0x55; |
Lerche | 0:bbc789f8d849 | 87 | #endif |
Lerche | 0:bbc789f8d849 | 88 | } |
Lerche | 0:bbc789f8d849 | 89 | void WatchDog_us::feed() { |
Lerche | 0:bbc789f8d849 | 90 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
Lerche | 0:bbc789f8d849 | 91 | LPC_WDT->WDFEED = 0xAA; // Routine to feed the WatchDog |
Lerche | 0:bbc789f8d849 | 92 | LPC_WDT->WDFEED = 0x55; // according to user manual |
Lerche | 0:bbc789f8d849 | 93 | #elif defined(TARGET_LPC11U24) |
Lerche | 0:bbc789f8d849 | 94 | LPC_WWDT->FEED = 0xAA; |
Lerche | 0:bbc789f8d849 | 95 | LPC_WWDT->FEED = 0x55; |
Lerche | 0:bbc789f8d849 | 96 | #endif |
Lerche | 0:bbc789f8d849 | 97 | } |