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