TaskSchedulerSleepMethods.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Cooperative multitasking library for Arduino
  2. // Copyright (c) 2015-2022 Anatoli Arkhipenko
  3. #ifndef _TASKSCHEDULERSLEEPMETHODS_H_
  4. #define _TASKSCHEDULERSLEEPMETHODS_H_
  5. #if defined( ARDUINO_ARCH_AVR ) // Could be used only for AVR-based boards.
  6. #include <avr/sleep.h>
  7. #include <avr/power.h>
  8. void SleepMethod( unsigned long aDuration ) {
  9. set_sleep_mode(SLEEP_MODE_IDLE);
  10. sleep_enable();
  11. /* Now enter sleep mode. */
  12. sleep_mode();
  13. /* The program will continue from here after the timer timeout ~1 ms */
  14. sleep_disable(); /* First thing to do is disable sleep. */
  15. }
  16. // ARDUINO_ARCH_AVR
  17. #elif defined( CORE_TEENSY )
  18. void SleepMethod( unsigned long aDuration ) {
  19. asm("wfi");
  20. }
  21. //CORE_TEENSY
  22. #elif defined( ARDUINO_ARCH_ESP8266 )
  23. #ifndef _TASK_ESP8266_DLY_THRESHOLD
  24. #define _TASK_ESP8266_DLY_THRESHOLD 200L
  25. #endif
  26. extern "C" {
  27. #include "user_interface.h"
  28. }
  29. void SleepMethod( unsigned long aDuration ) {
  30. // to do: find suitable sleep function for esp8266
  31. if ( aDuration < _TASK_ESP8266_DLY_THRESHOLD) delay(1); // ESP8266 implementation of delay() uses timers and yield
  32. }
  33. // ARDUINO_ARCH_ESP8266
  34. #elif defined( ARDUINO_ARCH_ESP32 )
  35. #include <esp_sleep.h>
  36. #ifndef _TASK_ESP32_DLY_THRESHOLD
  37. #define _TASK_ESP32_DLY_THRESHOLD 200L
  38. #endif
  39. extern unsigned long tStart, tFinish;
  40. const unsigned long tRem = 1000-_TASK_ESP32_DLY_THRESHOLD;
  41. void SleepMethod( unsigned long aDuration ) {
  42. if ( aDuration < tRem ) {
  43. esp_sleep_enable_timer_wakeup((uint64_t) (1000 - aDuration));
  44. esp_light_sleep_start();
  45. }
  46. }
  47. // ARDUINO_ARCH_ESP32
  48. #elif defined( ARDUINO_ARCH_STM32F1 )
  49. #include <libmaple/pwr.h>
  50. #include <libmaple/scb.h>
  51. void SleepMethod( unsigned long aDuration ) {
  52. // Now go into stop mode, wake up on interrupt.
  53. // Systick interrupt will run every 1 milliseconds.
  54. asm(" wfi");
  55. }
  56. // ARDUINO_ARCH_STM32
  57. #elif defined( ENERGIA_ARCH_MSP432 )
  58. void SleepMethod( unsigned long aDuration ) {
  59. delay(1);
  60. }
  61. // ENERGIA_ARCH_MSP432
  62. #elif defined( ENERGIA_ARCH_MSP430 )
  63. void SleepMethod( unsigned long aDuration ) {
  64. sleep(1);
  65. }
  66. // ENERGIA_ARCH_MSP430
  67. #else
  68. void SleepMethod( unsigned long aDuration ) {
  69. }
  70. #endif // SLEEP METHODS
  71. #endif // _TASKSCHEDULERSLEEPMETHODS_H_