Use on STM32L432KC
Dependencies: LPC1114_WakeInterruptIn
Fork of WakeUp by
WakeUp.h@25:b6bd5ab62934, 2018-01-13 (annotated)
- Committer:
- Tbroussard
- Date:
- Sat Jan 13 09:58:42 2018 +0000
- Revision:
- 25:b6bd5ab62934
- Parent:
- 24:65c04a02ad45
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:fc439458a359 | 1 | #include "mbed.h" |
Sissors | 0:fc439458a359 | 2 | |
Sissors | 0:fc439458a359 | 3 | /** |
Sissors | 8:8d9a6ac0fba8 | 4 | * Class to make wake up a microcontroller from deepsleep using a low-power timer. |
Sissors | 8:8d9a6ac0fba8 | 5 | * |
Sissors | 8:8d9a6ac0fba8 | 6 | * @code |
Sissors | 8:8d9a6ac0fba8 | 7 | * // Depending on the LED connections either the LED is off the 2 seconds |
Sissors | 8:8d9a6ac0fba8 | 8 | * // the target spends in deepsleep(), and on for the other second. Or it is inverted |
Sissors | 8:8d9a6ac0fba8 | 9 | * |
Sissors | 8:8d9a6ac0fba8 | 10 | * #include "mbed.h" |
Sissors | 8:8d9a6ac0fba8 | 11 | * #include "WakeUp.h" |
Sissors | 8:8d9a6ac0fba8 | 12 | * |
Sissors | 8:8d9a6ac0fba8 | 13 | * DigitalOut myled(LED1); |
Sissors | 8:8d9a6ac0fba8 | 14 | * |
Sissors | 8:8d9a6ac0fba8 | 15 | * int main() { |
Sissors | 10:c41bc9154a7c | 16 | * wait(5); |
Sissors | 10:c41bc9154a7c | 17 | * |
Sissors | 8:8d9a6ac0fba8 | 18 | * //The low-power oscillator can be quite inaccurate on some targets |
Sissors | 8:8d9a6ac0fba8 | 19 | * //this function calibrates it against the main clock |
Sissors | 8:8d9a6ac0fba8 | 20 | * WakeUp::calibrate(); |
Sissors | 8:8d9a6ac0fba8 | 21 | * |
Sissors | 8:8d9a6ac0fba8 | 22 | * while(1) { |
Sissors | 8:8d9a6ac0fba8 | 23 | * //Set LED to zero |
Sissors | 8:8d9a6ac0fba8 | 24 | * myled = 0; |
Sissors | 8:8d9a6ac0fba8 | 25 | * |
Sissors | 8:8d9a6ac0fba8 | 26 | * //Set wakeup time for 2 seconds |
Sissors | 8:8d9a6ac0fba8 | 27 | * WakeUp::set_ms(2000); |
Sissors | 8:8d9a6ac0fba8 | 28 | * |
Sissors | 8:8d9a6ac0fba8 | 29 | * //Enter deepsleep, the program won't go beyond this point until it is woken up |
Sissors | 8:8d9a6ac0fba8 | 30 | * deepsleep(); |
Sissors | 8:8d9a6ac0fba8 | 31 | * |
Sissors | 8:8d9a6ac0fba8 | 32 | * //Set LED for 1 second to one |
Sissors | 8:8d9a6ac0fba8 | 33 | * myled = 1; |
Sissors | 8:8d9a6ac0fba8 | 34 | * wait(1); |
Sissors | 8:8d9a6ac0fba8 | 35 | * } |
Sissors | 8:8d9a6ac0fba8 | 36 | * } |
Sissors | 8:8d9a6ac0fba8 | 37 | * @endcode |
Sissors | 8:8d9a6ac0fba8 | 38 | */ |
Sissors | 0:fc439458a359 | 39 | class WakeUp |
Sissors | 0:fc439458a359 | 40 | { |
Sissors | 0:fc439458a359 | 41 | public: |
Sissors | 0:fc439458a359 | 42 | /** |
Sissors | 0:fc439458a359 | 43 | * Set the timeout |
Sissors | 0:fc439458a359 | 44 | * |
Sissors | 8:8d9a6ac0fba8 | 45 | * @param s required time in seconds |
Sissors | 0:fc439458a359 | 46 | */ |
Sissors | 0:fc439458a359 | 47 | static void set(uint32_t s) { |
Sissors | 0:fc439458a359 | 48 | set_ms(1000 * s); |
Sissors | 0:fc439458a359 | 49 | } |
Sissors | 0:fc439458a359 | 50 | |
Sissors | 0:fc439458a359 | 51 | /** |
Sissors | 0:fc439458a359 | 52 | * Set the timeout |
Sissors | 0:fc439458a359 | 53 | * |
Sissors | 8:8d9a6ac0fba8 | 54 | * @param ms required time in milliseconds |
Sissors | 0:fc439458a359 | 55 | */ |
Sissors | 0:fc439458a359 | 56 | static void set_ms(uint32_t ms); |
Sissors | 0:fc439458a359 | 57 | |
Sissors | 0:fc439458a359 | 58 | /** |
Sissors | 0:fc439458a359 | 59 | * Attach a function to be called after timeout |
Sissors | 0:fc439458a359 | 60 | * |
Sissors | 0:fc439458a359 | 61 | * This is optional, if you just want to wake up you |
Sissors | 0:fc439458a359 | 62 | * do not need to attach a function. |
Sissors | 0:fc439458a359 | 63 | * |
Sissors | 8:8d9a6ac0fba8 | 64 | * Important: Many targets will run the wake-up routine |
Sissors | 8:8d9a6ac0fba8 | 65 | * at reduced clock speed, afterwards clock speed is restored. |
Sissors | 8:8d9a6ac0fba8 | 66 | * This means that clock speed dependent functions, such as printf |
Sissors | 8:8d9a6ac0fba8 | 67 | * might end up distorted. |
Sissors | 8:8d9a6ac0fba8 | 68 | * |
Sissors | 24:65c04a02ad45 | 69 | * @code |
Sissors | 24:65c04a02ad45 | 70 | * // Attaching regular function |
Sissors | 24:65c04a02ad45 | 71 | * WakeUp::attach(&yourFunc); |
Sissors | 24:65c04a02ad45 | 72 | * // Attaching member function inside another library |
Sissors | 24:65c04a02ad45 | 73 | * WakeUp::attach(callback(this, &YourLib::yourLibFunction)); |
Sissors | 24:65c04a02ad45 | 74 | * @endcode |
Sissors | 24:65c04a02ad45 | 75 | * |
Sissors | 23:69a0c843e4bd | 76 | * It uses the new Callback system to attach functions. |
Sissors | 0:fc439458a359 | 77 | * |
Sissors | 8:8d9a6ac0fba8 | 78 | * @param *function function to call |
Sissors | 0:fc439458a359 | 79 | */ |
Sissors | 23:69a0c843e4bd | 80 | static void attach(Callback<void()> function) { |
Sissors | 23:69a0c843e4bd | 81 | callback = function; |
Sissors | 0:fc439458a359 | 82 | } |
Sissors | 0:fc439458a359 | 83 | |
Sissors | 0:fc439458a359 | 84 | /** |
Sissors | 0:fc439458a359 | 85 | * Calibrate the timer |
Sissors | 0:fc439458a359 | 86 | * |
Sissors | 8:8d9a6ac0fba8 | 87 | * Some of the low-power timers have very bad accuracy. |
Sissors | 8:8d9a6ac0fba8 | 88 | * This function calibrates it against the main timer. |
Sissors | 0:fc439458a359 | 89 | * |
Sissors | 0:fc439458a359 | 90 | * Warning: Blocks for 100ms! |
Sissors | 0:fc439458a359 | 91 | */ |
Sissors | 0:fc439458a359 | 92 | static void calibrate(void); |
Sissors | 0:fc439458a359 | 93 | |
Sissors | 0:fc439458a359 | 94 | |
Sissors | 0:fc439458a359 | 95 | private: |
Sissors | 23:69a0c843e4bd | 96 | static Callback<void()> callback; |
Sissors | 0:fc439458a359 | 97 | static void irq_handler(void); |
Sissors | 0:fc439458a359 | 98 | static float cycles_per_ms; |
Sissors | 0:fc439458a359 | 99 | }; |