1 |
/* |
---|
2 |
FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd. |
---|
3 |
|
---|
4 |
This file is part of the FreeRTOS distribution. |
---|
5 |
|
---|
6 |
FreeRTOS is free software; you can redistribute it and/or modify it under |
---|
7 |
the terms of the GNU General Public License (version 2) as published by the |
---|
8 |
Free Software Foundation and modified by the FreeRTOS exception. |
---|
9 |
**NOTE** The exception to the GPL is included to allow you to distribute a |
---|
10 |
combined work that includes FreeRTOS without being obliged to provide the |
---|
11 |
source code for proprietary components outside of the FreeRTOS kernel. |
---|
12 |
Alternative commercial license and support terms are also available upon |
---|
13 |
request. See the licensing section of http://www.FreeRTOS.org for full |
---|
14 |
license details. |
---|
15 |
|
---|
16 |
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT |
---|
17 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
18 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
19 |
more details. |
---|
20 |
|
---|
21 |
You should have received a copy of the GNU General Public License along |
---|
22 |
with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59 |
---|
23 |
Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
---|
24 |
|
---|
25 |
|
---|
26 |
*************************************************************************** |
---|
27 |
* * |
---|
28 |
* Looking for a quick start? Then check out the FreeRTOS eBook! * |
---|
29 |
* See http://www.FreeRTOS.org/Documentation for details * |
---|
30 |
* * |
---|
31 |
*************************************************************************** |
---|
32 |
|
---|
33 |
1 tab == 4 spaces! |
---|
34 |
|
---|
35 |
Please ensure to read the configuration and relevant port sections of the |
---|
36 |
online documentation. |
---|
37 |
|
---|
38 |
http://www.FreeRTOS.org - Documentation, latest information, license and |
---|
39 |
contact details. |
---|
40 |
|
---|
41 |
http://www.SafeRTOS.com - A version that is certified for use in safety |
---|
42 |
critical systems. |
---|
43 |
|
---|
44 |
http://www.OpenRTOS.com - Commercial support, development, porting, |
---|
45 |
licensing and training services. |
---|
46 |
*/ |
---|
47 |
|
---|
48 |
/* |
---|
49 |
* This version of integer. c is for use on systems that have limited stack |
---|
50 |
* space and no display facilities. The complete version can be found in |
---|
51 |
* the Demo/Common/Full directory. |
---|
52 |
* |
---|
53 |
* As with the full version, the tasks created in this file are a good test |
---|
54 |
* of the scheduler context switch mechanism. The processor has to access |
---|
55 |
* 32bit variables in two or four chunks (depending on the processor). The low |
---|
56 |
* priority of these tasks means there is a high probability that a context |
---|
57 |
* switch will occur mid calculation. See flop. c documentation for |
---|
58 |
* more information. |
---|
59 |
* |
---|
60 |
*/ |
---|
61 |
|
---|
62 |
/* |
---|
63 |
Changes from V1.2.1 |
---|
64 |
|
---|
65 |
+ The constants used in the calculations are larger to ensure the |
---|
66 |
optimiser does not truncate them to 16 bits. |
---|
67 |
|
---|
68 |
Changes from V1.2.3 |
---|
69 |
|
---|
70 |
+ uxTaskCheck is now just used as a boolean. Instead of incrementing |
---|
71 |
the variable each cycle of the task, the variable is simply set to |
---|
72 |
true. sAreIntegerMathsTaskStillRunning() sets it back to false and |
---|
73 |
expects it to have been set back to true by the time it is called |
---|
74 |
again. |
---|
75 |
+ A division has been included in the calculation. |
---|
76 |
*/ |
---|
77 |
|
---|
78 |
#include <stdlib.h> |
---|
79 |
|
---|
80 |
/* Scheduler include files. */ |
---|
81 |
#include "FreeRTOS.h" |
---|
82 |
#include "task.h" |
---|
83 |
|
---|
84 |
/* Demo program include files. */ |
---|
85 |
#include "integer.h" |
---|
86 |
|
---|
87 |
/* The constants used in the calculation. */ |
---|
88 |
#define intgCONST1 ( ( portLONG ) 123 ) |
---|
89 |
#define intgCONST2 ( ( portLONG ) 234567 ) |
---|
90 |
#define intgCONST3 ( ( portLONG ) -3 ) |
---|
91 |
#define intgCONST4 ( ( portLONG ) 7 ) |
---|
92 |
#define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 ) |
---|
93 |
|
---|
94 |
#define intgSTACK_SIZE configMINIMAL_STACK_SIZE |
---|
95 |
|
---|
96 |
/* As this is the minimal version, we will only create one task. */ |
---|
97 |
#define intgNUMBER_OF_TASKS ( 1 ) |
---|
98 |
|
---|
99 |
/* The task function. Repeatedly performs a 32 bit calculation, checking the |
---|
100 |
result against the expected result. If the result is incorrect then the |
---|
101 |
context switch must have caused some corruption. */ |
---|
102 |
static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters ); |
---|
103 |
|
---|
104 |
/* Variables that are set to true within the calculation task to indicate |
---|
105 |
that the task is still executing. The check task sets the variable back to |
---|
106 |
false, flagging an error if the variable is still false the next time it |
---|
107 |
is called. */ |
---|
108 |
static volatile signed portBASE_TYPE xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( signed portBASE_TYPE ) pdFALSE }; |
---|
109 |
|
---|
110 |
/*-----------------------------------------------------------*/ |
---|
111 |
|
---|
112 |
void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority ) |
---|
113 |
{ |
---|
114 |
portSHORT sTask; |
---|
115 |
|
---|
116 |
for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ ) |
---|
117 |
{ |
---|
118 |
xTaskCreate( vCompeteingIntMathTask, ( signed portCHAR * ) "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( xTaskHandle * ) NULL ); |
---|
119 |
} |
---|
120 |
} |
---|
121 |
/*-----------------------------------------------------------*/ |
---|
122 |
|
---|
123 |
static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters ) |
---|
124 |
{ |
---|
125 |
/* These variables are all effectively set to constants so they are volatile to |
---|
126 |
ensure the compiler does not just get rid of them. */ |
---|
127 |
volatile portLONG lValue; |
---|
128 |
portSHORT sError = pdFALSE; |
---|
129 |
volatile signed portBASE_TYPE *pxTaskHasExecuted; |
---|
130 |
|
---|
131 |
/* Set a pointer to the variable we are going to set to true each |
---|
132 |
iteration. This is also a good test of the parameter passing mechanism |
---|
133 |
within each port. */ |
---|
134 |
pxTaskHasExecuted = ( volatile signed portBASE_TYPE * ) pvParameters; |
---|
135 |
|
---|
136 |
/* Keep performing a calculation and checking the result against a constant. */ |
---|
137 |
for( ;; ) |
---|
138 |
{ |
---|
139 |
/* Perform the calculation. This will store partial value in |
---|
140 |
registers, resulting in a good test of the context switch mechanism. */ |
---|
141 |
lValue = intgCONST1; |
---|
142 |
lValue += intgCONST2; |
---|
143 |
|
---|
144 |
/* Yield in case cooperative scheduling is being used. */ |
---|
145 |
#if configUSE_PREEMPTION == 0 |
---|
146 |
{ |
---|
147 |
taskYIELD(); |
---|
148 |
} |
---|
149 |
#endif |
---|
150 |
|
---|
151 |
/* Finish off the calculation. */ |
---|
152 |
lValue *= intgCONST3; |
---|
153 |
lValue /= intgCONST4; |
---|
154 |
|
---|
155 |
/* If the calculation is found to be incorrect we stop setting the |
---|
156 |
TaskHasExecuted variable so the check task can see an error has |
---|
157 |
occurred. */ |
---|
158 |
if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */ |
---|
159 |
{ |
---|
160 |
sError = pdTRUE; |
---|
161 |
} |
---|
162 |
|
---|
163 |
if( sError == pdFALSE ) |
---|
164 |
{ |
---|
165 |
/* We have not encountered any errors, so set the flag that show |
---|
166 |
we are still executing. This will be periodically cleared by |
---|
167 |
the check task. */ |
---|
168 |
portENTER_CRITICAL(); |
---|
169 |
*pxTaskHasExecuted = pdTRUE; |
---|
170 |
portEXIT_CRITICAL(); |
---|
171 |
} |
---|
172 |
|
---|
173 |
/* Yield in case cooperative scheduling is being used. */ |
---|
174 |
#if configUSE_PREEMPTION == 0 |
---|
175 |
{ |
---|
176 |
taskYIELD(); |
---|
177 |
} |
---|
178 |
#endif |
---|
179 |
} |
---|
180 |
} |
---|
181 |
/*-----------------------------------------------------------*/ |
---|
182 |
|
---|
183 |
/* This is called to check that all the created tasks are still running. */ |
---|
184 |
portBASE_TYPE xAreIntegerMathsTaskStillRunning( void ) |
---|
185 |
{ |
---|
186 |
portBASE_TYPE xReturn = pdTRUE; |
---|
187 |
portSHORT sTask; |
---|
188 |
|
---|
189 |
/* Check the maths tasks are still running by ensuring their check variables |
---|
190 |
are still being set to true. */ |
---|
191 |
for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ ) |
---|
192 |
{ |
---|
193 |
if( xTaskCheck[ sTask ] == pdFALSE ) |
---|
194 |
{ |
---|
195 |
/* The check has not incremented so an error exists. */ |
---|
196 |
xReturn = pdFALSE; |
---|
197 |
} |
---|
198 |
|
---|
199 |
/* Reset the check variable so we can tell if it has been set by |
---|
200 |
the next time around. */ |
---|
201 |
xTaskCheck[ sTask ] = pdFALSE; |
---|
202 |
} |
---|
203 |
|
---|
204 |
return xReturn; |
---|
205 |
} |
---|
206 |
|
---|