source: UsbWattMeter/trunk/asp_dcre/kernel/cyclic.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: 9.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-2014 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: cyclic.c 164 2016-03-07 11:33:50Z coas-nagasima $
45 */
46
47/*
48 * ŽüŠúƒnƒ“ƒhƒ‰‹@”\
49 */
50
51#include "kernel_impl.h"
52#include "check.h"
53#include "cyclic.h"
54
55/*
56 * ƒgƒŒ[ƒXƒƒOƒ}ƒNƒ‚̃fƒtƒHƒ‹ƒg’è‹`
57 */
58#ifndef LOG_CYC_ENTER
59#define LOG_CYC_ENTER(p_cyccb)
60#endif /* LOG_CYC_ENTER */
61
62#ifndef LOG_CYC_LEAVE
63#define LOG_CYC_LEAVE(p_cyccb)
64#endif /* LOG_CYC_LEAVE */
65
66#ifndef LOG_ACRE_CYC_ENTER
67#define LOG_ACRE_CYC_ENTER(pk_ccyc)
68#endif /* LOG_ACRE_CYC_ENTER */
69
70#ifndef LOG_ACRE_CYC_LEAVE
71#define LOG_ACRE_CYC_LEAVE(ercd)
72#endif /* LOG_ACRE_CYC_LEAVE */
73
74#ifndef LOG_DEL_CYC_ENTER
75#define LOG_DEL_CYC_ENTER(cycid)
76#endif /* LOG_DEL_CYC_ENTER */
77
78#ifndef LOG_DEL_CYC_LEAVE
79#define LOG_DEL_CYC_LEAVE(ercd)
80#endif /* LOG_DEL_CYC_LEAVE */
81
82#ifndef LOG_STA_CYC_ENTER
83#define LOG_STA_CYC_ENTER(cycid)
84#endif /* LOG_STA_CYC_ENTER */
85
86#ifndef LOG_STA_CYC_LEAVE
87#define LOG_STA_CYC_LEAVE(ercd)
88#endif /* LOG_STA_CYC_LEAVE */
89
90#ifndef LOG_STP_CYC_ENTER
91#define LOG_STP_CYC_ENTER(cycid)
92#endif /* LOG_STP_CYC_ENTER */
93
94#ifndef LOG_STP_CYC_LEAVE
95#define LOG_STP_CYC_LEAVE(ercd)
96#endif /* LOG_STP_CYC_LEAVE */
97
98#ifndef LOG_REF_CYC_ENTER
99#define LOG_REF_CYC_ENTER(cycid, pk_rcyc)
100#endif /* LOG_REF_CYC_ENTER */
101
102#ifndef LOG_REF_CYC_LEAVE
103#define LOG_REF_CYC_LEAVE(ercd, pk_rcyc)
104#endif /* LOG_REF_CYC_LEAVE */
105
106/*
107 * ŽüŠúƒnƒ“ƒhƒ‰‚̐”
108 */
109#define tnum_cyc ((uint_t)(tmax_cycid - TMIN_CYCID + 1))
110#define tnum_scyc ((uint_t)(tmax_scycid - TMIN_CYCID + 1))
111
112/*
113 * ŽüŠúƒnƒ“ƒhƒ‰ID‚©‚çŽüŠúƒnƒ“ƒhƒ‰ŠÇ—ƒuƒƒbƒN‚ðŽæ‚èo‚·‚½‚߂̃}ƒNƒ
114 */
115#define INDEX_CYC(cycid) ((uint_t)((cycid) - TMIN_CYCID))
116#define get_cyccb(cycid) (&(cyccb_table[INDEX_CYC(cycid)]))
117
118/*
119 * ŽüŠúƒnƒ“ƒhƒ‰‹N“®‚Ì‚½‚߂̃^ƒCƒ€ƒCƒxƒ“ƒgƒuƒƒbƒN‚Ì“o˜^
120 */
121Inline void
122tmevtb_enqueue_cyc(CYCCB *p_cyccb, EVTTIM evttim)
123{
124 tmevtb_enqueue_evttim(&(p_cyccb->tmevtb), evttim,
125 (CBACK) call_cychdr, (void *) p_cyccb);
126 p_cyccb->evttim = evttim;
127}
128
129#ifdef TOPPERS_cycini
130
131/*
132 * Žg—p‚µ‚Ä‚¢‚È‚¢ŽüŠúƒnƒ“ƒhƒ‰ŠÇ—ƒuƒƒbƒN‚̃ŠƒXƒg
133 *
134 * ŽüŠúƒnƒ“ƒhƒ‰ŠÇ—ƒuƒƒbƒN‚̐擪‚ɂ̓Lƒ…
135[‚ɂ‚Ȃ®‚½‚߂̗̈悪‚È‚¢‚½
136 * ‚߁Cƒ^ƒCƒ€ƒCƒxƒ“ƒgƒuƒƒbƒNitmevtbj‚̗̈æ‚ð—p‚¢‚éD
137 */
138QUEUE free_cyccb;
139
140/*
141 * ŽüŠúƒnƒ“ƒhƒ‰‹@”\‚̏‰Šú‰»
142 */
143void
144initialize_cyclic(void)
145{
146 uint_t i, j;
147 CYCCB *p_cyccb;
148 CYCINIB *p_cycinib;
149
150 for (i = 0; i < tnum_scyc; i++) {
151 p_cyccb = &(cyccb_table[i]);
152 p_cyccb->p_cycinib = &(cycinib_table[i]);
153 if ((p_cyccb->p_cycinib->cycatr & TA_STA) != 0U) {
154 p_cyccb->cycsta = true;
155 tmevtb_enqueue_cyc(p_cyccb, (EVTTIM)(p_cyccb->p_cycinib->cycphs));
156 }
157 else {
158 p_cyccb->cycsta = false;
159 }
160 }
161 queue_initialize(&free_cyccb);
162 for (j = 0; i < tnum_cyc; i++, j++) {
163 p_cyccb = &(cyccb_table[i]);
164 p_cycinib = &(acycinib_table[j]);
165 p_cycinib->cycatr = TA_NOEXS;
166 p_cyccb->p_cycinib = ((const CYCINIB *) p_cycinib);
167 queue_insert_prev(&free_cyccb, ((QUEUE *) &(p_cyccb->tmevtb)));
168 }
169}
170
171#endif /* TOPPERS_cycini */
172
173/*
174 * ŽüŠúƒnƒ“ƒhƒ‰‚̐¶¬
175 */
176#ifdef TOPPERS_acre_cyc
177
178ER_UINT
179acre_cyc(const T_CCYC *pk_ccyc)
180{
181 CYCCB *p_cyccb;
182 CYCINIB *p_cycinib;
183 ER ercd;
184
185 LOG_ACRE_CYC_ENTER(pk_ccyc);
186 CHECK_TSKCTX_UNL();
187 CHECK_RSATR(pk_ccyc->cycatr, TA_STA);
188 CHECK_ALIGN_FUNC(pk_ccyc->cychdr);
189 CHECK_NONNULL_FUNC(pk_ccyc->cychdr);
190 CHECK_PAR(0 < pk_ccyc->cyctim && pk_ccyc->cyctim <= TMAX_RELTIM);
191 CHECK_PAR(0 <= pk_ccyc->cycphs && pk_ccyc->cycphs <= TMAX_RELTIM);
192
193 t_lock_cpu();
194 if (tnum_cyc == 0 || queue_empty(&free_cyccb)) {
195 ercd = E_NOID;
196 }
197 else {
198 p_cyccb = ((CYCCB *)(((char *) queue_delete_next(&free_cyccb))
199 - offsetof(CYCCB, tmevtb)));
200 p_cycinib = (CYCINIB *)(p_cyccb->p_cycinib);
201 p_cycinib->cycatr = pk_ccyc->cycatr;
202 p_cycinib->exinf = pk_ccyc->exinf;
203 p_cycinib->cychdr = pk_ccyc->cychdr;
204 p_cycinib->cyctim = pk_ccyc->cyctim;
205 p_cycinib->cycphs = pk_ccyc->cycphs;
206
207 if ((p_cyccb->p_cycinib->cycatr & TA_STA) != 0U) {
208 p_cyccb->cycsta = true;
209 tmevtb_enqueue_cyc(p_cyccb,
210 base_time + p_cyccb->p_cycinib->cycphs);
211 }
212 else {
213 p_cyccb->cycsta = false;
214 }
215 ercd = CYCID(p_cyccb);
216 }
217 t_unlock_cpu();
218
219 error_exit:
220 LOG_ACRE_CYC_LEAVE(ercd);
221 return(ercd);
222}
223
224#endif /* TOPPERS_acre_cyc */
225
226/*
227 * ŽüŠúƒnƒ“ƒhƒ‰‚̍폜
228 */
229#ifdef TOPPERS_del_cyc
230
231ER
232del_cyc(ID cycid)
233{
234 CYCCB *p_cyccb;
235 CYCINIB *p_cycinib;
236 ER ercd;
237
238 LOG_DEL_CYC_ENTER(cycid);
239 CHECK_TSKCTX_UNL();
240 CHECK_CYCID(cycid);
241 p_cyccb = get_cyccb(cycid);
242
243 t_lock_cpu();
244 if (p_cyccb->p_cycinib->cycatr == TA_NOEXS) {
245 ercd = E_NOEXS;
246 }
247 else if (CYCID(p_cyccb) > tmax_scycid) {
248 if (p_cyccb->cycsta) {
249 p_cyccb->cycsta = false;
250 tmevtb_dequeue(&(p_cyccb->tmevtb));
251 }
252
253 p_cycinib = (CYCINIB *)(p_cyccb->p_cycinib);
254 p_cycinib->cycatr = TA_NOEXS;
255 queue_insert_prev(&free_cyccb, ((QUEUE *) &(p_cyccb->tmevtb)));
256 ercd = E_OK;
257 }
258 else {
259 ercd = E_OBJ;
260 }
261 t_unlock_cpu();
262
263 error_exit:
264 LOG_DEL_CYC_LEAVE(ercd);
265 return(ercd);
266}
267
268#endif /* TOPPERS_del_cyc */
269
270/*
271 * ŽüŠúƒnƒ“ƒhƒ‰‚Ì“®ìŠJŽn
272 */
273#ifdef TOPPERS_sta_cyc
274
275ER
276sta_cyc(ID cycid)
277{
278 CYCCB *p_cyccb;
279 ER ercd;
280
281 LOG_STA_CYC_ENTER(cycid);
282 CHECK_TSKCTX_UNL();
283 CHECK_CYCID(cycid);
284 p_cyccb = get_cyccb(cycid);
285
286 t_lock_cpu();
287 if (p_cyccb->p_cycinib->cycatr == TA_NOEXS) {
288 ercd = E_NOEXS;
289 }
290 else {
291 if (p_cyccb->cycsta) {
292 tmevtb_dequeue(&(p_cyccb->tmevtb));
293 }
294 else {
295 p_cyccb->cycsta = true;
296 }
297 tmevtb_enqueue_cyc(p_cyccb, base_time + p_cyccb->p_cycinib->cycphs);
298 ercd = E_OK;
299 }
300 t_unlock_cpu();
301
302 error_exit:
303 LOG_STA_CYC_LEAVE(ercd);
304 return(ercd);
305}
306
307#endif /* TOPPERS_sta_cyc */
308
309/*
310 * ŽüŠúƒnƒ“ƒhƒ‰‚Ì“®ì’âŽ~
311 */
312#ifdef TOPPERS_stp_cyc
313
314ER
315stp_cyc(ID cycid)
316{
317 CYCCB *p_cyccb;
318 ER ercd;
319
320 LOG_STP_CYC_ENTER(cycid);
321 CHECK_TSKCTX_UNL();
322 CHECK_CYCID(cycid);
323 p_cyccb = get_cyccb(cycid);
324
325 t_lock_cpu();
326 if (p_cyccb->p_cycinib->cycatr == TA_NOEXS) {
327 ercd = E_NOEXS;
328 }
329 else {
330 if (p_cyccb->cycsta) {
331 p_cyccb->cycsta = false;
332 tmevtb_dequeue(&(p_cyccb->tmevtb));
333 }
334 ercd = E_OK;
335 }
336 t_unlock_cpu();
337
338 error_exit:
339 LOG_STP_CYC_LEAVE(ercd);
340 return(ercd);
341}
342
343#endif /* TOPPERS_stp_cyc */
344
345/*
346 * ŽüŠúƒnƒ“ƒhƒ‰‚̏ó‘ÔŽQÆ
347 */
348#ifdef TOPPERS_ref_cyc
349
350ER
351ref_cyc(ID cycid, T_RCYC *pk_rcyc)
352{
353 CYCCB *p_cyccb;
354 ER ercd;
355
356 LOG_REF_CYC_ENTER(cycid, pk_rcyc);
357 CHECK_TSKCTX_UNL();
358 CHECK_CYCID(cycid);
359 p_cyccb = get_cyccb(cycid);
360
361 t_lock_cpu();
362 if (p_cyccb->p_cycinib->cycatr == TA_NOEXS) {
363 ercd = E_NOEXS;
364 }
365 else {
366 if (p_cyccb->cycsta) {
367 pk_rcyc->cycstat = TCYC_STA;
368 pk_rcyc->lefttim = tmevt_lefttim(&(p_cyccb->tmevtb));
369 }
370 else {
371 pk_rcyc->cycstat = TCYC_STP;
372 }
373 ercd = E_OK;
374 }
375 t_unlock_cpu();
376
377 error_exit:
378 LOG_REF_CYC_LEAVE(ercd, pk_rcyc);
379 return(ercd);
380}
381
382#endif /* TOPPERS_ref_cyc */
383
384/*
385 * ŽüŠúƒnƒ“ƒhƒ‰‹N“®ƒ‹[ƒ`ƒ“
386 */
387#ifdef TOPPERS_cyccal
388
389void
390call_cychdr(CYCCB *p_cyccb)
391{
392 PRI saved_ipm;
393
394 /*
395 * ŽŸ‰ñ‚Ì‹N“®‚Ì‚½‚߂̃^ƒCƒ€ƒCƒxƒ“ƒgƒuƒƒbƒN‚ð“o˜^‚·‚éD
396 *
397 * “¯‚¶ƒ^ƒCƒ€ƒeƒBƒbƒN‚ÅŽüŠúƒnƒ“ƒhƒ‰‚ðÄ“x‹N“®‚·‚ׂ«ê‡‚ɂ́C‚±‚Ì
398 * ŠÖ”‚©‚çsignal_time‚É–ß‚Á‚½Œã‚ɁCÄ“x‚±‚̊֐”‚ªŒÄ‚΂ê‚邱‚Æ‚É‚È
399 * ‚éD
400 */
401 tmevtb_enqueue_cyc(p_cyccb, p_cyccb->evttim + p_cyccb->p_cycinib->cyctim);
402
403 /*
404 * ŽüŠúƒnƒ“ƒhƒ‰‚ðCCPUƒƒbƒN‰ðœó‘ԂŌĂяo‚·D
405 */
406 saved_ipm = i_get_ipm();
407 i_unlock_cpu();
408
409 LOG_CYC_ENTER(p_cyccb);
410 (*((CYCHDR)(p_cyccb->p_cycinib->cychdr)))(p_cyccb->p_cycinib->exinf);
411 LOG_CYC_LEAVE(p_cyccb);
412
413 if (!i_sense_lock()) {
414 i_lock_cpu();
415 }
416 i_set_ipm(saved_ipm);
417}
418
419#endif /* TOPPERS_cyccal */
Note: See TracBrowser for help on using the repository browser.