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