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