PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
5:ea7377f3d1af
added fix for directrectangle()

Who changed what in which revision?

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