]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MZ_MPLAB/ISRTriggeredTask.c
d58ec8c318abad43f66b9a4e07e20138902139b5
[freertos] / FreeRTOS / Demo / PIC32MZ_MPLAB / ISRTriggeredTask.c
1 /*\r
2     FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * Interrupt service routines that cannot nest have no special requirements and\r
68  * can be written as per the compiler documentation. However interrupts written\r
69  * in this manner will utilise the stack of whichever task was interrupts,\r
70  * rather than the system stack, necessitating that adequate stack space be\r
71  * allocated to each created task. It is therefore not recommended to write\r
72  * interrupt service routines in this manner.\r
73  *\r
74  * Interrupts service routines that can nest require a simple assembly wrapper.\r
75  * This file is provided as a example of how this is done.\r
76  *\r
77  * The example in this file creates a single task.  The task blocks on a\r
78  * semaphore which is periodically 'given' from a timer interrupt.  The assembly\r
79  * wrapper for the interrupt is implemented in ISRTriggeredTask_isr.S.  The\r
80  * C function called by the assembly wrapper is implemented in this file.\r
81  *\r
82  * The task toggle LED mainISR_TRIGGERED_LED each time it is unblocked by the\r
83  * interrupt.\r
84  */\r
85 \r
86 \r
87 /* Standard includes. */\r
88 #include <stdio.h>\r
89 \r
90 /* Scheduler includes. */\r
91 #include "FreeRTOS.h"\r
92 #include "task.h"\r
93 #include "semphr.h"\r
94 \r
95 /* Standard demo includes. */\r
96 #include "ParTest.h"\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 /* The LED controlled by the ISR triggered task. */\r
101 #define mainISR_TRIGGERED_LED                           ( 1 )\r
102 \r
103 /* Constants used to configure T5. */\r
104 #define mainT5PRESCALAR                                         ( 6 )\r
105 #define mainT5_SEMAPHORE_RATE                           ( 31250 )\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /*\r
110  * The task that is periodically triggered by an interrupt, as described at the\r
111  * top of this file.\r
112  */\r
113 static void prvISRTriggeredTask( void* pvParameters );\r
114 \r
115 /*\r
116  * Configures the T5 timer peripheral to generate the interrupts that unblock\r
117  * the task implemented by the prvISRTriggeredTask() function.\r
118  */\r
119 static void prvSetupT5( void );\r
120 \r
121 /* The timer 5 interrupt handler.  As this interrupt uses the FreeRTOS assembly\r
122 entry point the IPL setting in the following function prototype has no effect. */\r
123 void __attribute__( (interrupt(ipl3), vector(_TIMER_5_VECTOR))) vT5InterruptWrapper( void );\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /* The semaphore given by the T5 interrupt to unblock the task implemented by\r
128  the prvISRTriggeredTask() function. */\r
129 static SemaphoreHandle_t xBlockSemaphore = NULL;\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 void vStartISRTriggeredTask( void )\r
133 {\r
134         /* Create the task described at the top of this file.  The timer is\r
135         configured by the task itself. */\r
136         xTaskCreate( prvISRTriggeredTask,               /* The function that implements the task. */\r
137                                 "ISRt",                                         /* Text name to help debugging - not used by the kernel. */\r
138                                 configMINIMAL_STACK_SIZE,       /* The size of the stack to allocate to the task - defined in words, not bytes. */\r
139                                 NULL,                                           /* The parameter to pass into the task.  Not used in this case. */\r
140                                 configMAX_PRIORITIES - 1,       /* The priority at which the task is created. */\r
141                                 NULL );                                         /* Used to pass a handle to the created task out of the function.  Not used in this case. */\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 void vT5InterruptHandler( void )\r
146 {\r
147 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
148 \r
149         /* This function is the handler for the peripheral timer interrupt.\r
150         The interrupt is initially signalled in a separate assembly file\r
151         which switches to the system stack and then calls this function.\r
152         It gives a semaphore which signals the prvISRBlockTask */\r
153 \r
154         /* Give the semaphore.  If giving the semaphore causes the task to leave the\r
155         Blocked state, and the priority of the task is higher than the priority of\r
156         the interrupted task, then xHigherPriorityTaskWoken will be set to pdTRUE\r
157         inside the xSemaphoreGiveFromISR() function.  xHigherPriorityTaskWoken is\r
158         later passed into portEND_SWITCHING_ISR(), where a context switch is\r
159         requested if it is pdTRUE.  The context switch ensures the interrupt returns\r
160         directly to the unblocked task. */\r
161         xSemaphoreGiveFromISR( xBlockSemaphore, &xHigherPriorityTaskWoken );\r
162 \r
163         /* Clear the interrupt */\r
164         IFS0CLR = _IFS0_T5IF_MASK;\r
165 \r
166         /* See comment above the call to xSemaphoreGiveFromISR(). */\r
167         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 static void prvISRTriggeredTask( void* pvParameters )\r
172 {\r
173         /* Avoid compiler warnings. */\r
174         ( void ) pvParameters;\r
175 \r
176         /* Create the semaphore used to signal this task */\r
177         xBlockSemaphore = xSemaphoreCreateBinary();\r
178 \r
179         /* Configure the timer to generate the interrupts. */\r
180         prvSetupT5();\r
181 \r
182         for( ;; )\r
183         {\r
184                 /* Block on the binary semaphore given by the T5 interrupt. */\r
185                 xSemaphoreTake( xBlockSemaphore, portMAX_DELAY );\r
186 \r
187                 /* Toggle the LED. */\r
188                 vParTestToggleLED( mainISR_TRIGGERED_LED );\r
189         }\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 static void prvSetupT5( void )\r
194 {\r
195         /* Set up timer 5 to generate an interrupt every 50 ms */\r
196         T5CON = 0;\r
197         TMR5 = 0;\r
198         T5CONbits.TCKPS = mainT5PRESCALAR;\r
199         PR5 = mainT5_SEMAPHORE_RATE;\r
200 \r
201         /* Setup timer 5 interrupt priority to be the maximum from which interrupt\r
202         safe FreeRTOS API functions can be called.  Interrupt safe FreeRTOS API\r
203         functions are those that end "FromISR". */\r
204         IPC6bits.T5IP = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
205 \r
206         /* Clear the interrupt as a starting condition. */\r
207         IFS0bits.T5IF = 0;\r
208 \r
209         /* Enable the interrupt. */\r
210         IEC0bits.T5IE = 1;\r
211 \r
212         /* Start the timer. */\r
213         T5CONbits.TON = 1;\r
214 }\r