source: UsbWattMeter/trunk/asp_dcre/kernel/interrupt.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 10.1 KB
Line 
1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 * Copyright (C) 2005-2012 by Embedded and Real-Time Systems Laboratory
9 * Graduate School of Information Science, Nagoya Univ., JAPAN
10 *
11 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
12 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
13 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
14 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
15 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
16 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
17 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
18 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
19ƒƒ“ƒgi—˜—p
20 * ŽÒƒ}ƒjƒ…
21ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
22 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
23 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
24 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
25 * ‚ƁD
26 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
27ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
28ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
29 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
30 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
31 * •ñ‚·‚邱‚ƁD
32 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
33 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
34 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
35 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
36 * –Ɛӂ·‚邱‚ƁD
37 *
38 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
39 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
40 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
41 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
42 * ‚̐ӔC‚𕉂í‚È‚¢D
43 *
44 * @(#) $Id: interrupt.c 164 2016-03-07 11:33:50Z coas-nagasima $
45 */
46
47/*
48 * Š„ž‚ÝŠÇ—‹@”\
49 */
50
51#include "kernel_impl.h"
52#include "check.h"
53#include "task.h"
54#include "interrupt.h"
55
56/*
57 * ƒgƒŒ[ƒXƒƒOƒ}ƒNƒ‚̃fƒtƒHƒ‹ƒg’è‹`
58 */
59#ifndef LOG_ISR_ENTER
60#define LOG_ISR_ENTER(intno)
61#endif /* LOG_ISR_ENTER */
62
63#ifndef LOG_ISR_LEAVE
64#define LOG_ISR_LEAVE(intno)
65#endif /* LOG_ISR_LEAVE */
66
67#ifndef LOG_ACRE_ISR_ENTER
68#define LOG_ACRE_ISR_ENTER(pk_cisr)
69#endif /* LOG_ACRE_ISR_ENTER */
70
71#ifndef LOG_ACRE_ISR_LEAVE
72#define LOG_ACRE_ISR_LEAVE(ercd)
73#endif /* LOG_ACRE_ISR_LEAVE */
74
75#ifndef LOG_DEL_ISR_ENTER
76#define LOG_DEL_ISR_ENTER(isrid)
77#endif /* LOG_DEL_ISR_ENTER */
78
79#ifndef LOG_DEL_ISR_LEAVE
80#define LOG_DEL_ISR_LEAVE(ercd)
81#endif /* LOG_DEL_ISR_LEAVE */
82
83#ifndef LOG_DIS_INT_ENTER
84#define LOG_DIS_INT_ENTER(intno)
85#endif /* LOG_DIS_INT_ENTER */
86
87#ifndef LOG_DIS_INT_LEAVE
88#define LOG_DIS_INT_LEAVE(ercd)
89#endif /* LOG_DIS_INT_LEAVE */
90
91#ifndef LOG_ENA_INT_ENTER
92#define LOG_ENA_INT_ENTER(intno)
93#endif /* LOG_ENA_INT_ENTER */
94
95#ifndef LOG_ENA_INT_LEAVE
96#define LOG_ENA_INT_LEAVE(ercd)
97#endif /* LOG_ENA_INT_LEAVE */
98
99#ifndef LOG_CHG_IPM_ENTER
100#define LOG_CHG_IPM_ENTER(intpri)
101#endif /* LOG_CHG_IPM_ENTER */
102
103#ifndef LOG_CHG_IPM_LEAVE
104#define LOG_CHG_IPM_LEAVE(ercd)
105#endif /* LOG_CHG_IPM_LEAVE */
106
107#ifndef LOG_GET_IPM_ENTER
108#define LOG_GET_IPM_ENTER(p_intpri)
109#endif /* LOG_GET_IPM_ENTER */
110
111#ifndef LOG_GET_IPM_LEAVE
112#define LOG_GET_IPM_LEAVE(ercd, intpri)
113#endif /* LOG_GET_IPM_LEAVE */
114
115/*
116 * Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“‚̐”
117 */
118#define tnum_isr ((uint_t)(tmax_isrid - TMIN_SEMID + 1) + tnum_sisr)
119
120/*
121 * Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“ID‚©‚犄ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“ŠÇ—ƒuƒƒbƒN‚ðŽæ
122 * ‚èo‚·‚½‚߂̃}ƒNƒ
123 */
124#define INDEX_ISR(isrid) ((uint_t)((isrid) - TMIN_ISRID) + tnum_sisr)
125#define get_isrcb(isrid) (&(isrcb_table[INDEX_ISR(isrid)]))
126
127/*
128 * Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“ƒLƒ…
129[‚Ö‚Ì“o˜^
130 */
131Inline void
132enqueue_isr(QUEUE *p_isr_queue, ISRCB *p_isrcb)
133{
134 QUEUE *p_entry;
135 PRI isrpri = p_isrcb->p_isrinib->isrpri;
136
137 for (p_entry = p_isr_queue->p_next; p_entry != p_isr_queue;
138 p_entry = p_entry->p_next) {
139 if (isrpri < ((ISRCB *) p_entry)->p_isrinib->isrpri) {
140 break;
141 }
142 }
143 queue_insert_prev(p_entry, &(p_isrcb->isr_queue));
144}
145
146#ifdef TOPPERS_isrini
147
148/*
149 * Žg—p‚µ‚Ä‚¢‚È‚¢Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“ŠÇ—ƒuƒƒbƒN‚̃ŠƒXƒg
150 */
151QUEUE free_isrcb;
152
153/*
154 * Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“‹@”\‚̏‰Šú‰»
155 */
156void
157initialize_isr(void)
158{
159 uint_t i, j;
160 ISRCB *p_isrcb;
161 ISRINIB *p_isrinib;
162
163 for (i = 0; i < tnum_isr_queue; i++) {
164 queue_initialize(&(isr_queue_table[i]));
165 }
166 for (i = 0; i < tnum_sisr; i++) {
167 p_isrcb = &(isrcb_table[i]);
168 p_isrcb->p_isrinib = &(sisrinib_table[i]);
169 enqueue_isr(p_isrcb->p_isrinib->p_isr_queue, p_isrcb);
170 }
171 queue_initialize(&free_isrcb);
172 for (j = 0; i < tnum_isr; i++, j++) {
173 p_isrcb = &(isrcb_table[i]);
174 p_isrinib = &(aisrinib_table[j]);
175 p_isrinib->isratr = TA_NOEXS;
176 p_isrcb->p_isrinib = ((const ISRINIB *) p_isrinib);
177 queue_insert_prev(&free_isrcb, &(p_isrcb->isr_queue));
178 }
179}
180
181#endif /* TOPPERS_isrini */
182
183/*
184 * Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“‚̌ďo‚µ
185 */
186#ifdef TOPPERS_isrcal
187
188void
189call_isr(QUEUE *p_isr_queue)
190{
191 QUEUE *p_queue;
192 ISRINIB *p_isrinib;
193 PRI saved_ipm;
194
195 saved_ipm = i_get_ipm();
196 for (p_queue = p_isr_queue->p_next; p_queue != p_isr_queue;
197 p_queue = p_queue->p_next) {
198 p_isrinib = (ISRINIB *)(((ISRCB *) p_queue)->p_isrinib);
199 LOG_ISR_ENTER(p_isrinib->intno);
200 (*(p_isrinib->isr))(p_isrinib->exinf);
201 LOG_ISR_LEAVE(p_isrinib->intno);
202
203 if (p_queue->p_next != p_isr_queue) {
204 /* ISR‚̌ďo‚µ‘O‚̏ó‘Ô‚É–ß‚· */
205 if (i_sense_lock()) {
206 i_unlock_cpu();
207 }
208 i_set_ipm(saved_ipm);
209 }
210 }
211}
212
213#endif /* TOPPERS_isrcal */
214
215/*
216 * Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“ŒÄo‚µƒLƒ…
217[‚ÌŒŸõ
218 */
219Inline QUEUE *
220search_isr_queue(INTNO intno)
221{
222 int_t left, right, i;
223
224 if (tnum_isr_queue == 0) {
225 return(NULL);
226 }
227
228 left = 0;
229 right = tnum_isr_queue - 1;
230 while (left < right) {
231 i = (left + right + 1) / 2;
232 if (intno < isr_queue_list[i].intno) {
233 right = i - 1;
234 }
235 else {
236 left = i;
237 }
238 }
239 if (isr_queue_list[left].intno == intno) {
240 return(isr_queue_list[left].p_isr_queue);
241 }
242 else {
243 return(NULL);
244 }
245}
246
247/*
248 * Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“‚̐¶¬
249 */
250#ifdef TOPPERS_acre_isr
251
252ER_UINT
253acre_isr(const T_CISR *pk_cisr)
254{
255 ISRCB *p_isrcb;
256 ISRINIB *p_isrinib;
257 QUEUE *p_isr_queue;
258 ER ercd;
259
260 LOG_ACRE_ISR_ENTER(pk_cisr);
261 CHECK_TSKCTX_UNL();
262 CHECK_RSATR(pk_cisr->isratr, TARGET_ISRATR);
263 CHECK_INTNO_CREISR(pk_cisr->intno);
264 CHECK_ALIGN_FUNC(pk_cisr->isr);
265 CHECK_NONNULL_FUNC(pk_cisr->isr);
266 CHECK_ISRPRI(pk_cisr->isrpri);
267
268 p_isr_queue = search_isr_queue(pk_cisr->intno);
269 CHECK_OBJ(p_isr_queue != NULL);
270
271 t_lock_cpu();
272 if (tnum_isr == 0 || queue_empty(&free_isrcb)) {
273 ercd = E_NOID;
274 }
275 else {
276 p_isrcb = ((ISRCB *) queue_delete_next(&free_isrcb));
277 p_isrinib = (ISRINIB *)(p_isrcb->p_isrinib);
278 p_isrinib->isratr = pk_cisr->isratr;
279 p_isrinib->exinf = pk_cisr->exinf;
280 p_isrinib->intno = pk_cisr->intno;
281 p_isrinib->p_isr_queue = p_isr_queue;
282 p_isrinib->isr = pk_cisr->isr;
283 p_isrinib->isrpri = pk_cisr->isrpri;
284 enqueue_isr(p_isr_queue, p_isrcb);
285 ercd = ISRID(p_isrcb);
286 }
287 t_unlock_cpu();
288
289 error_exit:
290 LOG_ACRE_ISR_LEAVE(ercd);
291 return(ercd);
292}
293
294#endif /* TOPPERS_acre_isr */
295
296/*
297 * Š„ž‚݃T[ƒrƒXƒ‹[ƒ`ƒ“‚̍폜
298 */
299#ifdef TOPPERS_del_isr
300
301ER
302del_isr(ID isrid)
303{
304 ISRCB *p_isrcb;
305 ISRINIB *p_isrinib;
306 ER ercd;
307
308 LOG_DEL_ISR_ENTER(isrid);
309 CHECK_TSKCTX_UNL();
310 CHECK_ISRID(isrid);
311 p_isrcb = get_isrcb(isrid);
312
313 t_lock_cpu();
314 if (p_isrcb->p_isrinib->isratr == TA_NOEXS) {
315 ercd = E_NOEXS;
316 }
317 else {
318 queue_delete(&(p_isrcb->isr_queue));
319 p_isrinib = (ISRINIB *)(p_isrcb->p_isrinib);
320 p_isrinib->isratr = TA_NOEXS;
321 queue_insert_prev(&free_isrcb, &(p_isrcb->isr_queue));
322 ercd = E_OK;
323 }
324 t_unlock_cpu();
325
326 error_exit:
327 LOG_DEL_ISR_LEAVE(ercd);
328 return(ercd);
329}
330
331#endif /* TOPPERS_del_isr */
332
333/*
334 * Š„ž‚ÝŠÇ—‹@”\‚̏‰Šú‰»
335 */
336#ifdef TOPPERS_intini
337#ifndef OMIT_INITIALIZE_INTERRUPT
338
339void
340initialize_interrupt(void)
341{
342 uint_t i;
343 const INHINIB *p_inhinib;
344 const INTINIB *p_intinib;
345
346 for (i = 0; i < tnum_inhno; i++) {
347 p_inhinib = &(inhinib_table[i]);
348 x_define_inh(p_inhinib->inhno, p_inhinib->int_entry);
349 }
350 for (i = 0; i < tnum_intno; i++) {
351 p_intinib = &(intinib_table[i]);
352 x_config_int(p_intinib->intno, p_intinib->intatr, p_intinib->intpri);
353 }
354}
355
356#endif /* OMIT_INITIALIZE_INTERRUPT */
357#endif /* TOPPERS_intini */
358
359/*
360 * Š„ž‚Ý‚Ì‹ÖŽ~
361 */
362#ifdef TOPPERS_dis_int
363#ifdef TOPPERS_SUPPORT_DIS_INT
364
365ER
366dis_int(INTNO intno)
367{
368 bool_t locked;
369 ER ercd;
370
371 LOG_DIS_INT_ENTER(intno);
372 CHECK_TSKCTX();
373 CHECK_INTNO_DISINT(intno);
374
375 locked = t_sense_lock();
376 if (!locked) {
377 t_lock_cpu();
378 }
379 if (t_disable_int(intno)) {
380 ercd = E_OK;
381 }
382 else {
383 ercd = E_OBJ;
384 }
385 if (!locked) {
386 t_unlock_cpu();
387 }
388
389 error_exit:
390 LOG_DIS_INT_LEAVE(ercd);
391 return(ercd);
392}
393
394#endif /* TOPPERS_SUPPORT_DIS_INT */
395#endif /* TOPPERS_dis_int */
396
397/*
398 * Š„ž‚Ý‚Ì‹–‰Â
399 */
400#ifdef TOPPERS_ena_int
401#ifdef TOPPERS_SUPPORT_ENA_INT
402
403ER
404ena_int(INTNO intno)
405{
406 bool_t locked;
407 ER ercd;
408
409 LOG_ENA_INT_ENTER(intno);
410 CHECK_TSKCTX();
411 CHECK_INTNO_DISINT(intno);
412
413 locked = t_sense_lock();
414 if (!locked) {
415 t_lock_cpu();
416 }
417 if (t_enable_int(intno)) {
418 ercd = E_OK;
419 }
420 else {
421 ercd = E_OBJ;
422 }
423 if (!locked) {
424 t_unlock_cpu();
425 }
426
427 error_exit:
428 LOG_ENA_INT_LEAVE(ercd);
429 return(ercd);
430}
431
432#endif /* TOPPERS_SUPPORT_ENA_INT */
433#endif /* TOPPERS_ena_int */
434
435/*
436 * Š„ž‚Ý—Dæ“xƒ}ƒXƒN‚̕ύX
437 */
438#ifdef TOPPERS_chg_ipm
439
440ER
441chg_ipm(PRI intpri)
442{
443 ER ercd;
444
445 LOG_CHG_IPM_ENTER(intpri);
446 CHECK_TSKCTX_UNL();
447 CHECK_INTPRI_CHGIPM(intpri);
448
449 t_lock_cpu();
450 t_set_ipm(intpri);
451 if (intpri == TIPM_ENAALL) {
452 ipmflg = true;
453 if (!disdsp) {
454 dspflg = true;
455 if (p_runtsk != p_schedtsk) {
456 dispatch();
457 }
458 }
459 if (p_runtsk->enatex && p_runtsk->texptn != 0U) {
460 call_texrtn();
461 }
462 }
463 else {
464 ipmflg = false;
465 dspflg = false;
466 }
467 ercd = E_OK;
468 t_unlock_cpu();
469
470 error_exit:
471 LOG_CHG_IPM_LEAVE(ercd);
472 return(ercd);
473}
474
475#endif /* TOPPERS_chg_ipm */
476
477/*
478 * Š„ž‚Ý—Dæ“xƒ}ƒXƒN‚ÌŽQÆ
479 */
480#ifdef TOPPERS_get_ipm
481
482ER
483get_ipm(PRI *p_intpri)
484{
485 ER ercd;
486
487 LOG_GET_IPM_ENTER(p_intpri);
488 CHECK_TSKCTX_UNL();
489
490 t_lock_cpu();
491 *p_intpri = t_get_ipm();
492 ercd = E_OK;
493 t_unlock_cpu();
494
495 error_exit:
496 LOG_GET_IPM_LEAVE(ercd, *p_intpri);
497 return(ercd);
498}
499
500#endif /* TOPPERS_get_ipm */
Note: See TracBrowser for help on using the repository browser.