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