Fork of original library to fix mbed 5 deprecation warnings

Dependencies:   LPC1114_WakeInterruptIn

Dependents:   low-power-sleep

Fork of WakeUp by Erik -

Committer:
Sissors
Date:
Sat Nov 23 11:35:14 2013 +0000
Revision:
0:fc439458a359
v1.0
;

Who changed what in which revision?

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