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