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