AutosarOS
Counter.c
Go to the documentation of this file.
1 
14 #include "Counter.h"
15 #include "ScheduleTables.h"
16 #include "OCB.h"
17 #include "OS_API.h"
18 
19 #include <util/atomic.h>
20 
22 {
23  OS_SET_ERROR_INFO1(OSServiceId_IncrementCounter, &counterID, sizeof(counterID));
24 
25  if (OS_EXTENDED && counterID >= INVALID_COUNTER) {
27 
28  return E_OS_ID;
29  }
30 
31  if (OS_EXTENDED && Counter_Cfg[counterID]->type == HARDWARE) {
33 
34  return E_OS_ID;
35  }
36 
37  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
38  if (Counter_Cfg[counterID]->value + 1 > Counter_Cfg[counterID]->maxallowedvalue) {
39  Counter_Cfg[counterID]->value = 0;
40  } else {
41  Counter_Cfg[counterID]->value += 1;
42  }
43  }
44 
45  Alarm_evaluateAlarm(counterID);
46  ScheduleTable_handleTick(counterID);
47 
48  return E_OK;
49 }
50 
52 {
53  OS_SET_ERROR_INFO2(OSServiceId_GetCounterValue, &counterID, sizeof(counterID), &value, sizeof(value));
54 
55  if (OS_EXTENDED && counterID >= INVALID_COUNTER) {
57 
58  return E_OS_ID;
59  }
60 
61  if (OS_EXTENDED && value == NULL) {
63 
64  return E_OS_PARAM_POINTER;
65  }
66 
67  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
68  if (counterID == SYSTEM_COUNTER) {
69  *value = sysTick;
70  } else {
71  *value = Counter_Cfg[counterID]->value;
72  }
73  }
74 
75  return E_OK;
76 }
77 
79 {
80  OS_SET_ERROR_INFO3(OSServiceId_GetElapsedValue, &counterID, sizeof(counterID), &value, sizeof(value), &elapsedValue,
81  sizeof(elapsedValue));
82 
83  if (OS_EXTENDED && counterID >= INVALID_COUNTER) {
85 
86  return E_OS_ID;
87  }
88 
89  if (OS_EXTENDED && (value == NULL || elapsedValue == NULL)) {
91 
92  return E_OS_PARAM_POINTER;
93  }
94 
95  if (OS_EXTENDED && *value > Counter_Cfg[counterID]->maxallowedvalue) {
97 
98  return E_OS_VALUE;
99  }
100 
101  TickType currentValue = 0;
102 
103  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
104  if (counterID == SYSTEM_COUNTER) {
105  currentValue = sysTick;
106  } else {
107  currentValue = Counter_Cfg[counterID]->value;
108  }
109  }
110 
111  if (currentValue >= *value) {
112  *elapsedValue = currentValue - *value;
113  } else {
114  *elapsedValue = Counter_Cfg[counterID]->maxallowedvalue - *value + currentValue + 1;
115  }
116 
117  *value = currentValue;
118 
119  return E_OK;
120 }
StatusType
enum StatusType_e StatusType
Type for status.
HARDWARE
@ HARDWARE
Counter is implemented in hardware.
Definition: CounterTypes.h:39
ScheduleTables.h
Schedule tables management.
TickType
uint64_t TickType
Data type of counter values.
Definition: CounterTypes.h:21
TickRefType
TickType * TickRefType
Reference to counter values.
Definition: CounterTypes.h:28
CounterType
enum counters_e CounterType
Type for counter reference.
Definition: CounterTypes.h:33
sysTick
volatile uint32_t sysTick
Current system tick.
Definition: OCB.c:25
E_OK
@ E_OK
Definition: Types.h:40
OS_SET_ERROR_INFO1
#define OS_SET_ERROR_INFO1(serviceId, paramPtr1, size1)
Set error info with up to one parameter.
Definition: ErrorTypes.h:220
ScheduleTable_handleTick
void ScheduleTable_handleTick(CounterType counter)
Handle tick.
Definition: ScheduleTables.c:248
counter_s::maxallowedvalue
const TickType maxallowedvalue
Maximum allowed value of counter.
Definition: CounterTypes.h:47
Counter_IncrementCounter
StatusType Counter_IncrementCounter(CounterType counterID)
Increment counter.
Definition: Counter.c:21
OSServiceId_IncrementCounter
@ OSServiceId_IncrementCounter
Definition: ErrorTypes.h:29
Alarm_evaluateAlarm
void Alarm_evaluateAlarm(CounterType counter)
Evaluate alarms with user generated counter.
Definition: Alarm.c:211
Counter_GetCounterValue
StatusType Counter_GetCounterValue(CounterType counterID, TickRefType value)
Read current counter value.
Definition: Counter.c:51
OS_API.h
Operating System API.
counter_s::value
TickType value
Current tick value of counter.
Definition: CounterTypes.h:52
OS_EXTENDED
#define OS_EXTENDED
Definition: OCB.h:156
OS_SET_ERROR_INFO3
#define OS_SET_ERROR_INFO3(serviceId, paramPtr1, size1, paramPtr2, size2, paramPtr3, size3)
Set error info with up to three parameters.
Definition: ErrorTypes.h:250
E_OS_ID
@ E_OS_ID
Definition: Types.h:43
Counter.h
Counter management.
Counter_Cfg
volatile struct counter_s * Counter_Cfg[]
Current counter control blocks.
OS_SET_ERROR_INFO2
#define OS_SET_ERROR_INFO2(serviceId, paramPtr1, size1, paramPtr2, size2)
Set error info with up to two parameters.
Definition: ErrorTypes.h:234
E_OS_PARAM_POINTER
@ E_OS_PARAM_POINTER
Definition: Types.h:50
OCB.h
Operating System Control Block.
OS_CALL_ERROR_HOOK
#define OS_CALL_ERROR_HOOK(error)
Call error hook if configured.
Definition: ErrorTypes.h:93
OSServiceId_GetCounterValue
@ OSServiceId_GetCounterValue
Definition: ErrorTypes.h:30
OSServiceId_GetElapsedValue
@ OSServiceId_GetElapsedValue
Definition: ErrorTypes.h:31
Counter_GetElapsedValue
StatusType Counter_GetElapsedValue(CounterType counterID, TickRefType value, TickRefType elapsedValue)
Get elapse counter value.
Definition: Counter.c:78
E_OS_VALUE
@ E_OS_VALUE
Definition: Types.h:48