/
sw1sw2_interrupts_lab5.c
287 lines (221 loc) · 10.2 KB
/
sw1sw2_interrupts_lab5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
*********************************************************************************************************
*
* Micrium uC/OS-III on
* Freescale Kinetis K64
* on the
*
* Freescale FRDM-K64F
* Evaluation Board
*
*
*
* Create two tasks: 1) turns on/off red led when SW1 is pressed; 2) turns on/off green led when SW2 is pressed
* Use an ISR to respond to the triggering of the switches and use two distinct semaphores;
* leds can be on at the same time
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#include "fsl_interrupt_manager.h"
#include <math.h>
#include <lib_math.h>
#include <cpu_core.h>
#include <app_cfg.h>
#include <os.h>
#include <fsl_os_abstraction.h>
#include <system_MK64F12.h>
#include <board.h>
#include <bsp_ser.h>
#include <fsl_gpio_common.h> // externs g_PortBaseAddr needed in ISR
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
static OS_TCB AppTaskStartTCB;
static CPU_STK AppTaskStartStk[APP_CFG_TASK_START_STK_SIZE];
static OS_TCB TaskRedTCB;
static CPU_STK TaskRedStk[APP_CFG_TASK_START_STK_SIZE];
static OS_TCB TaskGreenTCB;
static CPU_STK TaskGreenStk[APP_CFG_TASK_START_STK_SIZE];
static OS_SEM MySem1;
static OS_SEM MySem2;
// static CPU_TS ts;
static OS_ERR os_err;
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static void AppTaskStart (void *p_arg);
static void AppTaskRed (void *p_arg);
static void AppTaskGreen (void *p_arg);
/*
*********************************************************************************************************
* main()
*********************************************************************************************************
*/
// handler associated to SW1 (labeled SW2 on board)
void SW1_Intr_Handler(void)
{
static uint32_t c_ifsr; // port c interrupt flag status register
uint32_t c_portBaseAddr = g_portBaseAddr[GPIO_EXTRACT_PORT(kGpioSW1)];
uint32_t portPinMask = (1 << GPIO_EXTRACT_PIN(kGpioSW1));
CPU_CRITICAL_ENTER(); // enter critical section (disable interrupts)
OSIntEnter(); // notify to scheduler the beginning of an ISR ("This allows ?C/OS-III to keep track of interrupt nesting")
c_ifsr = PORT_HAL_GetPortIntFlag(c_portBaseAddr); // get intr flag reg related to port C
if( (c_ifsr & portPinMask) ) // check if kGpioSW1 generated the interrupt [pin 6 -> 7th flag (flags start with index 0)]
{
//sem_sw1_post
OSSemPost(&MySem1,
OS_OPT_POST_1 + OS_OPT_POST_NO_SCHED,
&os_err);
}
GPIO_DRV_ClearPinIntFlag( kGpioSW1 );
CPU_CRITICAL_EXIT(); // renable interrupts
OSIntExit(); /* notify to scheduler the end of an ISR ("determines if a higher priority task is ready-to-run.
If so, the interrupt returns to the higher priority task instead of the interrupted task.") */
}
// handler associated to SW2 (labeled SW3 on board)
void SW2_Intr_Handler(void)
{
static uint32_t a_ifsr; // port a interrupt flag status register
uint32_t a_portBaseAddr = g_portBaseAddr[GPIO_EXTRACT_PORT(kGpioSW2)];
uint32_t portPinMask = (1 << GPIO_EXTRACT_PIN(kGpioSW2));
CPU_CRITICAL_ENTER(); // enter critical section (disable interrupts)
OSIntEnter(); // notify to scheduler the beginning of an ISR ("This allows ?C/OS-III to keep track of interrupt nesting")
a_ifsr = PORT_HAL_GetPortIntFlag(a_portBaseAddr); // get intr flag reg related to port A
if((a_ifsr & portPinMask))
{
//sem_sw2_post
OSSemPost(&MySem2,
OS_OPT_POST_1 + OS_OPT_POST_NO_SCHED,
&os_err);
}
GPIO_DRV_ClearPinIntFlag( kGpioSW2 );
CPU_CRITICAL_EXIT(); // renable interrupts
OSIntExit(); /* notify to scheduler the end of an ISR ("determines if a higher priority task is ready-to-run.
If so, the interrupt returns to the higher priority task instead of the interrupted task.") */
}
int main (void)
{
OS_ERR err;
#if (CPU_CFG_NAME_EN == DEF_ENABLED)
CPU_ERR cpu_err;
#endif
hardware_init();
GPIO_DRV_Init(switchPins, ledPins);
#if (CPU_CFG_NAME_EN == DEF_ENABLED)
CPU_NameSet((CPU_CHAR *)"MK64FN1M0VMD12",
(CPU_ERR *)&cpu_err);
#endif
OSA_Init(); /* Init uC/OS-III. */
OSSemCreate(&MySem1, /* Create Semaphore 1 */
"sem 1",
0,
&err);
OSSemCreate(&MySem2, /* Create Semaphore 2 */
"sem 2",
0,
&err);
INT_SYS_InstallHandler(PORTC_IRQn, SW1_Intr_Handler); // associate ISR with sw1 intr source
INT_SYS_InstallHandler(PORTA_IRQn, SW2_Intr_Handler); // associate ISR with sw2 intr source
OSTaskCreate(&AppTaskStartTCB, /* Create the start task */
"App Task Start",
AppTaskStart,
0u,
APP_CFG_TASK_START_PRIO,
&AppTaskStartStk[0u],
(APP_CFG_TASK_START_STK_SIZE / 10u),
APP_CFG_TASK_START_STK_SIZE,
0u,
0u,
0u,
(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR | OS_OPT_TASK_SAVE_FP),
&err);
OSA_Start(); /* Start multitasking (i.e. give control to uC/OS-III). */
while (DEF_ON) { /* Should Never Get Here */
;
}
}
/*
*********************************************************************************************************
* TASKS
*********************************************************************************************************
*/
static void AppTaskStart (void *p_arg)
{
OS_ERR os_err;
(void)p_arg;
CPU_Init(); /* Initialize the uC/CPU Services. */
Mem_Init(); /* Initialize the Memory Management Module */
Math_Init(); /* Initialize the Mathematical Module */
BSP_Ser_Init(115200u);
OSTaskCreate(&TaskRedTCB, /* Create the red task */
"App Task Red",
AppTaskRed,
0u,
APP_CFG_TASK_START_PRIO,
&TaskRedStk[0u],
(APP_CFG_TASK_START_STK_SIZE / 10u),
APP_CFG_TASK_START_STK_SIZE,
0u,
0u,
0u,
(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR | OS_OPT_TASK_SAVE_FP),
&os_err);
OSTaskCreate(&TaskGreenTCB, /* Create the green task */
"App Task Green",
AppTaskGreen,
0u,
APP_CFG_TASK_START_PRIO,
&TaskGreenStk[0u],
(APP_CFG_TASK_START_STK_SIZE / 10u),
APP_CFG_TASK_START_STK_SIZE,
0u,
0u,
0u,
(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR | OS_OPT_TASK_SAVE_FP),
&os_err);
while (DEF_TRUE) { /* Task body, always written as an infinite loop. */
}
}
static void AppTaskRed (void *p_arg)
{
OS_ERR os_err;
CPU_TS ts;
(void)p_arg;
APP_TRACE_DBG(("listening on kgpiosw1, toggling red led...\n\r"));
while (DEF_TRUE) { /* Task body, always written as an infinite loop. */
OSSemPend(&MySem1,
0,
OS_OPT_PEND_BLOCKING,
&ts,
&os_err);
GPIO_DRV_TogglePinOutput(BOARD_GPIO_LED_RED);
}
}
static void AppTaskGreen (void *p_arg)
{
OS_ERR os_err;
CPU_TS ts;
(void)p_arg;
APP_TRACE_DBG(("listening on kgpiosw2, toggling green led...\n\r"));
while (DEF_TRUE) { /* Task body, always written as an infinite loop. */
OSSemPend(&MySem2,
0,
OS_OPT_PEND_BLOCKING,
&ts,
&os_err);
GPIO_DRV_TogglePinOutput(BOARD_GPIO_LED_GREEN);
}
}