ms2s,STM32 Private

Fork of WakeUp by Erik -

Committer:
Sissors
Date:
Thu Jul 24 12:59:16 2014 +0000
Revision:
8:8d9a6ac0fba8
Parent:
2:648712aa15b4
Child:
10:c41bc9154a7c
Updated documentation

Who changed what in which revision?

UserRevisionLine numberNew 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 8:8d9a6ac0fba8 16 * //The low-power oscillator can be quite inaccurate on some targets
Sissors 8:8d9a6ac0fba8 17 * //this function calibrates it against the main clock
Sissors 8:8d9a6ac0fba8 18 * WakeUp::calibrate();
Sissors 8:8d9a6ac0fba8 19 *
Sissors 8:8d9a6ac0fba8 20 * while(1) {
Sissors 8:8d9a6ac0fba8 21 * //Set LED to zero
Sissors 8:8d9a6ac0fba8 22 * myled = 0;
Sissors 8:8d9a6ac0fba8 23 *
Sissors 8:8d9a6ac0fba8 24 * //Set wakeup time for 2 seconds
Sissors 8:8d9a6ac0fba8 25 * WakeUp::set_ms(2000);
Sissors 8:8d9a6ac0fba8 26 *
Sissors 8:8d9a6ac0fba8 27 * //Enter deepsleep, the program won't go beyond this point until it is woken up
Sissors 8:8d9a6ac0fba8 28 * deepsleep();
Sissors 8:8d9a6ac0fba8 29 *
Sissors 8:8d9a6ac0fba8 30 * //Set LED for 1 second to one
Sissors 8:8d9a6ac0fba8 31 * myled = 1;
Sissors 8:8d9a6ac0fba8 32 * wait(1);
Sissors 8:8d9a6ac0fba8 33 * }
Sissors 8:8d9a6ac0fba8 34 * }
Sissors 8:8d9a6ac0fba8 35 * @endcode
Sissors 8:8d9a6ac0fba8 36 */
Sissors 0:fc439458a359 37 class WakeUp
Sissors 0:fc439458a359 38 {
Sissors 0:fc439458a359 39 public:
Sissors 0:fc439458a359 40 /**
Sissors 0:fc439458a359 41 * Set the timeout
Sissors 0:fc439458a359 42 *
Sissors 8:8d9a6ac0fba8 43 * @param s required time in seconds
Sissors 0:fc439458a359 44 */
Sissors 0:fc439458a359 45 static void set(uint32_t s) {
Sissors 0:fc439458a359 46 set_ms(1000 * s);
Sissors 0:fc439458a359 47 }
Sissors 0:fc439458a359 48
Sissors 0:fc439458a359 49 /**
Sissors 0:fc439458a359 50 * Set the timeout
Sissors 0:fc439458a359 51 *
Sissors 8:8d9a6ac0fba8 52 * @param ms required time in milliseconds
Sissors 0:fc439458a359 53 */
Sissors 0:fc439458a359 54 static void set_ms(uint32_t ms);
Sissors 0:fc439458a359 55
Sissors 0:fc439458a359 56 /**
Sissors 0:fc439458a359 57 * Attach a function to be called after timeout
Sissors 0:fc439458a359 58 *
Sissors 0:fc439458a359 59 * This is optional, if you just want to wake up you
Sissors 0:fc439458a359 60 * do not need to attach a function.
Sissors 0:fc439458a359 61 *
Sissors 8:8d9a6ac0fba8 62 * Important: Many targets will run the wake-up routine
Sissors 8:8d9a6ac0fba8 63 * at reduced clock speed, afterwards clock speed is restored.
Sissors 8:8d9a6ac0fba8 64 * This means that clock speed dependent functions, such as printf
Sissors 8:8d9a6ac0fba8 65 * might end up distorted.
Sissors 8:8d9a6ac0fba8 66 *
Sissors 0:fc439458a359 67 * Also supports normal way to attach member functions
Sissors 0:fc439458a359 68 * (not documented for simplicity)
Sissors 0:fc439458a359 69 *
Sissors 8:8d9a6ac0fba8 70 * @param *function function to call
Sissors 0:fc439458a359 71 */
Sissors 0:fc439458a359 72 static void attach(void (*function)(void)) {
Sissors 0:fc439458a359 73 callback.attach(function);
Sissors 0:fc439458a359 74 }
Sissors 0:fc439458a359 75
Sissors 0:fc439458a359 76 template<typename T>
Sissors 0:fc439458a359 77 static void attach(T *object, void (T::*member)(void)) {
Sissors 0:fc439458a359 78 callback.attach(object, member);
Sissors 0:fc439458a359 79 }
Sissors 0:fc439458a359 80
Sissors 0:fc439458a359 81 /**
Sissors 0:fc439458a359 82 * Calibrate the timer
Sissors 0:fc439458a359 83 *
Sissors 8:8d9a6ac0fba8 84 * Some of the low-power timers have very bad accuracy.
Sissors 8:8d9a6ac0fba8 85 * This function calibrates it against the main timer.
Sissors 0:fc439458a359 86 *
Sissors 0:fc439458a359 87 * Warning: Blocks for 100ms!
Sissors 0:fc439458a359 88 */
Sissors 0:fc439458a359 89 static void calibrate(void);
Sissors 0:fc439458a359 90
Sissors 0:fc439458a359 91
Sissors 0:fc439458a359 92 private:
Sissors 0:fc439458a359 93 static FunctionPointer callback;
Sissors 0:fc439458a359 94 static void irq_handler(void);
Sissors 0:fc439458a359 95 static float cycles_per_ms;
Sissors 0:fc439458a359 96 };