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  StatusType stat = ActivateTask(Task3);
35  assert(stat == E_OK);
36 
37  stat = TerminateTask();
38  assert(stat == E_OK);
39 }
40 
41 TASK(Task2)
42 {
43  StatusType stat = WaitEvent(0b10);
44  assert(stat == E_OK);
45 
46  stat = ClearEvent(0b10);
47  assert(stat == E_OK);
48 
49  stat = ActivateTask(Task4);
50  assert(stat == E_OK);
51 
52  stat = TerminateTask();
53  assert(stat == E_OK);
54 }
55 
56 TASK(Task3)
57 {
58  StatusType stat = SetRelAlarm(Alarm1, 1, 0);
59  assert(stat == E_OK);
60 
61  stat = IncrementCounter(Counter1);
62  assert(stat == E_OK);
63 
64  TaskStateType state = SUSPENDED;
65  stat = GetTaskState(Task2, &state);
66  assert(stat == E_OK && state == READY);
67 
68  stat = TerminateTask();
69  assert(stat == E_OK);
70 }
71 
72 TASK(Task4)
73 {
74  StatusType stat = SetRelAlarm(Alarm1, 1, 0);
75  assert(stat == E_OK);
76 
77  stat = IncrementCounter(Counter1);
78  assert(stat == E_OK);
79 
80  EventMaskType mask;
81  GetEvent(Task2, &mask);
82  assert(stat == E_OK);
83 
84  stat = TerminateTask();
85  assert(stat == E_OK);
86 }
87 
88 extern void StartupHook(void)
89 {
90  DDRB = 0xFF; // PB as output
91  PORTB = 0xFF; // keep all LEDs off
92 
93  DDRD = 0x00; // PD as input
94  PORTD = 0xFF; // enable PU on PD
95 
96 #if defined (__AVR_ATmega32__)
97 
98 #elif defined (__AVR_ATmega128__) || defined (__AVR_ATmega1284__)
99  /* Timer 2 */
100 #if defined (OS_CONFIG_SIM) && OS_CONFIG_SIM == true
101 #if defined (__AVR_ATmega128__)
102  TCCR2 = (1 << CS20); // Enable Timer2 with Prescaler 1
103  TIMSK |= 1 << TOIE2; // Enable Overflow Interrupt (Timer2)
104 #else /* defined (__AVR_ATmega128__) */
105  TCCR2B = (1 << CS20); // Enable Timer2 with Prescaler 1
106  TIMSK2 |= 1 << TOIE2; // Enable Overflow Interrupt (Timer2)
107 #endif /* defined (__AVR_ATmega128__) */
108 #endif /* defined (OS_CONFIG_SIM) && OS_CONFIG_SIM == true */
109 #else /* defined (__AVR_ATmega32__) */
110 #error Unknown CPU defined!
111 #endif /* defined (__AVR_ATmega32__) */
112 
113  uint8_t t = 0;
114 
115  while (t < 6) {
116  uint8_t r = PORTB;
117  r ^= (1 << 7);
118  PORTB = r;
119  DELAY_MS(50);
120  t++;
121  }
122 }
123 
124 extern void ShutdownHook(StatusType error)
125 {
126  DDRB = 0xFF; // PB as output
127  PORTB = 0xFF; // keep all LEDs off
128 
129  for (uint8_t i = 0; i < 11; i++) {
130  PORTB ^= 0xFF;
131  DELAY_MS(1000);
132  }
133 }
134 
135 extern void PreTaskHook(void)
136 {
137  TaskType task;
138  GetTaskID(&task);
139  TaskStateType state = SUSPENDED;
140  GetTaskState(task, &state);
141 }
142 
143 extern void PostTaskHook(void)
144 {
145  TaskType task;
146  GetTaskID(&task);
147  TaskStateType state = SUSPENDED;
148  GetTaskState(task, &state);
149 }
150 
151 extern void ErrorHook(StatusType error)
152 {
153 
154 }
155 
156 ISR(TIMER2_OVF_vect)
157 {
158 
159 }
TaskType
enum tasks_e TaskType
Type for task reference.
Definition: TaskTypes.h:29
EventMaskType
uint8_t EventMaskType
Data type of the event mask.
Definition: EventTypes.h:21
StatusType
enum StatusType_e StatusType
Type for status.
ErrorHook
void ErrorHook(StatusType error)
PostTask hook function.
Definition: App.c:784
StartupHook
void StartupHook(void)
Definition: App.c:702
ClearEvent
#define ClearEvent
Definition: OS_API.h:46
WaitEvent
#define WaitEvent
Definition: OS_API.h:48
GetTaskState
#define GetTaskState
Definition: OS_API.h:83
assert.h
Assert macros and functions.
IncrementCounter
#define IncrementCounter
Definition: OS_API.h:36
TASK
TASK(Idle)
Definition: App.c:33
E_OK
@ E_OK
Definition: Types.h:40
ISR
ISR(INT0_vect)
Definition: App.c:806
PreTaskHook
void PreTaskHook(void)
PreTask hook function.
Definition: App.c:768
ActivateTask
#define ActivateTask
Definition: OS_API.h:78
READY
@ READY
The task is ready to be scheduled.
Definition: TaskTypes.h:60
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.
SetRelAlarm
#define SetRelAlarm
Definition: OS_API.h:27
GetTaskID
#define GetTaskID
Definition: OS_API.h:82
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.
GetEvent
#define GetEvent
Definition: OS_API.h:47
DELAY_MS
#define DELAY_MS(ms)
Definition: App.c:24