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