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