]> git.sur5r.net Git - freertos/blob - Demo/PIC24_MPLAB/main.c
V4.2.1 files.
[freertos] / Demo / PIC24_MPLAB / main.c
1 /*\r
2         FreeRTOS.org V4.2.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 for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
38  * documentation provides more details of the standard demo application tasks.\r
39  * In addition to the standard demo tasks, the following tasks are defined\r
40  * within this file:\r
41  * \r
42  * "Register test" tasks - These tasks first set all the general purpose \r
43  * registers to a known value (with each register containing a different value)\r
44  * then test each general purpose register to ensure it still contains the\r
45  * set value.  There are two register test tasks, with different values being\r
46  * used by each.  The register test tasks will be preempted frequently due to\r
47  * their low priority.  Setting then testing the value of each register in this\r
48  * manner ensures the context of the tasks is being correctly saved and then\r
49  * restored as the preemptive context switches occur.  An error is flagged\r
50  * should any register be found to contain an unexpected value.  In addition\r
51  * the register test tasks maintain a count of the number of times they cycle, \r
52  * so an error can also be flagged should the cycle count not increment as\r
53  * expected (indicating the the tasks are not executing at all).\r
54  *\r
55  * "Check" task -  This only executes every three seconds but has the highest \r
56  * priority so is guaranteed to get processor time.  Its main function is to \r
57  * check that all the other tasks are still operational.  Each task maintains a \r
58  * unique count that is incremented each time the task successfully completes \r
59  * its function.  Should any error occur within such a task the count is \r
60  * permanently halted.  The check task inspects the count of each task to \r
61  * ensure it has changed since the last time the check task executed.  If all \r
62  * the count variables have changed all the tasks are still executing error \r
63  * free, and the check task toggles the onboard LED.  Should any task contain \r
64  * an error at any time check task cycle frequency is increased to 500ms, \r
65  * causing the LED toggle rate to increase from 3 seconds to 500ms and in so\r
66  * doing providing visual feedback that an error has occurred.\r
67  *\r
68  */\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 #include "croutine.h"\r
74 \r
75 /* Demo application includes. */\r
76 #include "BlockQ.h"\r
77 #include "crflash.h"\r
78 #include "blocktim.h"\r
79 #include "integer.h"\r
80 #include "comtest2.h"\r
81 #include "partest.h"\r
82 \r
83 /* Demo task priorities. */\r
84 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
85 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
86 #define mainCOM_TEST_PRIORITY                           ( 2 )\r
87 \r
88 /* Delay between check task cycles when an error has/has not been detected. */\r
89 #define mainNO_ERROR_DELAY                                      ( ( portTickType ) 3000 / portTICK_RATE_MS )\r
90 #define mainERROR_DELAY                                         ( ( portTickType ) 500 / portTICK_RATE_MS )\r
91 \r
92 /* The number of flash co-routines to create. */\r
93 #define mainNUM_FLASH_COROUTINES                        ( 3 )\r
94 \r
95 /* Baud rate used by the comtest tasks. */\r
96 #define mainCOM_TEST_BAUD_RATE                          ( 19200 )\r
97 \r
98 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.\r
99 See the comtest.c file for more information. */\r
100 #define mainCOM_TEST_LED                                        ( 4 )\r
101 \r
102 /* The LED used by the check task. */\r
103 #define mainCHECK_LED                                           ( 7 )\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /*\r
108  * The register test tasks as described at the top of this file. \r
109  */ \r
110 void xRegisterTest1( void *pvParameters );\r
111 void xRegisterTest2( void *pvParameters );\r
112 \r
113 /*\r
114  * The check task as described at the top of this file.\r
115  */\r
116 static void vCheckTask( void *pvParameters );\r
117 \r
118 /*\r
119  * Setup the processor ready for the demo.\r
120  */\r
121 static void prvSetupHardware( void );\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /* Variables used to detect errors within the register test tasks. */\r
126 static volatile unsigned portSHORT usTest1CycleCounter = 0, usTest2CycleCounter = 0;\r
127 static unsigned portSHORT usPreviousTest1Count = 0, usPreviousTest2Count = 0;\r
128 \r
129 /* Set to pdTRUE should an error be detected in any of the standard demo tasks\r
130 or tasks defined within this file. */\r
131 static unsigned portSHORT usErrorDetected = pdFALSE;\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 /*\r
136  * Create the demo tasks then start the scheduler.\r
137  */\r
138 int main( void )\r
139 {\r
140         /* Configure any hardware required for this demo. */\r
141         prvSetupHardware();\r
142 \r
143         /* Create the standard demo tasks. */\r
144         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );       \r
145         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
146         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );\r
147         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
148         vCreateBlockTimeTasks();\r
149 \r
150         /* Create the test tasks defined within this file. */\r
151         xTaskCreate( xRegisterTest1, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) &usTest1CycleCounter, tskIDLE_PRIORITY, NULL );\r
152         xTaskCreate( xRegisterTest2, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) &usTest2CycleCounter, tskIDLE_PRIORITY, NULL );\r
153         xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
154 \r
155         /* Finally start the scheduler. */\r
156         vTaskStartScheduler();\r
157 \r
158         /* Will only reach here if there is insufficient heap available to start\r
159         the scheduler. */\r
160         return 0;\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 static void prvSetupHardware( void )\r
165 {\r
166         vParTestInitialise();\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static void vCheckTask( void *pvParameters )\r
171 {\r
172 portTickType xLastExecutionTime;\r
173 \r
174 /* Start with the no error delay.  The long delay will cause the LED to flash\r
175 slowly. */\r
176 portTickType xDelay = mainNO_ERROR_DELAY;\r
177 \r
178         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
179         works correctly. */\r
180         xLastExecutionTime = xTaskGetTickCount();\r
181 \r
182         for( ;; )\r
183         {\r
184                 /* Wait until it is time for the next cycle. */\r
185                 vTaskDelayUntil( &xLastExecutionTime, xDelay );\r
186 \r
187                 /* Has an error been found in any of the standard demo tasks? */\r
188 \r
189                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
190                 {\r
191                         usErrorDetected = pdTRUE;\r
192                 }\r
193         \r
194                 if( xAreComTestTasksStillRunning() != pdTRUE )\r
195                 {\r
196                         usErrorDetected = pdTRUE;\r
197                 }\r
198 \r
199                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
200                 {\r
201                         usErrorDetected = pdTRUE;\r
202                 }\r
203 \r
204                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
205                 {\r
206                         usErrorDetected = pdTRUE;\r
207                 }\r
208 \r
209 \r
210                 /* Are the register test tasks still cycling? */\r
211 \r
212                 if( usTest1CycleCounter == usPreviousTest1Count )\r
213                 {\r
214                         usErrorDetected = pdTRUE;\r
215                 }\r
216 \r
217                 if( usTest2CycleCounter == usPreviousTest2Count )\r
218                 {\r
219                         usErrorDetected = pdTRUE;\r
220                 }\r
221 \r
222                 usPreviousTest2Count = usTest2CycleCounter;\r
223                 usPreviousTest1Count = usTest1CycleCounter;\r
224 \r
225                 \r
226                 /* If an error has been detected in any task then the delay will be\r
227                 reduced to increase the cycle rate of this task.  This has the effect\r
228                 of causing the LED to flash much faster giving a visual indication of\r
229                 the error condition. */\r
230                 if( usErrorDetected != pdFALSE )\r
231                 {\r
232                         xDelay = mainERROR_DELAY;\r
233                 }\r
234 \r
235                 /* Finally, toggle the LED before returning to delay to wait for the\r
236                 next cycle. */\r
237                 vParTestToggleLED( mainCHECK_LED );\r
238         }\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 void xRegisterTest1( void *pvParameters )\r
243 {\r
244 /* This static so as not to use the frame pointer.   They are volatile\r
245 also to avoid it being stored in a register that we clobber during the test. */\r
246 static unsigned portSHORT * volatile pusParameter;\r
247 \r
248         /* The variable incremented by this task is passed in as the parameter\r
249         even though it is defined within this file.  This is just to test the\r
250         parameter passing mechanism. */\r
251         pusParameter = pvParameters;\r
252 \r
253         for( ;; )\r
254         {\r
255                 /* Increment the variable to show this task is still cycling. */\r
256                 ( *pusParameter )++;\r
257 \r
258                 /* Set the w registers to known values, then check that each register\r
259                 contains the expected value.  See the explanation at the top of this\r
260                 file for more information. */\r
261                 asm volatile(   "mov.w  #0x0101, W0             \n"             \\r
262                                                 "mov.w  #0x0102, W1             \n"             \\r
263                                                 "mov.w  #0x0103, W2             \n"             \\r
264                                                 "mov.w  #0x0104, W3             \n"             \\r
265                                                 "mov.w  #0x0105, W4             \n"             \\r
266                                                 "mov.w  #0x0106, W5             \n"             \\r
267                                                 "mov.w  #0x0107, W6             \n"             \\r
268                                                 "mov.w  #0x0108, W7             \n"             \\r
269                                                 "mov.w  #0x0109, W8             \n"             \\r
270                                                 "mov.w  #0x010a, W9             \n"             \\r
271                                                 "mov.w  #0x010b, W10    \n"             \\r
272                                                 "mov.w  #0x010c, W11    \n"             \\r
273                                                 "mov.w  #0x010d, W12    \n"             \\r
274                                                 "mov.w  #0x010e, W13    \n"             \\r
275                                                 "mov.w  #0x010f, W14    \n"             \\r
276                                                 "sub    #0x0101, W0             \n"             \\r
277                                                 "cp0.w  W0                              \n"     \\r
278                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
279                                                 "sub    #0x0102, W1             \n"             \\r
280                                                 "cp0.w  W1                              \n"     \\r
281                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
282                                                 "sub    #0x0103, W2             \n"             \\r
283                                                 "cp0.w  W2                              \n"     \\r
284                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
285                                                 "sub    #0x0104, W3             \n"             \\r
286                                                 "cp0.w  W3                              \n"     \\r
287                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
288                                                 "sub    #0x0105, W4             \n"             \\r
289                                                 "cp0.w  W4                              \n"     \\r
290                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
291                                                 "sub    #0x0106, W5             \n"             \\r
292                                                 "cp0.w  W5                              \n"     \\r
293                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
294                                                 "sub    #0x0107, W6             \n"             \\r
295                                                 "cp0.w  W6                              \n"     \\r
296                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
297                                                 "sub    #0x0108, W7             \n"             \\r
298                                                 "cp0.w  W7                              \n"     \\r
299                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
300                                                 "sub    #0x0109, W8             \n"             \\r
301                                                 "cp0.w  W8                              \n"     \\r
302                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
303                                                 "sub    #0x010a, W9             \n"             \\r
304                                                 "cp0.w  W9                              \n"     \\r
305                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
306                                                 "sub    #0x010b, W10    \n"             \\r
307                                                 "cp0.w  W10                             \n"     \\r
308                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
309                                                 "sub    #0x010c, W11    \n"             \\r
310                                                 "cp0.w  W11                             \n"     \\r
311                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
312                                                 "sub    #0x010d, W12    \n"             \\r
313                                                 "cp0.w  W12                             \n"     \\r
314                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
315                                                 "sub    #0x010e, W13    \n"             \\r
316                                                 "cp0.w  W13                             \n"     \\r
317                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
318                                                 "sub    #0x010f, W14    \n"             \\r
319                                                 "cp0.w  W14                             \n"     \\r
320                                                 "bra    NZ, ERROR_TEST1 \n"             \\r
321                                                 "bra    NO_ERROR1               \n"     \\r
322                                                 "ERROR_TEST1:                   \n"             \\r
323                                                 "mov.w  #1, W0                  \n"             \\r
324                                                 "mov.w  W0, _usErrorDetected\n" \\r
325                                                 "NO_ERROR1:                             \n" );\r
326         }\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 void xRegisterTest2( void *pvParameters )\r
331 {\r
332 /* This static so as not to use the frame pointer.   They are volatile\r
333 also to avoid it being stored in a register that we clobber during the test. */\r
334 static unsigned portSHORT * volatile pusParameter;\r
335 \r
336         /* The variable incremented by this task is passed in as the parameter\r
337         even though it is defined within this file.  This is just to test the\r
338         parameter passing mechanism. */\r
339         pusParameter = pvParameters;\r
340 \r
341         for( ;; )\r
342         {\r
343                 /* Increment the variable to show this task is still cycling. */\r
344                 ( *pusParameter )++;\r
345 \r
346                 /* Set the w registers to known values, then check that each register\r
347                 contains the expected value.  See the explanation at the top of this\r
348                 file for more information. */\r
349                 asm volatile(   "mov.w  #0x0100, W0             \n"             \\r
350                                                 "mov.w  #0x0101, W1             \n"             \\r
351                                                 "mov.w  #0x0102, W2             \n"             \\r
352                                                 "mov.w  #0x0103, W3             \n"             \\r
353                                                 "mov.w  #0x0104, W4             \n"             \\r
354                                                 "mov.w  #0x0105, W5             \n"             \\r
355                                                 "mov.w  #0x0106, W6             \n"             \\r
356                                                 "mov.w  #0x0107, W7             \n"             \\r
357                                                 "mov.w  #0x0108, W8             \n"             \\r
358                                                 "mov.w  #0x0109, W9             \n"             \\r
359                                                 "mov.w  #0x010a, W10    \n"             \\r
360                                                 "mov.w  #0x010b, W11    \n"             \\r
361                                                 "mov.w  #0x010c, W12    \n"             \\r
362                                                 "mov.w  #0x010d, W13    \n"             \\r
363                                                 "mov.w  #0x010e, W14    \n"             \\r
364                                                 "sub    #0x0100, W0             \n"             \\r
365                                                 "cp0.w  W0                              \n"     \\r
366                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
367                                                 "sub    #0x0101, W1             \n"             \\r
368                                                 "cp0.w  W1                              \n"     \\r
369                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
370                                                 "sub    #0x0102, W2             \n"             \\r
371                                                 "cp0.w  W2                              \n"     \\r
372                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
373                                                 "sub    #0x0103, W3             \n"             \\r
374                                                 "cp0.w  W3                              \n"     \\r
375                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
376                                                 "sub    #0x0104, W4             \n"             \\r
377                                                 "cp0.w  W4                              \n"     \\r
378                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
379                                                 "sub    #0x0105, W5             \n"             \\r
380                                                 "cp0.w  W5                              \n"     \\r
381                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
382                                                 "sub    #0x0106, W6             \n"             \\r
383                                                 "cp0.w  W6                              \n"     \\r
384                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
385                                                 "sub    #0x0107, W7             \n"             \\r
386                                                 "cp0.w  W7                              \n"     \\r
387                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
388                                                 "sub    #0x0108, W8             \n"             \\r
389                                                 "cp0.w  W8                              \n"     \\r
390                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
391                                                 "sub    #0x0109, W9             \n"             \\r
392                                                 "cp0.w  W9                              \n"     \\r
393                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
394                                                 "sub    #0x010a, W10    \n"             \\r
395                                                 "cp0.w  W10                             \n"     \\r
396                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
397                                                 "sub    #0x010b, W11    \n"             \\r
398                                                 "cp0.w  W11                             \n"     \\r
399                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
400                                                 "sub    #0x010c, W12    \n"             \\r
401                                                 "cp0.w  W12                             \n"     \\r
402                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
403                                                 "sub    #0x010d, W13    \n"             \\r
404                                                 "cp0.w  W13                             \n"     \\r
405                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
406                                                 "sub    #0x010e, W14    \n"             \\r
407                                                 "cp0.w  W14                             \n"     \\r
408                                                 "bra    NZ, ERROR_TEST2 \n"             \\r
409                                                 "bra    NO_ERROR2               \n"     \\r
410                                                 "ERROR_TEST2:                   \n"             \\r
411                                                 "mov.w  #1, W0                  \n"             \\r
412                                                 "mov.w  W0, _usErrorDetected\n" \\r
413                                                 "NO_ERROR2:                             \n" );\r
414         }\r
415 }\r
416 /*-----------------------------------------------------------*/\r
417 \r
418 void vApplicationIdleHook( void )\r
419 {\r
420         /* Schedule the co-routines from within the idle task hook. */\r
421         vCoRoutineSchedule();\r
422 }\r
423 /*-----------------------------------------------------------*/\r
424 \r