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