source: ssp_qb_r5f100le_cs/trunk/kernel/task.c@ 93

Last change on this file since 93 was 93, checked in by nmir-saito, 8 years ago

add Combined package of SSP kernel for QB-R5F100LE-TB(RL78 processor)

File size: 9.6 KB
Line 
1/*
2 * TOPPERS/SSP Kernel
3 * Smallest Set Profile Kernel
4 *
5 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
6 * Toyohashi Univ. of Technology, JAPAN
7 * Copyright (C) 2005-2009 by Embedded and Real-Time Systems Laboratory
8 * Graduate School of Information Science, Nagoya Univ., JAPAN
9 * Copyright (C) 2010-2015 by Naoki Saito
10 * Nagoya Municipal Industrial Research Institute, JAPAN
11 * Copyright (C) 2010 by Meika Sugimoto
12 *
13 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì (1)`(4) ‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
14 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü•ÏE
15 * Ä”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
16 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ìŒ 
17 * •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[ƒX
18 * ƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
19 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg—p
20 * ‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
21ƒƒ“ƒgi—˜—pŽÒƒ}
22 * ƒjƒ…
23ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•Û
24 * Ø‹K’è‚ðŒfÚ‚·‚邱‚ƁD
25 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg—p
26 * ‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±‚ƁD
27 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
28ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
29ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ì
30 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
31 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É•ñ
32 * ‚·‚邱‚ƁD
33 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹ŠQ
34 * ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD‚Ü‚½C
35 * –{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝—R‚ÉŠî‚Ã
36 * ‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚Ɓ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ƒA‚Ì—˜—p
41 * ‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»‚̐ӔC‚ð•‰
42 * ‚í‚È‚¢D
43 *
44 */
45
46#include "kernel_impl.h"
47#include "task.h"
48
49/*
50 * ƒgƒŒ[ƒXƒƒOƒ}ƒNƒ‚̃fƒtƒHƒ‹ƒg’è‹`
51 */
52
53#ifndef LOG_DSP_ENTER
54 #define LOG_DSP_ENTER(tskidx)
55#endif /* LOG_DSP_ENTER */
56
57#ifndef LOG_DSP_LEAVE
58 #define LOG_DSP_LEAVE(tskidx)
59#endif /* LOG_DSP_ENTER */
60
61
62extern const intptr_t tinib_exinf[]; /* ƒ^ƒXƒN‚ÌŠg’£î•ñ */
63extern const TASK tinib_task[]; /* ƒ^ƒXƒN‚Ì‹N“®”Ô’n */
64extern const uint_t tinib_epriority[]; /* ƒ^ƒXƒN‚ÌŽÀsŽž—Dæ“xi“à•”•\Œ»j */
65
66/* “à•”ŠÖ”‚̃vƒƒgƒ^ƒCƒvéŒ¾ */
67Inline bool_t primap_empty(void);
68Inline bool_t primap_test(uint_t pri);
69Inline uint_t primap_search(void);
70Inline void primap_set(uint_t pri);
71Inline void primap_clear(uint_t pri);
72Inline uint_t bitmap_search(uint_t bitmap);
73
74
75#ifdef TOPPERS_tskini
76
77/*
78 * ŽÀsó‘Ôƒ^ƒXƒN‚ÌŒ»Ý—Dæ“x
79 */
80uint_t runtsk_curpri;
81
82/*
83 * ŽÀsó‘Ôƒ^ƒXƒN‚Ì‹N“®Žž—Dæ“x
84 */
85uint_t runtsk_ipri;
86
87/*
88 * ƒŒƒfƒBƒLƒ…
89[ƒT[ƒ`‚Ì‚½‚߂̃rƒbƒgƒ}ƒbƒv
90 */
91volatile uint_t ready_primap;
92
93/*
94 * ‹N“®—v‹ƒLƒ…
95[ƒCƒ“ƒO‚̃rƒbƒgƒ}ƒbƒv
96 */
97uint_t actque_bitmap;
98
99/*
100 * ƒ^ƒXƒNƒfƒBƒXƒpƒbƒ`‹N“®—v‹ƒtƒ‰ƒO
101 */
102bool_t reqflg;
103
104/*
105 * ƒfƒBƒXƒpƒbƒ`‹ÖŽ~ó‘Ô
106 */
107bool_t disdsp;
108
109/*
110 * read_primap‚̏‰Šú’l
111 */
112extern const uint_t init_rdypmap;
113
114#endif /* TOPPERS_tskini */
115
116#ifdef TOPPERS_get_ipriself
117
118/*
119 * ƒ^ƒXƒNID‚©‚ç‚Ì‹N“®Žž—Dæ“xŽæ“¾(ƒ^ƒXƒNƒRƒ“ƒeƒLƒXƒg—p)
120 */
121uint_t
122get_ipri_self(ID tskid)
123{
124 uint_t ipri;
125
126 if(tskid != TSK_SELF)
127 {
128 ipri = (uint_t)((tskid) - TMIN_TSKID);
129 }
130 else
131 {
132 ipri = runtsk_ipri;
133 }
134 return ipri;
135}
136
137#endif /* TOPPERS_get_ipriself */
138
139/*
140 * ƒ^ƒXƒNID‚©‚ç‚Ì‹N“®Žž—Dæ“xŽæ“¾(”ñƒ^ƒXƒNƒRƒ“ƒeƒLƒXƒg—p)
141 */
142#ifdef TOPPERS_get_ipri
143
144uint_t
145get_ipri(ID tskid)
146{
147 return (uint_t)(tskid - TMIN_TSKID);
148}
149
150#endif /* TOPPERS_get_ipri */
151
152/*
153 * ƒrƒbƒgƒ}ƒbƒvƒT[ƒ`ŠÖ”
154 *
155 * bitmap“à‚Ì1‚̃rƒbƒg‚Ì“àCÅ‚à‰ºˆÊi‰Ej‚Ì‚à‚Ì‚ðƒT[ƒ`‚µC‚»‚̃rƒb
156 * ƒg”ԍ†‚ð•Ô‚·Dƒrƒbƒg”ԍ†‚́CÅ‰ºˆÊƒrƒbƒg‚ð0‚Æ‚·‚éDbitmap‚É0‚ðŽw’è
157 * ‚µ‚Ä‚Í‚È‚ç‚È‚¢D‚±‚̊֐”‚ł́Cbitmap‚ª8ƒrƒbƒg‚Å‚ ‚邱‚Æ‚ð‰¼’è‚µC
158 * uint8_tŒ^‚Æ‚µ‚Ä‚¢‚éD
159 *
160 * ƒrƒbƒgƒT[ƒ`–½—ß‚ðŽ‚ÂƒvƒƒZƒbƒT‚ł́CƒrƒbƒgƒT[ƒ`–½—ß‚ðŽg‚¤‚悤‚É
161 * ‘‚«’¼‚µ‚½•û‚ªŒø—¦‚ª—Ç‚¢ê‡‚ª‚ ‚éD‚±‚̂悤‚ȏꍇ‚ɂ́Cƒ^[ƒQƒbƒg
162 * ˆË‘¶•”‚ŃrƒbƒgƒT[ƒ`–½—ß‚ðŽg‚Á‚½bitmap_search‚ð’è‹`‚µC
163 * OMIT_BITMAP_SEARCH‚ðƒ}ƒNƒ’è‹`‚·‚ê‚΂悢D‚Ü‚½CƒrƒbƒgƒT[ƒ`–½—ß‚Ì
164 * ƒT[ƒ`•ûŒü‚ª‹t‚È‚Ç‚Ì——R‚Å—Dæ“x‚ƃrƒbƒg‚Ƃ̑Ήž‚ð•ÏX‚µ‚½‚¢ê‡‚É
165 * ‚́CPRIMAP_BIT‚ðƒ}ƒNƒ’è‹`‚·‚ê‚΂悢D
166 *
167 * ‚Ü‚½C•W€ƒ‰ƒCƒuƒ‰ƒŠ‚Éffs‚ª‚ ‚é‚È‚çCŽŸ‚̂悤‚É’è‹`‚µ‚Ä•W€ƒ‰ƒCƒu
168 * ƒ‰ƒŠ‚ðŽg‚Á‚½•û‚ªŒø—¦‚ª—Ç‚¢‰Â”\«‚à‚ ‚éD
169 * #define bitmap_search(bitmap) (ffs(bitmap) - 1)
170 */
171#ifndef PRIMAP_BIT
172#define PRIMAP_BIT(pri) (1U << (pri))
173#endif /* PRIMAP_BIT */
174
175#ifndef OMIT_BITMAP_SEARCH
176
177static const uint8_t bitmap_search_table[] = { 0U, 1U, 0U, 2U, 0U, 1U, 0U,
178 3U, 0U, 1U, 0U, 2U, 0U, 1U, 0U };
179
180Inline uint_t
181bitmap_search(uint_t bitmap)
182{
183 uint_t n = 0U;
184
185#if TMAX_TPRI != 8
186 if ((bitmap & 0x00ffU) == 0U) {
187 bitmap >>= 8U;
188 n += 8U;
189 }
190#endif
191
192 if ((bitmap & 0x0fU) == 0U) {
193 bitmap >>= 4U;
194 n += 4U;
195 }
196 return (n + bitmap_search_table[(bitmap & 0x0fU) - 1U]);
197}
198
199#endif /* OMIT_BITMAP_SEARCH */
200
201
202/*
203 * ‹N“®Žž—Dæ“xƒrƒbƒgƒ}ƒbƒv‚ª‹ó‚©‚̃`ƒFƒbƒN
204 */
205Inline bool_t
206primap_empty(void)
207{
208 return (ready_primap == 0U);
209}
210
211/*
212 * Žw’肵‚½‹N“®Žž—Dæ“x‚Ì‹N“®Žž—Dæ“xƒrƒbƒgƒ}ƒbƒv‚ªƒZƒbƒg‚³‚ê‚Ä‚¢‚é‚©‚Ç‚¤‚©‚̃`ƒFƒbƒN
213 */
214Inline bool_t
215primap_test(uint_t pri)
216{
217 return ((ready_primap & PRIMAP_BIT(pri)) != 0U);
218}
219
220/*
221 * ‹N“®Žž—Dæ“xƒrƒbƒgƒ}ƒbƒv‚̃T[ƒ`
222 */
223Inline uint_t
224primap_search(void)
225{
226 return bitmap_search(ready_primap);
227}
228
229/*
230 * ‹N“®Žž—Dæ“xƒrƒbƒgƒ}ƒbƒv‚̃Zƒbƒg
231 */
232Inline void
233primap_set(uint_t pri)
234{
235 ready_primap |= PRIMAP_BIT(pri);
236}
237
238/*
239 * ‹N“®Žž—Dæ“xƒrƒbƒgƒ}ƒbƒv‚̃NƒŠƒA
240 */
241Inline void
242primap_clear(uint_t pri)
243{
244 ready_primap &= ~PRIMAP_BIT(pri);
245}
246
247/*
248 * Å‚—Dæ‡ˆÊƒ^ƒXƒN‚̃T[ƒ`
249 */
250
251#ifdef TOPPERS_tsksched
252
253uint_t
254search_schedtsk(void)
255{
256 return primap_search();
257}
258
259#endif /* TOPPERS_tsksched */
260
261#ifdef TOPPERS_tskdmt
262/*
263 * Žw’肵‚½‹N“®Žž—Dæ“x‚̃^ƒXƒN‚ªŽÀs‚Å‚«‚éó‘Ô‚©‚Ç‚¤‚©‚̃eƒXƒg
264 *
265 */
266bool_t
267test_dormant(uint_t ipri)
268{
269 return !primap_test(ipri);
270}
271
272#endif /* TOPPERS_tskdmt */
273
274/*
275 * ƒ^ƒXƒNŠÇ—ƒ‚ƒWƒ…
276[ƒ‹‚̏‰Šú‰»
277 */
278
279
280#ifdef TOPPERS_tskini
281
282void
283initialize_task(void)
284{
285 /* ‹N“®Žž—Dæ“xƒrƒbƒgƒ}ƒbƒv‚̏‰Šú‰» */
286 ready_primap = init_rdypmap;
287
288 /* ƒ^ƒXƒN—Dæ“x‚̏‰Šú‰» */
289 runtsk_curpri = TSKPRI_NULL;
290 runtsk_ipri = TSKPRI_NULL;
291
292 /* ‹N“®—v‹ƒLƒ…
293[ƒCƒ“ƒO‚̏‰Šú‰» */
294 actque_bitmap = 0U;
295
296 /* Š„ž‚Ý‹ÖŽ~ƒtƒ‰ƒO‚̏‰Šú‰» */
297 disdsp = false;
298}
299
300#endif /* TOPPERS_tskini */
301
302/*
303 * ƒ^ƒXƒN‚Ì‹N“®
304 */
305
306#ifdef TOPPERS_tskact
307
308bool_t
309make_active(uint_t ipri)
310{
311 bool_t dsp;
312
313 primap_set(ipri);
314
315 /*
316 * ŽÀsó‘Ôƒ^ƒXƒN‚ÌŒ»Ý—Dæ“x‚ƈø”‚ÅŽw’肳‚ꂽ
317 * ‹N“®‘Ώۃ^ƒXƒN‚̏‰Šú—Dæ“x‚ð”äŠr‚µCŽÀsó‘Ôƒ^ƒXƒN‚ð
318 * •ÏX‚·‚é‚©”»’è‚·‚éD
319 *
320 * ‚½‚¾‚µŽÀsó‘Ôƒ^ƒXƒN‚ð•ÏX‚·‚éê‡‚̓fƒBƒXƒpƒbƒ`‹ÖŽ~ó‘Ô‚àŽQÆ‚·‚éD
321 */
322 if(ipri < runtsk_curpri) {
323 dsp = !disdsp;
324 }
325 else {
326 dsp = false;
327 }
328
329 return dsp;
330}
331
332#endif /* TOPPERS_tskact */
333
334
335/*
336 * ƒ^ƒXƒN‚ÌŽÀs
337 */
338
339#ifdef TOPPERS_tskrun
340
341void
342run_task(uint_t ipri)
343{
344 uint_t next_pri; /* ŽŸ‚ÉŽÀsŠJŽn‚·‚éƒ^ƒXƒN‚Ì‹N“®Žž—Dæ“x */
345 uint_t saved_ipri; /* ŒÄ‚яo‚µŒ³ƒ^ƒXƒN‚̏‰Šú—Dæ“x */
346 uint_t saved_curpri; /* ŒÄ‚яo‚µŒ³ƒ^ƒXƒN‚ÌŒ»Ý—Dæ“x */
347 bool_t cont;
348
349 /* Å‚—Dæ‡ˆÊƒ^ƒXƒN‚ðŽÀs‚·‚é‘O‚ɁCŒ»ÝŽÀs’†‚̃^ƒXƒN‚ÌŒ»Ý—Dæ“x‚Ə‰Šú—Dæ“x‚ð•Û‘¶‚·‚é */
350 next_pri = ipri;
351 saved_ipri = runtsk_ipri;
352 saved_curpri = runtsk_curpri;
353
354 do {
355 /* ŽÀs‚·‚éƒ^ƒXƒN‚ÌŒ»Ý—Dæ“x‚ðŽÀsŽž—Dæ“x‚ɐݒ肷‚é */
356 runtsk_curpri = tinib_epriority[next_pri];
357 runtsk_ipri = next_pri;
358
359 /* CPUƒƒbƒN‰ðœ */
360 t_unlock_cpu();
361
362 /* ƒ^ƒXƒNŽÀsŠJŽn */
363 (*((TASK)(tinib_task[next_pri])))(tinib_exinf[next_pri]);
364
365 if (t_sense_lock()) {
366 /*
367 * CPUƒƒbƒNó‘Ô‚Åext_tsk‚ªŒÄ‚΂ꂽê‡‚́CCPUƒƒbƒN‚ð‰ðœ‚µ
368 * ‚Ä‚©‚çƒ^ƒXƒN‚ðI—¹‚·‚éDŽÀ‘•ã‚́CƒT[ƒrƒXƒR[ƒ‹“à‚Å‚ÌCPU
369 * ƒƒbƒN‚ðÈ—ª‚·‚ê‚΂悢‚¾‚¯D
370 */
371 }
372 else {
373 /*
374 * ‚±‚Ìt_lock_cpu‚ð‚±‚Ì‰º‚Ìdisdsp‚̐ݒè‚̂悤‚É‚µ‚È‚¢‚̂́C
375 * CPUƒƒbƒN’†‚ɍēxt_lock_cpu‚ðŒÄ‚΂Ȃ¢‚½‚ß‚Å‚ ‚éD
376 */
377 t_lock_cpu();
378 }
379
380 /* Š„ž‚Ý—Dæ“xƒ}ƒXƒN‚Í‘S‰ðœó‘Ô‚Ì‚Í‚¸‚Ȃ̂ŁC‰½‚à‚µ‚È‚¢ */
381
382 /*
383 * ƒfƒBƒXƒpƒbƒ`‹ÖŽ~ó‘Ô‚Åext_tsk‚ªŒÄ‚΂ꂽê‡‚́CƒfƒBƒXƒpƒb
384 * ƒ`‹–‰Âó‘Ô‚É‚µ‚Ä‚©‚çƒ^ƒXƒN‚ðI—¹‚·‚éD
385 *
386 * –{—ˆ‚͈ȉº‚̂悤‚É‹Lq‚·‚ׂ«‚Å‚ ‚邪C‚¢‚¸‚ê‚É‚¹‚ædisdsp‚ð
387 * false‚É‚·‚ê‚΂¢‚¢‚½‚߁C’P‚Éfalse‚ɐݒ肷‚éD
388 *
389 * if (disdsp) {
390 * disdsp = false;
391 * }
392 */
393 disdsp = false;
394
395 /* ƒrƒbƒgƒ}ƒbƒvƒNƒŠƒAD */
396 primap_clear(next_pri);
397
398 cont = false;
399
400 /* ƒ^ƒXƒN‹N“®—v‹ƒLƒ…
401[ƒCƒ“ƒO‚̃`ƒFƒbƒN */
402 if (actque_test(next_pri)) {
403 actque_clear(next_pri);
404 (void) make_active(next_pri);
405 }
406
407 /* ‚¢‚¸‚ê‚©‚̃^ƒXƒN‚ªŽÀs‰Â”\ó‘Ô‚É‚È‚Á‚Ä‚¢‚é‚© */
408 if(!primap_empty())
409 {
410 /* –ß‚èæƒ^ƒXƒN‚ÌŒ»Ý—Dæ“x‚æ‚荂‚¢‹N“®Žž—Dæ“x‚ð‚à‚ƒ^ƒXƒN‚ª‹N“®‚³‚ꂽ‚© */
411 next_pri = search_schedtsk();
412 if(saved_curpri > next_pri)
413 {
414 cont = true;
415 }
416 }
417 } while(cont);
418
419 runtsk_curpri = saved_curpri;
420 runtsk_ipri = saved_ipri;
421}
422
423#endif /* TOPPERS_tskrun */
424
425/*
426 * ƒ^ƒXƒNƒfƒBƒXƒpƒbƒ`ƒƒ
427 */
428
429#ifdef TOPPERS_tskdsp
430
431void
432dispatcher(void)
433{
434 do {
435 if(!primap_empty()) {
436 /* ƒ^ƒXƒN‚ÌŠJŽn */
437 run_task(search_schedtsk());
438 }
439 else {
440 idle_loop();
441 }
442 } while(true);
443}
444
445#endif /* TOPPERS_tsk_dsp */
Note: See TracBrowser for help on using the repository browser.