]> git.sur5r.net Git - freertos/blob - Demo/PIC18_MPLAB/main2.c
Update to V6.1.1
[freertos] / Demo / PIC18_MPLAB / main2.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Instead of the normal single demo application, the PIC18F demo is split \r
56  * into several smaller programs of which this is the second.  This enables the \r
57  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin \r
58  * devices require a more costly development platform and are not so readily \r
59  * available.\r
60  *\r
61  * The RTOSDemo2 project is configured for a PIC18F452 device.  Main2.c starts  \r
62  * 5 tasks (including the idle task).\r
63  * \r
64  * The first, second and third tasks do nothing but flash an LED.  This gives\r
65  * visual feedback that everything is executing as expected.  One task flashes\r
66  * an LED every 333ms (i.e. on and off every 333/2 ms), then next every 666ms\r
67  * and the last every 999ms.\r
68  *\r
69  * The last task runs at the idle priority.  It repeatedly performs a 32bit \r
70  * calculation and checks it's result against the expected value.  This checks \r
71  * that the temporary storage utilised by the compiler to hold intermediate \r
72  * results does not get corrupted when the task gets switched in and out.\r
73  * should the calculation ever provide an incorrect result the final LED is\r
74  * turned on.\r
75  *\r
76  * On entry to main() an 'X' is transmitted.  Monitoring the serial port using a\r
77  * dumb terminal allows for verification that the device is not continuously \r
78  * being reset (no more than one 'X' should be transmitted).\r
79  *\r
80  * http://www.FreeRTOS.org contains important information on the use of the \r
81  * PIC18F port.\r
82  */\r
83 \r
84 /*\r
85 Changes from V2.0.0\r
86 \r
87         + Delay periods are now specified using variables and constants of\r
88           portTickType rather than unsigned long.\r
89 */\r
90 \r
91 /* Scheduler include files. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 \r
95 /* Demo app include files. */\r
96 #include "flash.h"\r
97 #include "partest.h"\r
98 #include "serial.h"\r
99 \r
100 /* Priority definitions for the LED tasks.  Other tasks just use the idle\r
101 priority. */\r
102 #define mainLED_FLASH_PRIORITY                  ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 1 )\r
103 \r
104 /* The LED that is lit when should the calculation fail. */\r
105 #define mainCHECK_TASK_LED                              ( ( unsigned portBASE_TYPE ) 3 )\r
106 \r
107 /* Constants required for the communications.  Only one character is ever \r
108 transmitted. */\r
109 #define mainCOMMS_QUEUE_LENGTH                  ( ( unsigned portBASE_TYPE ) 5 )\r
110 #define mainNO_BLOCK                                    ( ( portTickType ) 0 )\r
111 #define mainBAUD_RATE                                   ( ( unsigned long ) 9600 )\r
112 \r
113 /*\r
114  * The task that performs the 32 bit calculation at the idle priority.\r
115  */\r
116 static void vCalculationTask( void *pvParameters );\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /* Creates the tasks, then starts the scheduler. */\r
121 void main( void )\r
122 {\r
123         /* Initialise the required hardware. */\r
124         vParTestInitialise();\r
125         vPortInitialiseBlocks();\r
126 \r
127         /* Send a character so we have some visible feedback of a reset. */\r
128         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
129         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
130 \r
131         /* Start the standard LED flash tasks as defined in demo/common/minimal. */\r
132         vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );\r
133 \r
134         /* Start the check task defined in this file. */\r
135         xTaskCreate( vCalculationTask, ( const char * const ) "Check", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
136 \r
137         /* Start the scheduler. */\r
138         vTaskStartScheduler();\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 static void vCalculationTask( void *pvParameters )\r
143 {\r
144 volatile unsigned long ulCalculatedValue; /* Volatile to ensure optimisation is minimal. */\r
145 \r
146         /* Continuously perform a calculation.  If the calculation result is ever\r
147         incorrect turn the LED on. */\r
148         for( ;; )\r
149         {\r
150                 /* A good optimising compiler would just remove all this! */\r
151                 ulCalculatedValue = 1234UL;\r
152                 ulCalculatedValue *= 99UL;\r
153 \r
154                 if( ulCalculatedValue != 122166UL )\r
155                 {\r
156                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );\r
157                 }\r
158 \r
159                 ulCalculatedValue *= 9876UL;\r
160 \r
161                 if( ulCalculatedValue != 1206511416UL )\r
162                 {\r
163                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );\r
164                 }\r
165 \r
166                 ulCalculatedValue /= 15UL;\r
167 \r
168                 if( ulCalculatedValue != 80434094UL )\r
169                 {\r
170                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );\r
171                 }\r
172 \r
173                 ulCalculatedValue += 918273UL;\r
174 \r
175                 if( ulCalculatedValue != 81352367UL )\r
176                 {\r
177                         vParTestSetLED( mainCHECK_TASK_LED, pdTRUE );\r
178                 }\r
179         }\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r