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