]> rtime.felk.cvut.cz Git - arc.git/blob - common/sleep.c
EcuM: More watchdog stuff
[arc.git] / common / sleep.c
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 #include "sleep.h"\r
17 #include "Mcu.h"\r
18 #include "Os.h"\r
19 \r
20 struct timeoutlist_t{\r
21         uint32_t timeout;\r
22         boolean active;\r
23         EventMaskType mask;\r
24 };\r
25 static struct timeoutlist_t timeoutlist[OS_TASK_CNT];\r
26 \r
27 static uint32_t ticks = 0;\r
28 \r
29 void SleepInit()\r
30 {\r
31         uint32_t i;\r
32 \r
33         for(i=0;i<OS_TASK_CNT;i++)\r
34         {\r
35                 timeoutlist[i].active = FALSE;\r
36         }\r
37 }\r
38 \r
39 void Sleep(uint32_t nofTicks, TaskType TaskID, EventMaskType Mask )\r
40 {\r
41         imask_t state;\r
42     Irq_Save(state);\r
43         if(nofTicks == 0){\r
44                 nofTicks=1;\r
45         }\r
46     if(TaskID < OS_TASK_CNT){\r
47         timeoutlist[TaskID].timeout = ticks + nofTicks;\r
48         timeoutlist[TaskID].active = TRUE;\r
49         timeoutlist[TaskID].mask = Mask;\r
50     }else{\r
51         /* Error */\r
52         ErrorHook(E_OS_LIMIT);\r
53     }\r
54     Irq_Restore(state);\r
55 }\r
56 \r
57 \r
58 void SleepTask(void)\r
59 {\r
60         uint32_t i;\r
61         for(;;) {\r
62                 // Alarms every tick\r
63                 WaitEvent(EVENT_MASK_SLEEP_ALARM_TASK);\r
64                 ClearEvent(EVENT_MASK_SLEEP_ALARM_TASK);\r
65 \r
66                 ticks++;\r
67 \r
68                 for(i=0;i<OS_TASK_CNT;i++)\r
69                 {\r
70                         if((timeoutlist[i].active == TRUE) && (timeoutlist[i].timeout == ticks))\r
71                         {\r
72                                 timeoutlist[i].active = FALSE;\r
73                                 SetEvent(i,timeoutlist[i].mask);\r
74                         }\r
75                 }\r
76         }\r
77 \r
78 }\r