initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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