ms2s,STM32 Private

Fork of WakeUp by Erik -

Committer:
labishrestha
Date:
Wed Nov 11 17:56:03 2015 +0000
Branch:
LPC11UXX
Revision:
21:30845899cca2
Modified to support for LPC11UXX in new branch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
labishrestha 21:30845899cca2 1 /**
labishrestha 21:30845899cca2 2 Due to lack of another option for the LPC11u24 the watchdog timer is used as wakeup source.
labishrestha 21:30845899cca2 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
labishrestha 21:30845899cca2 4 the most likely result is that it just resets your board.
labishrestha 21:30845899cca2 5 **/
labishrestha 21:30845899cca2 6
labishrestha 21:30845899cca2 7
labishrestha 21:30845899cca2 8 #ifdef TARGET_LPC11UXX
labishrestha 21:30845899cca2 9
labishrestha 21:30845899cca2 10 #include "WakeUp.h"
labishrestha 21:30845899cca2 11
labishrestha 21:30845899cca2 12 FunctionPointer WakeUp::callback;
labishrestha 21:30845899cca2 13 float WakeUp::cycles_per_ms = 5.0;
labishrestha 21:30845899cca2 14
labishrestha 21:30845899cca2 15 void WakeUp::set_ms(uint32_t ms)
labishrestha 21:30845899cca2 16 {
labishrestha 21:30845899cca2 17 if (ms != 0) {
labishrestha 21:30845899cca2 18 LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000;
labishrestha 21:30845899cca2 19 LPC_SYSCON->PDRUNCFG &= ~(1<<6);
labishrestha 21:30845899cca2 20 LPC_SYSCON->PDSLEEPCFG &= ~(1<<6);
labishrestha 21:30845899cca2 21 LPC_SYSCON->STARTERP1 |= 1<<12;
labishrestha 21:30845899cca2 22
labishrestha 21:30845899cca2 23 //Set oscillator for 20kHz = 5kHz after divide by 4 in WDT
labishrestha 21:30845899cca2 24 LPC_SYSCON->WDTOSCCTRL = 14 | (1<<5);
labishrestha 21:30845899cca2 25
labishrestha 21:30845899cca2 26 LPC_WWDT->MOD = 1; //Enable WDT
labishrestha 21:30845899cca2 27 LPC_WWDT->TC = (uint32_t)((float)ms * cycles_per_ms);
labishrestha 21:30845899cca2 28 LPC_WWDT->CLKSEL = 1; //WDTOSC
labishrestha 21:30845899cca2 29 LPC_WWDT->WARNINT = 0;
labishrestha 21:30845899cca2 30
labishrestha 21:30845899cca2 31 NVIC_SetVector(WDT_IRQn, (uint32_t)WakeUp::irq_handler);
labishrestha 21:30845899cca2 32
labishrestha 21:30845899cca2 33 //Feeeeeed me
labishrestha 21:30845899cca2 34 LPC_WWDT->FEED = 0xAA;
labishrestha 21:30845899cca2 35 LPC_WWDT->FEED = 0x55;
labishrestha 21:30845899cca2 36
labishrestha 21:30845899cca2 37 NVIC_EnableIRQ(WDT_IRQn);
labishrestha 21:30845899cca2 38 } else
labishrestha 21:30845899cca2 39 NVIC_DisableIRQ(WDT_IRQn);
labishrestha 21:30845899cca2 40
labishrestha 21:30845899cca2 41 }
labishrestha 21:30845899cca2 42
labishrestha 21:30845899cca2 43 void WakeUp::irq_handler(void)
labishrestha 21:30845899cca2 44 {
labishrestha 21:30845899cca2 45 LPC_WWDT->MOD = 1<<3;
labishrestha 21:30845899cca2 46 callback.call();
labishrestha 21:30845899cca2 47 }
labishrestha 21:30845899cca2 48
labishrestha 21:30845899cca2 49 void WakeUp::calibrate(void)
labishrestha 21:30845899cca2 50 {
labishrestha 21:30845899cca2 51 cycles_per_ms = 5.0;
labishrestha 21:30845899cca2 52 set_ms(1100);
labishrestha 21:30845899cca2 53 wait_ms(10); //Give time to sync
labishrestha 21:30845899cca2 54 uint32_t count1 = LPC_WWDT->TV;
labishrestha 21:30845899cca2 55 wait_ms(100);
labishrestha 21:30845899cca2 56 uint32_t count2 = LPC_WWDT->TV;
labishrestha 21:30845899cca2 57 set_ms(0);
labishrestha 21:30845899cca2 58 count1 = count1 - count2;
labishrestha 21:30845899cca2 59
labishrestha 21:30845899cca2 60 cycles_per_ms = count1 / 100.0;
labishrestha 21:30845899cca2 61 }
labishrestha 21:30845899cca2 62
labishrestha 21:30845899cca2 63 #endif