]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PPC440_DP_FPU_Xilinx_Virtex5_GCC/RTOSDemo/flop/flop-reg-test.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PPC440_DP_FPU_Xilinx_Virtex5_GCC / RTOSDemo / flop / flop-reg-test.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * Tests the floating point context save and restore mechanism.\r
31  *\r
32  * Two tasks are created - each of which is allocated a buffer of \r
33  * portNO_FLOP_REGISTERS_TO_SAVE 32bit variables into which the flop context\r
34  * of the task is saved when the task is switched out, and from which the\r
35  * flop context of the task is restored when the task is switch in.  Prior to \r
36  * the tasks being created each position in the two buffers is filled with a \r
37  * unique value - this way the flop context of each task is different.\r
38  *\r
39  * The two test tasks never block so are always in either the Running or\r
40  * Ready state.  They execute at the lowest priority so will get pre-empted\r
41  * regularly, although the yield frequently so will not get much execution\r
42  * time.  The lack of execution time is not a problem as its only the \r
43  * switching in and out that is being tested.\r
44  *\r
45  * Whenever a task is moved from the Ready to the Running state its flop \r
46  * context will be loaded from the buffer, but while the task is in the\r
47  * Running state the buffer is not used and can contain any value - in this\r
48  * case and for test purposes the task itself clears the buffer to zero.  \r
49  * The next time the task is moved out of the Running state into the\r
50  * Ready state the flop context will once more get saved to the buffer - \r
51  * overwriting the zeros.\r
52  *\r
53  * Therefore whenever the task is not in the Running state its buffer contains\r
54  * the most recent values of its floating point registers - the zeroing out\r
55  * of the buffer while the task was executing being used to ensure the values \r
56  * the buffer contains are not stale.\r
57  *\r
58  * When neither test task is in the Running state the buffers should contain\r
59  * the unique values allocated before the tasks were created.  If so then\r
60  * the floating point context has been maintained.  This check is performed\r
61  * by the 'check' task (defined in main.c) by calling \r
62  * xAreFlopRegisterTestsStillRunning().\r
63  *\r
64  * The test tasks also increment a value each time they execute.\r
65  * xAreFlopRegisterTestsStillRunning() also checks that this value has changed\r
66  * since it last ran to ensure the test tasks are still getting processing time.\r
67  */\r
68 \r
69 /* Standard includes files. */\r
70 #include <string.h>\r
71 \r
72 /* Scheduler include files. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 \r
76 /*-----------------------------------------------------------*/\r
77 \r
78 #define flopNUMBER_OF_TASKS             2\r
79 #define flopSTART_VALUE ( 0x0000000100000001LL )\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /* The two test tasks as described at the top of this file. */\r
84 static void vFlopTest1( void *pvParameters );\r
85 static void vFlopTest2( void *pvParameters );\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 /* Buffers into which the flop registers will be saved.  There is a buffer for \r
90 both tasks. */\r
91 static volatile portDOUBLE dFlopRegisters[ flopNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];\r
92 \r
93 /* Variables that are incremented by the tasks to indicate that they are still\r
94 running. */\r
95 static volatile unsigned long ulFlop1CycleCount = 0, ulFlop2CycleCount = 0;\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 void vStartFlopRegTests( void )\r
100 {\r
101 TaskHandle_t xTaskJustCreated;\r
102 unsigned portBASE_TYPE x, y;\r
103 portDOUBLE z = flopSTART_VALUE;\r
104 \r
105         /* Fill the arrays into which the flop registers are to be saved with \r
106         known values.  These are the values that will be written to the flop\r
107         registers when the tasks start, and as the tasks do not perform any\r
108         flop operations the values should never change.  Each position in the\r
109         buffer contains a different value so the flop context of each task\r
110         will be different. */\r
111         for( x = 0; x < flopNUMBER_OF_TASKS; x++ )\r
112         {\r
113                 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1); y++ )\r
114                 {\r
115                         dFlopRegisters[ x ][ y ] = z;\r
116                         z+=flopSTART_VALUE;\r
117                 }\r
118         }\r
119 \r
120 \r
121         /* Create the first task. */\r
122         xTaskCreate( vFlopTest1, "flop1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTaskJustCreated );\r
123 \r
124         /* The task     tag value is a value that can be associated with a task, but \r
125         is not used by the scheduler itself.  Its use is down to the application so\r
126         it makes a convenient place in this case to store the pointer to the buffer\r
127         into which the flop context of the task will be stored.  The first created\r
128         task uses dFlopRegisters[ 0 ], the second dFlopRegisters[ 1 ]. */\r
129         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 0 ][ 0 ] ) );\r
130 \r
131         /* Do the same for the second task. */\r
132         xTaskCreate( vFlopTest2, "flop2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTaskJustCreated );\r
133         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( dFlopRegisters[ 1 ][ 0 ] ) );\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 static void vFlopTest1( void *pvParameters )\r
138 {\r
139         /* Just to remove compiler warning. */\r
140         ( void ) pvParameters;\r
141 \r
142         for( ;; )\r
143         {\r
144                 /* The values from the buffer should have now been written to the flop\r
145                 registers.  Clear the buffer to ensure the same values then get written\r
146                 back the next time the task runs.  Being preempted during this memset\r
147                 could cause the test to fail, hence the critical section. */\r
148                 portENTER_CRITICAL();\r
149                         memset( ( void * ) dFlopRegisters[ 0 ], 0x00, ( portNO_FLOP_REGISTERS_TO_SAVE * sizeof( portDOUBLE ) ) );\r
150                 portEXIT_CRITICAL();\r
151 \r
152                 /* We don't have to do anything other than indicate that we are \r
153                 still running. */\r
154                 ulFlop1CycleCount++;\r
155                 taskYIELD();\r
156         }\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 static void vFlopTest2( void *pvParameters )\r
161 {\r
162         /* Just to remove compiler warning. */\r
163         ( void ) pvParameters;\r
164 \r
165         for( ;; )\r
166         {\r
167                 /* The values from the buffer should have now been written to the flop\r
168                 registers.  Clear the buffer to ensure the same values then get written\r
169                 back the next time the task runs. */\r
170                 portENTER_CRITICAL();\r
171                         memset( ( void * ) dFlopRegisters[ 1 ], 0x00, ( portNO_FLOP_REGISTERS_TO_SAVE * sizeof( portDOUBLE ) ) );\r
172                 portEXIT_CRITICAL();\r
173 \r
174                 /* We don't have to do anything other than indicate that we are \r
175                 still running. */\r
176                 ulFlop2CycleCount++;\r
177                 taskYIELD();\r
178         }\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 portBASE_TYPE xAreFlopRegisterTestsStillRunning( void )\r
183 {\r
184 portBASE_TYPE xReturn = pdPASS;\r
185 unsigned portBASE_TYPE x, y;\r
186 portDOUBLE z = flopSTART_VALUE;\r
187 static unsigned long ulLastFlop1CycleCount = 0, ulLastFlop2CycleCount = 0;\r
188 \r
189         /* Called from the 'check' task.\r
190         \r
191         The flop tasks cannot be currently running, check their saved registers\r
192         are as expected.  The tests tasks do not perform any flop operations so\r
193         their registers should be as per their initial setting. */\r
194         for( x = 0; x < flopNUMBER_OF_TASKS; x++ )\r
195         {\r
196                 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )\r
197                 {\r
198                         if( dFlopRegisters[ x ][ y ] != z )\r
199                         {\r
200                                 xReturn = pdFAIL;\r
201                                 break;\r
202                         }\r
203 \r
204                         z+=flopSTART_VALUE;\r
205                 }\r
206         }\r
207 \r
208         /* Check both tasks have actually been swapped in and out since this function\r
209         last executed. */\r
210         if( ulFlop1CycleCount == ulLastFlop1CycleCount )\r
211         {\r
212                 xReturn = pdFAIL;\r
213         }\r
214 \r
215         if( ulFlop2CycleCount == ulLastFlop2CycleCount )\r
216         {\r
217                 xReturn = pdFAIL;\r
218         }\r
219 \r
220         ulLastFlop1CycleCount = ulFlop1CycleCount;\r
221         ulLastFlop2CycleCount = ulFlop2CycleCount;\r
222 \r
223         return xReturn;\r
224 }\r
225 \r