From 154b133b93037814ce0012d9f486d51fd1080b8a Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 13 May 2024 17:45:12 +0200 Subject: [PATCH] IRQManager: implement callback based method for custom irq handlers --- cores/arduino/IRQManager.cpp | 25 +++++++++++++++++++++++++ cores/arduino/IRQManager.h | 2 ++ 2 files changed, 27 insertions(+) diff --git a/cores/arduino/IRQManager.cpp b/cores/arduino/IRQManager.cpp index 4d9e6e1e8..107c01426 100644 --- a/cores/arduino/IRQManager.cpp +++ b/cores/arduino/IRQManager.cpp @@ -939,6 +939,31 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) { return rv; } +/* + The implementer should update the value of last_interrupt once done + eg: + + bool config_my_funky_peripheral(unsigned int* last_interrupt, volatile uint32_t *irq_ptr, void* config) { + funky_peripheral_cfg_t* cfg = (funky_peripheral_cfg_t*)config; + *(irq_ptr + *last_interrupt) = (uint32_t)cfg->irq_callback; + cfg->interrupt = *last_interrupt; + // increase the interrupt count + *last_interrupt++; + return true; + } + + and then use as: + + funky_peripheral_cfg_t funky_cfg; + IRQManager::getInstance().addCustomPeripheral(config_my_funky_peripheral, &funky_cfg); +*/ +bool IRQManager::addCustomPeripheral(bool (*cb)(unsigned int* last_interrupt, volatile uint32_t *irq_ptr, void* conf), void* conf) { + volatile uint32_t *irq_ptr = (volatile uint32_t *)SCB->VTOR; + irq_ptr += FIXED_IRQ_NUM; + + return cb(&last_interrupt_index, irq_ptr, conf); +} + bool IRQManager::set_adc_end_link_event(int li, int ch){ bool rv = false; if (0) {} diff --git a/cores/arduino/IRQManager.h b/cores/arduino/IRQManager.h index 2f9e093cc..8e253abe2 100644 --- a/cores/arduino/IRQManager.h +++ b/cores/arduino/IRQManager.h @@ -190,6 +190,8 @@ using Irq_f = void (*)(void); class IRQManager { public: bool addPeripheral(Peripheral_t p, void *cfg); + bool addCustomPeripheral(bool (*cb)(unsigned int* last_interrupt, volatile uint32_t *irq_ptr, void* conf), void* conf); + static IRQManager& getInstance(); #ifdef HAS_DMAC