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