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