Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of WakeUp by
Device/WakeUp_LPC11u24.cpp@19:c1fc9bb48d2b, 2015-06-25 (annotated)
- Committer:
- AndyA
- Date:
- Thu Jun 25 08:22:40 2015 +0000
- Revision:
- 19:c1fc9bb48d2b
- Parent:
- 18:e71f388710b7
- Child:
- 20:1bd0c314f043
Fixed sign issue
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; |
| AndyA | 18:e71f388710b7 | 14 | uint32_t WakeUp::timeoutSet = 0; |
| Sissors | 4:ec26a6713bb9 | 15 | |
| Sissors | 4:ec26a6713bb9 | 16 | void WakeUp::set_ms(uint32_t ms) |
| Sissors | 4:ec26a6713bb9 | 17 | { |
| Sissors | 4:ec26a6713bb9 | 18 | if (ms != 0) { |
| Sissors | 4:ec26a6713bb9 | 19 | LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000; |
| Sissors | 4:ec26a6713bb9 | 20 | LPC_SYSCON->PDRUNCFG &= ~(1<<6); |
| Sissors | 4:ec26a6713bb9 | 21 | LPC_SYSCON->PDSLEEPCFG &= ~(1<<6); |
| Sissors | 4:ec26a6713bb9 | 22 | LPC_SYSCON->STARTERP1 |= 1<<12; |
| AndyA | 18:e71f388710b7 | 23 | |
| Sissors | 4:ec26a6713bb9 | 24 | //Set oscillator for 20kHz = 5kHz after divide by 4 in WDT |
| Sissors | 4:ec26a6713bb9 | 25 | LPC_SYSCON->WDTOSCCTRL = 14 | (1<<5); |
| AndyA | 18:e71f388710b7 | 26 | |
| Sissors | 4:ec26a6713bb9 | 27 | LPC_WWDT->MOD = 1; //Enable WDT |
| AndyA | 18:e71f388710b7 | 28 | timeoutSet = (uint32_t)((float)ms * cycles_per_ms); |
| AndyA | 18:e71f388710b7 | 29 | LPC_WWDT->TC = timeoutSet; |
| Sissors | 4:ec26a6713bb9 | 30 | LPC_WWDT->CLKSEL = 1; //WDTOSC |
| Sissors | 4:ec26a6713bb9 | 31 | LPC_WWDT->WARNINT = 0; |
| AndyA | 18:e71f388710b7 | 32 | |
| Sissors | 4:ec26a6713bb9 | 33 | NVIC_SetVector(WDT_IRQn, (uint32_t)WakeUp::irq_handler); |
| AndyA | 18:e71f388710b7 | 34 | |
| Sissors | 4:ec26a6713bb9 | 35 | //Feeeeeed me |
| Sissors | 4:ec26a6713bb9 | 36 | LPC_WWDT->FEED = 0xAA; |
| Sissors | 4:ec26a6713bb9 | 37 | LPC_WWDT->FEED = 0x55; |
| AndyA | 18:e71f388710b7 | 38 | |
| Sissors | 4:ec26a6713bb9 | 39 | NVIC_EnableIRQ(WDT_IRQn); |
| Sissors | 4:ec26a6713bb9 | 40 | } else |
| Sissors | 4:ec26a6713bb9 | 41 | NVIC_DisableIRQ(WDT_IRQn); |
| AndyA | 18:e71f388710b7 | 42 | |
| Sissors | 4:ec26a6713bb9 | 43 | } |
| Sissors | 4:ec26a6713bb9 | 44 | |
| Sissors | 4:ec26a6713bb9 | 45 | void WakeUp::irq_handler(void) |
| Sissors | 4:ec26a6713bb9 | 46 | { |
| Sissors | 4:ec26a6713bb9 | 47 | LPC_WWDT->MOD = 1<<3; |
| Sissors | 4:ec26a6713bb9 | 48 | callback.call(); |
| Sissors | 4:ec26a6713bb9 | 49 | } |
| Sissors | 4:ec26a6713bb9 | 50 | |
| Sissors | 4:ec26a6713bb9 | 51 | void WakeUp::calibrate(void) |
| Sissors | 4:ec26a6713bb9 | 52 | { |
| Sissors | 4:ec26a6713bb9 | 53 | cycles_per_ms = 5.0; |
| Sissors | 4:ec26a6713bb9 | 54 | set_ms(1100); |
| Sissors | 4:ec26a6713bb9 | 55 | wait_ms(10); //Give time to sync |
| Sissors | 4:ec26a6713bb9 | 56 | uint32_t count1 = LPC_WWDT->TV; |
| Sissors | 4:ec26a6713bb9 | 57 | wait_ms(100); |
| Sissors | 4:ec26a6713bb9 | 58 | uint32_t count2 = LPC_WWDT->TV; |
| Sissors | 4:ec26a6713bb9 | 59 | set_ms(0); |
| Sissors | 4:ec26a6713bb9 | 60 | count1 = count1 - count2; |
| AndyA | 18:e71f388710b7 | 61 | |
| Sissors | 4:ec26a6713bb9 | 62 | cycles_per_ms = count1 / 100.0; |
| Sissors | 4:ec26a6713bb9 | 63 | } |
| Sissors | 4:ec26a6713bb9 | 64 | |
| AndyA | 19:c1fc9bb48d2b | 65 | float WakeUp::timeRemaining(void) |
| AndyA | 18:e71f388710b7 | 66 | { |
| AndyA | 19:c1fc9bb48d2b | 67 | return (timeoutSet - LPC_WWDT->TV) / (cycles_per_ms*1000); |
| AndyA | 18:e71f388710b7 | 68 | } |
| AndyA | 18:e71f388710b7 | 69 | |
| Sissors | 4:ec26a6713bb9 | 70 | #endif |
