mbed library sources with internal temperature sensor for nucleo f401

Committer:
elessair
Date:
Sat Jan 17 18:03:58 2015 +0000
Revision:
0:7e2bd16f80af
nucleo f401re internal temperature added

Who changed what in which revision?

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