fft
Dependencies: BSP_DISCO_F746NG SDFileSystem_Warning_Fixed
mbed_src_STM32F7/common/InterruptManager.cpp@0:3e46577dc273, 2016-10-09 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Sun Oct 09 10:11:14 2016 +0000
- Revision:
- 0:3e46577dc273
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:3e46577dc273 | 1 | #include "cmsis.h" |
MikamiUitOpen | 0:3e46577dc273 | 2 | #if defined(NVIC_NUM_VECTORS) |
MikamiUitOpen | 0:3e46577dc273 | 3 | |
MikamiUitOpen | 0:3e46577dc273 | 4 | #include "InterruptManager.h" |
MikamiUitOpen | 0:3e46577dc273 | 5 | #include <string.h> |
MikamiUitOpen | 0:3e46577dc273 | 6 | |
MikamiUitOpen | 0:3e46577dc273 | 7 | #define CHAIN_INITIAL_SIZE 4 |
MikamiUitOpen | 0:3e46577dc273 | 8 | |
MikamiUitOpen | 0:3e46577dc273 | 9 | namespace mbed { |
MikamiUitOpen | 0:3e46577dc273 | 10 | |
MikamiUitOpen | 0:3e46577dc273 | 11 | typedef void (*pvoidf)(void); |
MikamiUitOpen | 0:3e46577dc273 | 12 | |
MikamiUitOpen | 0:3e46577dc273 | 13 | InterruptManager* InterruptManager::_instance = (InterruptManager*)NULL; |
MikamiUitOpen | 0:3e46577dc273 | 14 | |
MikamiUitOpen | 0:3e46577dc273 | 15 | InterruptManager* InterruptManager::get() { |
MikamiUitOpen | 0:3e46577dc273 | 16 | if (NULL == _instance) |
MikamiUitOpen | 0:3e46577dc273 | 17 | _instance = new InterruptManager(); |
MikamiUitOpen | 0:3e46577dc273 | 18 | return _instance; |
MikamiUitOpen | 0:3e46577dc273 | 19 | } |
MikamiUitOpen | 0:3e46577dc273 | 20 | |
MikamiUitOpen | 0:3e46577dc273 | 21 | InterruptManager::InterruptManager() { |
MikamiUitOpen | 0:3e46577dc273 | 22 | memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain*)); |
MikamiUitOpen | 0:3e46577dc273 | 23 | } |
MikamiUitOpen | 0:3e46577dc273 | 24 | |
MikamiUitOpen | 0:3e46577dc273 | 25 | void InterruptManager::destroy() { |
MikamiUitOpen | 0:3e46577dc273 | 26 | // Not a good idea to call this unless NO interrupt at all |
MikamiUitOpen | 0:3e46577dc273 | 27 | // is under the control of the handler; otherwise, a system crash |
MikamiUitOpen | 0:3e46577dc273 | 28 | // is very likely to occur |
MikamiUitOpen | 0:3e46577dc273 | 29 | if (NULL != _instance) { |
MikamiUitOpen | 0:3e46577dc273 | 30 | delete _instance; |
MikamiUitOpen | 0:3e46577dc273 | 31 | _instance = (InterruptManager*)NULL; |
MikamiUitOpen | 0:3e46577dc273 | 32 | } |
MikamiUitOpen | 0:3e46577dc273 | 33 | } |
MikamiUitOpen | 0:3e46577dc273 | 34 | |
MikamiUitOpen | 0:3e46577dc273 | 35 | InterruptManager::~InterruptManager() { |
MikamiUitOpen | 0:3e46577dc273 | 36 | for(int i = 0; i < NVIC_NUM_VECTORS; i++) |
MikamiUitOpen | 0:3e46577dc273 | 37 | if (NULL != _chains[i]) |
MikamiUitOpen | 0:3e46577dc273 | 38 | delete _chains[i]; |
MikamiUitOpen | 0:3e46577dc273 | 39 | } |
MikamiUitOpen | 0:3e46577dc273 | 40 | |
MikamiUitOpen | 0:3e46577dc273 | 41 | bool InterruptManager::must_replace_vector(IRQn_Type irq) { |
MikamiUitOpen | 0:3e46577dc273 | 42 | int irq_pos = get_irq_index(irq); |
MikamiUitOpen | 0:3e46577dc273 | 43 | |
MikamiUitOpen | 0:3e46577dc273 | 44 | if (NULL == _chains[irq_pos]) { |
MikamiUitOpen | 0:3e46577dc273 | 45 | _chains[irq_pos] = new CallChain(CHAIN_INITIAL_SIZE); |
MikamiUitOpen | 0:3e46577dc273 | 46 | _chains[irq_pos]->add((pvoidf)NVIC_GetVector(irq)); |
MikamiUitOpen | 0:3e46577dc273 | 47 | return true; |
MikamiUitOpen | 0:3e46577dc273 | 48 | } |
MikamiUitOpen | 0:3e46577dc273 | 49 | return false; |
MikamiUitOpen | 0:3e46577dc273 | 50 | } |
MikamiUitOpen | 0:3e46577dc273 | 51 | |
MikamiUitOpen | 0:3e46577dc273 | 52 | pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front) { |
MikamiUitOpen | 0:3e46577dc273 | 53 | int irq_pos = get_irq_index(irq); |
MikamiUitOpen | 0:3e46577dc273 | 54 | bool change = must_replace_vector(irq); |
MikamiUitOpen | 0:3e46577dc273 | 55 | |
MikamiUitOpen | 0:3e46577dc273 | 56 | pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(function) : _chains[irq_pos]->add(function); |
MikamiUitOpen | 0:3e46577dc273 | 57 | if (change) |
MikamiUitOpen | 0:3e46577dc273 | 58 | NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); |
MikamiUitOpen | 0:3e46577dc273 | 59 | return pf; |
MikamiUitOpen | 0:3e46577dc273 | 60 | } |
MikamiUitOpen | 0:3e46577dc273 | 61 | |
MikamiUitOpen | 0:3e46577dc273 | 62 | bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) { |
MikamiUitOpen | 0:3e46577dc273 | 63 | int irq_pos = get_irq_index(irq); |
MikamiUitOpen | 0:3e46577dc273 | 64 | |
MikamiUitOpen | 0:3e46577dc273 | 65 | if (NULL == _chains[irq_pos]) |
MikamiUitOpen | 0:3e46577dc273 | 66 | return false; |
MikamiUitOpen | 0:3e46577dc273 | 67 | if (!_chains[irq_pos]->remove(handler)) |
MikamiUitOpen | 0:3e46577dc273 | 68 | return false; |
MikamiUitOpen | 0:3e46577dc273 | 69 | // If there's a single function left in the chain, swith the interrupt vector |
MikamiUitOpen | 0:3e46577dc273 | 70 | // to call that function directly. This way we save both time and space. |
MikamiUitOpen | 0:3e46577dc273 | 71 | if (_chains[irq_pos]->size() == 1 && NULL != _chains[irq_pos]->get(0)->get_function()) { |
MikamiUitOpen | 0:3e46577dc273 | 72 | NVIC_SetVector(irq, (uint32_t)_chains[irq_pos]->get(0)->get_function()); |
MikamiUitOpen | 0:3e46577dc273 | 73 | delete _chains[irq_pos]; |
MikamiUitOpen | 0:3e46577dc273 | 74 | _chains[irq_pos] = (CallChain*) NULL; |
MikamiUitOpen | 0:3e46577dc273 | 75 | } |
MikamiUitOpen | 0:3e46577dc273 | 76 | return true; |
MikamiUitOpen | 0:3e46577dc273 | 77 | } |
MikamiUitOpen | 0:3e46577dc273 | 78 | |
MikamiUitOpen | 0:3e46577dc273 | 79 | void InterruptManager::irq_helper() { |
MikamiUitOpen | 0:3e46577dc273 | 80 | _chains[__get_IPSR()]->call(); |
MikamiUitOpen | 0:3e46577dc273 | 81 | } |
MikamiUitOpen | 0:3e46577dc273 | 82 | |
MikamiUitOpen | 0:3e46577dc273 | 83 | int InterruptManager::get_irq_index(IRQn_Type irq) { |
MikamiUitOpen | 0:3e46577dc273 | 84 | return (int)irq + NVIC_USER_IRQ_OFFSET; |
MikamiUitOpen | 0:3e46577dc273 | 85 | } |
MikamiUitOpen | 0:3e46577dc273 | 86 | |
MikamiUitOpen | 0:3e46577dc273 | 87 | void InterruptManager::static_irq_helper() { |
MikamiUitOpen | 0:3e46577dc273 | 88 | InterruptManager::get()->irq_helper(); |
MikamiUitOpen | 0:3e46577dc273 | 89 | } |
MikamiUitOpen | 0:3e46577dc273 | 90 | |
MikamiUitOpen | 0:3e46577dc273 | 91 | } // namespace mbed |
MikamiUitOpen | 0:3e46577dc273 | 92 | |
MikamiUitOpen | 0:3e46577dc273 | 93 | #endif |