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