]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_MPLAB/main2.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PIC18_MPLAB / main2.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  * Instead of the normal single demo application, the PIC18F demo is split\r
31  * into several smaller programs of which this is the second.  This enables the\r
32  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin\r
33  * devices require a more costly development platform and are not so readily\r
34  * available.\r
35  *\r
36  * The RTOSDemo2 project is configured for a PIC18F452 device.  Main2.c starts\r
37  * 5 tasks (including the idle task).\r
38  *\r
39  * The first, second and third tasks do nothing but flash an LED.  This gives\r
40  * visual feedback that everything is executing as expected.  One task flashes\r
41  * an LED every 333ms (i.e. on and off every 333/2 ms), then next every 666ms\r
42  * and the last every 999ms.\r
43  *\r
44  * The last task runs at the idle priority.  It repeatedly performs a 32bit\r
45  * calculation and checks it's result against the expected value.  This checks\r
46  * that the temporary storage utilised by the compiler to hold intermediate\r
47  * results does not get corrupted when the task gets switched in and out.\r
48  * should the calculation ever provide an incorrect result the final LED is\r
49  * turned on.\r
50  *\r
51  * On entry to main() an 'X' is transmitted.  Monitoring the serial port using a\r
52  * dumb terminal allows for verification that the device is not continuously\r
53  * being reset (no more than one 'X' should be transmitted).\r
54  *\r
55  * http://www.FreeRTOS.org contains important information on the use of the\r
56  * PIC18F port.\r
57  */\r
58 \r
59 /*\r
60 Changes from V2.0.0\r
61 \r
62         + Delay periods are now specified using variables and constants of\r
63           TickType_t rather than unsigned long.\r
64 */\r
65 \r
66 /* Scheduler include files. */\r
67 #include "FreeRTOS.h"\r
68 #include "task.h"\r
69 \r
70 /* Demo app include files. */\r
71 #include "flash.h"\r
72 #include "partest.h"\r
73 #include "serial.h"\r
74 \r
75 /* Priority definitions for the LED tasks.  Other tasks just use the idle\r
76 priority. */\r
77 #define mainLED_FLASH_PRIORITY                  ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 1 )\r
78 \r
79 /* The LED that is lit when should the calculation fail. */\r
80 #define mainCHECK_TASK_LED                              ( ( unsigned portBASE_TYPE ) 3 )\r
81 \r
82 /* Constants required for the communications.  Only one character is ever\r
83 transmitted. */\r
84 #define mainCOMMS_QUEUE_LENGTH                  ( ( unsigned portBASE_TYPE ) 5 )\r
85 #define mainNO_BLOCK                                    ( ( TickType_t ) 0 )\r
86 #define mainBAUD_RATE                                   ( ( unsigned long ) 9600 )\r
87 \r
88 /*\r
89  * The task that performs the 32 bit calculation at the idle priority.\r
90  */\r
91 static void vCalculationTask( void *pvParameters );\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* Creates the tasks, then starts the scheduler. */\r
96 void main( void )\r
97 {\r
98         /* Initialise the required hardware. */\r
99         vParTestInitialise();\r
100         vPortInitialiseBlocks();\r
101 \r
102         /* Send a character so we have some visible feedback of a reset. */\r
103         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
104         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
105 \r
106         /* Start the standard LED flash tasks as defined in demo/common/minimal. */\r
107         vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );\r
108 \r
109         /* Start the check task defined in this file. */\r
110         xTaskCreate( vCalculationTask, "Check", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
111 \r
112         /* Start the scheduler. */\r
113         vTaskStartScheduler();\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 static void vCalculationTask( void *pvParameters )\r
118 {\r
119 volatile unsigned long ulCalculatedValue; /* Volatile to ensure optimisation is minimal. */\r
120 \r
121         /* Continuously perform a calculation.  If the calculation result is ever\r
122         incorrect turn the LED on. */\r
123         for( ;; )\r
124         {\r
125                 /* A good optimising compiler would just remove all this! */\r
126                 ulCalculatedValue = 1234UL;\r
127                 ulCalculatedValue *= 99UL;\r
128 \r
129                 if( ulCalculatedValue != 122166UL )\r
130                 {\r
131                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );\r
132                 }\r
133 \r
134                 ulCalculatedValue *= 9876UL;\r
135 \r
136                 if( ulCalculatedValue != 1206511416UL )\r
137                 {\r
138                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );\r
139                 }\r
140 \r
141                 ulCalculatedValue /= 15UL;\r
142 \r
143                 if( ulCalculatedValue != 80434094UL )\r
144                 {\r
145                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );\r
146                 }\r
147 \r
148                 ulCalculatedValue += 918273UL;\r
149 \r
150                 if( ulCalculatedValue != 81352367UL )\r
151                 {\r
152                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );\r
153                 }\r
154         }\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r