]> rtime.felk.cvut.cz Git - arc.git/blob - arch/arm/arm_cm3/kernel/irq.c
Again, loads of refactoring and removing and adding files.
[arc.git] / arch / arm / arm_cm3 / kernel / irq.c
1 /* -------------------------------- Arctic Core ------------------------------
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com
3  *
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>
5  *
6  * This source code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by the
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  * -------------------------------- Arctic Core ------------------------------*/
15
16 #include "pcb.h"
17 #include "sys.h"
18 #include "internal.h"
19 #include "task_i.h"
20 #include "hooks.h"
21 #include "stm32f10x.h"
22 #include "misc.h"
23 #include "irq.h"
24 #include "core_cm3.h"
25
26 extern void *Irq_VectorTable[NUMBER_OF_INTERRUPTS_AND_EXCEPTIONS];
27
28 void Irq_Init( void ) {
29         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
30 }
31
32 void Irq_EOI( void ) {
33
34 }
35
36 #define ICSR_VECTACTIVE         0x1ff
37
38 /**
39  * Get Active ISR number field.
40  * You can subtract 16 from the VECTACTIVE field to index into the Interrupt
41  * Clear/Set Enable, Interrupt Clear Pending/SetPending and Interrupt Priority
42  * Registers. INTISR[0] has vector number 16.
43  *
44  */
45 static uint32_t NVIC_GetActiveVector( void) {
46         return (SCB->ICSR &  ICSR_VECTACTIVE);
47 }
48
49 /**
50  *
51  * @param stack_p Ptr to the current stack.
52  *
53  * The stack holds C, NVGPR, VGPR and the EXC frame.
54  *
55  */
56 void *Irq_Entry( void *stack_p )
57 {
58         uint32_t vector = 0;
59         uint32_t *stack;
60
61         Irq_Disable();
62         stack = (uint32_t *)stack_p;
63
64         /* 0. Set the default handler here....
65          * 1. Grab the vector from the interrupt controller
66          *    INT_CTRL_ST[VECTACTIVE]
67          * 2. Irq_VectorTable[vector] is odd -> ISR1
68          *    Irq_VectorTable[vector] is even-> ISR2
69          */
70
71
72         vector = NVIC_GetActiveVector();
73
74         stack = Os_Isr(stack, (void *)Irq_VectorTable[vector]);
75         Irq_Enable();
76         return stack;
77 }
78
79 /**
80  * Attach an ISR type 1 to the interrupt controller.
81  *
82  * @param entry
83  * @param int_ctrl
84  * @param vector
85  * @param prio
86  */
87 void Irq_AttachIsr1( void (*entry)(void), void *int_ctrl, uint32_t vector, uint8_t prio) {
88
89         // TODO: Use NVIC_Init here
90         /*
91   NVIC_InitTypeDef NVIC_InitStructure;
92
93   // Enable and configure RCC global IRQ channel
94   NVIC_InitStructure.NVIC_IRQChannel = RCC_IRQn;
95   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
96   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
97   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
98   NVIC_Init(&NVIC_InitStructure);
99    */
100 }
101
102 static inline int osPrioToCpuPio( uint8_t prio ) {
103         assert(prio<32);
104         return prio>>1;
105 }
106
107
108 /**
109  * Attach a ISR type 2 to the interrupt controller.
110  *
111  * @param tid
112  * @param int_ctrl
113  * @param vector
114  */
115 void Irq_AttachIsr2(TaskType tid,void *int_ctrl,IrqType vector ) {
116         OsPcbType *pcb;
117         NVIC_InitTypeDef irqInit;
118
119         pcb = os_find_task(tid);
120         Irq_VectorTable[vector+16] = (void *)pcb;
121
122         irqInit.NVIC_IRQChannel = vector;
123         irqInit.NVIC_IRQChannelPreemptionPriority = osPrioToCpuPio(pcb->prio);
124         irqInit.NVIC_IRQChannelSubPriority = 0;
125         irqInit.NVIC_IRQChannelCmd = ENABLE;
126
127
128         // TODO: Same as for AttachIsr1
129         NVIC_Init(&irqInit);
130 }
131
132
133 /**
134  * Generates a soft interrupt, ie sets pending bit.
135  * This could also be implemented using ISPR regs.
136  *
137  * @param vector
138  */
139 void Irq_GenerateSoftInt( IrqType vector ) {
140
141         NVIC->STIR = (vector + 16);
142 }
143
144 /**
145  * Get the current priority from the interrupt controller.
146  * @param cpu
147  * @return
148  */
149 uint8_t Irq_GetCurrentPriority( Cpu_t cpu) {
150
151         uint8_t prio = 0;
152
153         // SCB_ICSR contains the active vector
154         return prio;
155 }
156
157 typedef struct {
158         uint32_t dummy;
159 } exc_stack_t;
160
161