2 * FreeRTOS Kernel V10.0.1
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
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.
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.
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.
44 * The task toggle LED mainISR_TRIGGERED_LED each time it is unblocked by the
49 /* Standard includes. */
52 /* Scheduler includes. */
57 /* Standard demo includes. */
60 /*-----------------------------------------------------------*/
62 /* The LED controlled by the ISR triggered task. */
63 #define mainISR_TRIGGERED_LED ( 1 )
65 /* Constants used to configure T5. */
66 #define mainT5PRESCALAR ( 6 )
67 #define mainT5_SEMAPHORE_RATE ( 31250 )
69 /*-----------------------------------------------------------*/
72 * The task that is periodically triggered by an interrupt, as described at the
75 static void prvISRTriggeredTask( void* pvParameters );
78 * Configures the T5 timer peripheral to generate the interrupts that unblock
79 * the task implemented by the prvISRTriggeredTask() function.
81 static void prvSetupT5( void );
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 );
87 /*-----------------------------------------------------------*/
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 /*-----------------------------------------------------------*/
94 void vStartISRTriggeredTask( void )
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. */
105 /*-----------------------------------------------------------*/
107 void vT5InterruptHandler( void )
109 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
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 */
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 );
125 /* Clear the interrupt */
126 IFS0CLR = _IFS0_T5IF_MASK;
128 /* See comment above the call to xSemaphoreGiveFromISR(). */
129 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
131 /*-----------------------------------------------------------*/
133 static void prvISRTriggeredTask( void* pvParameters )
135 /* Avoid compiler warnings. */
136 ( void ) pvParameters;
138 /* Create the semaphore used to signal this task */
139 xBlockSemaphore = xSemaphoreCreateBinary();
141 /* Configure the timer to generate the interrupts. */
146 /* Block on the binary semaphore given by the T5 interrupt. */
147 xSemaphoreTake( xBlockSemaphore, portMAX_DELAY );
149 /* Toggle the LED. */
150 vParTestToggleLED( mainISR_TRIGGERED_LED );
153 /*-----------------------------------------------------------*/
155 static void prvSetupT5( void )
157 /* Set up timer 5 to generate an interrupt every 50 ms */
160 T5CONbits.TCKPS = mainT5PRESCALAR;
161 PR5 = mainT5_SEMAPHORE_RATE;
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;
168 /* Clear the interrupt as a starting condition. */
171 /* Enable the interrupt. */
174 /* Start the timer. */