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