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