]> git.sur5r.net Git - freertos/blob - Demo/PIC18_WizC/Demo2/main.c
Ready for V5.2.0 release.
[freertos] / Demo / PIC18_WizC / Demo2 / main.c
1 /*\r
2         FreeRTOS.org V5.2.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 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\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 Changes from V3.0.0\r
54 \r
55 Changes from V3.0.1\r
56 */\r
57 \r
58 /*\r
59  * Instead of the normal single demo application, the PIC18F demo is split \r
60  * into several smaller programs of which this is the second.  This enables the \r
61  * demo's to be executed on the RAM limited PIC-devices.\r
62  *\r
63  * The Demo2 project is configured for a PIC18F4620 device.  Main.c starts 12 \r
64  * tasks (including the idle task). See the indicated files in the demo/common\r
65  * directory for more information.\r
66  *\r
67  * demo/common/minimal/integer.c:       Creates 1 task\r
68  * demo/common/minimal/PollQ.c:         Creates 2 tasks\r
69  * demo/common/minimal/semtest.c:       Creates 4 tasks\r
70  * demo/common/minimal/flash.c:         Creates 3 tasks\r
71  *\r
72  * Main.c also creates a check task.  This periodically checks that all the \r
73  * other tasks are still running and have not experienced any unexpected \r
74  * results.  If all the other tasks are executing correctly an LED is flashed \r
75  * once every mainCHECK_PERIOD milliseconds.  If any of the tasks have not \r
76  * executed, or report an error, the frequency of the LED flash will increase \r
77  * to mainERROR_FLASH_RATE.\r
78  *\r
79  * On entry to main an 'X' is transmitted.  Monitoring the serial port using a\r
80  * dumb terminal allows for verification that the device is not continuously \r
81  * being reset (no more than one 'X' should be transmitted).\r
82  *\r
83  * http://www.FreeRTOS.org contains important information on the use of the \r
84  * wizC PIC18F port.\r
85  */\r
86 \r
87 /* Scheduler include files. */\r
88 #include <FreeRTOS.h>\r
89 #include <task.h>\r
90 \r
91 /* Demo app include files. */\r
92 #include "integer.h"\r
93 #include "PollQ.h"\r
94 #include "semtest.h"\r
95 #include "flash.h"\r
96 #include "partest.h"\r
97 #include "serial.h"\r
98 \r
99 /* The period between executions of the check task before and after an error\r
100 has been discovered.  If an error has been discovered the check task runs\r
101 more frequently - increasing the LED flash rate. */\r
102 #define mainNO_ERROR_CHECK_PERIOD       ( ( portTickType ) 10000 / portTICK_RATE_MS )\r
103 #define mainERROR_CHECK_PERIOD          ( ( portTickType )  1000 / portTICK_RATE_MS )\r
104 #define mainCHECK_TASK_LED                      ( ( unsigned portCHAR ) 3 )\r
105 \r
106 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
107 priority. */\r
108 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 3 )\r
109 #define mainLED_FLASH_PRIORITY  ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 2 )\r
110 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 1 )\r
111 #define mainSEM_TEST_PRIORITY   ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 1 )\r
112 #define mainINTEGER_PRIORITY    ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 0 )\r
113 \r
114 /* Constants required for the communications.  Only one character is ever \r
115 transmitted. */\r
116 #define mainCOMMS_QUEUE_LENGTH          ( ( unsigned portCHAR ) 5 )\r
117 #define mainNO_BLOCK                            ( ( portTickType ) 0 )\r
118 #define mainBAUD_RATE                           ( ( unsigned portLONG ) 57600 )\r
119 \r
120 /*\r
121  * The task function for the "Check" task.\r
122  */\r
123 static portTASK_FUNCTION_PROTO( vErrorChecks, 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 portCHAR 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 \r
139         /* Send a character so we have some visible feedback of a reset. */\r
140         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
141         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
142 \r
143         /* Start a few of the standard demo tasks found in the demo\common directory. */\r
144         vStartIntegerMathTasks( mainINTEGER_PRIORITY);\r
145         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
146         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
147         vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );\r
148 \r
149         /* Start the check task defined in this file. */\r
150         xTaskCreate( vErrorChecks, ( const portCHAR * const ) "Check", portMINIMAL_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         while(1)        /* This point should never be reached. */\r
156         {\r
157         }\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 static portTASK_FUNCTION( vErrorChecks, pvParameters )\r
162 {\r
163 portTickType xLastCheckTime;\r
164 portTickType xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
165 portCHAR cErrorOccurred;\r
166 \r
167         /* We need to initialise xLastCheckTime prior to the first call to \r
168         vTaskDelayUntil(). */\r
169         xLastCheckTime = xTaskGetTickCount();\r
170         \r
171         /* Cycle for ever, delaying then checking all the other tasks are still\r
172         operating without error. */\r
173         for( ;; )\r
174         {\r
175                 /* Wait until it is time to check the other tasks again. */\r
176                 vTaskDelayUntil( &xLastCheckTime, xDelayTime );\r
177                 \r
178                 /* Check all the other tasks are running, and running without ever\r
179                 having an error. */\r
180                 cErrorOccurred = prvCheckOtherTasksAreStillRunning();\r
181 \r
182                 /* If an error was detected increase the frequency of the LED flash. */\r
183                 if( cErrorOccurred == pdTRUE )\r
184                 {\r
185                         xDelayTime = mainERROR_CHECK_PERIOD;\r
186                 }\r
187 \r
188                 /* Flash the LED for visual feedback. */\r
189                 vParTestToggleLED( mainCHECK_TASK_LED );\r
190         }\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static portCHAR prvCheckOtherTasksAreStillRunning( void )\r
195 {\r
196         portCHAR cErrorHasOccurred = ( portCHAR ) pdFALSE;\r
197 \r
198         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
199         {\r
200                 cErrorHasOccurred = ( portCHAR ) pdTRUE;\r
201         }\r
202 \r
203         if( xArePollingQueuesStillRunning() != pdTRUE )\r
204         {\r
205                 cErrorHasOccurred = ( portCHAR ) pdTRUE;\r
206         }\r
207 \r
208         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
209         {\r
210                 cErrorHasOccurred = ( portCHAR ) pdTRUE;\r
211         }\r
212 \r
213         return cErrorHasOccurred;\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 \r