123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #include "cmsis.h"
- #if defined(NVIC_NUM_VECTORS)
- #include "mbed_toolchain.h"
- #undef MBED_DEPRECATED_SINCE
- #define MBED_DEPRECATED_SINCE(...)
- #include "drivers/InterruptManager.h"
- #include "platform/mbed_critical.h"
- #include <string.h>
- #define CHAIN_INITIAL_SIZE 4
- namespace mbed {
- typedef void (*pvoidf)(void);
- InterruptManager *InterruptManager::_instance = (InterruptManager *)NULL;
- InterruptManager *InterruptManager::get()
- {
- if (NULL == _instance) {
- InterruptManager *temp = new InterruptManager();
-
- core_util_critical_section_enter();
- if (NULL == _instance) {
- _instance = temp;
- }
- core_util_critical_section_exit();
-
- if (temp != _instance) {
- delete temp;
- }
- }
- return _instance;
- }
- InterruptManager::InterruptManager()
- {
-
- memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain *));
- }
- void InterruptManager::destroy()
- {
-
-
-
- if (NULL != _instance) {
- delete _instance;
- _instance = (InterruptManager *)NULL;
- }
- }
- InterruptManager::~InterruptManager()
- {
- for (int i = 0; i < NVIC_NUM_VECTORS; i++)
- if (NULL != _chains[i]) {
- delete _chains[i];
- }
- }
- bool InterruptManager::must_replace_vector(IRQn_Type irq)
- {
- lock();
- int ret = false;
- int irq_pos = get_irq_index(irq);
- if (NULL == _chains[irq_pos]) {
- _chains[irq_pos] = new CallChain(CHAIN_INITIAL_SIZE);
- _chains[irq_pos]->add((pvoidf)NVIC_GetVector(irq));
- ret = true;
- }
- unlock();
- return ret;
- }
- pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front)
- {
- lock();
- int irq_pos = get_irq_index(irq);
- bool change = must_replace_vector(irq);
- pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(function) : _chains[irq_pos]->add(function);
- if (change) {
- NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
- }
- unlock();
- return pf;
- }
- bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq)
- {
- int irq_pos = get_irq_index(irq);
- bool ret = false;
- lock();
- if (_chains[irq_pos] != NULL) {
- if (_chains[irq_pos]->remove(handler)) {
- ret = true;
- }
- }
- unlock();
- return ret;
- }
- void InterruptManager::irq_helper()
- {
- _chains[__get_IPSR()]->call();
- }
- int InterruptManager::get_irq_index(IRQn_Type irq)
- {
-
- return (int)irq + NVIC_USER_IRQ_OFFSET;
- }
- void InterruptManager::static_irq_helper()
- {
- InterruptManager::get()->irq_helper();
- }
- void InterruptManager::lock()
- {
- _mutex.lock();
- }
- void InterruptManager::unlock()
- {
- _mutex.unlock();
- }
- }
- #endif
|