ms2s,STM32 Private
Fork of WakeUp by
WakeUp.h@10:c41bc9154a7c, 2014-07-28 (annotated)
- Committer:
- Sissors
- Date:
- Mon Jul 28 19:46:03 2014 +0000
- Revision:
- 10:c41bc9154a7c
- Parent:
- 8:8d9a6ac0fba8
Added initial LPC11xx support (LPC1114). Might need another commit :)
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 | 0:fc439458a359 | 69 | * Also supports normal way to attach member functions |
Sissors | 0:fc439458a359 | 70 | * (not documented for simplicity) |
Sissors | 0:fc439458a359 | 71 | * |
Sissors | 8:8d9a6ac0fba8 | 72 | * @param *function function to call |
Sissors | 0:fc439458a359 | 73 | */ |
Sissors | 0:fc439458a359 | 74 | static void attach(void (*function)(void)) { |
Sissors | 0:fc439458a359 | 75 | callback.attach(function); |
Sissors | 0:fc439458a359 | 76 | } |
Sissors | 0:fc439458a359 | 77 | |
Sissors | 0:fc439458a359 | 78 | template<typename T> |
Sissors | 0:fc439458a359 | 79 | static void attach(T *object, void (T::*member)(void)) { |
Sissors | 0:fc439458a359 | 80 | callback.attach(object, member); |
Sissors | 0:fc439458a359 | 81 | } |
Sissors | 0:fc439458a359 | 82 | |
Sissors | 0:fc439458a359 | 83 | /** |
Sissors | 0:fc439458a359 | 84 | * Calibrate the timer |
Sissors | 0:fc439458a359 | 85 | * |
Sissors | 8:8d9a6ac0fba8 | 86 | * Some of the low-power timers have very bad accuracy. |
Sissors | 8:8d9a6ac0fba8 | 87 | * This function calibrates it against the main timer. |
Sissors | 0:fc439458a359 | 88 | * |
Sissors | 0:fc439458a359 | 89 | * Warning: Blocks for 100ms! |
Sissors | 0:fc439458a359 | 90 | */ |
Sissors | 0:fc439458a359 | 91 | static void calibrate(void); |
Sissors | 0:fc439458a359 | 92 | |
Sissors | 0:fc439458a359 | 93 | |
Sissors | 0:fc439458a359 | 94 | private: |
Sissors | 0:fc439458a359 | 95 | static FunctionPointer callback; |
Sissors | 0:fc439458a359 | 96 | static void irq_handler(void); |
Sissors | 0:fc439458a359 | 97 | static float cycles_per_ms; |
Sissors | 0:fc439458a359 | 98 | }; |