Use on STM32L432KC

Dependencies:   LPC1114_WakeInterruptIn

Fork of WakeUp by Erik -

Files at this revision

API Documentation at this revision

Comitter:
Sissors
Date:
Thu Dec 05 21:32:11 2013 +0000
Parent:
0:fc439458a359
Child:
2:648712aa15b4
Commit message:
Added KL25, doesn't play nice with mbed yet

Changed in this revision

WakeUp.cpp Show diff for this revision Revisions of this file
WakeUp.h Show annotated file Show diff for this revision Revisions of this file
WakeUp_KL25Z.cpp Show annotated file Show diff for this revision Revisions of this file
WakeUp_LPC812.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/WakeUp.cpp	Sat Nov 23 11:35:14 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-#include "WakeUp.h"
-
-FunctionPointer WakeUp::callback;
-float WakeUp::cycles_per_ms = 10.0;
-
-void WakeUp::set_ms(uint32_t ms)
-{
-    //Enable clock to register interface:
-    LPC_SYSCON->SYSAHBCLKCTRL |= 1<<9;
-
-    //Clear the counter:
-    LPC_WKT->CTRL |= 1<<2;
-    if (ms != 0) {
-        //Enable clock to register interface:
-        LPC_SYSCON->SYSAHBCLKCTRL |= 1<<9;
-
-        //Set 10kHz timer as source, and just to be sure clear status bit
-        LPC_WKT->CTRL = 3;
-
-        //Enable the 10kHz timer
-        LPC_PMU->DPDCTRL |= (1<<2) | (1<<3);
-
-        //Set interrupts
-        NVIC_SetVector(WKT_IRQn, (uint32_t)WakeUp::irq_handler);
-        NVIC_EnableIRQ(WKT_IRQn);
-
-        //Load the timer
-        LPC_WKT->COUNT = (uint32_t)((float)ms * cycles_per_ms);
-
-    } else {
-        //Disable clock to register interface:
-        LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<9);
-
-        //Disable the 10kHz timer
-        LPC_PMU->DPDCTRL &= ~((1<<2) | (1<<3));
-    }
-}
-
-void WakeUp::irq_handler(void)
-{
-    //Clear status
-    LPC_WKT->CTRL |= 2;
-
-    //Disable clock to register interface:
-    LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<9);
-
-    //Disable the 10kHz timer
-    LPC_PMU->DPDCTRL &= ~((1<<2) | (1<<3));
-
-    callback.call();
-}
-
-void WakeUp::calibrate(void)
-{
-    cycles_per_ms = 10.0;
-    set_ms(1100);
-    wait_ms(100);
-
-    uint32_t prevread = LPC_WKT->COUNT;
-    uint32_t read = LPC_WKT->COUNT;
-    while( read != prevread) {
-        prevread = read;
-        read = LPC_WKT->COUNT;
-    }
-
-    uint32_t ticks = 11000 - read;
-
-    cycles_per_ms = ticks / 100.0;
-    set_ms(0);
-}
-
-
--- a/WakeUp.h	Sat Nov 23 11:35:14 2013 +0000
+++ b/WakeUp.h	Thu Dec 05 21:32:11 2013 +0000
@@ -55,9 +55,12 @@
     /**
     * Calibrate the timer
     *
-    * The 10kHz timer has a very bad accuracy: +/- 45%.
+    * The LPC812's low-power timer has a very bad accuracy: +/- 45%.
     * This function calibrates it for 100ms against the main clock.
-    * So you get almost that accuracy by calling this function. 
+    * So you get almost that accuracy by calling this function.
+    *
+    * For the KL25 it is less of a problem, with an accuracy of +/- 10%
+    * Still it can be useful for your application
     *
     * Warning: Blocks for 100ms!
     */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WakeUp_KL25Z.cpp	Thu Dec 05 21:32:11 2013 +0000
