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