]> rtime.felk.cvut.cz Git - arc.git/blob - include/isr.h
More ISR things.
[arc.git] / include / isr.h
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 #ifndef ISR_H_\r
17 #define ISR_H_\r
18 \r
19 #include "task_i.h"\r
20 #include "resource_i.h"\r
21 \r
22 /*\r
23  * INCLUDE "RULES"\r
24  *  Since the types and methods defined here are used by the drivers, they should\r
25  *  include it. E.g. #include "isr.h"\r
26  *\r
27  *  This file is also used internally by the kernel\r
28  *\r
29  *\r
30  *  irq_types.h ( Vector enums )\r
31  *  irq.h       ( Interface )\r
32  *\r
33  *  Problem:\r
34  *    Os_Cfg.h needs types from isr.h\r
35  *\r
36  */\r
37 \r
38 /* ----------------------------[includes]------------------------------------*/\r
39 /* ----------------------------[define]--------------------------------------*/\r
40 \r
41 #define ISR_TYPE_1                      0\r
42 #define ISR_TYPE_2                      1\r
43 \r
44 /* ----------------------------[macro]---------------------------------------*/\r
45 #define ISR_DECLARE_ISR2(_name, _entry, _unique, _vector,_priority,_app )        \\r
46           const OsIsrConstType _entry ## _unique = { \\r
47                         .vector = _vector,   \\r
48                         .type = ISR_TYPE_2, \\r
49                         .priority = _priority,      \\r
50                         .entry = _entry,      \\r
51                         .name = _name,      \\r
52                         .resourceMask = 0,  \\r
53                         .timingProtPtr = NULL, \\r
54                         .appOwner = _app,      \\r
55                   };                    \\r
56 \r
57 \r
58 #define __ISR_INSTALL_ISR2(_name, _entry, _unique, _vector,_priority,_app )        \\r
59         do { \\r
60           const OsIsrConstType _entry ## _unique = { \\r
61                         .vector = _vector,   \\r
62                         .type = ISR_TYPE_2, \\r
63                         .priority = _priority,      \\r
64                         .entry = _entry,      \\r
65                         .name = _name,      \\r
66                         .resourceMask = 0,  \\r
67                         .timingProtPtr = NULL, \\r
68                         .appOwner = _app,      \\r
69                   };                    \\r
70           Os_IsrAdd( & _entry ## _unique);   \\r
71         } while(0);\r
72 \r
73 \r
74 \r
75 #define _ISR_INSTALL_ISR2(_name,_entry, _unique, _vector,_priority,_app)        \\r
76                 __ISR_INSTALL_ISR2(_name,_entry, _unique, _vector,_priority,_app)\r
77 \r
78 #define ISR_INSTALL_ISR2(_name,_entry, _vector,_priority,_app)        \\r
79                 _ISR_INSTALL_ISR2(_name,_entry, __LINE__, _vector,_priority,_app)\r
80 \r
81 \r
82 #define ISR_DECLARE_ISR1(_name, _entry, _unique, _vector,_priority,_app )        \\r
83           const OsIsrConstType _entry ## _unique = { \\r
84                         .vector = _vector,   \\r
85                         .type = ISR_TYPE_1, \\r
86                         .priority = _priority,      \\r
87                         .entry = _entry,      \\r
88                         .name = _name,      \\r
89                         .resourceMask = 0,  \\r
90                         .timingProtPtr = NULL, \\r
91                         .appOwner = _app,      \\r
92                   };                    \\r
93 \r
94 #define __ISR_INSTALL_ISR1(_name, _entry, _unique, _vector,_priority,_app )        \\r
95         do { \\r
96           const OsIsrConstType _entry ## _unique = { \\r
97                         .vector = _vector,   \\r
98                         .type = ISR_TYPE_2, \\r
99                         .priority = _priority,      \\r
100                         .entry = _entry,      \\r
101                         .name = _name,      \\r
102                         .resourceMask = 0,  \\r
103                         .timingProtPtr = NULL, \\r
104                         .appOwner = _app,      \\r
105                   };                    \\r
106           Os_IsrAdd( & _entry ## _unique);   \\r
107         } while(0);\r
108 \r
109 #define _ISR_INSTALL_ISR1(_name,_entry, _unique, _vector,_priority,_app)        \\r
110                 __ISR_INSTALL_ISR1(_name,_entry, _unique, _vector,_priority,_app)\r
111 \r
112 #define ISR_INSTALL_ISR1(_name,_entry, _vector,_priority,_app)        \\r
113                 _ISR_INSTALL_ISR1(_name,_entry, __LINE__, _vector,_priority,_app)\r
114 \r
115 \r
116 \r
117 /* ----------------------------[typedef]-------------------------------------*/\r
118 \r
119 \r
120 \r
121 /* STD container : OsIsrResourceLock\r
122  * Class: 2 and 4\r
123  *\r
124  * OsIsrResourceLockBudget      1    Float in seconds (MAXRESOURCELOCKINGTIME)\r
125  * OsIsrResourceLockResourceRef 1    Ref to OsResource\r
126  * */\r
127 \r
128 typedef struct OsIsrResourceLock {\r
129         uint32_t lockBudget;\r
130         uint32_t lockResourceRef;       /* Wrong type */\r
131 } OsIsrResourceLockType;\r
132 \r
133 \r
134 /* STD container : OsIsrTimingProtection\r
135  * Class: 2 and 4\r
136  *\r
137  * OsIsrAllInterruptLockBudget  0..1 float\r
138  * OsIsrExecutionBudget                 0..1 float\r
139  * OsIsrOsInterruptLockBudget   0..1 float\r
140  * OsIsrTimeFrame                               0..1 float\r
141  * OsIsrResourceLock[C]                 0..*\r
142  * */\r
143 \r
144 typedef struct OsIsrTimingProtection {\r
145         uint32_t allInterruptLockBudget;\r
146         uint32_t executionBudget;\r
147         uint32_t osInterruptLockBudget;\r
148         uint32_t timeFrame;\r
149         uint32_t resourceLock;          /* Wrong type */\r
150 } OsIsrTimingProtectionType;\r
151 \r
152 typedef struct {\r
153         void            *curr;  /* Current stack ptr( at swap time ) */\r
154         void            *top;   /* Top of the stack( low address )   */\r
155         uint32          size;   /* The size of the stack             */\r
156 } OsIsrStackType;\r
157 \r
158 \r
159 /* STD container : OsIsr\r
160  * Class: ALL\r
161  *\r
162  * OsIsrCategory:                               1    CATEGORY_1 or CATEGORY_2\r
163  * OsIsrResourceRef:                    0..* Reference to OsResources\r
164  * OsIsrTimingProtection[C]     0..1\r
165  * */\r
166 \r
167 typedef struct OsIsrConst {\r
168         const char              *name;\r
169         uint8_t                 core;\r
170         int16_t                 vector;\r
171         int16_t                 type;\r
172         int16_t                 priority;\r
173         void                    (*entry)();\r
174         uint32_t                appOwner;\r
175         /* Mapped against OsIsrResourceRef */\r
176         uint32_t                resourceMask;\r
177 #if (  OS_USE_ISR_TIMING_PROT == STD_ON )\r
178         /* Mapped against OsIsrTimingProtection[C] */\r
179         OsIsrTimingProtectionType *timingProtPtr;\r
180 #else\r
181         void *timingProtPtr;\r
182 #endif\r
183 } OsIsrConstType;\r
184 \r
185 /*\r
186  *\r
187  */\r
188 typedef struct {\r
189         ISRType id;\r
190 //      OsIsrStackType          stack;\r
191         int                                     state;\r
192         const OsIsrConstType *constPtr;\r
193         /* List of resource held by this ISR */\r
194         TAILQ_HEAD(,OsResource) resourceHead;\r
195 } OsIsrVarType;\r
196 \r
197 \r
198 /* ----------------------------[functions]-----------------------------------*/\r
199 \r
200 #if OS_ISR_MAX_CNT!=0\r
201 extern OsIsrVarType Os_IsrVarList[OS_ISR_MAX_CNT];\r
202 #endif\r
203 \r
204 void Os_IsrInit( void );\r
205 ISRType Os_IsrAdd( const OsIsrConstType * restrict isrPtr );\r
206 void Os_IsrGetStackInfo( OsIsrStackType *stack );\r
207 void *Os_Isr( void *stack, int16_t vector);\r
208 #if defined(CFG_ARM_CM3)\r
209 void Os_Isr_cm3( void *isr_p );\r
210 void TailChaining(void *stack);\r
211 #endif\r
212 \r
213 static inline const OsIsrVarType *Os_IsrGet( ISRType id ) {\r
214         return &Os_IsrVarList[id];\r
215 }\r
216 \r
217 static inline ApplicationType Os_IsrGetApplicationOwner( ISRType id ) {\r
218         ApplicationType rv = INVALID_OSAPPLICATION;\r
219 \r
220 #if (OS_ISR_CNT!=0)\r
221         if( id < OS_ISR_CNT ) {\r
222                 rv = Os_IsrGet(id)->constPtr->appOwner;\r
223         }\r
224 #else\r
225         (void)id;\r
226 #endif\r
227         return rv;\r
228 }\r
229 \r
230 static inline void Os_IsrResourceAdd( OsResourceType *rPtr, OsIsrVarType *isrPtr) {\r
231         /* Save old task prio in resource and set new task prio */\r
232         rPtr->owner = isrPtr->id;\r
233 \r
234         assert( rPtr->type != RESOURCE_TYPE_INTERNAL );\r
235 }\r
236 \r
237 static inline  void Os_IsrResourceRemove( OsResourceType *rPtr , OsIsrVarType *isrPtr) {\r
238         assert( rPtr->owner == isrPtr->id );\r
239         rPtr->owner = NO_TASK_OWNER;\r
240 \r
241         if( rPtr->type != RESOURCE_TYPE_INTERNAL ) {\r
242                 /* The list can't be empty here */\r
243                 assert( !TAILQ_EMPTY(&isrPtr->resourceHead) );\r
244 \r
245                 /* The list should be popped in LIFO order */\r
246                 assert( TAILQ_LAST(&isrPtr->resourceHead, head) == rPtr );\r
247 \r
248                 /* Remove the entry */\r
249                 TAILQ_REMOVE(&isrPtr->resourceHead, rPtr, listEntry);\r
250         }\r
251 }\r
252 \r
253 static inline void Os_IsrResourceFreeAll( OsIsrVarType *isrPtr ) {\r
254         OsResourceType *rPtr;\r
255 \r
256         /* Pop the queue */\r
257         TAILQ_FOREACH(rPtr, &isrPtr->resourceHead, listEntry ) {\r
258                 Os_IsrResourceRemove(rPtr,isrPtr);\r
259         }\r
260 }\r
261 \r
262 static inline _Bool Os_IsrOccupiesResources(  OsIsrVarType *isrPtr ) {\r
263         return !(TAILQ_EMPTY(&isrPtr->resourceHead));\r
264 }\r
265 \r
266 \r
267 \r
268 \r
269 \r
270 #endif /*ISR_H_*/\r