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