Andy A / WakeUp

Fork of WakeUp by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WakeUp_LPC11u24.cpp Source File

WakeUp_LPC11u24.cpp

00001 /**
00002 Due to lack of another option for the LPC11u24 the watchdog timer is used as wakeup source.
00003 Since if the reset on watchdog event bit is set, I cannot remove it again, this means if you also got watchdog code running
00004 the most likely result is that it just resets your board.
00005 **/
00006 
00007 
00008 #ifdef TARGET_LPC11U24
00009 
00010 #include "WakeUp.h"
00011 
00012 FunctionPointer WakeUp::callback;
00013 float WakeUp::cycles_per_ms = 5.0;
00014 uint32_t WakeUp::timeoutSet = 0;
00015 
00016 void WakeUp::set_ms(uint32_t ms)
00017 {
00018     if (ms != 0) {
00019         LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000;
00020         LPC_SYSCON->PDRUNCFG &= ~(1<<6);
00021         LPC_SYSCON->PDSLEEPCFG &= ~(1<<6);
00022         LPC_SYSCON->STARTERP1 |= 1<<12;
00023 
00024         //Set oscillator for 20kHz = 5kHz after divide by 4 in WDT
00025         LPC_SYSCON->WDTOSCCTRL = 14 | (1<<5);
00026 
00027         LPC_WWDT->MOD = 1;      //Enable WDT
00028         timeoutSet = (uint32_t)((float)ms * cycles_per_ms);
00029         LPC_WWDT->TC = timeoutSet;
00030         LPC_WWDT->CLKSEL = 1;   //WDTOSC
00031         LPC_WWDT->WARNINT = 0;
00032 
00033         NVIC_SetVector(WDT_IRQn, (uint32_t)WakeUp::irq_handler);
00034 
00035         //Feeeeeed me
00036         LPC_WWDT->FEED = 0xAA;
00037         LPC_WWDT->FEED = 0x55;
00038 
00039         NVIC_EnableIRQ(WDT_IRQn);
00040     } else
00041         NVIC_DisableIRQ(WDT_IRQn);
00042 
00043 }
00044 
00045 void WakeUp::irq_handler(void)
00046 {
00047     LPC_WWDT->MOD = 1<<3;
00048     callback.call();
00049 }
00050 
00051 void WakeUp::calibrate(void)
00052 {
00053     cycles_per_ms = 5.0;
00054     set_ms(1100);
00055     wait_ms(10);    //Give time to sync
00056     uint32_t count1 = LPC_WWDT->TV;
00057     wait_ms(100);
00058     uint32_t count2 = LPC_WWDT->TV;
00059     set_ms(0);
00060     count1 = count1 - count2;
00061 
00062     cycles_per_ms = count1 / 100.0;
00063 }
00064 
00065 float WakeUp::timeElapsed(void)
00066 {
00067     return (timeoutSet - LPC_WWDT->TV) / (cycles_per_ms*1000);
00068 }
00069 
00070 #endif