add the implementation of LP1768 wakeup based on the old WakeUp library
Dependencies: LPC1114_WakeInterruptIn
Fork of WakeUp by
WakeUp_LPC1768.cpp@7:542c0ca509e5, 2014-07-21 (annotated)
- Committer:
- steniu01
- Date:
- Mon Jul 21 13:45:48 2014 +0000
- Revision:
- 7:542c0ca509e5
used to wake up after enter power mode;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steniu01 | 7:542c0ca509e5 | 1 | /** |
steniu01 | 7:542c0ca509e5 | 2 | Due to lack of another option for the LPC11u24 the watchdog timer is used as wakeup source. |
steniu01 | 7:542c0ca509e5 | 3 | Since if the reset on watchdog event bit is set, I cannot remove it again, this means if you also got watchdog code running |
steniu01 | 7:542c0ca509e5 | 4 | the most likely result is that it just resets your board. |
steniu01 | 7:542c0ca509e5 | 5 | **/ |
steniu01 | 7:542c0ca509e5 | 6 | |
steniu01 | 7:542c0ca509e5 | 7 | |
steniu01 | 7:542c0ca509e5 | 8 | #ifdef TARGET_LPC1768 |
steniu01 | 7:542c0ca509e5 | 9 | |
steniu01 | 7:542c0ca509e5 | 10 | #include "WakeUp.h" |
steniu01 | 7:542c0ca509e5 | 11 | |
steniu01 | 7:542c0ca509e5 | 12 | FunctionPointer WakeUp::callback; |
steniu01 | 7:542c0ca509e5 | 13 | float WakeUp::cycles_per_ms = 5.0; |
steniu01 | 7:542c0ca509e5 | 14 | |
steniu01 | 7:542c0ca509e5 | 15 | void WakeUp::set_ms(uint32_t ms) |
steniu01 | 7:542c0ca509e5 | 16 | { |
steniu01 | 7:542c0ca509e5 | 17 | |
steniu01 | 7:542c0ca509e5 | 18 | LPC_WDT->WDCLKSEL = 0x2; // Set CLK src to PCLK |
steniu01 | 7:542c0ca509e5 | 19 | //uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
steniu01 | 7:542c0ca509e5 | 20 | // LPC_WDT->WDTC = ms * (float)clk/1000; |
steniu01 | 7:542c0ca509e5 | 21 | LPC_WDT->WDTC = (ms/4.0)*32768/1000; |
steniu01 | 7:542c0ca509e5 | 22 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset enabled, if not reset, WDINT will not be clearred |
steniu01 | 7:542c0ca509e5 | 23 | |
steniu01 | 7:542c0ca509e5 | 24 | NVIC_SetVector(WDT_IRQn, (uint32_t)WakeUp::irq_handler); |
steniu01 | 7:542c0ca509e5 | 25 | |
steniu01 | 7:542c0ca509e5 | 26 | //Feeeeeed me |
steniu01 | 7:542c0ca509e5 | 27 | LPC_WDT->WDFEED = 0xAA; |
steniu01 | 7:542c0ca509e5 | 28 | LPC_WDT->WDFEED = 0x55; |
steniu01 | 7:542c0ca509e5 | 29 | NVIC_ClearPendingIRQ(WDT_IRQn); |
steniu01 | 7:542c0ca509e5 | 30 | NVIC_EnableIRQ(WDT_IRQn); |
steniu01 | 7:542c0ca509e5 | 31 | |
steniu01 | 7:542c0ca509e5 | 32 | } |
steniu01 | 7:542c0ca509e5 | 33 | |
steniu01 | 7:542c0ca509e5 | 34 | void WakeUp::irq_handler(void) |
steniu01 | 7:542c0ca509e5 | 35 | { //SCB->AIRCR |= 1<< SCB_AIRCR_SYSRESETREQ_Pos; // request a reset to get out of deepesleep |
steniu01 | 7:542c0ca509e5 | 36 | LPC_WDT->WDMOD = 1<<3; |
steniu01 | 7:542c0ca509e5 | 37 | // LPC_WDT->WDMOD &= ~(1<<2); |
steniu01 | 7:542c0ca509e5 | 38 | // LPC_WDT->WDMOD |= 1<<2; |
steniu01 | 7:542c0ca509e5 | 39 | // SCB->AIRCR |= 1<< SCB_AIRCR_SYSRESETREQ_Pos; // request a reset to get out of deepesleep |
steniu01 | 7:542c0ca509e5 | 40 | NVIC_DisableIRQ(WDT_IRQn); |
steniu01 | 7:542c0ca509e5 | 41 | callback.call(); |
steniu01 | 7:542c0ca509e5 | 42 | } |
steniu01 | 7:542c0ca509e5 | 43 | |
steniu01 | 7:542c0ca509e5 | 44 | void WakeUp::calibrate(void) |
steniu01 | 7:542c0ca509e5 | 45 | { |
steniu01 | 7:542c0ca509e5 | 46 | cycles_per_ms = 5.0; |
steniu01 | 7:542c0ca509e5 | 47 | set_ms(1100); |
steniu01 | 7:542c0ca509e5 | 48 | wait_ms(10); //Give time to sync |
steniu01 | 7:542c0ca509e5 | 49 | uint32_t count1 = LPC_WDT->WDTV; |
steniu01 | 7:542c0ca509e5 | 50 | wait_ms(100); |
steniu01 | 7:542c0ca509e5 | 51 | uint32_t count2 = LPC_WDT->WDTV; |
steniu01 | 7:542c0ca509e5 | 52 | set_ms(0); |
steniu01 | 7:542c0ca509e5 | 53 | count1 = count1 - count2; |
steniu01 | 7:542c0ca509e5 | 54 | |
steniu01 | 7:542c0ca509e5 | 55 | cycles_per_ms = count1 / 100.0; |
steniu01 | 7:542c0ca509e5 | 56 | } |
steniu01 | 7:542c0ca509e5 | 57 | |
steniu01 | 7:542c0ca509e5 | 58 | #endif |
steniu01 | 7:542c0ca509e5 | 59 |