mBuino low current/ deep sleep LED flasher to be used as key finder.

Dependencies:   WakeUp mbed

Files at this revision

API Documentation at this revision

Comitter:
wernert
Date:
Sat Sep 06 09:30:35 2014 +0000
Child:
1:c0cfd1b0f4ef
Commit message:
This mBuino project demonstrates a low power LED flasher for mBuino that uses deep sleep to achieve low current consumption. It only flashes one LED at a time for 10 mS long every 1 second. The idea is for keyring flasher that works for long time.

Changed in this revision

WakeUp.h Show annotated file Show diff for this revision Revisions of this file
WakeUp.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WakeUp.h	Sat Sep 06 09:30:35 2014 +0000
@@ -0,0 +1,98 @@
+#include "mbed.h"
+
+/**
+ * Class to make wake up a microcontroller from deepsleep using a low-power timer. 
+ *
+ * @code
+ * // Depending on the LED connections either the LED is off the 2 seconds
+ * // the target spends in deepsleep(), and on for the other second. Or it is inverted 
+ * 
+ * #include "mbed.h"
+ * #include "WakeUp.h"
+ * 
+ * DigitalOut myled(LED1);
+ * 
+ * int main() {
+ *     wait(5);
+ *
+ *     //The low-power oscillator can be quite inaccurate on some targets
+ *     //this function calibrates it against the main clock
+ *     WakeUp::calibrate();
+ *    
+ *     while(1) {
+ *         //Set LED to zero
+ *         myled = 0;
+ *         
+ *         //Set wakeup time for 2 seconds
+ *         WakeUp::set_ms(2000);
+ *         
+ *         //Enter deepsleep, the program won't go beyond this point until it is woken up
+ *         deepsleep();
+ *         
+ *         //Set LED for 1 second to one
+ *         myled = 1;
+ *         wait(1);
+ *     }
+ * }
+ * @endcode
+ */
+class WakeUp
+{
+public:
+    /**
+    * Set the timeout
+    *
+    * @param s required time in seconds
+    */
+    static void set(uint32_t s) {
+        set_ms(1000 * s);
+    }
+    
+    /**
+    * Set the timeout
+    *
+    * @param ms required time in milliseconds
+    */
+    static void set_ms(uint32_t ms);
+    
+    /**
+    * Attach a function to be called after timeout
+    *
+    * This is optional, if you just want to wake up you 
+    * do not need to attach a function.
+    *
+    * Important: Many targets will run the wake-up routine
+    * at reduced clock speed, afterwards clock speed is restored.
+    * This means that clock speed dependent functions, such as printf
+    * might end up distorted.
+    *
+    * Also supports normal way to attach member functions
+    * (not documented for simplicity)
+    *
+    * @param *function function to call
+    */
+    static void attach(void (*function)(void)) {
+        callback.attach(function);
+    }
+
+    template<typename T>
+    static void attach(T *object, void (T::*member)(void)) {
+        callback.attach(object, member);
+    }
+    
+    /**
+    * Calibrate the timer
+    *
+    * Some of the low-power timers have very bad accuracy.
+    * This function calibrates it against the main timer.
+    *
+    * Warning: Blocks for 100ms!
+    */
+    static void calibrate(void);
+
+
+private:
+    static FunctionPointer callback;
+    static void irq_handler(void);
+    static float cycles_per_ms;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WakeUp.lib	Sat Sep 06 09:30:35 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/WakeUp/#b2a710aca356
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Sep 06 09:30:35 2014 +0000
@@ -0,0 +1,34 @@
+#include "mbed.h"
+#include "WakeUp.h"
+
+DigitalOut LED[] = {(P0_7), (P0_8), (P0_2), (P0_20), (P1_19), (P0_17), (P0_23)};// declare 7 LEDs
+
+void myDeepSleep() {
+    LPC_PMU->PCON = 0x1;
+    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
+    LPC_SYSCON->PDAWAKECFG &= 0xFFFFF800;
+    __WFI();
+}
+
+int main() {
+
+    int i = 0;
+    
+    //The low-power oscillator can be quite inaccurate on some targets
+    //this function calibrates it against the main clock
+    WakeUp::calibrate();
+    while(1)
+    {  
+        LED[i] = 1; // turn on
+        wait(.01);   // delay
+        LED[i] = 0; // turn off
+        
+         //Set wakeup time for second
+        WakeUp::set_ms(1000);
+        //Enter deepsleep, the program won't go beyond this point until it is woken up
+        myDeepSleep();
+        i++;
+        if (i >= 7) 
+           i = 0;             
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Sep 06 09:30:35 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013
\ No newline at end of file