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