]> git.sur5r.net Git - freertos/blob - Demo/PIC18_WizC/Demo2/main.c
Update to V6.0.2.
[freertos] / Demo / PIC18_WizC / Demo2 / main.c
1 /*\r
2     FreeRTOS V6.0.2 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55 Changes from V3.0.0\r
56 \r
57 Changes from V3.0.1\r
58 */\r
59 \r
60 /*\r
61  * Instead of the normal single demo application, the PIC18F demo is split \r
62  * into several smaller programs of which this is the second.  This enables the \r
63  * demo's to be executed on the RAM limited PIC-devices.\r
64  *\r
65  * The Demo2 project is configured for a PIC18F4620 device.  Main.c starts 12 \r
66  * tasks (including the idle task). See the indicated files in the demo/common\r
67  * directory for more information.\r
68  *\r
69  * demo/common/minimal/integer.c:       Creates 1 task\r
70  * demo/common/minimal/PollQ.c:         Creates 2 tasks\r
71  * demo/common/minimal/semtest.c:       Creates 4 tasks\r
72  * demo/common/minimal/flash.c:         Creates 3 tasks\r
73  *\r
74  * Main.c also creates a check task.  This periodically checks that all the \r
75  * other tasks are still running and have not experienced any unexpected \r
76  * results.  If all the other tasks are executing correctly an LED is flashed \r
77  * once every mainCHECK_PERIOD milliseconds.  If any of the tasks have not \r
78  * executed, or report an error, the frequency of the LED flash will increase \r
79  * to mainERROR_FLASH_RATE.\r
80  *\r
81  * On entry to main an 'X' is transmitted.  Monitoring the serial port using a\r
82  * dumb terminal allows for verification that the device is not continuously \r
83  * being reset (no more than one 'X' should be transmitted).\r
84  *\r
85  * http://www.FreeRTOS.org contains important information on the use of the \r
86  * wizC PIC18F port.\r
87  */\r
88 \r
89 /* Scheduler include files. */\r
90 #include <FreeRTOS.h>\r
91 #include <task.h>\r
92 \r
93 /* Demo app include files. */\r
94 #include "integer.h"\r
95 #include "PollQ.h"\r
96 #include "semtest.h"\r
97 #include "flash.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 ) 10000 / portTICK_RATE_MS )\r
105 #define mainERROR_CHECK_PERIOD          ( ( portTickType )  1000 / portTICK_RATE_MS )\r
106 #define mainCHECK_TASK_LED                      ( ( unsigned char ) 3 )\r
107 \r
108 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
109 priority. */\r
110 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + ( unsigned char ) 3 )\r
111 #define mainLED_FLASH_PRIORITY  ( tskIDLE_PRIORITY + ( unsigned char ) 2 )\r
112 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + ( unsigned char ) 1 )\r
113 #define mainSEM_TEST_PRIORITY   ( tskIDLE_PRIORITY + ( unsigned char ) 1 )\r
114 #define mainINTEGER_PRIORITY    ( tskIDLE_PRIORITY + ( unsigned char ) 0 )\r
115 \r
116 /* Constants required for the communications.  Only one character is ever \r
117 transmitted. */\r
118 #define mainCOMMS_QUEUE_LENGTH          ( ( unsigned char ) 5 )\r
119 #define mainNO_BLOCK                            ( ( portTickType ) 0 )\r
120 #define mainBAUD_RATE                           ( ( unsigned long ) 57600 )\r
121 \r
122 /*\r
123  * The task function for the "Check" task.\r
124  */\r
125 static portTASK_FUNCTION_PROTO( vErrorChecks, pvParameters );\r
126 \r
127 /*\r
128  * Checks the unique counts of other tasks to ensure they are still operational.\r
129  * Returns pdTRUE if an error is detected, otherwise pdFALSE.\r
130  */\r
131 static char prvCheckOtherTasksAreStillRunning( void );\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 /* Creates the tasks, then starts the scheduler. */\r
136 void main( void )\r
137 {\r
138         /* Initialise the required hardware. */\r
139         vParTestInitialise();\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 a few of the standard demo tasks found in the demo\common directory. */\r
146         vStartIntegerMathTasks( mainINTEGER_PRIORITY);\r
147         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
148         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
149         vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );\r
150 \r
151         /* Start the check task defined in this file. */\r
152         xTaskCreate( vErrorChecks, ( const char * const ) "Check", portMINIMAL_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         while(1)        /* This point should never be reached. */\r
158         {\r
159         }\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 static portTASK_FUNCTION( vErrorChecks, pvParameters )\r
164 {\r
165 portTickType xLastCheckTime;\r
166 portTickType xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
167 char cErrorOccurred;\r
168 \r
169         /* We need to initialise xLastCheckTime prior to the first call to \r
170         vTaskDelayUntil(). */\r
171         xLastCheckTime = xTaskGetTickCount();\r
172         \r
173         /* Cycle for ever, delaying then checking all the other tasks are still\r
174         operating without error. */\r
175         for( ;; )\r
176         {\r
177                 /* Wait until it is time to check the other tasks again. */\r
178                 vTaskDelayUntil( &xLastCheckTime, xDelayTime );\r
179                 \r
180                 /* Check all the other tasks are running, and running without ever\r
181                 having an error. */\r
182                 cErrorOccurred = prvCheckOtherTasksAreStillRunning();\r
183 \r
184                 /* If an error was detected increase the frequency of the LED flash. */\r
185                 if( cErrorOccurred == pdTRUE )\r
186                 {\r
187                         xDelayTime = mainERROR_CHECK_PERIOD;\r
188                 }\r
189 \r
190                 /* Flash the LED for visual feedback. */\r
191                 vParTestToggleLED( mainCHECK_TASK_LED );\r
192         }\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static char prvCheckOtherTasksAreStillRunning( void )\r
197 {\r
198         char cErrorHasOccurred = ( char ) pdFALSE;\r
199 \r
200         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
201         {\r
202                 cErrorHasOccurred = ( char ) pdTRUE;\r
203         }\r
204 \r
205         if( xArePollingQueuesStillRunning() != pdTRUE )\r
206         {\r
207                 cErrorHasOccurred = ( char ) pdTRUE;\r
208         }\r
209 \r
210         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
211         {\r
212                 cErrorHasOccurred = ( char ) pdTRUE;\r
213         }\r
214 \r
215         return cErrorHasOccurred;\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 \r