]> git.sur5r.net Git - freertos/blob - Demo/PIC18_WizC/Demo4/main.c
Update to V4.7.0.
[freertos] / Demo / PIC18_WizC / Demo4 / main.c
1 /*\r
2         FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 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         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com a version that has been certified for use\r
32         in safety critical systems, plus commercial licensing, development and\r
33         support options.\r
34         ***************************************************************************\r
35 */\r
36 \r
37 /*\r
38 Changes from V3.0.0\r
39 \r
40 Changes from V3.0.1\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 fourth.  This enables the \r
46  * demo's to be executed on the RAM limited PIC-devices.\r
47  *\r
48  * The Demo4 project is configured for a PIC18F4620 device.  Main.c starts 11 \r
49  * tasks (including the idle task). See the indicated files in the demo/common\r
50  * directory for more information.\r
51  *\r
52  * demo/common/minimal/integer.c:       Creates 1 task\r
53  * demo/common/minimal/dynamic.c:       Creates 5 tasks\r
54  * demo/common/minimal/flash.c:         Creates 3 tasks\r
55  *\r
56  * Main.c also creates a check task.  This periodically checks that all the \r
57  * other tasks are still running and have not experienced any unexpected \r
58  * results.  If all the other tasks are executing correctly an LED is flashed \r
59  * once every mainCHECK_PERIOD milliseconds.  If any of the tasks have not \r
60  * executed, or report an error, the frequency of the LED flash will increase \r
61  * to mainERROR_FLASH_RATE.\r
62  *\r
63  * On entry to main an 'X' is transmitted.  Monitoring the serial port using a\r
64  * dumb terminal allows for verification that the device is not continuously \r
65  * being reset (no more than one 'X' should be transmitted).\r
66  *\r
67  * http://www.FreeRTOS.org contains important information on the use of the \r
68  * wizC PIC18F port.\r
69  */\r
70 \r
71 /* Scheduler include files. */\r
72 #include <FreeRTOS.h>\r
73 #include <task.h>\r
74 \r
75 /* Demo app include files. */\r
76 #include "integer.h"\r
77 #include "dynamic.h"\r
78 #include "flash.h"\r
79 #include "partest.h"\r
80 #include "serial.h"\r
81 \r
82 /* The period between executions of the check task before and after an error\r
83 has been discovered.  If an error has been discovered the check task runs\r
84 more frequently - increasing the LED flash rate. */\r
85 #define mainNO_ERROR_CHECK_PERIOD       ( ( portTickType ) 10000 / portTICK_RATE_MS )\r
86 #define mainERROR_CHECK_PERIOD          ( ( portTickType )  1000 / portTICK_RATE_MS )\r
87 #define mainCHECK_TASK_LED                      ( ( unsigned portCHAR ) 3 )\r
88 \r
89 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
90 priority. */\r
91 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 3 )\r
92 #define mainLED_FLASH_PRIORITY  ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 2 )\r
93 #define mainINTEGER_PRIORITY    ( tskIDLE_PRIORITY + ( unsigned portCHAR ) 0 )\r
94 \r
95 /* Constants required for the communications.  Only one character is ever \r
96 transmitted. */\r
97 #define mainCOMMS_QUEUE_LENGTH          ( ( unsigned portCHAR ) 5 )\r
98 #define mainNO_BLOCK                            ( ( portTickType ) 0 )\r
99 #define mainBAUD_RATE                           ( ( unsigned portLONG ) 57600 )\r
100 \r
101 /*\r
102  * The task function for the "Check" task.\r
103  */\r
104 static portTASK_FUNCTION_PROTO( vErrorChecks, pvParameters );\r
105 \r
106 /*\r
107  * Checks the unique counts of other tasks to ensure they are still operational.\r
108  * Returns pdTRUE if an error is detected, otherwise pdFALSE.\r
109  */\r
110 static portCHAR prvCheckOtherTasksAreStillRunning( void );\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /* Creates the tasks, then starts the scheduler. */\r
115 void main( void )\r
116 {\r
117         /* Initialise the required hardware. */\r
118         vParTestInitialise();\r
119 \r
120         /* Send a character so we have some visible feedback of a reset. */\r
121         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
122         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
123 \r
124         /* Start the standard demo tasks found in the demo\common directory. */\r
125         vStartIntegerMathTasks( mainINTEGER_PRIORITY);\r
126         vStartDynamicPriorityTasks();\r
127         vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );\r
128 \r
129         /* Start the check task defined in this file. */\r
130         xTaskCreate( vErrorChecks, ( const portCHAR * const ) "Check", portMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
131 \r
132         /* Start the scheduler.  Will never return here. */\r
133         vTaskStartScheduler( );\r
134 \r
135         while(1)        /* This point should never be reached. */\r
136         {\r
137         }\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 static portTASK_FUNCTION( vErrorChecks, pvParameters )\r
142 {\r
143         portTickType xLastCheckTime;\r
144         portTickType xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
145         portCHAR cErrorOccurred;\r
146 \r
147         /* We need to initialise xLastCheckTime prior to the first call to \r
148         vTaskDelayUntil(). */\r
149         xLastCheckTime = xTaskGetTickCount();\r
150         \r
151         /* Cycle for ever, delaying then checking all the other tasks are still\r
152         operating without error. */\r
153         for( ;; )\r
154         {\r
155                 /* Wait until it is time to check the other tasks again. */\r
156                 vTaskDelayUntil( &xLastCheckTime, xDelayTime );\r
157 \r
158                 /* Check all the other tasks are running, and running without ever\r
159                 having an error. */\r
160                 cErrorOccurred = prvCheckOtherTasksAreStillRunning();\r
161 \r
162                 /* If an error was detected increase the frequency of the LED flash. */\r
163                 if( cErrorOccurred == pdTRUE )\r
164                 {\r
165                         xDelayTime = mainERROR_CHECK_PERIOD;\r
166                 }\r
167 \r
168                 /* Flash the LED for visual feedback. */\r
169                 vParTestToggleLED( mainCHECK_TASK_LED );\r
170         }\r
171 }\r
172 \r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static portCHAR prvCheckOtherTasksAreStillRunning( void )\r
176 {\r
177         portCHAR cErrorHasOccurred = ( portCHAR ) pdFALSE;\r
178 \r
179         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
180         {\r
181                 cErrorHasOccurred = ( portCHAR ) pdTRUE;\r
182         }\r
183 \r
184         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
185         {\r
186                 cErrorHasOccurred = ( portCHAR ) pdTRUE;\r
187         }\r
188 \r
189         return cErrorHasOccurred;\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 \r