Wake-up timer library to wake from deepsleep/power-down

Dependencies:   LPC1114_WakeInterruptIn

Fork of WakeUp by Erik -

Committer:
Sissors
Date:
Wed Jul 23 19:54:52 2014 +0000
Revision:
7:bb411115f814
Parent:
WakeUp_LPC812.cpp@1:92f4c2b52771
Child:
23:953f438d88a5
Added NUCLEO F030R8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 1:92f4c2b52771 1 #ifdef TARGET_LPC812
Sissors 1:92f4c2b52771 2
Sissors 0:fc439458a359 3 #include "WakeUp.h"
Sissors 0:fc439458a359 4
Sissors 0:fc439458a359 5 FunctionPointer WakeUp::callback;
Sissors 0:fc439458a359 6 float WakeUp::cycles_per_ms = 10.0;
Sissors 0:fc439458a359 7
Sissors 0:fc439458a359 8 void WakeUp::set_ms(uint32_t ms)
Sissors 0:fc439458a359 9 {
Sissors 0:fc439458a359 10 //Enable clock to register interface:
Sissors 0:fc439458a359 11 LPC_SYSCON->SYSAHBCLKCTRL |= 1<<9;
Sissors 0:fc439458a359 12
Sissors 0:fc439458a359 13 //Clear the counter:
Sissors 0:fc439458a359 14 LPC_WKT->CTRL |= 1<<2;
Sissors 0:fc439458a359 15 if (ms != 0) {
Sissors 0:fc439458a359 16 //Enable clock to register interface:
Sissors 0:fc439458a359 17 LPC_SYSCON->SYSAHBCLKCTRL |= 1<<9;
Sissors 0:fc439458a359 18
Sissors 0:fc439458a359 19 //Set 10kHz timer as source, and just to be sure clear status bit
Sissors 0:fc439458a359 20 LPC_WKT->CTRL = 3;
Sissors 0:fc439458a359 21
Sissors 0:fc439458a359 22 //Enable the 10kHz timer
Sissors 0:fc439458a359 23 LPC_PMU->DPDCTRL |= (1<<2) | (1<<3);
Sissors 0:fc439458a359 24
Sissors 0:fc439458a359 25 //Set interrupts
Sissors 0:fc439458a359 26 NVIC_SetVector(WKT_IRQn, (uint32_t)WakeUp::irq_handler);
Sissors 0:fc439458a359 27 NVIC_EnableIRQ(WKT_IRQn);
Sissors 0:fc439458a359 28
Sissors 0:fc439458a359 29 //Load the timer
Sissors 0:fc439458a359 30 LPC_WKT->COUNT = (uint32_t)((float)ms * cycles_per_ms);
Sissors 0:fc439458a359 31
Sissors 0:fc439458a359 32 } else {
Sissors 0:fc439458a359 33 //Disable clock to register interface:
Sissors 0:fc439458a359 34 LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<9);
Sissors 0:fc439458a359 35
Sissors 0:fc439458a359 36 //Disable the 10kHz timer
Sissors 0:fc439458a359 37 LPC_PMU->DPDCTRL &= ~((1<<2) | (1<<3));
Sissors 0:fc439458a359 38 }
Sissors 0:fc439458a359 39 }
Sissors 0:fc439458a359 40
Sissors 0:fc439458a359 41 void WakeUp::irq_handler(void)
Sissors 0:fc439458a359 42 {
Sissors 0:fc439458a359 43 //Clear status
Sissors 0:fc439458a359 44 LPC_WKT->CTRL |= 2;
Sissors 0:fc439458a359 45
Sissors 0:fc439458a359 46 //Disable clock to register interface:
Sissors 0:fc439458a359 47 LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<9);
Sissors 0:fc439458a359 48
Sissors 0:fc439458a359 49 //Disable the 10kHz timer
Sissors 0:fc439458a359 50 LPC_PMU->DPDCTRL &= ~((1<<2) | (1<<3));
Sissors 0:fc439458a359 51
Sissors 0:fc439458a359 52 callback.call();
Sissors 0:fc439458a359 53 }
Sissors 0:fc439458a359 54
Sissors 0:fc439458a359 55 void WakeUp::calibrate(void)
Sissors 0:fc439458a359 56 {
Sissors 0:fc439458a359 57 cycles_per_ms = 10.0;
Sissors 0:fc439458a359 58 set_ms(1100);
Sissors 0:fc439458a359 59 wait_ms(100);
Sissors 0:fc439458a359 60
Sissors 0:fc439458a359 61 uint32_t prevread = LPC_WKT->COUNT;
Sissors 0:fc439458a359 62 uint32_t read = LPC_WKT->COUNT;
Sissors 0:fc439458a359 63 while( read != prevread) {
Sissors 0:fc439458a359 64 prevread = read;
Sissors 0:fc439458a359 65 read = LPC_WKT->COUNT;
Sissors 0:fc439458a359 66 }
Sissors 0:fc439458a359 67
Sissors 0:fc439458a359 68 uint32_t ticks = 11000 - read;
Sissors 0:fc439458a359 69
Sissors 0:fc439458a359 70 cycles_per_ms = ticks / 100.0;
Sissors 0:fc439458a359 71 set_ms(0);
Sissors 0:fc439458a359 72 }
Sissors 0:fc439458a359 73
Sissors 1:92f4c2b52771 74 #endif