]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC-Static-Allocation-Only/main.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / WIN32-MSVC-Static-Allocation-Only / 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  *\r
30  * This project is provided as an example of how to create a FreeRTOS project\r
31  * that does not need a heap.  configSUPPORT_STATIC_ALLOCATION is set to 1 to\r
32  * allow RTOS objects to be created using statically allocated RAM, and\r
33  * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 to remove any build dependency\r
34  * on the FreeRTOS heap.  When configSUPPORT_DYNAMIC_ALLOCATION is set to 0\r
35  * pvPortMalloc() just equates to NULL, and calls to vPortFree() have no\r
36  * effect.  See:\r
37  *\r
38  * http://www.freertos.org/a00111.html and\r
39  * http://www.freertos.org/Static_Vs_Dynamic_Memory_Allocation.html\r
40  *\r
41  *******************************************************************************\r
42  */\r
43 \r
44 /* Standard includes. */\r
45 #include <stdio.h>\r
46 #include <stdlib.h>\r
47 #include <conio.h>\r
48 \r
49 /* FreeRTOS kernel includes. */\r
50 #include "FreeRTOS.h"\r
51 #include "task.h"\r
52 \r
53 /* Standard demo includes. */\r
54 #include "StaticAllocation.h"\r
55 \r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /*\r
60  * Prototypes for the standard FreeRTOS stack overflow hook (callback)\r
61  * function.  http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
62  */\r
63 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
64 \r
65 /*\r
66  * This demo has configSUPPORT_STATIC_ALLOCATION set to 1 so the following\r
67  * application callback function must be provided to supply the RAM that will\r
68  * get used for the Idle task data structures and stack.\r
69  */\r
70 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );\r
71 \r
72 /*\r
73 * This demo has configSUPPORT_STATIC_ALLOCATION set to 1 and configUSE_TIMERS\r
74 * set to 1 so the following application callback function must be provided to\r
75 * supply the RAM that will get used for the Timer task data structures and\r
76 * stack.\r
77 */\r
78 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );\r
79 \r
80 /* This demo only uses the standard demo tasks that use statically allocated\r
81 RAM.  A 'check' task is also created to periodically inspect the demo tasks to\r
82 ensure they are still running, and that no errors have been detected. */\r
83 static void prvStartCheckTask( void );\r
84 static void prvCheckTask( void *pvParameters );\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 int main( void )\r
89 {\r
90         /* This demo has configSUPPORT_STATIC_ALLOCATION set to 1 and\r
91         configSUPPORT_DYNAMIC_ALLOCATION set to 0, so the only standard temo tasks\r
92         created are the ones that only use static allocation.  This allow the\r
93         application to be built without including a FreeRTOS heap file (without one\r
94         of the heap files described on http://www.freertos.org/a00111.html */\r
95         vStartStaticallyAllocatedTasks();\r
96 \r
97         /* Start a task that periodically inspects the tasks created by\r
98         vStartStaticallyAllocatedTasks() to ensure they are still running, and not\r
99         reporting any errors. */\r
100         prvStartCheckTask();\r
101 \r
102         /* Start the scheduler so the demo tasks start to execute. */\r
103         vTaskStartScheduler();\r
104 \r
105         /* vTaskStartScheduler() would only return if RAM required by the Idle and\r
106         Timer tasks could not be allocated.  As this demo uses statically allocated\r
107         RAM only, there are no allocations that could fail, and\r
108         vTaskStartScheduler() cannot return - so there is no need to put the normal\r
109         infinite loop after the call to vTaskStartScheduler(). */\r
110 \r
111         return 0;\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 static void prvStartCheckTask( void )\r
116 {\r
117 /* Allocate the data structure that will hold the task's TCB.  NOTE:  This is\r
118 declared static so it still exists after this function has returned. */\r
119 static StaticTask_t xCheckTask;\r
120 \r
121 /* Allocate the stack that will be used by the task.  NOTE:  This is declared\r
122 static so it still exists after this function has returned. */\r
123 static StackType_t ucTaskStack[ configMINIMAL_STACK_SIZE * sizeof( StackType_t ) ];\r
124 \r
125         /* Create the task, which will use the RAM allocated by the linker to the\r
126         variables declared in this function. */\r
127         xTaskCreateStatic( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, ucTaskStack, &xCheckTask );\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 static void prvCheckTask( void *pvParameters )\r
132 {\r
133 const TickType_t xCycleFrequency = pdMS_TO_TICKS( 2500UL );\r
134 static char *pcStatusMessage = "No errors";\r
135 \r
136         /* Just to remove compiler warning. */\r
137         ( void ) pvParameters;\r
138 \r
139         for( ;; )\r
140         {\r
141                 /* Place this task in the blocked state until it is time to run again. */\r
142                 vTaskDelay( xCycleFrequency );\r
143 \r
144                 /* Check the tasks that use static allocation are still executing. */\r
145                 if( xAreStaticAllocationTasksStillRunning() != pdPASS )\r
146                 {\r
147                         pcStatusMessage = "Error: Static allocation";\r
148                 }\r
149 \r
150                 /* This is the only task that uses stdout so its ok to call printf()\r
151                 directly. */\r
152                 printf( "%s - tick count %d - number of tasks executing %d\r\n",\r
153                                                                                                         pcStatusMessage,\r
154                                                                                                         xTaskGetTickCount(),\r
155                                                                                                         uxTaskGetNumberOfTasks() );\r
156         }\r
157 }\r
158 \r
159 /*-----------------------------------------------------------*/\r
160 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
161 {\r
162         ( void ) pcTaskName;\r
163         ( void ) pxTask;\r
164 \r
165         /* Run time stack overflow checking is performed if\r
166         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
167         function is called if a stack overflow is detected.  This function is\r
168         provided as an example only as stack overflow checking does not function\r
169         when running the FreeRTOS Windows port. */\r
170         vAssertCalled( __LINE__, __FILE__ );\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
175 {\r
176 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;\r
177 \r
178         /* Called if an assertion passed to configASSERT() fails.  See\r
179         http://www.freertos.org/a00110.html#configASSERT for more information. */\r
180 \r
181         /* Parameters are not used. */\r
182         ( void ) ulLine;\r
183         ( void ) pcFileName;\r
184 \r
185         printf( "ASSERT! Line %d, file %s\r\n", ulLine, pcFileName );\r
186 \r
187         taskENTER_CRITICAL();\r
188         {\r
189                 /* You can step out of this function to debug the assertion by using\r
190                 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero\r
191                 value. */\r
192                 while( ulSetToNonZeroInDebuggerToContinue == 0 )\r
193                 {\r
194                         __asm{ NOP };\r
195                         __asm{ NOP };\r
196                 }\r
197         }\r
198         taskEXIT_CRITICAL();\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
203 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
204 used by the Idle task. */\r
205 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
206 {\r
207 /* If the buffers to be provided to the Idle task are declared inside this\r
208 function then they must be declared static - otherwise they will be allocated on\r
209 the stack and so not exists after this function exits. */\r
210 static StaticTask_t xIdleTaskTCB;\r
211 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
212 \r
213         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
214         state will be stored. */\r
215         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
216 \r
217         /* Pass out the array that will be used as the Idle task's stack. */\r
218         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
219 \r
220         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
221         Note that, as the array is necessarily of type StackType_t,\r
222         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
223         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
228 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
229 to provide the memory that is used by the Timer service task. */\r
230 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
231 {\r
232 /* If the buffers to be provided to the Timer task are declared inside this\r
233 function then they must be declared static - otherwise they will be allocated on\r
234 the stack and so not exists after this function exits. */\r
235 static StaticTask_t xTimerTaskTCB;\r
236 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
237 \r
238         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
239         task's state will be stored. */\r
240         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
241 \r
242         /* Pass out the array that will be used as the Timer task's stack. */\r
243         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
244 \r
245         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
246         Note that, as the array is necessarily of type StackType_t,\r
247         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
248         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
249 }\r
250 \r