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