ms2s,STM32 Private

Fork of WakeUp by Erik -

WakeUp.h

Committer:
Sissors
Date:
2013-12-07
Revision:
2:648712aa15b4
Parent:
1:92f4c2b52771
Child:
8:8d9a6ac0fba8

File content as of revision 2:648712aa15b4:

#include "mbed.h"

/**
* Class to make use of the LPC81x's and KL25Zs low power wake-up timer.
*
* One of the methods of waking up from deepsleep/powerdown is using the wake-up timer.
* This is an ultra low-power timer that can run from an always-on clock source.
* So while normal timers are shut-down, this one still runs, consuming only roughly 1uA.
* If the timer does not need to run everything is shut down again by the library.
*
* Example program for the LPC812: http://mbed.org/users/Sissors/code/LPC812_Sleep_HelloWorld/
*/
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.
    *
    * 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
    *
    * 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.
    *
    * 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!
    */
    static void calibrate(void);


private:
    static FunctionPointer callback;
    static void irq_handler(void);
    static float cycles_per_ms;
};