Sara Ojeda / Mbed 2 deprecated prueba_hsens

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WakeUp.h Source File

WakeUp.h

00001 #pragma once
00002 
00003 #include "mbed.h"
00004 
00005 /**
00006  * Class to make wake up a microcontroller from deepsleep using a low-power timer. 
00007  *
00008  * @code
00009  * // Depending on the LED connections either the LED is off the 2 seconds
00010  * // the target spends in deepsleep(), and on for the other second. Or it is inverted 
00011  * 
00012  * #include "mbed.h"
00013  * #include "WakeUp.h"
00014  * 
00015  * DigitalOut myled(LED1);
00016  * 
00017  * int main() {
00018  *     wait(5);
00019  *
00020  *     //The low-power oscillator can be quite inaccurate on some targets
00021  *     //this function calibrates it against the main clock
00022  *     WakeUp::calibrate();
00023  *    
00024  *     while(1) {
00025  *         //Set LED to zero
00026  *         myled = 0;
00027  *         
00028  *         //Set wakeup time for 2 seconds
00029  *         WakeUp::set_ms(2000);
00030  *         
00031  *         //Enter deepsleep, the program won't go beyond this point until it is woken up
00032  *         deepsleep();
00033  *         
00034  *         //Set LED for 1 second to one
00035  *         myled = 1;
00036  *         wait(1);
00037  *     }
00038  * }
00039  * @endcode
00040  */
00041 class WakeUp
00042 {
00043 public:
00044     /**
00045     * Set the timeout
00046     *
00047     * @param s required time in seconds
00048     */
00049     static void set(uint32_t s) {
00050         set_ms(1000 * s);
00051     }
00052     
00053     /**
00054     * Set the timeout
00055     *
00056     * @param ms required time in milliseconds
00057     */
00058     static void set_ms(uint32_t ms);
00059     
00060     /**
00061     * Attach a function to be called after timeout
00062     *
00063     * This is optional, if you just want to wake up you 
00064     * do not need to attach a function.
00065     *
00066     * Important: Many targets will run the wake-up routine
00067     * at reduced clock speed, afterwards clock speed is restored.
00068     * This means that clock speed dependent functions, such as printf
00069     * might end up distorted.
00070     *
00071     * Also supports normal way to attach member functions
00072     * (not documented for simplicity)
00073     *
00074     * @param *function function to call
00075     */
00076     static void attach(void (*function)(void)) {
00077         callback.attach(function);
00078     }
00079 
00080     template<typename T>
00081     static void attach(T *object, void (T::*member)(void)) {
00082         callback.attach(object, member);
00083     }
00084     
00085     /**
00086     * Calibrate the timer
00087     *
00088     * Some of the low-power timers have very bad accuracy.
00089     * This function calibrates it against the main timer.
00090     *
00091     * Warning: Blocks for 100ms!
00092     */
00093     static void calibrate(void);
00094 
00095 
00096 private:
00097     static FunctionPointer callback;
00098     static void irq_handler(void);
00099     static float cycles_per_ms;
00100 };