]> rtime.felk.cvut.cz Git - arc.git/blob - system/kernel/counter.c
Updated testsystem. Added test suites. Added Autostart tests. Autotester with T32.
[arc.git] / system / kernel / counter.c
1 /* -------------------------------- Arctic Core ------------------------------
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com
3  *
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>
5  *
6  * This source code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by the
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  * -------------------------------- Arctic Core ------------------------------*/
15
16 #include <assert.h>
17 #include <stdlib.h>
18 #include "Os.h"
19 #include "internal.h"
20 #include "arc.h"
21 \r
22 #define COUNTER_STD_END         \\r
23                 goto ok;                \\r
24         err:                            \\r
25                 ERRORHOOK(rv);  \\r
26         ok:                                     \\r
27                 return rv;\r
28 \r
29 \r
30 /* Accessor functions */
31 #if ( OS_SC2 == STD_ON ) || ( OS_SC4 == STD_ON )\r
32 static inline OsSchTblAdjExpPointType *getAdjExpPoint( OsSchTblType *stblPtr ) {\r
33         return &stblPtr->adjExpPoint;\r
34 }
35 #endif
36 \r
37 \r
38 static inline const struct OsSchTblAutostart *getAutoStart( OsSchTblType *stblPtr ) {\r
39         return stblPtr->autostartPtr;\r
40 }\r
41
42 #if ( OS_SC2 == STD_ON ) || ( OS_SC4 == STD_ON )\r
43 static inline struct OsScheduleTableSync *getSync( OsSchTblType *stblPtr ) {\r
44         return &stblPtr->sync;\r
45 }\r
46 #endif\r
47
48
49 #define IsCounterValid(_counterId)   ((_counterId) <= Os_CfgGetCounterCnt())
50 \r
51 /**\r
52  *
53  * @param counter_id
54  * @return
55  */\r
56
57 /** @req OS399 */\r
58 StatusType IncrementCounter( CounterType counter_id ) {\r
59         StatusType rv = E_OK;\r
60         OsCounterType *cPtr;\r
61         cPtr = Os_CfgGetCounter(counter_id);
62
63         /** @req OS376 */
64         if( !IsCounterValid(counter_id) ) {
65                 rv = E_OS_ID;
66                 goto err;
67         }\r
68 \r
69         /* Check param */\r
70         /** @req OS285 */\r
71         if( ( cPtr->type != COUNTER_TYPE_SOFT ) ||\r
72                 ( counter_id >= Os_CfgGetCounterCnt() ) ) {\r
73                 rv =  E_OS_ID;\r
74                 goto err;\r
75         }\r
76
77         /** @req OS286 */\r
78         cPtr->val = Os_CounterAdd( cPtr->val, Os_CounterGetMaxValue(cPtr), 1 );\r
79
80         Os_AlarmCheck(cPtr);\r
81         Os_SchTblCheck(cPtr);\r
82
83         /** @req OS321 */\r
84         COUNTER_STD_END;\r
85 }\r
86 \r
87
88 /** @req OS383 */\r
89 StatusType GetCounterValue( CounterType counter_id , TickRefType tick_ref)\r
90 {
91         StatusType rv = E_OK;\r
92         OsCounterType *cPtr;\r
93         cPtr = Os_CfgGetCounter(counter_id);
94
95         /** @req OS376 */
96         if( !IsCounterValid(counter_id) ) {
97                 rv = E_OS_ID;
98                 goto err;
99         }
100
101         /** @req OS377 */
102         if( cPtr->type == COUNTER_TYPE_HARD ) {
103                 if( cPtr->driver == NULL ) {
104                         /* It's OSINTERNAL */
105                         *tick_ref = os_sys.tick;
106                 } else {
107 #if 0
108                 /* We support only GPT for now */
109                 *tick_ref  = (TickType)Gpt_GetTimeElapsed(cPtr->driver.OsGptChannelRef);
110 #endif
111
112                 }
113         } else {
114                 *tick_ref = cPtr->val;
115         }
116 \r
117         COUNTER_STD_END;
118 }\r
119
120 /**
121  *
122  * @param counter_id            The counter to be read
123  * @param val[in,out]           in,  The previously read tick value of the counter
124  *                                                      out, Contains the current tick value of the counter.
125  * @param elapsed_val[out]  The difference
126  * @return
127  */
128
129 /** @req OS392 */\r
130 StatusType GetElapsedCounterValue( CounterType counter_id, TickRefType val, TickRefType elapsed_val)\r
131 {
132         StatusType rv = E_OK;\r
133         OsCounterType *cPtr;
134         TickType currTick = 0;
135         TickType max;
136
137         cPtr = Os_CfgGetCounter(counter_id);
138
139         /** @req OS381 */
140         if( !IsCounterValid(counter_id) ) {
141                 rv = E_OS_ID;
142                 goto err;
143         }
144
145         max = Os_CounterGetMaxValue(cPtr);
146         /** @req OS391 */
147         if( *val > max ) {
148                 rv = E_OS_VALUE;
149                 goto err;
150         }
151
152         GetCounterValue(counter_id,&currTick);
153
154         /** @req OS382 */
155         *elapsed_val = Os_CounterDiff(currTick,*val,max);
156
157         /** @req OS460 */
158         *val = currTick;
159
160         COUNTER_STD_END;
161 }\r
162 \r
163 /*\r
164  * The OsTick():\r
165  * 1. The Decrementer is setup by Os_SysTickStart(period_ticks)\r
166  * 2. Os_SysTickInit() setup INTC[7] to trigger OsTick\r
167  * 3. OsTick() then increment counter os_tick_counter if used
168  */\r
169 \r
170 /*\r
171  * Non-Autosar stuff\r
172  */\r
173
174 /* The id of the counter driven by the os tick, or -1 if not used.
175  * Using weak linking to set default value -1 if not set by config.
176  */
177 CounterType Os_Arc_OsTickCounter __attribute__((weak)) = -1;
178 \r
179 void OsTick( void ) {
180         // if not used, os_tick_counter < 0
181         if (Os_Arc_OsTickCounter >= 0) {
182
183                 OsCounterType *cPtr = Os_CfgGetCounter(Os_Arc_OsTickCounter);
184
185                 os_sys.tick++;
186
187                 cPtr->val = Os_CounterAdd( cPtr->val, Os_CounterGetMaxValue(cPtr), 1 );
188
189         //      os_sys.tick = cPtr->val;
190
191                 Os_AlarmCheck(cPtr);
192                 Os_SchTblCheck(cPtr);
193         }
194 }\r
195 \r
196 TickType GetOsTick( void ) {\r
197         return get_os_tick();\r
198 }\r
199
200
201 /**
202  * Initialize alarms and schedule-tables for the counters
203  */
204 void Os_CounterInit( void ) {
205         OsCounterType *cPtr;
206         OsAlarmType *aPtr;
207         OsSchTblType *sPtr;
208
209         /* Add the alarms to counters */
210         for(int i=0; i < Os_CfgGetAlarmCnt(); i++ ) {
211                 aPtr = Os_CfgGetAlarmObj(i);
212                 cPtr = aPtr->counter;
213                 SLIST_INSERT_HEAD(&cPtr->alarm_head,aPtr, alarm_list);
214         }
215
216         /* Add the schedule tables to counters */
217         for(int i=0; i < Os_CfgGetSchedCnt(); i++ ) {
218
219                 sPtr = Os_CfgGetSched(i);
220                 cPtr = sPtr->counter;
221                 SLIST_INSERT_HEAD(&cPtr->sched_head, sPtr, sched_list);
222         }
223 }
224 \r