Modified WakeUp program to run on STM32L152RE
Dependencies: mbed LPC1114_WakeInterruptIn
Fork of WakeUp by
Device/WakeUp_LPC11u24.cpp@7:bb411115f814, 2014-07-23 (annotated)
- Committer:
- Sissors
- Date:
- Wed Jul 23 19:54:52 2014 +0000
- Revision:
- 7:bb411115f814
- Parent:
- WakeUp_LPC11u24.cpp@4:ec26a6713bb9
- Child:
- 23:69a0c843e4bd
Added NUCLEO F030R8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 4:ec26a6713bb9 | 1 | /** |
Sissors | 4:ec26a6713bb9 | 2 | Due to lack of another option for the LPC11u24 the watchdog timer is used as wakeup source. |
Sissors | 4:ec26a6713bb9 | 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 |
Sissors | 4:ec26a6713bb9 | 4 | the most likely result is that it just resets your board. |
Sissors | 4:ec26a6713bb9 | 5 | **/ |
Sissors | 4:ec26a6713bb9 | 6 | |
Sissors | 4:ec26a6713bb9 | 7 | |
Sissors | 4:ec26a6713bb9 | 8 | #ifdef TARGET_LPC11U24 |
Sissors | 4:ec26a6713bb9 | 9 | |
Sissors | 4:ec26a6713bb9 | 10 | #include "WakeUp.h" |
Sissors | 4:ec26a6713bb9 | 11 | |
Sissors | 4:ec26a6713bb9 | 12 | FunctionPointer WakeUp::callback; |
Sissors | 4:ec26a6713bb9 | 13 | float WakeUp::cycles_per_ms = 5.0; |
Sissors | 4:ec26a6713bb9 | 14 | |
Sissors | 4:ec26a6713bb9 | 15 | void WakeUp::set_ms(uint32_t ms) |
Sissors | 4:ec26a6713bb9 | 16 | { |
Sissors | 4:ec26a6713bb9 | 17 | if (ms != 0) { |
Sissors | 4:ec26a6713bb9 | 18 | LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000; |
Sissors | 4:ec26a6713bb9 | 19 | LPC_SYSCON->PDRUNCFG &= ~(1<<6); |
Sissors | 4:ec26a6713bb9 | 20 | LPC_SYSCON->PDSLEEPCFG &= ~(1<<6); |
Sissors | 4:ec26a6713bb9 | 21 | LPC_SYSCON->STARTERP1 |= 1<<12; |
Sissors | 4:ec26a6713bb9 | 22 | |
Sissors | 4:ec26a6713bb9 | 23 | //Set oscillator for 20kHz = 5kHz after divide by 4 in WDT |
Sissors | 4:ec26a6713bb9 | 24 | LPC_SYSCON->WDTOSCCTRL = 14 | (1<<5); |
Sissors | 4:ec26a6713bb9 | 25 | |
Sissors | 4:ec26a6713bb9 | 26 | LPC_WWDT->MOD = 1; //Enable WDT |
Sissors | 4:ec26a6713bb9 | 27 | LPC_WWDT->TC = (uint32_t)((float)ms * cycles_per_ms); |
Sissors | 4:ec26a6713bb9 | 28 | LPC_WWDT->CLKSEL = 1; //WDTOSC |
Sissors | 4:ec26a6713bb9 | 29 | LPC_WWDT->WARNINT = 0; |
Sissors | 4:ec26a6713bb9 | 30 | |
Sissors | 4:ec26a6713bb9 | 31 | NVIC_SetVector(WDT_IRQn, (uint32_t)WakeUp::irq_handler); |
Sissors | 4:ec26a6713bb9 | 32 | |
Sissors | 4:ec26a6713bb9 | 33 | //Feeeeeed me |
Sissors | 4:ec26a6713bb9 | 34 | LPC_WWDT->FEED = 0xAA; |
Sissors | 4:ec26a6713bb9 | 35 | LPC_WWDT->FEED = 0x55; |
Sissors | 4:ec26a6713bb9 | 36 | |
Sissors | 4:ec26a6713bb9 | 37 | NVIC_EnableIRQ(WDT_IRQn); |
Sissors | 4:ec26a6713bb9 | 38 | } else |
Sissors | 4:ec26a6713bb9 | 39 | NVIC_DisableIRQ(WDT_IRQn); |
Sissors | 4:ec26a6713bb9 | 40 | |
Sissors | 4:ec26a6713bb9 | 41 | } |
Sissors | 4:ec26a6713bb9 | 42 | |
Sissors | 4:ec26a6713bb9 | 43 | void WakeUp::irq_handler(void) |
Sissors | 4:ec26a6713bb9 | 44 | { |
Sissors | 4:ec26a6713bb9 | 45 | LPC_WWDT->MOD = 1<<3; |
Sissors | 4:ec26a6713bb9 | 46 | callback.call(); |
Sissors | 4:ec26a6713bb9 | 47 | } |
Sissors | 4:ec26a6713bb9 | 48 | |
Sissors | 4:ec26a6713bb9 | 49 | void WakeUp::calibrate(void) |
Sissors | 4:ec26a6713bb9 | 50 | { |
Sissors | 4:ec26a6713bb9 | 51 | cycles_per_ms = 5.0; |
Sissors | 4:ec26a6713bb9 | 52 | set_ms(1100); |
Sissors | 4:ec26a6713bb9 | 53 | wait_ms(10); //Give time to sync |
Sissors | 4:ec26a6713bb9 | 54 | uint32_t count1 = LPC_WWDT->TV; |
Sissors | 4:ec26a6713bb9 | 55 | wait_ms(100); |
Sissors | 4:ec26a6713bb9 | 56 | uint32_t count2 = LPC_WWDT->TV; |
Sissors | 4:ec26a6713bb9 | 57 | set_ms(0); |
Sissors | 4:ec26a6713bb9 | 58 | count1 = count1 - count2; |
Sissors | 4:ec26a6713bb9 | 59 | |
Sissors | 4:ec26a6713bb9 | 60 | cycles_per_ms = count1 / 100.0; |
Sissors | 4:ec26a6713bb9 | 61 | } |
Sissors | 4:ec26a6713bb9 | 62 | |
Sissors | 4:ec26a6713bb9 | 63 | #endif |