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