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