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

Dependencies:   WakeUp mbed

Committer:
wernert
Date:
Sat Sep 06 09:30:35 2014 +0000
Revision:
0:346c8150ca74
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.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wernert 0:346c8150ca74 1 #include "mbed.h"
wernert 0:346c8150ca74 2
wernert 0:346c8150ca74 3 /**
wernert 0:346c8150ca74 4 * Class to make wake up a microcontroller from deepsleep using a low-power timer.
wernert 0:346c8150ca74 5 *
wernert 0:346c8150ca74 6 * @code
wernert 0:346c8150ca74 7 * // Depending on the LED connections either the LED is off the 2 seconds
wernert 0:346c8150ca74 8 * // the target spends in deepsleep(), and on for the other second. Or it is inverted
wernert 0:346c8150ca74 9 *
wernert 0:346c8150ca74 10 * #include "mbed.h"
wernert 0:346c8150ca74 11 * #include "WakeUp.h"
wernert 0:346c8150ca74 12 *
wernert 0:346c8150ca74 13 * DigitalOut myled(LED1);
wernert 0:346c8150ca74 14 *
wernert 0:346c8150ca74 15 * int main() {
wernert 0:346c8150ca74 16 * wait(5);
wernert 0:346c8150ca74 17 *
wernert 0:346c8150ca74 18 * //The low-power oscillator can be quite inaccurate on some targets
wernert 0:346c8150ca74 19 * //this function calibrates it against the main clock
wernert 0:346c8150ca74 20 * WakeUp::calibrate();
wernert 0:346c8150ca74 21 *
wernert 0:346c8150ca74 22 * while(1) {
wernert 0:346c8150ca74 23 * //Set LED to zero
wernert 0:346c8150ca74 24 * myled = 0;
wernert 0:346c8150ca74 25 *
wernert 0:346c8150ca74 26 * //Set wakeup time for 2 seconds
wernert 0:346c8150ca74 27 * WakeUp::set_ms(2000);
wernert 0:346c8150ca74 28 *
wernert 0:346c8150ca74 29 * //Enter deepsleep, the program won't go beyond this point until it is woken up
wernert 0:346c8150ca74 30 * deepsleep();
wernert 0:346c8150ca74 31 *
wernert 0:346c8150ca74 32 * //Set LED for 1 second to one
wernert 0:346c8150ca74 33 * myled = 1;
wernert 0:346c8150ca74 34 * wait(1);
wernert 0:346c8150ca74 35 * }
wernert 0:346c8150ca74 36 * }
wernert 0:346c8150ca74 37 * @endcode
wernert 0:346c8150ca74 38 */
wernert 0:346c8150ca74 39 class WakeUp
wernert 0:346c8150ca74 40 {
wernert 0:346c8150ca74 41 public:
wernert 0:346c8150ca74 42 /**
wernert 0:346c8150ca74 43 * Set the timeout
wernert 0:346c8150ca74 44 *
wernert 0:346c8150ca74 45 * @param s required time in seconds
wernert 0:346c8150ca74 46 */
wernert 0:346c8150ca74 47 static void set(uint32_t s) {
wernert 0:346c8150ca74 48 set_ms(1000 * s);
wernert 0:346c8150ca74 49 }
wernert 0:346c8150ca74 50
wernert 0:346c8150ca74 51 /**
wernert 0:346c8150ca74 52 * Set the timeout
wernert 0:346c8150ca74 53 *
wernert 0:346c8150ca74 54 * @param ms required time in milliseconds
wernert 0:346c8150ca74 55 */
wernert 0:346c8150ca74 56 static void set_ms(uint32_t ms);
wernert 0:346c8150ca74 57
wernert 0:346c8150ca74 58 /**
wernert 0:346c8150ca74 59 * Attach a function to be called after timeout
wernert 0:346c8150ca74 60 *
wernert 0:346c8150ca74 61 * This is optional, if you just want to wake up you
wernert 0:346c8150ca74 62 * do not need to attach a function.
wernert 0:346c8150ca74 63 *
wernert 0:346c8150ca74 64 * Important: Many targets will run the wake-up routine
wernert 0:346c8150ca74 65 * at reduced clock speed, afterwards clock speed is restored.
wernert 0:346c8150ca74 66 * This means that clock speed dependent functions, such as printf
wernert 0:346c8150ca74 67 * might end up distorted.
wernert 0:346c8150ca74 68 *
wernert 0:346c8150ca74 69 * Also supports normal way to attach member functions
wernert 0:346c8150ca74 70 * (not documented for simplicity)
wernert 0:346c8150ca74 71 *
wernert 0:346c8150ca74 72 * @param *function function to call
wernert 0:346c8150ca74 73 */
wernert 0:346c8150ca74 74 static void attach(void (*function)(void)) {
wernert 0:346c8150ca74 75 callback.attach(function);
wernert 0:346c8150ca74 76 }
wernert 0:346c8150ca74 77
wernert 0:346c8150ca74 78 template<typename T>
wernert 0:346c8150ca74 79 static void attach(T *object, void (T::*member)(void)) {
wernert 0:346c8150ca74 80 callback.attach(object, member);
wernert 0:346c8150ca74 81 }
wernert 0:346c8150ca74 82
wernert 0:346c8150ca74 83 /**
wernert 0:346c8150ca74 84 * Calibrate the timer
wernert 0:346c8150ca74 85 *
wernert 0:346c8150ca74 86 * Some of the low-power timers have very bad accuracy.
wernert 0:346c8150ca74 87 * This function calibrates it against the main timer.
wernert 0:346c8150ca74 88 *
wernert 0:346c8150ca74 89 * Warning: Blocks for 100ms!
wernert 0:346c8150ca74 90 */
wernert 0:346c8150ca74 91 static void calibrate(void);
wernert 0:346c8150ca74 92
wernert 0:346c8150ca74 93
wernert 0:346c8150ca74 94 private:
wernert 0:346c8150ca74 95 static FunctionPointer callback;
wernert 0:346c8150ca74 96 static void irq_handler(void);
wernert 0:346c8150ca74 97 static float cycles_per_ms;
wernert 0:346c8150ca74 98 };