Scheduler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include <stdint.h>
  3. #include <AP_Common/AP_Common.h>
  4. #include "AP_HAL_Boards.h"
  5. #include "AP_HAL_Namespace.h"
  6. class AP_HAL::Scheduler {
  7. public:
  8. Scheduler() {}
  9. virtual void init() = 0;
  10. virtual void delay(uint16_t ms) = 0;
  11. /*
  12. delay for the given number of microseconds. This needs to be as
  13. accurate as possible - preferably within 100 microseconds.
  14. */
  15. virtual void delay_microseconds(uint16_t us) = 0;
  16. /*
  17. delay for the given number of microseconds. On supported
  18. platforms this boosts the priority of the main thread for a
  19. short time when the time completes. The aim is to ensure the
  20. main thread runs at a higher priority than drivers for the start
  21. of each loop
  22. */
  23. virtual void delay_microseconds_boost(uint16_t us) { delay_microseconds(us); }
  24. /*
  25. inform the scheduler that we are calling an operation from the
  26. main thread that may take an extended amount of time. This can
  27. be used to prevent watchdog reset during expected long delays
  28. A value of zero cancels the previous expected delay
  29. */
  30. virtual void expect_delay_ms(uint32_t ms) { }
  31. /*
  32. end the priority boost from delay_microseconds_boost()
  33. */
  34. virtual void boost_end(void) {}
  35. // register a function to be called by the scheduler if it needs
  36. // to sleep for more than min_time_ms
  37. virtual void register_delay_callback(AP_HAL::Proc,
  38. uint16_t min_time_ms);
  39. // returns true if the Scheduler has called the delay callback
  40. // function. If you are on the main thread that means your call
  41. // stack has the scheduler on it somewhere.
  42. virtual bool in_delay_callback() const { return _in_delay_callback; }
  43. // register a high priority timer task
  44. virtual void register_timer_process(AP_HAL::MemberProc) = 0;
  45. // register a low priority IO task
  46. virtual void register_io_process(AP_HAL::MemberProc) = 0;
  47. virtual void register_timer_failsafe(AP_HAL::Proc,
  48. uint32_t period_us) = 0;
  49. virtual void system_initialized() = 0;
  50. virtual void reboot(bool hold_in_bootloader) = 0;
  51. /**
  52. optional function to stop clock at a given time, used by log replay
  53. */
  54. virtual void stop_clock(uint64_t time_usec) {}
  55. virtual bool in_main_thread() const = 0;
  56. /*
  57. disable interrupts and return a context that can be used to
  58. restore the interrupt state. This can be used to protect
  59. critical regions
  60. Warning: may not be implemented on all HALs
  61. */
  62. virtual void *disable_interrupts_save(void) { return nullptr; }
  63. /*
  64. restore interrupt state from disable_interrupts_save()
  65. */
  66. virtual void restore_interrupts(void *) {}
  67. // called by subclasses when they need to delay for some time
  68. virtual void call_delay_cb();
  69. uint16_t _min_delay_cb_ms;
  70. /*
  71. priority_base is used to select what the priority for a new
  72. thread is relative to
  73. */
  74. enum priority_base {
  75. PRIORITY_BOOST,
  76. PRIORITY_MAIN,
  77. PRIORITY_SPI,
  78. PRIORITY_I2C,
  79. PRIORITY_CAN,
  80. PRIORITY_TIMER,
  81. PRIORITY_RCIN,
  82. PRIORITY_IO,
  83. PRIORITY_UART,
  84. PRIORITY_STORAGE,
  85. PRIORITY_SCRIPTING,
  86. };
  87. /*
  88. create a new thread
  89. */
  90. virtual bool thread_create(AP_HAL::MemberProc proc, const char *name,
  91. uint32_t stack_size, priority_base base, int8_t priority) {
  92. return false;
  93. }
  94. private:
  95. AP_HAL::Proc _delay_cb;
  96. bool _in_delay_callback : 1;
  97. };
  98. /*
  99. helper macro and class to make using expect_delay_ms() safer and easier
  100. */
  101. class ExpectDelay {
  102. public:
  103. ExpectDelay(uint32_t ms);
  104. ~ExpectDelay();
  105. };
  106. #define EXPECT_DELAY_MS(ms) DELAY_JOIN( ms, __COUNTER__ )
  107. #define DELAY_JOIN( ms, counter) _DO_DELAY_JOIN( ms, counter )
  108. #define _DO_DELAY_JOIN( ms, counter ) ExpectDelay _getdelay ## counter(ms)