source: UsbWattMeter/trunk/asp_dcre/kernel/alarm.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.9 KB
RevLine 
[164]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: alarm.c 164 2016-03-07 11:33:50Z coas-nagasima $
45 */
46
47/*
48 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‹@”\
49 */
50
51#include "kernel_impl.h"
52#include "check.h"
53#include "alarm.h"
54
55/*
56 * ƒgƒŒ[ƒXƒƒOƒ}ƒNƒ‚̃fƒtƒHƒ‹ƒg’è‹`
57 */
58#ifndef LOG_ALM_ENTER
59#define LOG_ALM_ENTER(p_almcb)
60#endif /* LOG_ALM_ENTER */
61
62#ifndef LOG_ALM_LEAVE
63#define LOG_ALM_LEAVE(p_almcb)
64#endif /* LOG_ALM_LEAVE */
65
66#ifndef LOG_ACRE_ALM_ENTER
67#define LOG_ACRE_ALM_ENTER(pk_calm)
68#endif /* LOG_ACRE_ALM_ENTER */
69
70#ifndef LOG_ACRE_ALM_LEAVE
71#define LOG_ACRE_ALM_LEAVE(ercd)
72#endif /* LOG_ACRE_ALM_LEAVE */
73
74#ifndef LOG_DEL_ALM_ENTER
75#define LOG_DEL_ALM_ENTER(almid)
76#endif /* LOG_DEL_ALM_ENTER */
77
78#ifndef LOG_DEL_ALM_LEAVE
79#define LOG_DEL_ALM_LEAVE(ercd)
80#endif /* LOG_DEL_ALM_LEAVE */
81
82#ifndef LOG_STA_ALM_ENTER
83#define LOG_STA_ALM_ENTER(almid, almtim)
84#endif /* LOG_STA_ALM_ENTER */
85
86#ifndef LOG_STA_ALM_LEAVE
87#define LOG_STA_ALM_LEAVE(ercd)
88#endif /* LOG_STA_ALM_LEAVE */
89
90#ifndef LOG_ISTA_ALM_ENTER
91#define LOG_ISTA_ALM_ENTER(almid, almtim)
92#endif /* LOG_ISTA_ALM_ENTER */
93
94#ifndef LOG_ISTA_ALM_LEAVE
95#define LOG_ISTA_ALM_LEAVE(ercd)
96#endif /* LOG_ISTA_ALM_LEAVE */
97
98#ifndef LOG_STP_ALM_ENTER
99#define LOG_STP_ALM_ENTER(almid)
100#endif /* LOG_STP_ALM_ENTER */
101
102#ifndef LOG_STP_ALM_LEAVE
103#define LOG_STP_ALM_LEAVE(ercd)
104#endif /* LOG_STP_ALM_LEAVE */
105
106#ifndef LOG_ISTP_ALM_ENTER
107#define LOG_ISTP_ALM_ENTER(almid)
108#endif /* LOG_ISTP_ALM_ENTER */
109
110#ifndef LOG_ISTP_ALM_LEAVE
111#define LOG_ISTP_ALM_LEAVE(ercd)
112#endif /* LOG_ISTP_ALM_LEAVE */
113
114#ifndef LOG_REF_ALM_ENTER
115#define LOG_REF_ALM_ENTER(almid, pk_ralm)
116#endif /* LOG_REF_ALM_ENTER */
117
118#ifndef LOG_REF_ALM_LEAVE
119#define LOG_REF_ALM_LEAVE(ercd, pk_ralm)
120#endif /* LOG_REF_ALM_LEAVE */
121
122/*
123 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚̐”
124 */
125#define tnum_alm ((uint_t)(tmax_almid - TMIN_ALMID + 1))
126#define tnum_salm ((uint_t)(tmax_salmid - TMIN_ALMID + 1))
127
128/*
129 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰ID‚©‚çƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰ŠÇ—ƒuƒƒbƒN‚ðŽæ‚èo‚·‚½‚߂̃}ƒNƒ
130 */
131#define INDEX_ALM(almid) ((uint_t)((almid) - TMIN_ALMID))
132#define get_almcb(almid) (&(almcb_table[INDEX_ALM(almid)]))
133
134#ifdef TOPPERS_almini
135
136/*
137 * Žg—p‚µ‚Ä‚¢‚È‚¢ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰ŠÇ—ƒuƒƒbƒN‚̃ŠƒXƒg
138 *
139 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰ŠÇ—ƒuƒƒbƒN‚̐擪‚ɂ̓Lƒ…
140[‚ɂ‚Ȃ®‚½‚߂̗̈悪‚È
141 * ‚¢‚½‚߁Cƒ^ƒCƒ€ƒCƒxƒ“ƒgƒuƒƒbƒNitmevtbj‚̗̈æ‚ð—p‚¢‚éD
142 */
143QUEUE free_almcb;
144
145/*
146 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‹@”\‚̏‰Šú‰»
147 */
148void
149initialize_alarm(void)
150{
151 uint_t i, j;
152 ALMCB *p_almcb;
153 ALMINIB *p_alminib;
154
155 for (i = 0; i < tnum_salm; i++) {
156 p_almcb = &(almcb_table[i]);
157 p_almcb->p_alminib = &(alminib_table[i]);
158 p_almcb->almsta = false;
159 }
160 queue_initialize(&free_almcb);
161 for (j = 0; i < tnum_alm; i++, j++) {
162 p_almcb = &(almcb_table[i]);
163 p_alminib = &(aalminib_table[j]);
164 p_alminib->almatr = TA_NOEXS;
165 p_almcb->p_alminib = ((const ALMINIB *) p_alminib);
166 queue_insert_prev(&free_almcb, ((QUEUE *) &(p_almcb->tmevtb)));
167 }
168}
169
170#endif /* TOPPERS_almini */
171
172/*
173 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚̐¶¬
174 */
175#ifdef TOPPERS_acre_alm
176
177ER_UINT
178acre_alm(const T_CALM *pk_calm)
179{
180 ALMCB *p_almcb;
181 ALMINIB *p_alminib;
182 ER ercd;
183
184 LOG_ACRE_ALM_ENTER(pk_calm);
185 CHECK_TSKCTX_UNL();
186 CHECK_RSATR(pk_calm->almatr, TA_NULL);
187 CHECK_ALIGN_FUNC(pk_calm->almhdr);
188 CHECK_NONNULL_FUNC(pk_calm->almhdr);
189
190 t_lock_cpu();
191 if (tnum_alm == 0 || queue_empty(&free_almcb)) {
192 ercd = E_NOID;
193 }
194 else {
195 p_almcb = ((ALMCB *)(((char *) queue_delete_next(&free_almcb))
196 - offsetof(ALMCB, tmevtb)));
197 p_alminib = (ALMINIB *)(p_almcb->p_alminib);
198 p_alminib->almatr = pk_calm->almatr;
199 p_alminib->exinf = pk_calm->exinf;
200 p_alminib->almhdr = pk_calm->almhdr;
201
202 p_almcb->almsta = false;
203 ercd = ALMID(p_almcb);
204 }
205 t_unlock_cpu();
206
207 error_exit:
208 LOG_ACRE_ALM_LEAVE(ercd);
209 return(ercd);
210}
211
212#endif /* TOPPERS_acre_alm */
213
214/*
215 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚̍폜
216 */
217#ifdef TOPPERS_del_alm
218
219ER
220del_alm(ID almid)
221{
222 ALMCB *p_almcb;
223 ALMINIB *p_alminib;
224 ER ercd;
225
226 LOG_DEL_ALM_ENTER(almid);
227 CHECK_TSKCTX_UNL();
228 CHECK_ALMID(almid);
229 p_almcb = get_almcb(almid);
230
231 t_lock_cpu();
232 if (p_almcb->p_alminib->almatr == TA_NOEXS) {
233 ercd = E_NOEXS;
234 }
235 else if (ALMID(p_almcb) > tmax_salmid) {
236 if (p_almcb->almsta) {
237 p_almcb->almsta = false;
238 tmevtb_dequeue(&(p_almcb->tmevtb));
239 }
240
241 p_alminib = (ALMINIB *)(p_almcb->p_alminib);
242 p_alminib->almatr = TA_NOEXS;
243 queue_insert_prev(&free_almcb, ((QUEUE *) &(p_almcb->tmevtb)));
244 ercd = E_OK;
245 }
246 else {
247 ercd = E_OBJ;
248 }
249 t_unlock_cpu();
250
251 error_exit:
252 LOG_DEL_ALM_LEAVE(ercd);
253 return(ercd);
254}
255
256#endif /* TOPPERS_del_alm */
257
258/*
259 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚Ì“®ìŠJŽn
260 */
261#ifdef TOPPERS_sta_alm
262
263ER
264sta_alm(ID almid, RELTIM almtim)
265{
266 ALMCB *p_almcb;
267 ER ercd;
268
269 LOG_STA_ALM_ENTER(almid, almtim);
270 CHECK_TSKCTX_UNL();
271 CHECK_ALMID(almid);
272 CHECK_PAR(almtim <= TMAX_RELTIM);
273 p_almcb = get_almcb(almid);
274
275 t_lock_cpu();
276 if (p_almcb->p_alminib->almatr == TA_NOEXS) {
277 ercd = E_NOEXS;
278 }
279 else {
280 if (p_almcb->almsta) {
281 tmevtb_dequeue(&(p_almcb->tmevtb));
282 }
283 else {
284 p_almcb->almsta = true;
285 }
286 tmevtb_enqueue(&(p_almcb->tmevtb), almtim,
287 (CBACK) call_almhdr, (void *) p_almcb);
288 ercd = E_OK;
289 }
290 t_unlock_cpu();
291
292 error_exit:
293 LOG_STA_ALM_LEAVE(ercd);
294 return(ercd);
295}
296
297#endif /* TOPPERS_sta_alm */
298
299/*
300 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚Ì“®ìŠJŽni”ñƒ^ƒXƒNƒRƒ“ƒeƒLƒXƒg—pj
301 */
302#ifdef TOPPERS_ista_alm
303
304ER
305ista_alm(ID almid, RELTIM almtim)
306{
307 ALMCB *p_almcb;
308 ER ercd;
309
310 LOG_ISTA_ALM_ENTER(almid, almtim);
311 CHECK_INTCTX_UNL();
312 CHECK_ALMID(almid);
313 CHECK_PAR(almtim <= TMAX_RELTIM);
314 p_almcb = get_almcb(almid);
315
316 i_lock_cpu();
317 if (p_almcb->p_alminib->almatr == TA_NOEXS) {
318 ercd = E_NOEXS;
319 }
320 else {
321 if (p_almcb->almsta) {
322 tmevtb_dequeue(&(p_almcb->tmevtb));
323 }
324 else {
325 p_almcb->almsta = true;
326 }
327 tmevtb_enqueue(&(p_almcb->tmevtb), almtim,
328 (CBACK) call_almhdr, (void *) p_almcb);
329 ercd = E_OK;
330 }
331 i_unlock_cpu();
332
333 error_exit:
334 LOG_ISTA_ALM_LEAVE(ercd);
335 return(ercd);
336}
337
338#endif /* TOPPERS_ista_alm */
339
340/*
341 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚Ì“®ì’âŽ~
342 */
343#ifdef TOPPERS_stp_alm
344
345ER
346stp_alm(ID almid)
347{
348 ALMCB *p_almcb;
349 ER ercd;
350
351 LOG_STP_ALM_ENTER(almid);
352 CHECK_TSKCTX_UNL();
353 CHECK_ALMID(almid);
354 p_almcb = get_almcb(almid);
355
356 t_lock_cpu();
357 if (p_almcb->p_alminib->almatr == TA_NOEXS) {
358 ercd = E_NOEXS;
359 }
360 else {
361 if (p_almcb->almsta) {
362 p_almcb->almsta = false;
363 tmevtb_dequeue(&(p_almcb->tmevtb));
364 }
365 ercd = E_OK;
366 }
367 t_unlock_cpu();
368
369 error_exit:
370 LOG_STP_ALM_LEAVE(ercd);
371 return(ercd);
372}
373
374#endif /* TOPPERS_stp_alm */
375
376/*
377 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚Ì“®ì’âŽ~i”ñƒ^ƒXƒNƒRƒ“ƒeƒLƒXƒg—pj
378 */
379#ifdef TOPPERS_istp_alm
380
381ER
382istp_alm(ID almid)
383{
384 ALMCB *p_almcb;
385 ER ercd;
386
387 LOG_ISTP_ALM_ENTER(almid);
388 CHECK_INTCTX_UNL();
389 CHECK_ALMID(almid);
390 p_almcb = get_almcb(almid);
391
392 i_lock_cpu();
393 if (p_almcb->p_alminib->almatr == TA_NOEXS) {
394 ercd = E_NOEXS;
395 }
396 else {
397 if (p_almcb->almsta) {
398 p_almcb->almsta = false;
399 tmevtb_dequeue(&(p_almcb->tmevtb));
400 }
401 ercd = E_OK;
402 }
403 i_unlock_cpu();
404
405 error_exit:
406 LOG_ISTP_ALM_LEAVE(ercd);
407 return(ercd);
408}
409
410#endif /* TOPPERS_istp_alm */
411
412/*
413 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚̏ó‘ÔŽQÆ
414 */
415#ifdef TOPPERS_ref_alm
416
417ER
418ref_alm(ID almid, T_RALM *pk_ralm)
419{
420 ALMCB *p_almcb;
421 ER ercd;
422
423 LOG_REF_ALM_ENTER(almid, pk_ralm);
424 CHECK_TSKCTX_UNL();
425 CHECK_ALMID(almid);
426 p_almcb = get_almcb(almid);
427
428 t_lock_cpu();
429 if (p_almcb->p_alminib->almatr == TA_NOEXS) {
430 ercd = E_NOEXS;
431 }
432 else {
433 if (p_almcb->almsta) {
434 pk_ralm->almstat = TALM_STA;
435 pk_ralm->lefttim = tmevt_lefttim(&(p_almcb->tmevtb));
436 }
437 else {
438 pk_ralm->almstat = TALM_STP;
439 }
440 ercd = E_OK;
441 }
442 t_unlock_cpu();
443
444 error_exit:
445 LOG_REF_ALM_LEAVE(ercd, pk_ralm);
446 return(ercd);
447}
448
449#endif /* TOPPERS_ref_alm */
450
451/*
452 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‹N“®ƒ‹[ƒ`ƒ“
453 */
454#ifdef TOPPERS_almcal
455
456void
457call_almhdr(ALMCB *p_almcb)
458{
459 PRI saved_ipm;
460
461 /*
462 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚ð’âŽ~ó‘Ô‚É‚·‚éD
463 */
464 p_almcb->almsta = false;
465
466 /*
467 * ƒAƒ‰[ƒ€ƒnƒ“ƒhƒ‰‚ðCCPUƒƒbƒN‰ðœó‘ԂŌĂяo‚·D
468 */
469 saved_ipm = i_get_ipm();
470 i_unlock_cpu();
471
472 LOG_ALM_ENTER(p_almcb);
473 (*((ALMHDR)(p_almcb->p_alminib->almhdr)))(p_almcb->p_alminib->exinf);
474 LOG_ALM_LEAVE(p_almcb);
475
476 if (!i_sense_lock()) {
477 i_lock_cpu();
478 }
479 i_set_ipm(saved_ipm);
480}
481
482#endif /* TOPPERS_almcal */
Note: See TracBrowser for help on using the repository browser.