Added Restart(by RESET) function from Standby mode only for some Nucleo boards (STM32 series)

Dependencies:   LPC1114_WakeInterruptIn

Dependents:   Check_StandBy

Fork of WakeUp by Erik -

Example program using "Standby function" for Nucleo series is here.
/users/kenjiArai/code/Check_StandBy/

Committer:
kenjiArai
Date:
Tue Oct 03 00:30:47 2017 +0000
Revision:
26:df9d01556394
Parent:
25:2bd9df8c3ac8
disable DEBUG mode

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 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
kenjiArai 25:2bd9df8c3ac8 94 /**
kenjiArai 25:2bd9df8c3ac8 95 * Enter Standby mode then Reset
kenjiArai 25:2bd9df8c3ac8 96 * (available only for confirmed Nucleo boards)
kenjiArai 25:2bd9df8c3ac8 97 *
kenjiArai 25:2bd9df8c3ac8 98 * @param ms required time in milliseconds
kenjiArai 25:2bd9df8c3ac8 99 */
kenjiArai 25:2bd9df8c3ac8 100 #if defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F411RE)\
kenjiArai 25:2bd9df8c3ac8 101 || defined(TARGET_NUCLEO_F401RE)\
kenjiArai 25:2bd9df8c3ac8 102 || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_L073RZ)\
kenjiArai 25:2bd9df8c3ac8 103 || defined(TARGET_NUCLEO_L053R8)
kenjiArai 25:2bd9df8c3ac8 104 // added by JH1PJL 2017-9-21
kenjiArai 25:2bd9df8c3ac8 105 static void standby_then_reset(uint32_t ms);
kenjiArai 25:2bd9df8c3ac8 106
kenjiArai 25:2bd9df8c3ac8 107 #endif
Sissors 0:fc439458a359 108
Sissors 0:fc439458a359 109 private:
Sissors 23:69a0c843e4bd 110 static Callback<void()> callback;
Sissors 0:fc439458a359 111 static void irq_handler(void);
Sissors 0:fc439458a359 112 static float cycles_per_ms;
kenjiArai 25:2bd9df8c3ac8 113 static bool use_reset; // added by JH1PJL 2017-9-21
Sissors 0:fc439458a359 114 };