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 = SetEvent(INVALID_TASK, 0b1);
37  assert(stat == E_OS_ID);
38 
39  stat = SetEvent(Task1, 0b1);
40  assert(stat == E_OS_ACCESS);
41 
42  stat = SetEvent(Task2, 0b1);
43  assert(stat == E_OS_STATE);
44 
45  stat = ClearEvent(0b1);
46  assert(stat == E_OS_ACCESS);
47 
48  while (!isrFlag) {
49  ;
50  }
51 
52  EventMaskType mask = 0;
53  stat = GetEvent(INVALID_TASK, &mask);
54  assert(stat == E_OS_ID);
55 
56  stat = GetEvent(Task1, &mask);
57  assert(stat == E_OS_ACCESS);
58 
59  stat = GetEvent(Task2, &mask);
60  assert(stat == E_OS_STATE);
61 
62  stat = WaitEvent(0b1);
63  assert(stat == E_OS_ACCESS);
64 
65  stat = ChainTask(Task2);
66  assert(stat == E_OK);
67 }
68 
69 TASK(Task2)
70 {
71  StatusType stat = GetResource(Res1);
72  assert(stat == E_OK);
73 
74  stat = WaitEvent(0b1);
75  assert(stat == E_OS_RESOURCE);
76 
77  stat = ReleaseResource(Res1);
78  assert(stat == E_OK);
79 
80  stat = TerminateTask();
81  assert(stat == E_OK);
82 }
83 
84 extern void StartupHook(void)
85 {
86  DDRB = 0xFF; // PB as output
87  PORTB = 0xFF; // keep all LEDs off
88 
89  DDRD = 0x00; // PD as input
90  PORTD = 0xFF; // enable PU on PD
91 
92 #if defined (__AVR_ATmega32__)
93  GICR = 1 << INT0; // Enable INT0 and INT1
94  MCUCR = 1 << ISC01 | 0 << ISC00; // Trigger INT0 and INT1 on falling edge
95 #elif defined (__AVR_ATmega128__) || defined (__AVR_ATmega1284__)
96  EICRA = 1 << ISC01 | 0 << ISC00; // Trigger INT0 and INT1 on falling edge
97  EIMSK |= 1 << INT0; // Enable INT0 and INT1
98 
99  /* Timer 2 */
100 #if defined (OS_CONFIG_SIM) && OS_CONFIG_SIM == true
101 #if defined (__AVR_ATmega128__)
102  TCCR2 = (1 << CS20); // Enable Timer2 with Prescaler 1
103  TIMSK |= 1 << TOIE2; // Enable Overflow Interrupt (Timer2)
104 #else /* defined (__AVR_ATmega128__) */
105  TCCR2B = (1 << CS20); // Enable Timer2 with Prescaler 1
106  TIMSK2 |= 1 << TOIE2; // Enable Overflow Interrupt (Timer2)
107 #endif /* defined (__AVR_ATmega128__) */
108 #endif /* defined (OS_CONFIG_SIM) && OS_CONFIG_SIM == true */
109 #else /* defined (__AVR_ATmega32__) */
110 #error Unknown CPU defined!
111 #endif /* defined (__AVR_ATmega32__) */
112 
113  uint8_t t = 0;
114 
115  while (t < 6) {
116  uint8_t r = PORTB;
117  r ^= (1 << 7);
118  PORTB = r;
119  DELAY_MS(50);
120  t++;
121  }
122 }
123 
124 extern void ShutdownHook(StatusType error)
125 {
126  DDRB = 0xFF; // PB as output
127  PORTB = 0xFF; // keep all LEDs off
128 
129  for (uint8_t i = 0; i < 11; i++) {
130  PORTB ^= 0xFF;
131  DELAY_MS(1000);
132  }
133 }
134 
135 extern void PreTaskHook(void)
136 {
137  TaskType task;
138  GetTaskID(&task);
139  TaskStateType state = SUSPENDED;
140  GetTaskState(task, &state);
141 }
142 
143 extern void PostTaskHook(void)
144 {
145  TaskType task;
146  GetTaskID(&task);
147  TaskStateType state = SUSPENDED;
148  GetTaskState(task, &state);
149 }
150 
151 extern void ErrorHook(StatusType error)
152 {
153 
154 }
155 
156 ISR(INT0_vect)
157 {
158  assert(isISR && isCat2ISR);
159 
160  StatusType stat = ClearEvent(0b1);
161  assert(stat == E_OS_CALLLEVEL);
162 
163  stat = WaitEvent(0b1);
164  assert(stat == E_OS_CALLLEVEL);
165 
166  isrFlag = true;
167 }
168 
169 ISR(TIMER2_OVF_vect)
170 {
171 
172 }
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
StartupHook
void StartupHook(void)
Definition: App.c:702
ClearEvent
#define ClearEvent
Definition: OS_API.h:46
WaitEvent
#define WaitEvent
Definition: OS_API.h:48
E_OS_RESOURCE
@ E_OS_RESOURCE
Definition: Types.h:46
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
SetEvent
#define SetEvent
Definition: OS_API.h:45
E_OS_CALLLEVEL
@ E_OS_CALLLEVEL
Definition: Types.h:42
E_OS_ACCESS
@ E_OS_ACCESS
Definition: Types.h:41
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
isISR
volatile bool isISR
Is currently ISR context?
Definition: OCB.c:20
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
ReleaseResource
#define ReleaseResource
Definition: OS_API.h:71
E_OS_STATE
@ E_OS_STATE
Definition: Types.h:47
E_OS_ID
@ E_OS_ID
Definition: Types.h:43
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.
GetResource
#define GetResource
Definition: OS_API.h:70
GetEvent
#define GetEvent
Definition: OS_API.h:47