-
Notifications
You must be signed in to change notification settings - Fork 4
/
interruptmanager.h
138 lines (111 loc) · 4.09 KB
/
interruptmanager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* interruptmanager.h
*
* Created on: 23 нояб. 2015 г.
* Author: tihonov
*/
#ifndef INTERRUPT_INTERRUPTMANAGER_H_
#define INTERRUPT_INTERRUPTMANAGER_H_
#include "stm32f4xx.h"
#include<stdint.h>
#include "string.h"
//Add another vecotrs implimentaton if you use another mcu
#if defined(STM32F407xx)
#include "vectorsf407.h"
#endif
//******************************************************************************************
//How to use this manager
//
// First define INTERRUPTMANAGER_ENABLED INTERRUPTMANAGER_SRAM or INTERRUPTMANAGER_SRAM
//INTERRUPTMANAGER::init(); then init interruptmanager
//INTERRUPTMANAGER::AddHandler add or remove interrupts
//INTERRUPTMANAGER::AddHandler(InterruptFunction, EXTI4_IRQn);
//NVIC_SetPriority(EXTI4_IRQn, 6);
//NVIC_EnableIRQ (EXTI4_IRQn);
//NVIC_DisableIRQ (EXTI4_IRQn);
//INTERRUPTMANAGER::RemoveHandler(InterruptFunction, EXTI4_IRQn);
//INTERRUPTMANAGER::AddHandler(this->IrqHandle, ETH_IRQn);
//NVIC_SetPriority(ETH_IRQn, 6);
//NVIC_EnableIRQ (ETH_IRQn);
//******************************************************************************************
//******************************************************************************************
//Select which interruptmanager will be used in your project
//******************************************************************************************
#define INTERRUPTMANAGER_SRAM 1
#define INTERRUPTMANAGER_FLASH 2
// uncomment one of this sections
#define INTERRUPTMANAGER_ENABLED INTERRUPTMANAGER_SRAM
//#define INTERRUPTMANAGER_ENABLED INTERRUPTMANAGER_FLASH
#ifndef INTERRUPTMANAGER_ENABLED
#define INTERRUPTMANAGER_ENABLED INTERRUPTMANAGER_FLASH
#endif
//******************************************************************************************
//Specify this parameters by adding
//******************************************************************************************
#if defined(STM32F407xx)
#define VECTORTABLE_SIZE (100)
#define VECTORTABLE_FIRST_SPEC_IRQ_OFT (16) // offset of first specific interrupt
#endif
//******************************************************************************************
//Function pointer
//******************************************************************************************
typedef void(*pHandlerPointer_t)();
//******************************************************************************************
//Sram interrupt manager
//******************************************************************************************
#if INTERRUPTMANAGER_ENABLED==INTERRUPTMANAGER_SRAM
#define INTERRUPTMANAGER InterruptManagerSram
extern pHandlerPointer_t volatile sramVectorsTable[VECTORTABLE_SIZE];
#define VECTORTABLE__FIRST_SPEC_IRQ_ALIGNMENT sramVectorsTable + VECTORTABLE_FIRST_SPEC_IRQ_OFT
class InterruptManagerSram {
private:
static pHandlerPointer_t volatile *vectors;
public:
static void init()
{
__disable_irq();
memcpy((void*)sramVectorsTable, (void*)SCB->VTOR, sizeof(sramVectorsTable));
SCB->VTOR = (uint32_t)sramVectorsTable;
__DSB();
__enable_irq();
__ISB();
};
static void defaultHandler() {
asm volatile("nop"::);
};
static void addHandler(pHandlerPointer_t handler, IRQn_Type irq) {
vectors[irq] = handler;
};
static void removeHanlder(IRQn_Type irq) {
vectors[irq] = defaultHandler;
};
static void raise(IRQn_Type irq) {
vectors[irq]();
};
};
#endif
//******************************************************************************************
//Flash interrupt manager
//******************************************************************************************
#if INTERRUPTMANAGER_ENABLED==INTERRUPTMANAGER_FLASH
#define INTERRUPTMANAGER InterruptManagerFlash
class InterruptManagerFlash {
private:
static pHandlerPointer_t volatile vectors[VECTORTABLE_SIZE];
public:
static void init() { }
static void defaultHandler() {
asm volatile("nop"::);
};
static void addHandler(pHandlerPointer_t handler, IRQn_Type irq) {
vectors[irq] = handler;
};
static void removeHanlder(IRQn_Type irq) {
vectors[irq] = defaultHandler;
};
static void raise(IRQn_Type irq) {
vectors[irq]();
};
};
#endif
#endif /* INTERRUPT_INTERRUPTMANAGER_H_ */