]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/integer.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / Common / Minimal / integer.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * Creates one or more tasks that repeatedly perform a set of integer\r
77  * calculations.  The result of each run-time calculation is compared to the \r
78  * known expected result - with a mismatch being indicative of an error in the\r
79  * context switch mechanism.\r
80  */\r
81 \r
82 #include <stdlib.h>\r
83 \r
84 /* Scheduler include files. */\r
85 #include "FreeRTOS.h"\r
86 #include "task.h"\r
87 \r
88 /* Demo program include files. */\r
89 #include "integer.h"\r
90 \r
91 /* The constants used in the calculation. */\r
92 #define intgCONST1                              ( ( long ) 123 )\r
93 #define intgCONST2                              ( ( long ) 234567 )\r
94 #define intgCONST3                              ( ( long ) -3 )\r
95 #define intgCONST4                              ( ( long ) 7 )\r
96 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
97 \r
98 #define intgSTACK_SIZE                  configMINIMAL_STACK_SIZE\r
99 \r
100 /* As this is the minimal version, we will only create one task. */\r
101 #define intgNUMBER_OF_TASKS             ( 1 )\r
102 \r
103 /* The task function.  Repeatedly performs a 32 bit calculation, checking the\r
104 result against the expected result.  If the result is incorrect then the\r
105 context switch must have caused some corruption. */\r
106 static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters );\r
107 \r
108 /* Variables that are set to true within the calculation task to indicate\r
109 that the task is still executing.  The check task sets the variable back to\r
110 false, flagging an error if the variable is still false the next time it\r
111 is called. */\r
112 static volatile signed portBASE_TYPE xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( signed portBASE_TYPE ) pdFALSE };\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )\r
117 {\r
118 short sTask;\r
119 \r
120         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
121         {\r
122                 xTaskCreate( vCompeteingIntMathTask, ( signed char * ) "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( xTaskHandle * ) NULL );\r
123         }\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters )\r
128 {\r
129 /* These variables are all effectively set to constants so they are volatile to\r
130 ensure the compiler does not just get rid of them. */\r
131 volatile long lValue;\r
132 short sError = pdFALSE;\r
133 volatile signed portBASE_TYPE *pxTaskHasExecuted;\r
134 \r
135         /* Set a pointer to the variable we are going to set to true each\r
136         iteration.  This is also a good test of the parameter passing mechanism\r
137         within each port. */\r
138         pxTaskHasExecuted = ( volatile signed portBASE_TYPE * ) pvParameters;\r
139 \r
140         /* Keep performing a calculation and checking the result against a constant. */\r
141         for( ;; )\r
142         {\r
143                 /* Perform the calculation.  This will store partial value in\r
144                 registers, resulting in a good test of the context switch mechanism. */\r
145                 lValue = intgCONST1;\r
146                 lValue += intgCONST2;\r
147 \r
148                 /* Yield in case cooperative scheduling is being used. */\r
149                 #if configUSE_PREEMPTION == 0\r
150                 {\r
151                         taskYIELD();\r
152                 }\r
153                 #endif\r
154 \r
155                 /* Finish off the calculation. */\r
156                 lValue *= intgCONST3;\r
157                 lValue /= intgCONST4;\r
158 \r
159                 /* If the calculation is found to be incorrect we stop setting the \r
160                 TaskHasExecuted variable so the check task can see an error has \r
161                 occurred. */\r
162                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
163                 {\r
164                         sError = pdTRUE;\r
165                 }\r
166 \r
167                 if( sError == pdFALSE )\r
168                 {\r
169                         /* We have not encountered any errors, so set the flag that show\r
170                         we are still executing.  This will be periodically cleared by\r
171                         the check task. */\r
172                         portENTER_CRITICAL();\r
173                                 *pxTaskHasExecuted = pdTRUE;\r
174                         portEXIT_CRITICAL();\r
175                 }\r
176 \r
177                 /* Yield in case cooperative scheduling is being used. */\r
178                 #if configUSE_PREEMPTION == 0\r
179                 {\r
180                         taskYIELD();\r
181                 }\r
182                 #endif\r
183         }\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 /* This is called to check that all the created tasks are still running. */\r
188 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )\r
189 {\r
190 portBASE_TYPE xReturn = pdTRUE;\r
191 short sTask;\r
192 \r
193         /* Check the maths tasks are still running by ensuring their check variables \r
194         are still being set to true. */\r
195         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
196         {\r
197                 if( xTaskCheck[ sTask ] == pdFALSE )\r
198                 {\r
199                         /* The check has not incremented so an error exists. */\r
200                         xReturn = pdFALSE;\r
201                 }\r
202 \r
203                 /* Reset the check variable so we can tell if it has been set by\r
204                 the next time around. */\r
205                 xTaskCheck[ sTask ] = pdFALSE;\r
206         }\r
207 \r
208         return xReturn;\r
209 }\r
210 \r