3 * Copyright (C) 2020 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
28 /******************************************************************************
29 * This project provides two demo applications. A simple blinky style project,
30 * and a more comprehensive test and demo application. The
31 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
32 * select between the two. The simply blinky demo is implemented and described
33 * in main_blinky.c. The more comprehensive test and demo application is
34 * implemented and described in main_full.c.
36 * This file implements the code that is not demo specific, including the
37 * hardware setup and FreeRTOS hook functions. It also contains a dummy
38 * interrupt service routine called Dummy_IRQHandler() that is provided as an
39 * example of how to use interrupt safe FreeRTOS API functions (those that end
42 *****************************************************************************/
44 /* Standard includes. */
47 /* FreeRTOS includes. */
51 /* Hardware specific includes. */
54 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
55 or 0 to run the more comprehensive test and demo application. */
56 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 0
58 /* The bit on port 0 to which the LED is wired. */
59 #define mainLED_BIT ( 1UL << 7UL )
61 /* The configCHECK_FOR_STACK_OVERFLOW setting in FreeRTOSConifg can be used to
62 check task stacks for overflows. It does not however check the stack used by
63 interrupts. This demo has a simple addition that will also check the stack used
64 by interrupts if mainCHECK_INTERRUPT_STACK is set to 1. Note that this check is
65 only performed from the tick hook function (which runs in an interrupt context).
66 It is a good debugging aid - but won't catch interrupt stack problems until the
67 tick interrupt next executes. */
68 #define mainCHECK_INTERRUPT_STACK 1
69 #if mainCHECK_INTERRUPT_STACK == 1
70 const unsigned char ucExpectedInterruptStackValues[] = { 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
73 /*-----------------------------------------------------------*/
76 * Perform any application specific hardware configuration. The clocks,
77 * memory, etc. are configured before main() is called.
79 static void prvSetupHardware( void );
82 * The hardware only has a single LED. Simply toggle it.
84 void vMainToggleLED( void );
86 /* main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
87 main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0. */
88 void main_blinky( void );
89 void main_full( void );
91 /*-----------------------------------------------------------*/
93 /* The GPIO port to which the LED is attached. */
94 static LPC_GPIO_TypeDef * const xGPIO0 = LPC_GPIO0;
96 /*-----------------------------------------------------------*/
99 /* Prepare the hardware to run this demo. */
102 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
104 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1
116 /*-----------------------------------------------------------*/
118 void vMainToggleLED( void )
120 static unsigned long ulLEDState = 0UL;
122 if( ulLEDState == 0UL )
124 xGPIO0->MASKED_ACCESS[ mainLED_BIT ] = 0UL;
128 xGPIO0->MASKED_ACCESS[ mainLED_BIT ] = mainLED_BIT;
131 ulLEDState = !ulLEDState;
133 /*-----------------------------------------------------------*/
135 static void prvSetupHardware( void )
137 extern unsigned long _vStackTop[], _pvHeapStart[];
138 unsigned long ulInterruptStackSize;
140 /* Enable AHB clock for GPIO. */
141 LPC_SYSCON->SYSAHBCLKCTRL |= ( 1 << 6 );
143 /* Configure GPIO for LED output. */
144 xGPIO0->DIR |= mainLED_BIT;
146 /* The size of the stack used by main and interrupts is not defined in
147 the linker, but just uses whatever RAM is left. Calculate the amount of
148 RAM available for the main/interrupt/system stack, and check it against
149 a reasonable number. If this assert is hit then it is likely you don't
150 have enough stack to start the kernel, or to allow interrupts to nest.
151 Note - this is separate to the stacks that are used by tasks. The stacks
152 that are used by tasks are automatically checked if
153 configCHECK_FOR_STACK_OVERFLOW is not 0 in FreeRTOSConfig.h - but the stack
154 used by interrupts is not. Reducing the conifgTOTAL_HEAP_SIZE setting will
155 increase the stack available to main() and interrupts. */
156 ulInterruptStackSize = ( ( unsigned long ) _vStackTop ) - ( ( unsigned long ) _pvHeapStart );
157 configASSERT( ulInterruptStackSize > 350UL );
159 /* Fill the stack used by main() and interrupts to a known value, so its
160 use can be manually checked. */
161 memcpy( ( void * ) _pvHeapStart, ucExpectedInterruptStackValues, sizeof( ucExpectedInterruptStackValues ) );
163 /*-----------------------------------------------------------*/
165 void vApplicationMallocFailedHook( void )
167 /* vApplicationMallocFailedHook() will only be called if
168 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
169 function that will get called if a call to pvPortMalloc() fails.
170 pvPortMalloc() is called internally by the kernel whenever a task, queue,
171 timer or semaphore is created. It is also called by various parts of the
172 demo application. If heap_1.c or heap_2.c are used, then the size of the
173 heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
174 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
175 to query the size of free heap space that remains (although it does not
176 provide information on how the remaining heap might be fragmented). */
177 taskDISABLE_INTERRUPTS();
180 /*-----------------------------------------------------------*/
182 void vApplicationIdleHook( void )
184 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
185 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
186 task. It is essential that code added to this hook function never attempts
187 to block in any way (for example, call xQueueReceive() with a block time
188 specified, or call vTaskDelay()). If the application makes use of the
189 vTaskDelete() API function (as this demo application does) then it is also
190 important that vApplicationIdleHook() is permitted to return to its calling
191 function, because it is the responsibility of the idle task to clean up
192 memory allocated by the kernel to any task that has since been deleted. */
194 /*-----------------------------------------------------------*/
196 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
201 /* Run time stack overflow checking is performed if
202 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
203 function is called if a stack overflow is detected. */
204 taskDISABLE_INTERRUPTS();
207 /*-----------------------------------------------------------*/
209 void vApplicationTickHook( void )
211 #if mainCHECK_INTERRUPT_STACK == 1
212 extern unsigned long _pvHeapStart[];
214 /* This function will be called by each tick interrupt if
215 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
216 added here, but the tick hook is called from an interrupt context, so
217 code must not attempt to block, and only the interrupt safe FreeRTOS API
218 functions can be used (those that end in FromISR()). */
220 /* Manually check the last few bytes of the interrupt stack to check they
221 have not been overwritten. Note - the task stacks are automatically
222 checked for overflow if configCHECK_FOR_STACK_OVERFLOW is set to 1 or 2
223 in FreeRTOSConifg.h, but the interrupt stack is not. */
224 configASSERT( memcmp( ( void * ) _pvHeapStart, ucExpectedInterruptStackValues, sizeof( ucExpectedInterruptStackValues ) ) == 0U );
225 #endif /* mainCHECK_INTERRUPT_STACK */
227 /*-----------------------------------------------------------*/
229 #ifdef JUST_AN_EXAMPLE_ISR
231 void Dummy_IRQHandler(void)
233 long lHigherPriorityTaskWoken = pdFALSE;
235 /* Clear the interrupt if necessary. */
236 Dummy_ClearITPendingBit();
238 /* This interrupt does nothing more than demonstrate how to synchronise a
239 task with an interrupt. A semaphore is used for this purpose. Note
240 lHigherPriorityTaskWoken is initialised to zero. Only FreeRTOS API functions
241 that end in "FromISR" can be called from an ISR. */
242 xSemaphoreGiveFromISR( xTestSemaphore, &lHigherPriorityTaskWoken );
244 /* If there was a task that was blocked on the semaphore, and giving the
245 semaphore caused the task to unblock, and the unblocked task has a priority
246 higher than the current Running state task (the task that this interrupt
247 interrupted), then lHigherPriorityTaskWoken will have been set to pdTRUE
248 internally within xSemaphoreGiveFromISR(). Passing pdTRUE into the
249 portEND_SWITCHING_ISR() macro will result in a context switch being pended to
250 ensure this interrupt returns directly to the unblocked, higher priority,
251 task. Passing pdFALSE into portEND_SWITCHING_ISR() has no effect. */
252 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
255 #endif /* JUST_AN_EXAMPLE_ISR */