]> begriffs open source - cmsis-freertos/blob - Demo/PIC18_MPLAB/main2.c
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Demo / PIC18_MPLAB / main2.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
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
33  * available.
34  *
35  * The RTOSDemo2 project is configured for a PIC18F452 device.  Main2.c starts
36  * 5 tasks (including the idle task).
37  *
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.
42  *
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
48  * turned on.
49  *
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).
53  *
54  * http://www.FreeRTOS.org contains important information on the use of the
55  * PIC18F port.
56  */
57
58 /*
59 Changes from V2.0.0
60
61         + Delay periods are now specified using variables and constants of
62           TickType_t rather than unsigned long.
63 */
64
65 /* Scheduler include files. */
66 #include "FreeRTOS.h"
67 #include "task.h"
68
69 /* Demo app include files. */
70 #include "flash.h"
71 #include "partest.h"
72 #include "serial.h"
73
74 /* Priority definitions for the LED tasks.  Other tasks just use the idle
75 priority. */
76 #define mainLED_FLASH_PRIORITY                  ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 1 )
77
78 /* The LED that is lit when should the calculation fail. */
79 #define mainCHECK_TASK_LED                              ( ( unsigned portBASE_TYPE ) 3 )
80
81 /* Constants required for the communications.  Only one character is ever
82 transmitted. */
83 #define mainCOMMS_QUEUE_LENGTH                  ( ( unsigned portBASE_TYPE ) 5 )
84 #define mainNO_BLOCK                                    ( ( TickType_t ) 0 )
85 #define mainBAUD_RATE                                   ( ( unsigned long ) 9600 )
86
87 /*
88  * The task that performs the 32 bit calculation at the idle priority.
89  */
90 static void vCalculationTask( void *pvParameters );
91
92 /*-----------------------------------------------------------*/
93
94 /* Creates the tasks, then starts the scheduler. */
95 void main( void )
96 {
97         /* Initialise the required hardware. */
98         vParTestInitialise();
99         vPortInitialiseBlocks();
100
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 );
104
105         /* Start the standard LED flash tasks as defined in demo/common/minimal. */
106         vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );
107
108         /* Start the check task defined in this file. */
109         xTaskCreate( vCalculationTask, "Check", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
110
111         /* Start the scheduler. */
112         vTaskStartScheduler();
113 }
114 /*-----------------------------------------------------------*/
115
116 static void vCalculationTask( void *pvParameters )
117 {
118 volatile unsigned long ulCalculatedValue; /* Volatile to ensure optimisation is minimal. */
119
120         /* Continuously perform a calculation.  If the calculation result is ever
121         incorrect turn the LED on. */
122         for( ;; )
123         {
124                 /* A good optimising compiler would just remove all this! */
125                 ulCalculatedValue = 1234UL;
126                 ulCalculatedValue *= 99UL;
127
128                 if( ulCalculatedValue != 122166UL )
129                 {
130                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
131                 }
132
133                 ulCalculatedValue *= 9876UL;
134
135                 if( ulCalculatedValue != 1206511416UL )
136                 {
137                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
138                 }
139
140                 ulCalculatedValue /= 15UL;
141
142                 if( ulCalculatedValue != 80434094UL )
143                 {
144                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
145                 }
146
147                 ulCalculatedValue += 918273UL;
148
149                 if( ulCalculatedValue != 81352367UL )
150                 {
151                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );
152                 }
153         }
154 }
155 /*-----------------------------------------------------------*/
156