WakeUpLib

Dependencies:   LPC1114_WakeInterruptIn

Fork of WakeUp by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WakeUp_LPC812.cpp Source File

WakeUp_LPC812.cpp

00001 #ifdef TARGET_LPC812
00002 
00003 #include "WakeUp.h"
00004 
00005 FunctionPointer WakeUp::callback;
00006 float WakeUp::cycles_per_ms = 10.0;
00007 
00008 void WakeUp::set_ms(uint32_t ms)
00009 {
00010     //Enable clock to register interface:
00011     LPC_SYSCON->SYSAHBCLKCTRL |= 1<<9;
00012 
00013     //Clear the counter:
00014     LPC_WKT->CTRL |= 1<<2;
00015     if (ms != 0) {
00016         //Enable clock to register interface:
00017         LPC_SYSCON->SYSAHBCLKCTRL |= 1<<9;
00018 
00019         //Set 10kHz timer as source, and just to be sure clear status bit
00020         LPC_WKT->CTRL = 3;
00021 
00022         //Enable the 10kHz timer
00023         LPC_PMU->DPDCTRL |= (1<<2) | (1<<3);
00024 
00025         //Set interrupts
00026         NVIC_SetVector(WKT_IRQn, (uint32_t)WakeUp::irq_handler);
00027         NVIC_EnableIRQ(WKT_IRQn);
00028 
00029         //Load the timer
00030         LPC_WKT->COUNT = (uint32_t)((float)ms * cycles_per_ms);
00031 
00032     } else {
00033         //Disable clock to register interface:
00034         LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<9);
00035 
00036         //Disable the 10kHz timer
00037         LPC_PMU->DPDCTRL &= ~((1<<2) | (1<<3));
00038     }
00039 }
00040 
00041 void WakeUp::irq_handler(void)
00042 {
00043     //Clear status
00044     LPC_WKT->CTRL |= 2;
00045 
00046     //Disable clock to register interface:
00047     LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<9);
00048 
00049     //Disable the 10kHz timer
00050     LPC_PMU->DPDCTRL &= ~((1<<2) | (1<<3));
00051 
00052     callback.call();
00053 }
00054 
00055 void WakeUp::calibrate(void)
00056 {
00057     cycles_per_ms = 10.0;
00058     set_ms(1100);
00059     wait_ms(100);
00060 
00061     uint32_t prevread = LPC_WKT->COUNT;
00062     uint32_t read = LPC_WKT->COUNT;
00063     while( read != prevread) {
00064         prevread = read;
00065         read = LPC_WKT->COUNT;
00066     }
00067 
00068     uint32_t ticks = 11000 - read;
00069 
00070     cycles_per_ms = ticks / 100.0;
00071     set_ms(0);
00072 }
00073 
00074 #endif