]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC-Static-Allocation-Only/main.c
Remove compiler warning by ensure prvInitialiseMutex() is not included if configUSE_M...
[freertos] / FreeRTOS / Demo / WIN32-MSVC-Static-Allocation-Only / main.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  *\r
72  * This project is provided as an example of how to create a FreeRTOS project\r
73  * that does not need a heap.  configSUPPORT_STATIC_ALLOCATION is set to 1 to\r
74  * allow RTOS objects to be created using statically allocated RAM, and\r
75  * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 to remove any build dependency\r
76  * on the FreeRTOS heap.  When configSUPPORT_DYNAMIC_ALLOCATION is set to 0\r
77  * pvPortMalloc() just equates to NULL, and calls to vPortFree() have no\r
78  * effect.  See:\r
79  *\r
80  * http://www.freertos.org/a00111.html and\r
81  * http://www.freertos.org/Static_Vs_Dynamic_Memory_Allocation.html\r
82  *\r
83  *******************************************************************************\r
84  */\r
85 \r
86 /* Standard includes. */\r
87 #include <stdio.h>\r
88 #include <stdlib.h>\r
89 #include <conio.h>\r
90 \r
91 /* FreeRTOS kernel includes. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 \r
95 /* Standard demo includes. */\r
96 #include "StaticAllocation.h"\r
97 \r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /*\r
102  * Prototypes for the standard FreeRTOS stack overflow hook (callback)\r
103  * function.  http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
104  */\r
105 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
106 \r
107 /*\r
108  * This demo has configSUPPORT_STATIC_ALLOCATION set to 1 so the following\r
109  * application callback function must be provided to supply the RAM that will\r
110  * get used for the Idle task data structures and stack.\r
111  */\r
112 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );\r
113 \r
114 /*\r
115 * This demo has configSUPPORT_STATIC_ALLOCATION set to 1 and configUSE_TIMERS\r
116 * set to 1 so the following application callback function must be provided to\r
117 * supply the RAM that will get used for the Timer task data structures and\r
118 * stack.\r
119 */\r
120 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );\r
121 \r
122 /* This demo only uses the standard demo tasks that use statically allocated\r
123 RAM.  A 'check' task is also created to periodically inspect the demo tasks to\r
124 ensure they are still running, and that no errors have been detected. */\r
125 static void prvStartCheckTask( void );\r
126 static void prvCheckTask( void *pvParameters );\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 int main( void )\r
131 {\r
132         /* This demo has configSUPPORT_STATIC_ALLOCATION set to 1 and\r
133         configSUPPORT_DYNAMIC_ALLOCATION set to 0, so the only standard temo tasks\r
134         created are the ones that only use static allocation.  This allow the\r
135         application to be built without including a FreeRTOS heap file (without one\r
136         of the heap files described on http://www.freertos.org/a00111.html */\r
137         vStartStaticallyAllocatedTasks();\r
138 \r
139         /* Start a task that periodically inspects the tasks created by\r
140         vStartStaticallyAllocatedTasks() to ensure they are still running, and not\r
141         reporting any errors. */\r
142         prvStartCheckTask();\r
143 \r
144         /* Start the scheduler so the demo tasks start to execute. */\r
145         vTaskStartScheduler();\r
146 \r
147         /* vTaskStartScheduler() would only return if RAM required by the Idle and\r
148         Timer tasks could not be allocated.  As this demo uses statically allocated\r
149         RAM only, there are no allocations that could fail, and\r
150         vTaskStartScheduler() cannot return - so there is no need to put the normal\r
151         infinite loop after the call to vTaskStartScheduler(). */\r
152 \r
153         return 0;\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 static void prvStartCheckTask( void )\r
158 {\r
159 /* Allocate the data structure that will hold the task's TCB.  NOTE:  This is\r
160 declared static so it still exists after this function has returned. */\r
161 static StaticTask_t xCheckTask;\r
162 \r
163 /* Allocate the stack that will be used by the task.  NOTE:  This is declared\r
164 static so it still exists after this function has returned. */\r
165 static StackType_t ucTaskStack[ configMINIMAL_STACK_SIZE * sizeof( StackType_t ) ];\r
166 \r
167         /* Create the task, which will use the RAM allocated by the linker to the\r
168         variables declared in this function. */\r
169         xTaskCreateStatic( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, ucTaskStack, &xCheckTask );\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static void prvCheckTask( void *pvParameters )\r
174 {\r
175 const TickType_t xCycleFrequency = pdMS_TO_TICKS( 2500UL );\r
176 static char *pcStatusMessage = "No errors";\r
177 \r
178         /* Just to remove compiler warning. */\r
179         ( void ) pvParameters;\r
180 \r
181         for( ;; )\r
182         {\r
183                 /* Place this task in the blocked state until it is time to run again. */\r
184                 vTaskDelay( xCycleFrequency );\r
185 \r
186                 /* Check the tasks that use static allocation are still executing. */\r
187                 if( xAreStaticAllocationTasksStillRunning() != pdPASS )\r
188                 {\r
189                         pcStatusMessage = "Error: Static allocation";\r
190                 }\r
191 \r
192                 /* This is the only task that uses stdout so its ok to call printf()\r
193                 directly. */\r
194                 printf( "%s - tick count %d - number of tasks executing %d\r\n",\r
195                                                                                                         pcStatusMessage,\r
196                                                                                                         xTaskGetTickCount(),\r
197                                                                                                         uxTaskGetNumberOfTasks() );\r
198         }\r
199 }\r
200 \r
201 /*-----------------------------------------------------------*/\r
202 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
203 {\r
204         ( void ) pcTaskName;\r
205         ( void ) pxTask;\r
206 \r
207         /* Run time stack overflow checking is performed if\r
208         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
209         function is called if a stack overflow is detected.  This function is\r
210         provided as an example only as stack overflow checking does not function\r
211         when running the FreeRTOS Windows port. */\r
212         vAssertCalled( __LINE__, __FILE__ );\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
217 {\r
218 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;\r
219 \r
220         /* Called if an assertion passed to configASSERT() fails.  See\r
221         http://www.freertos.org/a00110.html#configASSERT for more information. */\r
222 \r
223         /* Parameters are not used. */\r
224         ( void ) ulLine;\r
225         ( void ) pcFileName;\r
226 \r
227         printf( "ASSERT! Line %d, file %s\r\n", ulLine, pcFileName );\r
228 \r
229         taskENTER_CRITICAL();\r
230         {\r
231                 /* You can step out of this function to debug the assertion by using\r
232                 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero\r
233                 value. */\r
234                 while( ulSetToNonZeroInDebuggerToContinue == 0 )\r
235                 {\r
236                         __asm{ NOP };\r
237                         __asm{ NOP };\r
238                 }\r
239         }\r
240         taskEXIT_CRITICAL();\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
245 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
246 used by the Idle task. */\r
247 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
248 {\r
249 /* If the buffers to be provided to the Idle task are declared inside this\r
250 function then they must be declared static - otherwise they will be allocated on\r
251 the stack and so not exists after this function exits. */\r
252 static StaticTask_t xIdleTaskTCB;\r
253 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
254 \r
255         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
256         state will be stored. */\r
257         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
258 \r
259         /* Pass out the array that will be used as the Idle task's stack. */\r
260         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
261 \r
262         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
263         Note that, as the array is necessarily of type StackType_t,\r
264         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
265         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
270 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
271 to provide the memory that is used by the Timer service task. */\r
272 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
273 {\r
274 /* If the buffers to be provided to the Timer task are declared inside this\r
275 function then they must be declared static - otherwise they will be allocated on\r
276 the stack and so not exists after this function exits. */\r
277 static StaticTask_t xTimerTaskTCB;\r
278 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
279 \r
280         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
281         task's state will be stored. */\r
282         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
283 \r
284         /* Pass out the array that will be used as the Timer task's stack. */\r
285         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
286 \r
287         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
288         Note that, as the array is necessarily of type StackType_t,\r
289         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
290         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
291 }\r
292 \r