This is the code used on my video series "Hybrid Supercapacitor Car Battery" for my own hardware monitoring system. THe videos can be found on madelectronengineering.com

Dependencies:   BurstSPI Fonts INA219 mbed LPC1114_WakeInterruptIn

Fork of SharpMemoryLCD by Paul Staron

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WakeUp.h Source File

WakeUp.h

00001 #include "mbed.h"
00002 
00003 /**
00004  * Class to make wake up a microcontroller from deepsleep using a low-power timer. 
00005  *
00006  * @code
00007  * // Depending on the LED connections either the LED is off the 2 seconds
00008  * // the target spends in deepsleep(), and on for the other second. Or it is inverted 
00009  * 
00010  * #include "mbed.h"
00011  * #include "WakeUp.h"
00012  * 
00013  * DigitalOut myled(LED1);
00014  * 
00015  * int main() {
00016  *     wait(5);
00017  *
00018  *     //The low-power oscillator can be quite inaccurate on some targets
00019  *     //this function calibrates it against the main clock
00020  *     WakeUp::calibrate();
00021  *    
00022  *     while(1) {
00023  *         //Set LED to zero
00024  *         myled = 0;
00025  *         
00026  *         //Set wakeup time for 2 seconds
00027  *         WakeUp::set_ms(2000);
00028  *         
00029  *         //Enter deepsleep, the program won't go beyond this point until it is woken up
00030  *         deepsleep();
00031  *         
00032  *         //Set LED for 1 second to one
00033  *         myled = 1;
00034  *         wait(1);
00035  *     }
00036  * }
00037  * @endcode
00038  */
00039 class WakeUp
00040 {
00041 public:
00042     /**
00043     * Set the timeout
00044     *
00045     * @param s required time in seconds
00046     */
00047     static void set(uint32_t s) {
00048         set_ms(1000 * s);
00049     }
00050     
00051     /**
00052     * Set the timeout
00053     *
00054     * @param ms required time in milliseconds
00055     */
00056     static void set_ms(uint32_t ms);
00057     
00058     /**
00059     * Attach a function to be called after timeout
00060     *
00061     * This is optional, if you just want to wake up you 
00062     * do not need to attach a function.
00063     *
00064     * Important: Many targets will run the wake-up routine
00065     * at reduced clock speed, afterwards clock speed is restored.
00066     * This means that clock speed dependent functions, such as printf
00067     * might end up distorted.
00068     *
00069     * @code
00070     * // Attaching regular function
00071     * WakeUp::attach(&yourFunc);
00072     * // Attaching member function inside another library    
00073     * WakeUp::attach(callback(this, &YourLib::yourLibFunction));    
00074     * @endcode
00075     *
00076     * It uses the new Callback system to attach functions.
00077     *
00078     * @param *function function to call
00079     */
00080     static void attach(Callback<void()> function) {
00081         callback = function;
00082     }
00083     
00084     /**
00085     * Calibrate the timer
00086     *
00087     * Some of the low-power timers have very bad accuracy.
00088     * This function calibrates it against the main timer.
00089     *
00090     * Warning: Blocks for 100ms!
00091     */
00092     static void calibrate(void);
00093 
00094     /**
00095     * Enter Standby mode then Reset
00096     *  (available only for confirmed Nucleo boards)
00097     *
00098     * @param ms required time in milliseconds
00099     */
00100 #if defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F411RE)\
00101  || defined(TARGET_NUCLEO_F401RE)\
00102  || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_L073RZ)\
00103  || defined(TARGET_NUCLEO_L053R8)
00104     // added by JH1PJL 2017-9-21
00105     static void standby_then_reset(uint32_t ms);
00106 
00107 #endif
00108 
00109 private:
00110     static Callback<void()> callback;
00111     static void irq_handler(void);
00112     static float cycles_per_ms;
00113     static bool use_reset;          // added by JH1PJL 2017-9-21
00114 };