]> git.sur5r.net Git - freertos/blob - Demo/PIC18_MPLAB/main1.c
Update version numbers to V4.8.0
[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!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /*\r
50  * Instead of the normal single demo application, the PIC18F demo is split \r
51  * into several smaller programs of which this is the first.  This enables the \r
52  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin \r
53  * devices require a more costly development platform and are not so readily \r
54  * available.\r
55  *\r
56  * The RTOSDemo1 project is configured for a PIC18F452 device.  Main1.c starts 5 \r
57  * tasks (including the idle task).\r
58  *\r
59  * The first task runs at the idle priority.  It repeatedly performs a 32bit \r
60  * calculation and checks it's result against the expected value.  This checks \r
61  * that the temporary storage utilised by the compiler to hold intermediate \r
62  * results does not get corrupted when the task gets switched in and out.  See \r
63  * demo/common/minimal/integer.c for more information.\r
64  *\r
65  * The second and third tasks pass an incrementing value between each other on \r
66  * a message queue.  See demo/common/minimal/PollQ.c for more information.\r
67  *\r
68  * Main1.c also creates a check task.  This periodically checks that all the \r
69  * other tasks are still running and have not experienced any unexpected \r
70  * results.  If all the other tasks are executing correctly an LED is flashed \r
71  * once every mainCHECK_PERIOD milliseconds.  If any of the tasks have not \r
72  * executed, or report and error, the frequency of the LED flash will increase \r
73  * to mainERROR_FLASH_RATE.\r
74  *\r
75  * On entry to main an 'X' is transmitted.  Monitoring the serial port using a\r
76  * dumb terminal allows for verification that the device is not continuously \r
77  * being reset (no more than one 'X' should be transmitted).\r
78  *\r
79  * http://www.FreeRTOS.org contains important information on the use of the \r
80  * PIC18F port.\r
81  */\r
82 \r
83 /*\r
84 Changes from V2.0.0\r
85 \r
86         + Delay periods are now specified using variables and constants of\r
87           portTickType rather than unsigned portLONG.\r
88 */\r
89 \r
90 /* Scheduler include files. */\r
91 #include "FreeRTOS.h"\r
92 #include "task.h"\r
93 \r
94 /* Demo app include files. */\r
95 #include "PollQ.h"\r
96 #include "integer.h"\r
97 #include "partest.h"\r
98 #include "serial.h"\r
99 \r
100 /* The period between executions of the check task before and after an error\r
101 has been discovered.  If an error has been discovered the check task runs\r
102 more frequently - increasing the LED flash rate. */\r
103 #define mainNO_ERROR_CHECK_PERIOD               ( ( portTickType ) 1000 / portTICK_RATE_MS )\r
104 #define mainERROR_CHECK_PERIOD                  ( ( portTickType ) 100 / portTICK_RATE_MS )\r
105 \r
106 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
107 priority. */\r
108 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
109 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
110 \r
111 /* The LED that is flashed by the check task. */\r
112 #define mainCHECK_TASK_LED                              ( 0 )\r
113 \r
114 /* Constants required for the communications.  Only one character is ever \r
115 transmitted. */\r
116 #define mainCOMMS_QUEUE_LENGTH                  ( 5 )\r
117 #define mainNO_BLOCK                                    ( ( portTickType ) 0 )\r
118 #define mainBAUD_RATE                                   ( ( unsigned portLONG ) 9600 )\r
119 \r
120 /*\r
121  * The task function for the "Check" task.\r
122  */\r
123 static void vErrorChecks( void *pvParameters );\r
124 \r
125 /*\r
126  * Checks the unique counts of other tasks to ensure they are still operational.\r
127  * Returns pdTRUE if an error is detected, otherwise pdFALSE.\r
128  */\r
129 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* Creates the tasks, then starts the scheduler. */\r
134 void main( void )\r
135 {\r
136         /* Initialise the required hardware. */\r
137         vParTestInitialise();\r
138         vPortInitialiseBlocks();\r
139 \r
140         /* Send a character so we have some visible feedback of a reset. */\r
141         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
142         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
143 \r
144         /* Start the standard demo tasks found in the demo\common directory. */\r
145         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
146         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
147 \r
148         /* Start the check task defined in this file. */\r
149         xTaskCreate( vErrorChecks, ( const portCHAR * const ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
150 \r
151         /* Start the scheduler.  Will never return here. */\r
152         vTaskStartScheduler();\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 static void vErrorChecks( void *pvParameters )\r
157 {\r
158 portTickType xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
159 portBASE_TYPE xErrorOccurred;\r
160 \r
161         /* Cycle for ever, delaying then checking all the other tasks are still\r
162         operating without error. */\r
163         for( ;; )\r
164         {\r
165                 /* Wait until it is time to check the other tasks. */\r
166                 vTaskDelay( xDelayTime );\r
167 \r
168                 /* Check all the other tasks are running, and running without ever\r
169                 having an error. */\r
170                 xErrorOccurred = prvCheckOtherTasksAreStillRunning();\r
171 \r
172                 /* If an error was detected increase the frequency of the LED flash. */\r
173                 if( xErrorOccurred == pdTRUE )\r
174                 {\r
175                         xDelayTime = mainERROR_CHECK_PERIOD;\r
176                 }\r
177 \r
178                 /* Flash the LED for visual feedback. */\r
179                 vParTestToggleLED( mainCHECK_TASK_LED );\r
180         }\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )\r
185 {\r
186 portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
187 \r
188         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
189         {\r
190                 xErrorHasOccurred = pdTRUE;\r
191         }\r
192 \r
193         if( xArePollingQueuesStillRunning() != pdTRUE )\r
194         {\r
195                 xErrorHasOccurred = pdTRUE;\r
196         }\r
197 \r
198         return xErrorHasOccurred;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 \r