]> git.sur5r.net Git - freertos/blob - Demo/PIC18_MPLAB/main1.c
Prepare for V5.3.0 release.
[freertos] / Demo / PIC18_MPLAB / main1.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53  * Instead of the normal single demo application, the PIC18F demo is split \r
54  * into several smaller programs of which this is the first.  This enables the \r
55  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin \r
56  * devices require a more costly development platform and are not so readily \r
57  * available.\r
58  *\r
59  * The RTOSDemo1 project is configured for a PIC18F452 device.  Main1.c starts 5 \r
60  * tasks (including the idle task).\r
61  *\r
62  * The first task runs at the idle priority.  It repeatedly performs a 32bit \r
63  * calculation and checks it's result against the expected value.  This checks \r
64  * that the temporary storage utilised by the compiler to hold intermediate \r
65  * results does not get corrupted when the task gets switched in and out.  See \r
66  * demo/common/minimal/integer.c for more information.\r
67  *\r
68  * The second and third tasks pass an incrementing value between each other on \r
69  * a message queue.  See demo/common/minimal/PollQ.c for more information.\r
70  *\r
71  * Main1.c also creates a check task.  This periodically checks that all the \r
72  * other tasks are still running and have not experienced any unexpected \r
73  * results.  If all the other tasks are executing correctly an LED is flashed \r
74  * once every mainCHECK_PERIOD milliseconds.  If any of the tasks have not \r
75  * executed, or report and error, the frequency of the LED flash will increase \r
76  * to mainERROR_FLASH_RATE.\r
77  *\r
78  * On entry to main an 'X' is transmitted.  Monitoring the serial port using a\r
79  * dumb terminal allows for verification that the device is not continuously \r
80  * being reset (no more than one 'X' should be transmitted).\r
81  *\r
82  * http://www.FreeRTOS.org contains important information on the use of the \r
83  * PIC18F port.\r
84  */\r
85 \r
86 /*\r
87 Changes from V2.0.0\r
88 \r
89         + Delay periods are now specified using variables and constants of\r
90           portTickType rather than unsigned portLONG.\r
91 */\r
92 \r
93 /* Scheduler include files. */\r
94 #include "FreeRTOS.h"\r
95 #include "task.h"\r
96 \r
97 /* Demo app include files. */\r
98 #include "PollQ.h"\r
99 #include "integer.h"\r
100 #include "partest.h"\r
101 #include "serial.h"\r
102 \r
103 /* The period between executions of the check task before and after an error\r
104 has been discovered.  If an error has been discovered the check task runs\r
105 more frequently - increasing the LED flash rate. */\r
106 #define mainNO_ERROR_CHECK_PERIOD               ( ( portTickType ) 1000 / portTICK_RATE_MS )\r
107 #define mainERROR_CHECK_PERIOD                  ( ( portTickType ) 100 / portTICK_RATE_MS )\r
108 \r
109 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
110 priority. */\r
111 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
112 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
113 \r
114 /* The LED that is flashed by the check task. */\r
115 #define mainCHECK_TASK_LED                              ( 0 )\r
116 \r
117 /* Constants required for the communications.  Only one character is ever \r
118 transmitted. */\r
119 #define mainCOMMS_QUEUE_LENGTH                  ( 5 )\r
120 #define mainNO_BLOCK                                    ( ( portTickType ) 0 )\r
121 #define mainBAUD_RATE                                   ( ( unsigned portLONG ) 9600 )\r
122 \r
123 /*\r
124  * The task function for the "Check" task.\r
125  */\r
126 static void vErrorChecks( void *pvParameters );\r
127 \r
128 /*\r
129  * Checks the unique counts of other tasks to ensure they are still operational.\r
130  * Returns pdTRUE if an error is detected, otherwise pdFALSE.\r
131  */\r
132 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /* Creates the tasks, then starts the scheduler. */\r
137 void main( void )\r
138 {\r
139         /* Initialise the required hardware. */\r
140         vParTestInitialise();\r
141         vPortInitialiseBlocks();\r
142 \r
143         /* Send a character so we have some visible feedback of a reset. */\r
144         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
145         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
146 \r
147         /* Start the standard demo tasks found in the demo\common directory. */\r
148         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
149         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
150 \r
151         /* Start the check task defined in this file. */\r
152         xTaskCreate( vErrorChecks, ( const portCHAR * const ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
153 \r
154         /* Start the scheduler.  Will never return here. */\r
155         vTaskStartScheduler();\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 static void vErrorChecks( void *pvParameters )\r
160 {\r
161 portTickType xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
162 portBASE_TYPE xErrorOccurred;\r
163 \r
164         /* Cycle for ever, delaying then checking all the other tasks are still\r
165         operating without error. */\r
166         for( ;; )\r
167         {\r
168                 /* Wait until it is time to check the other tasks. */\r
169                 vTaskDelay( xDelayTime );\r
170 \r
171                 /* Check all the other tasks are running, and running without ever\r
172                 having an error. */\r
173                 xErrorOccurred = prvCheckOtherTasksAreStillRunning();\r
174 \r
175                 /* If an error was detected increase the frequency of the LED flash. */\r
176                 if( xErrorOccurred == pdTRUE )\r
177                 {\r
178                         xDelayTime = mainERROR_CHECK_PERIOD;\r
179                 }\r
180 \r
181                 /* Flash the LED for visual feedback. */\r
182                 vParTestToggleLED( mainCHECK_TASK_LED );\r
183         }\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )\r
188 {\r
189 portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
190 \r
191         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
192         {\r
193                 xErrorHasOccurred = pdTRUE;\r
194         }\r
195 \r
196         if( xArePollingQueuesStillRunning() != pdTRUE )\r
197         {\r
198                 xErrorHasOccurred = pdTRUE;\r
199         }\r
200 \r
201         return xErrorHasOccurred;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 \r