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