]> git.sur5r.net Git - freertos/blob - Demo/PIC32MX_MPLAB/main.c
Added BSP generation files to MicroBlaze directory.
[freertos] / Demo / PIC32MX_MPLAB / main.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
56  * documentation provides more details of the standard demo application tasks.\r
57  * In addition to the standard demo tasks, the following tasks and tests are\r
58  * defined and/or created within this file:\r
59  *\r
60  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
61  * is permitted to access the display directly.  Other tasks wishing to write a\r
62  * message to the LCD send the message on a queue to the LCD task instead of\r
63  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
64  * for messages - waking and displaying the messages as they arrive.\r
65  *\r
66  * "Check" task -  This only executes every three seconds but has the highest\r
67  * priority so is guaranteed to get processor time.  Its main function is to \r
68  * check that all the standard demo tasks are still operational.  Should any \r
69  * unexpected behaviour within a demo task be discovered the check task will \r
70  * write an error to the LCD (via the LCD task).  If all the demo tasks are \r
71  * executing with their expected behaviour then the check task instead writes \r
72  * a count of the number of times the high frequency interrupt has incremented\r
73  * ulHighFrequencyTimerInterrupts - which is one in every 20,000 interrupts.\r
74  *\r
75  * "Register test" tasks - These tasks are used in part to test the kernel port.\r
76  * They set each processor register to a known value, then check that the \r
77  * register still contains that value.  Each of the tasks sets the registers\r
78  * to different values, and will get swapping in and out between setting and \r
79  * then subsequently checking the register values.  Discovery of an incorrect\r
80  * value would be indicative of an error in the task switching mechanism.\r
81  *\r
82  * By way of demonstration, the demo application defines \r
83  * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY \r
84  * to be 1, and all other interrupts as follows:\r
85  *\r
86  *      + The UART is allocated a priority of 2. This means it can interrupt the \r
87  *    RTOS tick, and can also safely use queues.\r
88  *  + Two timers are configured to generate interrupts just to test the nesting \r
89  *    and queue access mechanisms. These timers are allocated priorities 2 and 3 \r
90  *    respectively. Even though they both access the same two queues, the \r
91  *    priority 3 interrupt can safely interrupt the priority 2 interrupt. Both \r
92  *    can interrupt the RTOS tick.\r
93  *  + Finally a high frequency timer interrupt is configured to use priority 4 - \r
94  *    therefore kernel activity will never prevent the high frequency timer from \r
95  *    executing immediately that the interrupt is raised (within the limitations \r
96  *    of the hardware itself). It would not be safe to access a queue from this \r
97  *    interrupt as it is above configMAX_SYSCALL_INTERRUPT_PRIORITY. \r
98  *\r
99  * See the online documentation for this demo for more information on interrupt\r
100  * usage.\r
101  */\r
102 \r
103 /* Standard includes. */\r
104 #include <stdio.h>\r
105 \r
106 /* Scheduler includes. */\r
107 #include "FreeRTOS.h"\r
108 #include "task.h"\r
109 #include "queue.h"\r
110 \r
111 /* Demo application includes. */\r
112 #include "partest.h"\r
113 #include "blocktim.h"\r
114 #include "flash.h"\r
115 #include "semtest.h"\r
116 #include "GenQTest.h"\r
117 #include "QPeek.h"\r
118 #include "lcd.h"\r
119 #include "comtest2.h"\r
120 #include "timertest.h"\r
121 #include "IntQueue.h"\r
122 \r
123 #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF\r
124 #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /* The rate at which the LED controlled by the 'check' task will flash when no\r
129 errors have been detected. */\r
130 #define mainNO_ERROR_PERIOD     ( 3000 / portTICK_RATE_MS )\r
131 \r
132 /* The rate at which the LED controlled by the 'check' task will flash when an\r
133 error has been detected. */\r
134 #define mainERROR_PERIOD        ( 500 / portTICK_RATE_MS )\r
135 \r
136 /* The priorities of the various demo application tasks. */\r
137 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 4 )\r
138 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
139 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
140 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
141 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
142 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
143 \r
144 /* The LED controlled by the 'check' task. */\r
145 #define mainCHECK_LED                                           ( 7 )\r
146 \r
147 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.\r
148 See the comtest.c file for more information. */\r
149 #define mainCOM_TEST_LED                                        ( 4 )\r
150 \r
151 /* Baud rate used by the comtest tasks. */\r
152 #define mainCOM_TEST_BAUD_RATE                          ( 115200 )\r
153 \r
154 /* Misc. */\r
155 #define mainDONT_WAIT                                           ( 0 )\r
156 \r
157 /* Dimension the buffer used to hold the value of the high frequency timer \r
158 count when it is converted to a string. */\r
159 #define mainMAX_STRING_LENGTH                           ( 20 )\r
160 \r
161 /* The frequency at which the "fast interrupt test" interrupt will occur. */\r
162 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
163 \r
164 /* The number of timer clocks we expect to occur between each "fast\r
165 interrupt test" interrupt. */\r
166 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( ( configCPU_CLOCK_HZ >> 1 ) / mainTEST_INTERRUPT_FREQUENCY )\r
167 \r
168 /* The number of nano seconds between each core clock. */\r
169 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 /*\r
174  * Setup the processor ready for the demo.\r
175  */\r
176 static void prvSetupHardware( void );\r
177 \r
178 /*\r
179  * Implements the 'check' task functionality as described at the top of this \r
180  * file. \r
181  */\r
182 static void prvCheckTask( void *pvParameters ) __attribute__((noreturn));\r
183 \r
184 /*\r
185  * Tasks that test the context switch mechanism by filling the processor \r
186  * registers with known values, then checking that the values contained\r
187  * within the registers is as expected.  The tasks are likely to get swapped\r
188  * in and out between setting the register values and checking the register\r
189  * values. */\r
190 static void prvTestTask1( void *pvParameters );\r
191 static void prvTestTask2( void *pvParameters );\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 /* The queue used to send messages to the LCD task. */\r
196 static xQueueHandle xLCDQueue;\r
197 \r
198 /* Flag used by prvTestTask1() and prvTestTask2() to indicate their status\r
199 (pass/fail). */\r
200 unsigned long ulStatus1 = pdPASS;\r
201 \r
202 /* Variables incremented by prvTestTask1() and prvTestTask2() respectively on \r
203 each iteration of their function.  This is used to detect either task stopping\r
204 their execution.. */\r
205 unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
206 \r
207 /*-----------------------------------------------------------*/\r
208 \r
209 /*\r
210  * Create the demo tasks then start the scheduler.\r
211  */\r
212 int main( void )\r
213 {\r
214         /* Configure any hardware required for this demo. */\r
215         prvSetupHardware();\r
216 \r
217         /* Create the LCD task - this returns the queue to use when writing \r
218         messages to the LCD. */\r
219         xLCDQueue = xStartLCDTask();\r
220 \r
221         /* Create all the other standard demo tasks. */\r
222         vStartLEDFlashTasks( tskIDLE_PRIORITY );\r
223     vCreateBlockTimeTasks();\r
224     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
225     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
226     vStartQueuePeekTasks();\r
227         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
228         vStartInterruptQueueTasks();\r
229 \r
230         /* Create the tasks defined within this file. */\r
231         xTaskCreate( prvTestTask1, "Tst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
232         xTaskCreate( prvTestTask2, "Tst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
233 \r
234         /* prvCheckTask uses sprintf so requires more stack. */\r
235         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
236 \r
237         /* Finally start the scheduler. */\r
238         vTaskStartScheduler();\r
239 \r
240         /* Will only reach here if there is insufficient heap available to start\r
241         the scheduler. */\r
242         return 0;\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 static void prvTestTask1( void *pvParameters )\r
247 {\r
248 extern void vRegTest1( unsigned long * );\r
249 \r
250         for( ;; )\r
251         {\r
252                 /* Perform the register test function. */\r
253                 vRegTest1( &ulStatus1 );\r
254 \r
255                 /* Increment the counter so the check task knows we are still \r
256                 running. */\r
257                 ulRegTest1Cycles++;\r
258         }\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 static void prvTestTask2( void *pvParameters )\r
263 {\r
264 extern void vRegTest2( unsigned long * );\r
265 \r
266         for( ;; )\r
267         {\r
268                 /* Perform the register test function. */\r
269                 vRegTest2( &ulStatus1 );\r
270 \r
271                 /* Increment the counter so the check task knows we are still\r
272                 running. */\r
273                 ulRegTest2Cycles++;\r
274         }\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 static void prvSetupHardware( void )\r
279 {\r
280         /* Set the system and peripheral bus speeds and enable the program cache*/\r
281     SYSTEMConfigPerformance( configCPU_CLOCK_HZ - 1 );\r
282         mOSCSetPBDIV( OSC_PB_DIV_2 );\r
283 \r
284         /* Setup to use the external interrupt controller. */\r
285     INTEnableSystemMultiVectoredInt();\r
286 \r
287         portDISABLE_INTERRUPTS();\r
288 \r
289         /* Setup the digital IO for the LED's. */\r
290         vParTestInitialise();\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 static void prvCheckTask( void *pvParameters )\r
295 {\r
296 unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulTicksToWait = mainNO_ERROR_PERIOD;\r
297 portTickType xLastExecutionTime;\r
298 \r
299 /* Buffer into which the high frequency timer count is written as a string. */\r
300 static char cStringBuffer[ mainMAX_STRING_LENGTH ];\r
301 \r
302 /* The count of the high frequency timer interrupts. */\r
303 extern unsigned long ulHighFrequencyTimerInterrupts;\r
304 xLCDMessage xMessage = { ( 200 / portTICK_RATE_MS ), cStringBuffer };\r
305 \r
306         /* Setup the high frequency, high priority, timer test.  It is setup here\r
307         to ensure it does not fire before the scheduler is started. */\r
308         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
309 \r
310         /* Initialise the variable used to control our iteration rate prior to\r
311         its first use. */\r
312         xLastExecutionTime = xTaskGetTickCount();\r
313 \r
314         for( ;; )\r
315         {\r
316                 /* Wait until it is time to run the tests again. */\r
317                 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );\r
318 \r
319                 /* Has either register check 1 or 2 task discovered an error? */\r
320                 if( ulStatus1 != pdPASS )\r
321                 {\r
322                         ulTicksToWait = mainERROR_PERIOD;\r
323                         xMessage.pcMessage = "Error: Reg test1";\r
324                 }\r
325 \r
326                 /* Check that the register test 1 task is still running. */\r
327                 if( ulLastRegTest1Value == ulRegTest1Cycles )\r
328                 {\r
329                         ulTicksToWait = mainERROR_PERIOD;\r
330                         xMessage.pcMessage = "Error: Reg test2";\r
331                 }\r
332                 ulLastRegTest1Value = ulRegTest1Cycles;\r
333 \r
334                 \r
335                 /* Check that the register test 2 task is still running. */\r
336                 if( ulLastRegTest2Value == ulRegTest2Cycles )\r
337                 {\r
338                         ulTicksToWait = mainERROR_PERIOD;\r
339                         xMessage.pcMessage = "Error: Reg test3";\r
340                 }\r
341                 ulLastRegTest2Value = ulRegTest2Cycles;\r
342                 \r
343 \r
344                 /* Have any of the standard demo tasks detected an error in their \r
345                 operation? */\r
346                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
347                 {\r
348                         ulTicksToWait = mainERROR_PERIOD;\r
349                         xMessage.pcMessage = "Error: Gen Q";\r
350                 }\r
351                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
352                 {\r
353                         ulTicksToWait = mainERROR_PERIOD;\r
354                         xMessage.pcMessage = "Error: Q Peek";\r
355                 }\r
356                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
357                 {\r
358                         ulTicksToWait = mainERROR_PERIOD;\r
359                         xMessage.pcMessage = "Error: COM test";\r
360                 }\r
361                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
362                 {\r
363                         ulTicksToWait = mainERROR_PERIOD;\r
364                         xMessage.pcMessage = "Error: Blck time";\r
365                 }\r
366             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
367             {\r
368                 ulTicksToWait = mainERROR_PERIOD;\r
369                         xMessage.pcMessage = "Error: Sem test";\r
370             }\r
371                 else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
372                 {\r
373                         ulTicksToWait = mainERROR_PERIOD;\r
374                         xMessage.pcMessage = "Error: Int queue";\r
375                 }\r
376 \r
377                 /* Write the ulHighFrequencyTimerInterrupts value to the string \r
378                 buffer.  It will only be displayed if no errors have been detected. */\r
379                 sprintf( cStringBuffer, "Pass %u", ( unsigned int ) ulHighFrequencyTimerInterrupts );\r
380 \r
381                 xQueueSend( xLCDQueue, &xMessage, mainDONT_WAIT );\r
382                 vParTestToggleLED( mainCHECK_LED );\r
383         }\r
384 }\r
385 /*-----------------------------------------------------------*/\r
386 \r
387 void vApplicationStackOverflowHook( void )\r
388 {\r
389         /* Look at pxCurrentTCB to see which task overflowed its stack. */\r
390         for( ;; );\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 void _general_exception_handler( unsigned long ulCause, unsigned long ulStatus )\r
395 {\r
396         /* This overrides the definition provided by the kernel.  Other exceptions \r
397         should be handled here. */\r
398         for( ;; );\r
399 }\r
400 /*-----------------------------------------------------------*/\r
401 \r