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 * Instead of the normal single demo application, the PIC18F demo is split
30 * into several smaller programs of which this is the second. This enables the
31 * demo's to be executed on the RAM limited 40 pin devices. The 64 and 80 pin
32 * devices require a more costly development platform and are not so readily
35 * The RTOSDemo2 project is configured for a PIC18F452 device. Main2.c starts
36 * 5 tasks (including the idle task).
38 * The first, second and third tasks do nothing but flash an LED. This gives
39 * visual feedback that everything is executing as expected. One task flashes
40 * an LED every 333ms (i.e. on and off every 333/2 ms), then next every 666ms
41 * and the last every 999ms.
43 * The last task runs at the idle priority. It repeatedly performs a 32bit
44 * calculation and checks it's result against the expected value. This checks
45 * that the temporary storage utilised by the compiler to hold intermediate
46 * results does not get corrupted when the task gets switched in and out.
47 * should the calculation ever provide an incorrect result the final LED is
50 * On entry to main() an 'X' is transmitted. Monitoring the serial port using a
51 * dumb terminal allows for verification that the device is not continuously
52 * being reset (no more than one 'X' should be transmitted).
54 * http://www.FreeRTOS.org contains important information on the use of the
61 + Delay periods are now specified using variables and constants of
62 TickType_t rather than unsigned long.
65 /* Scheduler include files. */
69 /* Demo app include files. */
74 /* Priority definitions for the LED tasks. Other tasks just use the idle
76 #define mainLED_FLASH_PRIORITY ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 1 )
78 /* The LED that is lit when should the calculation fail. */
79 #define mainCHECK_TASK_LED ( ( unsigned portBASE_TYPE ) 3 )
81 /* Constants required for the communications. Only one character is ever
83 #define mainCOMMS_QUEUE_LENGTH ( ( unsigned portBASE_TYPE ) 5 )
84 #define mainNO_BLOCK ( ( TickType_t ) 0 )
85 #define mainBAUD_RATE ( ( unsigned long ) 9600 )
88 * The task that performs the 32 bit calculation at the idle priority.
90 static void vCalculationTask( void *pvParameters );
92 /*-----------------------------------------------------------*/
94 /* Creates the tasks, then starts the scheduler. */
97 /* Initialise the required hardware. */
99 vPortInitialiseBlocks();
101 /* Send a character so we have some visible feedback of a reset. */
102 xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );
103 xSerialPutChar( NULL, 'X', mainNO_BLOCK );
105 /* Start the standard LED flash tasks as defined in demo/common/minimal. */
106 vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );
108 /* Start the check task defined in this file. */
109 xTaskCreate( vCalculationTask, "Check", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
111 /* Start the scheduler. */
112 vTaskStartScheduler();
114 /*-----------------------------------------------------------*/
116 static void vCalculationTask( void *pvParameters )
118 volatile unsigned long ulCalculatedValue; /* Volatile to ensure optimisation is minimal. */
120 /* Continuously perform a calculation. If the calculation result is ever
121 incorrect turn the LED on. */
124 /* A good optimising compiler would just remove all this! */
125 ulCalculatedValue = 1234UL;
126 ulCalculatedValue *= 99UL;
128 if( ulCalculatedValue != 122166UL )
130 vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
133 ulCalculatedValue *= 9876UL;
135 if( ulCalculatedValue != 1206511416UL )
137 vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
140 ulCalculatedValue /= 15UL;
142 if( ulCalculatedValue != 80434094UL )
144 vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
147 ulCalculatedValue += 918273UL;
149 if( ulCalculatedValue != 81352367UL )
151 vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
155 /*-----------------------------------------------------------*/