Skip to content

Commit

Permalink
AP_HAL_ChibiOS: add gpio attach interrupt with 64bit micros time in t…
Browse files Browse the repository at this point in the history
…he callback
  • Loading branch information
bugobliterator committed Jun 25, 2024
1 parent f8db86c commit 0455a02
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
47 changes: 40 additions & 7 deletions libraries/AP_HAL_ChibiOS/GPIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct gpio_entry {
uint8_t pwm_num;
ioline_t pal_line;
AP_HAL::GPIO::irq_handler_fn_t fn; // callback for GPIO interface
AP_HAL::GPIO::irq_handler_64_fn_t fn_64; // callback for GPIO interface
thread_reference_t thd_wait;
bool is_input;
uint8_t mode;
Expand Down Expand Up @@ -318,6 +319,29 @@ bool GPIO::attach_interrupt(uint8_t pin,
return false;
}
g->fn = fn;
g->fn_64 = nullptr;
g->isr_mode = mode;
return true;
}

bool GPIO::attach_interrupt(uint8_t pin,
irq_handler_64_fn_t fn_64,
INTERRUPT_TRIGGER_TYPE mode)
{
struct gpio_entry *g = gpio_by_pin_num(pin, false);
if (!g) {
return false;
}
g->isr_disabled_ticks = 0;
g->isr_quota = 0;
if (!_attach_interrupt(g->pal_line,
palcallback_t(fn_64?pal_interrupt_cb_functor:nullptr),
g,
mode)) {
return false;
}
g->fn = nullptr;
g->fn_64 = fn_64;
g->isr_mode = mode;
return true;
}
Expand Down Expand Up @@ -442,14 +466,19 @@ static void pal_interrupt_cb(void *arg)

static void pal_interrupt_cb_functor(void *arg)
{
const uint32_t now = AP_HAL::micros();

struct gpio_entry *g = (gpio_entry *)arg;
if (g == nullptr) {
// what?
return;
}
if (!(g->fn)) {
uint64_t now;
if (g->fn_64) {
now = AP_HAL::micros64();
} else {
now = AP_HAL::micros();
}

if (!(g->fn) && !(g->fn_64)) {
return;
}
if (g->isr_quota >= 1) {
Expand All @@ -467,7 +496,11 @@ static void pal_interrupt_cb_functor(void *arg)
}
g->isr_quota--;
}
(g->fn)(g->pin_num, palReadLine(g->pal_line), now);
if (g->fn) {
(g->fn)(g->pin_num, palReadLine(g->pal_line), now);
} else if (g->fn_64) {
(g->fn_64)(g->pin_num, palReadLine(g->pal_line), now);
}
}

/*
Expand Down Expand Up @@ -634,15 +667,15 @@ void GPIO::timer_tick()

// 100 * 100ms = 10 seconds
const uint8_t ISR_retry_ticks = 100U;
if ((_gpio_tab[i].isr_disabled_ticks > ISR_retry_ticks) && (_gpio_tab[i].fn != nullptr)) {
if ((_gpio_tab[i].isr_disabled_ticks > ISR_retry_ticks) && (_gpio_tab[i].fn != nullptr && _gpio_tab[i].fn_64 != nullptr)) {
// Try re-enabling
#ifndef HAL_NO_UARTDRIVER
GCS_SEND_TEXT(MAV_SEVERITY_NOTICE, "Retrying pin %d after ISR flood", _gpio_tab[i].pin_num);
#endif
if (attach_interrupt(_gpio_tab[i].pin_num, _gpio_tab[i].fn, _gpio_tab[i].isr_mode)) {
if ((_gpio_tab[i].fn != nullptr) && attach_interrupt(_gpio_tab[i].pin_num, _gpio_tab[i].fn, _gpio_tab[i].isr_mode)) {
// Success, reset quota
_gpio_tab[i].isr_quota = quota;
} else {
} else if ((_gpio_tab[i].fn_64 != nullptr) && attach_interrupt(_gpio_tab[i].pin_num, _gpio_tab[i].fn_64, _gpio_tab[i].isr_mode)) {
// Failed, reset disabled count to try again later
_gpio_tab[i].isr_disabled_ticks = 1;
}
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_HAL_ChibiOS/GPIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class ChibiOS::GPIO : public AP_HAL::GPIO {
irq_handler_fn_t fn,
INTERRUPT_TRIGGER_TYPE mode) override;

bool attach_interrupt(uint8_t pin,
irq_handler_64_fn_t fn,
INTERRUPT_TRIGGER_TYPE mode) override;

/* return true if USB cable is connected */
bool usb_connected(void) override;

Expand Down

0 comments on commit 0455a02

Please sign in to comment.