3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
29 * Creates one or more tasks that repeatedly perform a set of integer
30 * calculations. The result of each run-time calculation is compared to the
31 * known expected result - with a mismatch being indicative of an error in the
32 * context switch mechanism.
37 /* Scheduler include files. */
41 /* Demo program include files. */
44 /* The constants used in the calculation. */
45 #define intgCONST1 ( ( long ) 123 )
46 #define intgCONST2 ( ( long ) 234567 )
47 #define intgCONST3 ( ( long ) -3 )
48 #define intgCONST4 ( ( long ) 7 )
49 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
51 #define intgSTACK_SIZE configMINIMAL_STACK_SIZE
53 /* As this is the minimal version, we will only create one task. */
54 #define intgNUMBER_OF_TASKS ( 1 )
56 /* The task function. Repeatedly performs a 32 bit calculation, checking the
57 * result against the expected result. If the result is incorrect then the
58 * context switch must have caused some corruption. */
59 static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters );
61 /* Variables that are set to true within the calculation task to indicate
62 * that the task is still executing. The check task sets the variable back to
63 * false, flagging an error if the variable is still false the next time it
65 static BaseType_t xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( BaseType_t ) pdFALSE };
67 /*-----------------------------------------------------------*/
69 void vStartIntegerMathTasks( UBaseType_t uxPriority )
73 for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )
75 xTaskCreate( vCompeteingIntMathTask, "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( TaskHandle_t * ) NULL );
78 /*-----------------------------------------------------------*/
80 static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters )
82 /* These variables are all effectively set to constants so they are volatile to
83 * ensure the compiler does not just get rid of them. */
85 short sError = pdFALSE;
86 volatile BaseType_t * pxTaskHasExecuted;
88 /* Set a pointer to the variable we are going to set to true each
89 * iteration. This is also a good test of the parameter passing mechanism
90 * within each port. */
91 pxTaskHasExecuted = ( volatile BaseType_t * ) pvParameters;
93 /* Keep performing a calculation and checking the result against a constant. */
96 /* Perform the calculation. This will store partial value in
97 * registers, resulting in a good test of the context switch mechanism. */
101 /* Yield in case cooperative scheduling is being used. */
102 #if configUSE_PREEMPTION == 0
108 /* Finish off the calculation. */
109 lValue *= intgCONST3;
110 lValue /= intgCONST4;
112 /* If the calculation is found to be incorrect we stop setting the
113 * TaskHasExecuted variable so the check task can see an error has
115 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */
120 if( sError == pdFALSE )
122 /* We have not encountered any errors, so set the flag that show
123 * we are still executing. This will be periodically cleared by
125 portENTER_CRITICAL();
126 *pxTaskHasExecuted = pdTRUE;
130 /* Yield in case cooperative scheduling is being used. */
131 #if configUSE_PREEMPTION == 0
138 /*-----------------------------------------------------------*/
140 /* This is called to check that all the created tasks are still running. */
141 BaseType_t xAreIntegerMathsTaskStillRunning( void )
143 BaseType_t xReturn = pdTRUE;
146 /* Check the maths tasks are still running by ensuring their check variables
147 * are still being set to true. */
148 for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )
150 if( xTaskCheck[ sTask ] == pdFALSE )
152 /* The check has not incremented so an error exists. */
156 /* Reset the check variable so we can tell if it has been set by
157 * the next time around. */
158 xTaskCheck[ sTask ] = pdFALSE;