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