2016-10-01 07:11:36 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-10-24 02:38:53 +00:00
|
|
|
#include "cmsis.h"
|
|
|
|
#if defined(NVIC_NUM_VECTORS)
|
|
|
|
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "drivers/InterruptManager.h"
|
2017-01-27 11:10:28 +00:00
|
|
|
#include "platform/mbed_critical.h"
|
2013-08-07 11:43:26 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define CHAIN_INITIAL_SIZE 4
|
|
|
|
|
|
|
|
namespace mbed {
|
|
|
|
|
|
|
|
typedef void (*pvoidf)(void);
|
|
|
|
|
2014-07-17 14:38:34 +00:00
|
|
|
InterruptManager* InterruptManager::_instance = (InterruptManager*)NULL;
|
2013-08-07 11:43:26 +00:00
|
|
|
|
|
|
|
InterruptManager* InterruptManager::get() {
|
2016-05-31 22:57:41 +00:00
|
|
|
|
|
|
|
if (NULL == _instance) {
|
|
|
|
InterruptManager* temp = new InterruptManager();
|
|
|
|
|
|
|
|
// Atomically set _instance
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
if (NULL == _instance) {
|
|
|
|
_instance = temp;
|
|
|
|
}
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
|
|
|
|
// Another thread got there first so delete ours
|
|
|
|
if (temp != _instance) {
|
|
|
|
delete temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-08-07 11:43:26 +00:00
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
InterruptManager::InterruptManager() {
|
2016-05-31 22:57:41 +00:00
|
|
|
// No mutex needed in constructor
|
2013-08-07 11:43:26 +00:00
|
|
|
memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain*));
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterruptManager::destroy() {
|
|
|
|
// Not a good idea to call this unless NO interrupt at all
|
|
|
|
// is under the control of the handler; otherwise, a system crash
|
|
|
|
// is very likely to occur
|
|
|
|
if (NULL != _instance) {
|
|
|
|
delete _instance;
|
2014-07-17 14:38:34 +00:00
|
|
|
_instance = (InterruptManager*)NULL;
|
2013-08-07 11:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2016-06-10 16:54:00 +00:00
|
|
|
lock();
|
2013-08-07 11:43:26 +00:00
|
|
|
|
2016-05-31 22:57:41 +00:00
|
|
|
int ret = false;
|
|
|
|
int irq_pos = get_irq_index(irq);
|
2013-08-07 11:43:26 +00:00
|
|
|
if (NULL == _chains[irq_pos]) {
|
|
|
|
_chains[irq_pos] = new CallChain(CHAIN_INITIAL_SIZE);
|
|
|
|
_chains[irq_pos]->add((pvoidf)NVIC_GetVector(irq));
|
2016-05-31 22:57:41 +00:00
|
|
|
ret = true;
|
2013-08-07 11:43:26 +00:00
|
|
|
}
|
2016-06-10 16:54:00 +00:00
|
|
|
unlock();
|
2016-05-31 22:57:41 +00:00
|
|
|
return ret;
|
2013-08-07 11:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front) {
|
2016-06-10 16:54:00 +00:00
|
|
|
lock();
|
2013-08-07 11:43:26 +00:00
|
|
|
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);
|
2016-06-10 16:54:00 +00:00
|
|
|
unlock();
|
2013-08-07 11:43:26 +00:00
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) {
|
|
|
|
int irq_pos = get_irq_index(irq);
|
2016-05-31 22:57:41 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
2016-06-10 16:54:00 +00:00
|
|
|
lock();
|
2016-05-31 22:57:41 +00:00
|
|
|
if (_chains[irq_pos] != NULL) {
|
|
|
|
if (_chains[irq_pos]->remove(handler)) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
2016-06-10 16:54:00 +00:00
|
|
|
unlock();
|
2014-05-29 13:11:05 +00:00
|
|
|
|
2016-05-31 22:57:41 +00:00
|
|
|
return ret;
|
2013-08-07 11:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterruptManager::irq_helper() {
|
2013-08-07 11:43:36 +00:00
|
|
|
_chains[__get_IPSR()]->call();
|
2013-08-07 11:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int InterruptManager::get_irq_index(IRQn_Type irq) {
|
2016-05-31 22:57:41 +00:00
|
|
|
// Pure function - no lock needed
|
2013-08-07 11:43:26 +00:00
|
|
|
return (int)irq + NVIC_USER_IRQ_OFFSET;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterruptManager::static_irq_helper() {
|
|
|
|
InterruptManager::get()->irq_helper();
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:54:00 +00:00
|
|
|
void InterruptManager::lock() {
|
|
|
|
_mutex.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterruptManager::unlock() {
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
2013-08-07 11:43:26 +00:00
|
|
|
} // namespace mbed
|
|
|
|
|
2014-10-24 02:38:53 +00:00
|
|
|
#endif
|