]> begriffs open source - cmsis-freertos/blob - Demo/PIC32MZ_MPLAB/ISRTriggeredTask.c
osEventFlagsWait: Fix flag comparison
[cmsis-freertos] / Demo / PIC32MZ_MPLAB / ISRTriggeredTask.c
1 /*
2  * FreeRTOS Kernel V10.2.1
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29  * Interrupt service routines that cannot nest have no special requirements and
30  * can be written as per the compiler documentation. However interrupts written
31  * in this manner will utilise the stack of whichever task was interrupts,
32  * rather than the system stack, necessitating that adequate stack space be
33  * allocated to each created task. It is therefore not recommended to write
34  * interrupt service routines in this manner.
35  *
36  * Interrupts service routines that can nest require a simple assembly wrapper.
37  * This file is provided as a example of how this is done.
38  *
39  * The example in this file creates a single task.  The task blocks on a
40  * semaphore which is periodically 'given' from a timer interrupt.  The assembly
41  * wrapper for the interrupt is implemented in ISRTriggeredTask_isr.S.  The
42  * C function called by the assembly wrapper is implemented in this file.
43  *
44  * The task toggle LED mainISR_TRIGGERED_LED each time it is unblocked by the
45  * interrupt.
46  */
47
48
49 /* Standard includes. */
50 #include <stdio.h>
51
52 /* Scheduler includes. */
53 #include "FreeRTOS.h"
54 #include "task.h"
55 #include "semphr.h"
56
57 /* Standard demo includes. */
58 #include "ParTest.h"
59
60 /*-----------------------------------------------------------*/
61
62 /* The LED controlled by the ISR triggered task. */
63 #define mainISR_TRIGGERED_LED                           ( 1 )
64
65 /* Constants used to configure T5. */
66 #define mainT5PRESCALAR                                         ( 6 )
67 #define mainT5_SEMAPHORE_RATE                           ( 31250 )
68
69 /*-----------------------------------------------------------*/
70
71 /*
72  * The task that is periodically triggered by an interrupt, as described at the
73  * top of this file.
74  */
75 static void prvISRTriggeredTask( void* pvParameters );
76
77 /*
78  * Configures the T5 timer peripheral to generate the interrupts that unblock
79  * the task implemented by the prvISRTriggeredTask() function.
80  */
81 static void prvSetupT5( void );
82
83 /* The timer 5 interrupt handler.  As this interrupt uses the FreeRTOS assembly
84 entry point the IPL setting in the following function prototype has no effect. */
85 void __attribute__( (interrupt(IPL3AUTO), vector(_TIMER_5_VECTOR))) vT5InterruptWrapper( void );
86
87 /*-----------------------------------------------------------*/
88
89 /* The semaphore given by the T5 interrupt to unblock the task implemented by
90  the prvISRTriggeredTask() function. */
91 static SemaphoreHandle_t xBlockSemaphore = NULL;
92 /*-----------------------------------------------------------*/
93
94 void vStartISRTriggeredTask( void )
95 {
96         /* Create the task described at the top of this file.  The timer is
97         configured by the task itself. */
98         xTaskCreate( prvISRTriggeredTask,               /* The function that implements the task. */
99                                 "ISRt",                                         /* Text name to help debugging - not used by the kernel. */
100                                 configMINIMAL_STACK_SIZE,       /* The size of the stack to allocate to the task - defined in words, not bytes. */
101                                 NULL,                                           /* The parameter to pass into the task.  Not used in this case. */
102                                 configMAX_PRIORITIES - 1,       /* The priority at which the task is created. */
103                                 NULL );                                         /* Used to pass a handle to the created task out of the function.  Not used in this case. */
104 }
105 /*-----------------------------------------------------------*/
106
107 void vT5InterruptHandler( void )
108 {
109 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
110
111         /* This function is the handler for the peripheral timer interrupt.
112         The interrupt is initially signalled in a separate assembly file
113         which switches to the system stack and then calls this function.
114         It gives a semaphore which signals the prvISRBlockTask */
115
116         /* Give the semaphore.  If giving the semaphore causes the task to leave the
117         Blocked state, and the priority of the task is higher than the priority of
118         the interrupted task, then xHigherPriorityTaskWoken will be set to pdTRUE
119         inside the xSemaphoreGiveFromISR() function.  xHigherPriorityTaskWoken is
120         later passed into portEND_SWITCHING_ISR(), where a context switch is
121         requested if it is pdTRUE.  The context switch ensures the interrupt returns
122         directly to the unblocked task. */
123         xSemaphoreGiveFromISR( xBlockSemaphore, &xHigherPriorityTaskWoken );
124
125         /* Clear the interrupt */
126         IFS0CLR = _IFS0_T5IF_MASK;
127
128         /* See comment above the call to xSemaphoreGiveFromISR(). */
129         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
130 }
131 /*-----------------------------------------------------------*/
132
133 static void prvISRTriggeredTask( void* pvParameters )
134 {
135         /* Avoid compiler warnings. */
136         ( void ) pvParameters;
137
138         /* Create the semaphore used to signal this task */
139         xBlockSemaphore = xSemaphoreCreateBinary();
140
141         /* Configure the timer to generate the interrupts. */
142         prvSetupT5();
143
144         for( ;; )
145         {
146                 /* Block on the binary semaphore given by the T5 interrupt. */
147                 xSemaphoreTake( xBlockSemaphore, portMAX_DELAY );
148
149                 /* Toggle the LED. */
150                 vParTestToggleLED( mainISR_TRIGGERED_LED );
151         }
152 }
153 /*-----------------------------------------------------------*/
154
155 static void prvSetupT5( void )
156 {
157         /* Set up timer 5 to generate an interrupt every 50 ms */
158         T5CON = 0;
159         TMR5 = 0;
160         T5CONbits.TCKPS = mainT5PRESCALAR;
161         PR5 = mainT5_SEMAPHORE_RATE;
162
163         /* Setup timer 5 interrupt priority to be the maximum from which interrupt
164         safe FreeRTOS API functions can be called.  Interrupt safe FreeRTOS API
165         functions are those that end "FromISR". */
166         IPC6bits.T5IP = configMAX_SYSCALL_INTERRUPT_PRIORITY;
167
168         /* Clear the interrupt as a starting condition. */
169         IFS0bits.T5IF = 0;
170
171         /* Enable the interrupt. */
172         IEC0bits.T5IE = 1;
173
174         /* Start the timer. */
175         T5CONbits.TON = 1;
176 }