AutosarOS
App.c
Go to the documentation of this file.
1 
14 #include "assert.h"
15 
16 #include "OS_API.h"
17 
18 #include <avr/io.h>
19 #include <util/delay.h>
20 
21 #if defined (OS_CONFIG_SIM) && OS_CONFIG_SIM == true
22 #define DELAY_MS(ms)
23 #else
24 #define DELAY_MS(ms) _delay_ms(ms)
25 #endif /* defined (OS_CONFIG_SIM) && OS_CONFIG_SIM == true */
26 
27 TASK(Idle)
28 {
29  while (1);
30 }
31 
32 TASK(Task1)
33 {
34  AlarmBaseType base;
35  StatusType stat = GetAlarmBase(Alarm1, &base);
36  assert(stat == E_OK);
37 
38  TickType tick = 0;
39  stat = GetAlarm(Alarm1, &tick);
40  assert(stat == E_OS_NOFUNC);
41 
42  stat = CancelAlarm(Alarm1);
43  assert(stat == E_OS_NOFUNC);
44 
45  stat = SetAbsAlarm(Alarm1, 1, 1);
46  assert(stat == E_OK);
47 
48  stat = SetAbsAlarm(Alarm1, 3, 0);
49  assert(stat == E_OS_STATE);
50 
51  stat = IncrementCounter(Counter1);
52  assert(stat == E_OK);
53 
54  stat = Schedule();
55  assert(stat == E_OK);
56 
57  stat = IncrementCounter(Counter1);
58  assert(stat == E_OK);
59 
60  stat = Schedule();
61  assert(stat == E_OK);
62 
63  stat = CancelAlarm(Alarm1);
64  assert(stat == E_OK);
65 
66  stat = SetRelAlarm(Alarm1, 1, 0);
67  assert(stat == E_OK);
68 
69  stat = SetRelAlarm(Alarm1, 2, 0);
70  assert(stat == E_OS_STATE);
71 
72  stat = GetAlarm(Alarm1, &tick);
73  assert(stat == E_OK && tick == 1);
74 
75  stat = IncrementCounter(Counter1);
76  assert(stat == E_OK);
77 
78  stat = Schedule();
79  assert(stat == E_OK);
80 
81  stat = TerminateTask();
82  assert(stat == E_OK);
83 }
84 
85 TASK(Task2)
86 {
87  StatusType stat = TerminateTask();
88  assert(stat == E_OK);
89 }
90 
91 extern void StartupHook(void)
92 {
93  DDRB = 0xFF; // PB as output
94  PORTB = 0xFF; // keep all LEDs off
95 
96  DDRD = 0x00; // PD as input
97  PORTD = 0xFF; // enable PU on PD
98 
99 #if defined (__AVR_ATmega32__)
100 
101 #elif defined (__AVR_ATmega128__) || defined (__AVR_ATmega1284__)
102  /* Timer 2 */
103 #if defined (OS_CONFIG_SIM) && OS_CONFIG_SIM == true
104 #if defined (__AVR_ATmega128__)
105  TCCR2 = (1 << CS20); // Enable Timer2 with Prescaler 1
106  TIMSK |= 1 << TOIE2; // Enable Overflow Interrupt (Timer2)
107 #else /* defined (__AVR_ATmega128__) */
108  TCCR2B = (1 << CS20); // Enable Timer2 with Prescaler 1
109  TIMSK2 |= 1 << TOIE2; // Enable Overflow Interrupt (Timer2)
110 #endif /* defined (__AVR_ATmega128__) */
111 #endif /* defined (OS_CONFIG_SIM) && OS_CONFIG_SIM == true */
112 #else /* defined (__AVR_ATmega32__) */
113 #error Unknown CPU defined!
114 #endif /* defined (__AVR_ATmega32__) */
115 
116  uint8_t t = 0;
117 
118  while (t < 6) {
119  uint8_t r = PORTB;
120  r ^= (1 << 7);
121  PORTB = r;
122  DELAY_MS(50);
123  t++;
124  }
125 }
126 
127 extern void ShutdownHook(StatusType error)
128 {
129  DDRB = 0xFF; // PB as output
130  PORTB = 0xFF; // keep all LEDs off
131 
132  for (uint8_t i = 0; i < 11; i++) {
133  PORTB ^= 0xFF;
134  DELAY_MS(1000);
135  }
136 }
137 
138 extern void PreTaskHook(void)
139 {
140  TaskType task;
141  GetTaskID(&task);
142  TaskStateType state = SUSPENDED;
143  GetTaskState(task, &state);
144 }
145 
146 extern void PostTaskHook(void)
147 {
148  TaskType task;
149  GetTaskID(&task);
150  TaskStateType state = SUSPENDED;
151  GetTaskState(task, &state);
152 }
153 
154 extern void ErrorHook(StatusType error)
155 {
156 
157 }
158 
159 ISR(TIMER2_OVF_vect)
160 {
161 
162 }
TaskType
enum tasks_e TaskType
Type for task reference.
Definition: TaskTypes.h:29
StatusType
enum StatusType_e StatusType
Type for status.
AlarmBaseType
volatile struct counter_s AlarmBaseType
Type for alarm base.
Definition: AlarmTypes.h:39
ErrorHook
void ErrorHook(StatusType error)
PostTask hook function.
Definition: App.c:784
Schedule
#define Schedule
Definition: OS_API.h:81
GetAlarmBase
#define GetAlarmBase
Definition: OS_API.h:25
E_OS_NOFUNC
@ E_OS_NOFUNC
Definition: Types.h:45
TickType
uint64_t TickType
Data type of counter values.
Definition: CounterTypes.h:21
StartupHook
void StartupHook(void)
Definition: App.c:702
GetTaskState
#define GetTaskState
Definition: OS_API.h:83
assert.h
Assert macros and functions.
IncrementCounter
#define IncrementCounter
Definition: OS_API.h:36
SetAbsAlarm
#define SetAbsAlarm
Definition: OS_API.h:28
TASK
TASK(Idle)
Definition: App.c:33
E_OK
@ E_OK
Definition: Types.h:40
DELAY_MS
#define DELAY_MS(ms)
Definition: App.c:24
ISR
ISR(INT0_vect)
Definition: App.c:806
PreTaskHook
void PreTaskHook(void)
PreTask hook function.
Definition: App.c:768
assert
#define assert(expression)
Definition: assert.h:37
SUSPENDED
@ SUSPENDED
The task is suspended and will not be scheduled.
Definition: TaskTypes.h:58
TerminateTask
#define TerminateTask
Definition: OS_API.h:80
OS_API.h
Operating System API.
CancelAlarm
#define CancelAlarm
Definition: OS_API.h:29
SetRelAlarm
#define SetRelAlarm
Definition: OS_API.h:27
GetAlarm
#define GetAlarm
Definition: OS_API.h:26
GetTaskID
#define GetTaskID
Definition: OS_API.h:82
E_OS_STATE
@ E_OS_STATE
Definition: Types.h:47
PostTaskHook
void PostTaskHook(void)
PostTask hook function.
Definition: App.c:776
ShutdownHook
void ShutdownHook(StatusType error)
Definition: App.c:757
TaskStateType
enum OsTaskState_e TaskStateType
Task state.