]> rtime.felk.cvut.cz Git - arc.git/blob - common/sleep.c
Added checks for the maximum number of ISRs. New vectors does NOT install over old...
[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         uint32 pval = McuE_EnterCriticalSection();\r
42         if(nofTicks == 0){\r
43                 nofTicks=1;\r
44         }\r
45     if(TaskID < OS_TASK_CNT){\r
46         timeoutlist[TaskID].timeout = ticks + nofTicks;\r
47         timeoutlist[TaskID].active = TRUE;\r
48         timeoutlist[TaskID].mask = Mask;\r
49     }else{\r
50         /* Error */\r
51         ErrorHook(E_OS_LIMIT);\r
52     }\r
53         McuE_ExitCriticalSection(pval);\r
54 }\r
55 \r
56 \r
57 void SleepTask(void)\r
58 {\r
59         uint32_t i;\r
60         for(;;) {\r
61                 // Alarms every tick\r
62                 WaitEvent(EVENT_MASK_SLEEP_ALARM_TASK);\r
63                 ClearEvent(EVENT_MASK_SLEEP_ALARM_TASK);\r
64 \r
65                 ticks++;\r
66 \r
67                 for(i=0;i<OS_TASK_CNT;i++)\r
68                 {\r
69                         if((timeoutlist[i].active == TRUE) && (timeoutlist[i].timeout == ticks))\r
70                         {\r
71                                 timeoutlist[i].active = FALSE;\r
72                                 SetEvent(i,timeoutlist[i].mask);\r
73                         }\r
74                 }\r
75         }\r
76 \r
77 }\r