1 /*
2  * Copyright (c) 2022 OpenLuat & AirM2M
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
5  * this software and associated documentation files (the "Software"), to deal in
6  * the Software without restriction, including without limitation the rights to
7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8  * the Software, and to permit persons to whom the Software is furnished to do so,
9  * subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  */
21 
22 #include "user.h"
23 #define __IRQ_IN_RAM__
24 #ifdef __IRQ_IN_RAM__
25 #else
26 #define __FUNC_IN_RAM__
27 #endif
28 typedef struct {
29     void (*Irq_Handler)(int32_t IrqLine, void *pData);
30     void *pData;
31 }Irq_Handler_t;
32 
33 static Irq_Handler_t Irq_Table[IRQ_LINE_MAX + 16 - IRQ_LINE_OFFSET];
34 
ISR_DummyHandler(int32_t IrqLine,void * pData)35 static void ISR_DummyHandler(int32_t IrqLine, void *pData)
36 {
37 
38 }
39 
ISR_GlobalHandler(void)40 void __FUNC_IN_RAM__ ISR_GlobalHandler(void)
41 {
42     int IrqLine = ((SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) >> SCB_ICSR_VECTACTIVE_Pos) - IRQ_LINE_OFFSET;
43     if (IrqLine < 0 || IrqLine >= IRQ_LINE_MAX)
44     {
45         return;
46     }
47     rt_interrupt_enter();
48     if (Irq_Table[IrqLine].Irq_Handler)
49     {
50         Irq_Table[IrqLine].Irq_Handler(IrqLine + IRQ_LINE_OFFSET - 16, Irq_Table[IrqLine].pData);
51     }
52     rt_interrupt_leave();
53 }
54 
ISR_SetHandler(int32_t Irq,void * Handler,void * pData)55 void ISR_SetHandler(int32_t Irq, void *Handler, void *pData)
56 {
57     Irq_Table[Irq + 16 - IRQ_LINE_OFFSET].Irq_Handler = Handler;
58     Irq_Table[Irq + 16 - IRQ_LINE_OFFSET].pData = pData;
59 }
60 
ISR_SetPriority(int32_t Irq,uint32_t PriorityLevel)61 void ISR_SetPriority(int32_t Irq, uint32_t PriorityLevel)
62 {
63     NVIC_SetPriority(Irq, PriorityLevel);
64 }
65 
ISR_OnOff(int32_t Irq,uint32_t OnOff)66 void __FUNC_IN_RAM__ ISR_OnOff(int32_t Irq, uint32_t OnOff)
67 {
68     if (OnOff)
69     {
70         if (!Irq_Table[Irq + 16 - IRQ_LINE_OFFSET].Irq_Handler)
71         {
72             Irq_Table[Irq + 16 - IRQ_LINE_OFFSET].Irq_Handler = ISR_DummyHandler;
73         }
74         NVIC_EnableIRQ(Irq);
75     }
76     else
77     {
78         NVIC_DisableIRQ(Irq);
79     }
80 }
81 
ISR_Clear(int32_t Irq)82 void __FUNC_IN_RAM__ ISR_Clear(int32_t Irq)
83 {
84     NVIC_ClearPendingIRQ(Irq);
85 }
86 
ISR_CheckIn(void)87 uint32_t __FUNC_IN_RAM__ ISR_CheckIn(void)
88 {
89     return __get_IPSR();
90 }
91 
92