Scheduler_example07_WDT.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * TaskScheduler Test sketch - use of task IDs and watchdog timer to identify hung tasks
  3. * Test case:
  4. * Watchdog timer is set to 2 seconds (interrupt + reset)
  5. * A hearbeat task (resetting the watchdog timer) is scheduled with 500 ms interval
  6. * A number of tasks are running every 1 second and "rolling the dice" 0..19. If 5, task is made to enter infinite loop
  7. * Device should reset in 2 seconds after a task enters infinite loop
  8. * A task id and a control point number are saved to EEPROM prior to device reset, and are displayed after reboot.
  9. * In real life, device might chose to NOT activate certain tasks which failed previously (failed sensors for instance)
  10. */
  11. #define _TASK_SLEEP_ON_IDLE_RUN
  12. #define _TASK_WDT_IDS
  13. #include <TaskScheduler.h>
  14. #include <EEPROM.h>
  15. #include <avr/wdt.h>
  16. Scheduler ts;
  17. // Callback methods prototypes
  18. void TaskCB();
  19. void HB(); bool HBOn(); void HBOff();
  20. // Three tasks emulating accidental infinite loop
  21. Task tTask1(TASK_SECOND, TASK_FOREVER, &TaskCB, &ts, true);
  22. Task tTask2(TASK_SECOND, TASK_FOREVER, &TaskCB, &ts, true);
  23. Task tTask3(TASK_SECOND, TASK_FOREVER, &TaskCB, &ts, true);
  24. // Heartbeat task - resetting the watchdog timer periodically
  25. // Initiates WDT on enable, and deactivates it on disable
  26. Task tHB(500, TASK_FOREVER, &HB, &ts, false, &HBOn, &HBOff);
  27. /**
  28. * Emulating task callback function
  29. * Prints task id and randomly "hangs" in two places.
  30. * Control points are stored on the task prior to section which might hang,
  31. * making this information available to the WDT interrupt handler
  32. */
  33. void TaskCB() {
  34. Task& T = ts.currentTask();
  35. Serial.print("Task #:");
  36. Serial.print(T.getId());
  37. Serial.print(" current iteration = ");
  38. Serial.println(T.getRunCounter());
  39. // Hang if random number between 0 and 19 is 5 (5% probability)
  40. T.setControlPoint(10);
  41. if (random(20) == 5) for(;;);
  42. // Hang if random number between 0 and 99 is more that 95 (5% probability)
  43. T.setControlPoint(95);
  44. if (random(100) > 94) for(;;);
  45. }
  46. /**
  47. * This On Enable method sets up the WDT
  48. * for interrupt and reset after 2 seconds
  49. */
  50. bool HBOn() {
  51. //disable interrupts
  52. cli();
  53. //reset watchdog
  54. wdt_reset();
  55. //set up WDT interrupt
  56. WDTCSR = (1<<WDCE)|(1<<WDE);
  57. //Start watchdog timer with aDelay prescaller
  58. WDTCSR = (1<<WDIE)|(1<<WDE)|(WDTO_2S & 0x2F);
  59. // WDTCSR = (1<<WDIE)|(WDTO_2S & 0x2F); // interrupt only without reset
  60. //Enable global interrupts
  61. sei();
  62. }
  63. /**
  64. * This On Disable method disables WDT
  65. */
  66. void HBOff() {
  67. wdt_disable();
  68. }
  69. /**
  70. * This is a periodic reset of WDT
  71. */
  72. void HB() {
  73. wdt_reset();
  74. }
  75. /**
  76. * Watchdog timeout ISR
  77. *
  78. */
  79. ISR(WDT_vect)
  80. {
  81. Task& T = ts.currentTask();
  82. digitalWrite(13, HIGH);
  83. EEPROM.write(0, (byte)T.getId());
  84. EEPROM.write(1, (byte)T.getControlPoint());
  85. digitalWrite(13, LOW);
  86. }
  87. /**
  88. * Standard arduino setup routine
  89. */
  90. void setup() {
  91. Serial.begin(115200);
  92. randomSeed(analogRead(0)+analogRead(5));
  93. pinMode(13, OUTPUT);
  94. digitalWrite(13, LOW);
  95. Serial.println("WDT heartbeat test");
  96. Serial.print("Last task before reset="); Serial.println(EEPROM.read(0));
  97. Serial.print("Last control point before reset="); Serial.println(EEPROM.read(1));
  98. delay(2000);
  99. tHB.enableDelayed();
  100. }
  101. /**
  102. * Not much is left for the loop()
  103. */
  104. void loop() {
  105. ts.execute();
  106. }