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