AP_HAL Scheduler: simplify register_timer_process, min time for delay cb

This commit is contained in:
Pat Hickey 2012-10-26 18:02:52 -07:00 committed by Andrew Tridgell
parent 6a59ad143e
commit bae7e4b88e
6 changed files with 27 additions and 26 deletions

View File

@ -14,9 +14,9 @@ public:
virtual uint32_t millis() = 0; virtual uint32_t millis() = 0;
virtual uint32_t micros() = 0; virtual uint32_t micros() = 0;
virtual void delay_microseconds(uint16_t us) = 0; virtual void delay_microseconds(uint16_t us) = 0;
virtual void register_delay_callback(AP_HAL::Proc) = 0; virtual void register_delay_callback(AP_HAL::Proc,
virtual void register_timer_process(AP_HAL::TimedProc, uint16_t min_time_ms) = 0;
uint32_t period_us, uint16_t phase) = 0; virtual void register_timer_process(AP_HAL::TimedProc) = 0;
virtual bool defer_timer_process(AP_HAL::TimedProc) = 0; virtual bool defer_timer_process(AP_HAL::TimedProc) = 0;
virtual void register_timer_failsafe(AP_HAL::TimedProc, virtual void register_timer_failsafe(AP_HAL::TimedProc,
uint32_t period_us) = 0; uint32_t period_us) = 0;

View File

@ -14,7 +14,7 @@ APM1AnalogIn::APM1AnalogIn () :
void APM1AnalogIn::init(void* machtnichts) { void APM1AnalogIn::init(void* machtnichts) {
/* Register AVRAnalogIn::_timer_event with the scheduler. */ /* Register AVRAnalogIn::_timer_event with the scheduler. */
hal.scheduler->register_timer_process(_timer_event, 1000, 0); hal.scheduler->register_timer_process(_timer_event);
/* Register each private channel with AVRAnalogIn. */ /* Register each private channel with AVRAnalogIn. */
_register_channel(&_bat_voltage); _register_channel(&_bat_voltage);
_register_channel(&_bat_current); _register_channel(&_bat_current);

View File

@ -17,7 +17,7 @@ APM2AnalogIn::APM2AnalogIn () :
void APM2AnalogIn::init(void * machtichts) { void APM2AnalogIn::init(void * machtichts) {
/* Register AVRAnalogIn::_timer_event with the scheduler. */ /* Register AVRAnalogIn::_timer_event with the scheduler. */
hal.scheduler->register_timer_process(_timer_event, 1000, 0); hal.scheduler->register_timer_process(_timer_event);
/* Register each private channel with AVRAnalogIn. */ /* Register each private channel with AVRAnalogIn. */
_register_channel(&_pitot); _register_channel(&_pitot);
_register_channel(&_bat_voltage); _register_channel(&_bat_voltage);

View File

@ -173,16 +173,18 @@ uint32_t ArduinoScheduler::_micros() {
void ArduinoScheduler::delay(uint32_t ms) void ArduinoScheduler::delay(uint32_t ms)
{ {
uint16_t start = (uint16_t)micros(); uint16_t start = (uint16_t)micros();
while (ms > 0) { while (ms > 0) {
if (((uint16_t)micros() - start) >= 1000) { if (((uint16_t)micros() - start) >= 1000) {
ms--; ms--;
start += 1000; start += 1000;
if (_delay_cb) { if (_min_delay_cb_ms >= ms) {
_delay_cb(); if (_delay_cb) {
_delay_cb();
}
} }
} }
} }
} }
/* Delay for the given number of microseconds. Assumes a 16 MHz clock. */ /* Delay for the given number of microseconds. Assumes a 16 MHz clock. */
@ -209,14 +211,13 @@ void ArduinoScheduler::delay_microseconds(uint16_t us)
); );
} }
void ArduinoScheduler::register_delay_callback(AP_HAL::Proc proc) { void ArduinoScheduler::register_delay_callback(AP_HAL::Proc proc,
uint16_t min_time_ms) {
_delay_cb = proc; _delay_cb = proc;
_min_delay_cb_ms = min_time_ms;
} }
void ArduinoScheduler::register_timer_process( void ArduinoScheduler::register_timer_process(AP_HAL::TimedProc proc) {
AP_HAL::TimedProc proc, uint32_t period_us, uint16_t phase) {
/* XXX Assert period_us == 1000 */
/* XXX phase meaningless */
for (int i = 0; i < _num_timer_procs; i++) { for (int i = 0; i < _num_timer_procs; i++) {
if (_timer_proc[i] == proc) { if (_timer_proc[i] == proc) {
return; return;

View File

@ -19,9 +19,8 @@ public:
uint32_t millis(); uint32_t millis();
uint32_t micros(); uint32_t micros();
void delay_microseconds(uint16_t us); void delay_microseconds(uint16_t us);
void register_delay_callback(AP_HAL::Proc); void register_delay_callback(AP_HAL::Proc, uint16_t min_time_ms);
void register_timer_process(AP_HAL::TimedProc, void register_timer_process(AP_HAL::TimedProc);
uint32_t period_us, uint16_t phase);
bool defer_timer_process(AP_HAL::TimedProc); bool defer_timer_process(AP_HAL::TimedProc);
void register_timer_failsafe(AP_HAL::TimedProc, uint32_t period_us); void register_timer_failsafe(AP_HAL::TimedProc, uint32_t period_us);
void suspend_timer_procs(); void suspend_timer_procs();
@ -39,6 +38,7 @@ private:
static uint32_t _micros(); static uint32_t _micros();
AP_HAL::Proc _delay_cb; AP_HAL::Proc _delay_cb;
uint16_t _min_delay_cb_ms;
static AP_HAL::TimedProc _failsafe; static AP_HAL::TimedProc _failsafe;
static volatile bool _timer_suspended; static volatile bool _timer_suspended;

View File

@ -71,7 +71,7 @@ void setup (void) {
"Pin %d should toggle at 1khz:\r\n"), "Pin %d should toggle at 1khz:\r\n"),
(int) DELAY_TOGGLE_PIN); (int) DELAY_TOGGLE_PIN);
hal.scheduler->register_delay_callback(delay_toggle); hal.scheduler->register_delay_callback(delay_toggle,0);
hal.scheduler->delay(100); hal.scheduler->delay(100);
@ -89,8 +89,8 @@ void setup (void) {
hal.console->printf_P(PSTR("Pin %d should toggle at 1khz.\r\n"), hal.console->printf_P(PSTR("Pin %d should toggle at 1khz.\r\n"),
(int) SCHEDULED_TOGGLE_PIN_2); (int) SCHEDULED_TOGGLE_PIN_2);
hal.scheduler->register_timer_process(schedule_toggle_1, 1000, 0); hal.scheduler->register_timer_process(schedule_toggle_1);
hal.scheduler->register_timer_process(schedule_toggle_2, 1000, 0); hal.scheduler->register_timer_process(schedule_toggle_2);
hal.scheduler->delay(100); hal.scheduler->delay(100);
@ -99,7 +99,7 @@ void setup (void) {
"dominates the processor.")); "dominates the processor."));
hal.console->printf_P(PSTR("Pin %d should toggle then go high forever.\r\n"), hal.console->printf_P(PSTR("Pin %d should toggle then go high forever.\r\n"),
(int) SCHEDULED_TOGGLE_PIN_2); (int) SCHEDULED_TOGGLE_PIN_2);
hal.scheduler->register_timer_process(schedule_toggle_hang, 1000, 0); hal.scheduler->register_timer_process(schedule_toggle_hang);
} }
void loop (void) { } void loop (void) { }