@@ -0,0 +1,60 @@
+#ifdef TARGET_KL25Z
+
+#include "WakeUp.h"
+
+FunctionPointer WakeUp::callback;
+float WakeUp::cycles_per_ms = 1.0;
+
+void WakeUp::set_ms(uint32_t ms)
+{
+    /* Clock the timer */
+    SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK;
+    LPTMR0->CSR = 0;
+
+    if (ms != 0) {
+        //Clock directly from the 1kHz LPO
+        LPTMR0->PSR = LPTMR_PSR_PCS(1);
+        LPTMR0->PSR |= LPTMR_PSR_PBYP_MASK;
+
+        /* Set interrupt handler */
+        NVIC_SetVector(LPTimer_IRQn, (uint32_t)WakeUp::irq_handler);
+        NVIC_EnableIRQ(LPTimer_IRQn);
+
+        LPTMR0->CMR = (uint32_t)((float)ms * cycles_per_ms);
+
+        LPTMR0->CSR = LPTMR_CSR_TIE_MASK;
+        LPTMR0->CSR |= LPTMR_CSR_TEN_MASK;
+    }
+
+}
+
+
+void WakeUp::irq_handler(void)
+{
+    // write 1 to TCF to clear the LPT timer compare flag
+    LPTMR0->CSR |= LPTMR_CSR_TCF_MASK;
+    callback.call();
+}
+
+void WakeUp::calibrate(void)
+{
+    wait_us(1);     //Otherwise next wait might overwrite our settings
+    cycles_per_ms = 1.0;
+    set_ms(1100);
+    wait_ms(100);
+
+    //Write first to sync value
+    LPTMR0->CNR = 0;
+    uint32_t ticks = LPTMR0->CNR;
+    cycles_per_ms = ticks / 100.0;
+
+    set_ms(0);
+}
+
+
+
+
+
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WakeUp_LPC812.cpp	Thu Dec 05 21:32:11 2013 +0000
@@ -0,0 +1,74 @@
+#ifdef TARGET_LPC812
+
+#include "WakeUp.h"
+
+FunctionPointer WakeUp::callback;
+float WakeUp::cycles_per_ms = 10.0;
+
+void WakeUp::set_ms(uint32_t ms)
+{
+    //Enable clock to register interface:
+    LPC_SYSCON->SYSAHBCLKCTRL |= 1<<9;
+
+    //Clear the counter:
+    LPC_WKT->CTRL |= 1<<2;
+    if (ms != 0) {
+        //Enable clock to register interface:
+        LPC_SYSCON->SYSAHBCLKCTRL |= 1<<9;
+
+        //Set 10kHz timer as source, and just to be sure clear status bit
+        LPC_WKT->CTRL = 3;
+
+        //Enable the 10kHz timer
+        LPC_PMU->DPDCTRL |= (1<<2) | (1<<3);
+
+        //Set interrupts
+        NVIC_SetVector(WKT_IRQn, (uint32_t)WakeUp::irq_handler);
+        NVIC_EnableIRQ(WKT_IRQn);
+
+        //Load the timer
+        LPC_WKT->COUNT = (uint32_t)((float)ms * cycles_per_ms);
+
+    } else {
+        //Disable clock to register interface:
+        LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<9);
+
+        //Disable the 10kHz timer
+        LPC_PMU->DPDCTRL &= ~((1<<2) | (1<<3));
+    }
+}
+
+void WakeUp::irq_handler(void)
+{
+    //Clear status
+    LPC_WKT->CTRL |= 2;
+
+    //Disable clock to register interface:
+    LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<9);
+
+    //Disable the 10kHz timer
+    LPC_PMU->DPDCTRL &= ~((1<<2) | (1<<3));
+
+    callback.call();
+}
+
+void WakeUp::calibrate(void)
+{
+    cycles_per_ms = 10.0;
+    set_ms(1100);
+    wait_ms(100);
+
+    uint32_t prevread = LPC_WKT->COUNT;
+    uint32_t read = LPC_WKT->COUNT;
+    while( read != prevread) {
+        prevread = read;
+        read = LPC_WKT->COUNT;
+    }
+
+    uint32_t ticks = 11000 - read;
+
+    cycles_per_ms = ticks / 100.0;
+    set_ms(0);
+}
+
+#endif