]> git.sur5r.net Git - freertos/blob - Demo/RX600_RX62N-RSK_GNURX/RTOSDemo/main-full.c
Rename MDK to RSK for the RX ports.
[freertos] / Demo / RX600_RX62N-RSK_GNURX / RTOSDemo / main-full.c
1 /*\r
2     FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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  * This project includes a lot of tasks and tests and is therefore complex.\r
56  * If you would prefer a much simpler project to get started with then select\r
57  * the 'Blinky' build configuration within the HEW IDE.\r
58  *\r
59  * Creates all the demo application tasks, then starts the scheduler.  The web\r
60  * documentation provides more details of the standard demo application tasks,\r
61  * which provide no particular functionality but do provide a good example of\r
62  * how to use the FreeRTOS API.  The tasks defined in flop.c are included in the\r
63  * set of standard demo tasks to ensure the floating point unit gets some\r
64  * exercise. \r
65  *\r
66  * In addition to the standard demo tasks, the following tasks and tests are \r
67  * defined and/or created within this file:\r
68  *\r
69  * "Reg test" tasks - These fill the registers with known values, then check\r
70  * that each register still contains its expected value.  Each task uses\r
71  * different values.  The tasks run with very low priority so get preempted \r
72  * very frequently.  A check variable is incremented on each iteration of the \r
73  * test loop.  A register containing an unexpected value is indicative of an \r
74  * error in the context switching mechanism and will result in a branch to a \r
75  * null loop - which in turn will prevent the check variable from incrementing\r
76  * any further and allow the check task (described below) to determine that an\r
77  * error has occurred.  The nature of the reg test tasks necessitates that they \r
78  * are written in assembly code.\r
79  *\r
80  * "Check" task - This only executes every five seconds but has a high priority\r
81  * to ensure it gets processor time.  Its main function is to check that all the\r
82  * standard demo tasks are still operational.  While no errors have been\r
83  * discovered the check task will toggle LED 5 every 5 seconds - the toggle\r
84  * rate increasing to 200ms being a visual indication that at least one task has\r
85  * reported unexpected behaviour.\r
86  *\r
87  * "High frequency timer test" - A high frequency periodic interrupt is \r
88  * generated using a timer - the interrupt is assigned a priority above \r
89  * configMAX_SYSCALL_INTERRUPT_PRIORITY so should not be effected by anything\r
90  * the kernel is doing.  The interrupt service routine measures the number of \r
91  * counts a separate timer performs between each interrupt to determine the \r
92  * jitter in the interrupt timing.\r
93  *\r
94  * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application\r
95  * tasks are executing as expected and no errors have been reported in any \r
96  * tasks.  The toggle rate increasing to 200ms indicates that at least one task\r
97  * has reported unexpected behaviour.\r
98  * \r
99  * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let\r
100  * the application set up a timer to generate the tick interrupt.  In this\r
101  * example a compare match timer is used for this purpose.  \r
102  *\r
103  * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.\r
104  * The PowerON_Reset_PC() supplied in resetprg.c with this demo has \r
105  * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.\r
106  *\r
107  * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use\r
108  * of all the 8bit timers (as two cascaded 16bit units).\r
109 */\r
110 \r
111 /* Standard includes. */\r
112 #include "string.h"\r
113 \r
114 /* Hardware specific includes. */\r
115 #include "iodefine.h"\r
116 \r
117 /* Kernel includes. */\r
118 #include "FreeRTOS.h"\r
119 #include "task.h"\r
120 \r
121 /* Standard demo includes. */\r
122 #include "partest.h"\r
123 #include "flash.h"\r
124 #include "IntQueue.h"\r
125 #include "BlockQ.h"\r
126 #include "death.h"\r
127 #include "integer.h"\r
128 #include "blocktim.h"\r
129 #include "semtest.h"\r
130 #include "PollQ.h"\r
131 #include "GenQTest.h"\r
132 #include "QPeek.h"\r
133 #include "recmutex.h"\r
134 #include "flop.h"\r
135 \r
136 /* Values that are passed into the reg test tasks using the task parameter.  The \r
137 tasks check that the values are passed in correctly. */\r
138 #define mainREG_TEST_1_PARAMETER        ( 0x12121212UL )\r
139 #define mainREG_TEST_2_PARAMETER        ( 0x12345678UL )\r
140 \r
141 /* Priorities at which the tasks are created. */\r
142 #define mainCHECK_TASK_PRIORITY         ( configMAX_PRIORITIES - 1 )\r
143 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
144 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
145 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
146 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
147 #define mainFLASH_TASK_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
148 #define mainuIP_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
149 #define mainINTEGER_TASK_PRIORITY   ( tskIDLE_PRIORITY )\r
150 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
151 #define mainFLOP_TASK_PRIORITY          ( tskIDLE_PRIORITY )\r
152 \r
153 /* The LED toggled by the check task. */\r
154 #define mainCHECK_LED                           ( 5 )\r
155 \r
156 /* The rate at which mainCHECK_LED will toggle when all the tasks are running\r
157 without error.  Controlled by the check task as described at the top of this\r
158 file. */\r
159 #define mainNO_ERROR_CYCLE_TIME         ( 5000 / portTICK_RATE_MS )\r
160 \r
161 /* The rate at which mainCHECK_LED will toggle when an error has been reported\r
162 by at least one task.  Controlled by the check task as described at the top of \r
163 this file. */\r
164 #define mainERROR_CYCLE_TIME            ( 200 / portTICK_RATE_MS )\r
165 \r
166 /* The period of the peripheral clock in nano seconds.  This is used to calculate\r
167 the jitter time in nano seconds as part of the high frequency timer test.  The\r
168 clock driving the timer is divided by 8. */\r
169 #define mainNS_PER_CLOCK                        ( ( unsigned long ) ( ( 1.0 /  ( ( double ) configPERIPHERAL_CLOCK_HZ ) / 8.0 ) * 1000000000.0 ) )\r
170 \r
171 /*\r
172  * vApplicationMallocFailedHook() will only be called if\r
173  * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
174  * function that will execute if a call to pvPortMalloc() fails.\r
175  * pvPortMalloc() is called internally by the kernel whenever a task, queue or\r
176  * semaphore is created.  It is also called by various parts of the demo\r
177  * application.  \r
178  */\r
179 void vApplicationMallocFailedHook( void );\r
180 \r
181 /*\r
182  * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1\r
183  * in FreeRTOSConfig.h.  It is a hook function that is called on each iteration\r
184  * of the idle task.  It is essential that code added to this hook function\r
185  * never attempts to block in any way (for example, call xQueueReceive() with\r
186  * a block time specified).  If the application makes use of the vTaskDelete()\r
187  * API function (as this demo application does) then it is also important that\r
188  * vApplicationIdleHook() is permitted to return to its calling function because\r
189  * it is the responsibility of the idle task to clean up memory allocated by the\r
190  * kernel to any task that has since been deleted.\r
191  */\r
192 void vApplicationIdleHook( void );\r
193 \r
194 /*\r
195  * vApplicationStackOverflowHook() will only be called if \r
196  * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value.  The handle and \r
197  * name of the offending task should be passed in the function parameters, but\r
198  * it is possible that the stack overflow will have corrupted these - in which\r
199  * case pxCurrentTCB can be inspected to find the same information.\r
200  */\r
201 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );\r
202 \r
203 /*\r
204  * The reg test tasks as described at the top of this file.\r
205  */\r
206 static void prvRegTest1Task( void *pvParameters );\r
207 static void prvRegTest2Task( void *pvParameters );\r
208 \r
209 /*\r
210  * The actual implementation of the reg test functionality, which, because of\r
211  * the direct register access, have to be in assembly.\r
212  */\r
213 extern void prvRegTest1Implementation( void );\r
214 extern void prvRegTest2Implementation( void );\r
215 \r
216 \r
217 /*\r
218  * The check task as described at the top of this file.\r
219  */\r
220 static void prvCheckTask( void *pvParameters );\r
221 \r
222 /* Variables that are incremented on each iteration of the reg test tasks - \r
223 provided the tasks have not reported any errors.  The check task inspects these\r
224 variables to ensure they are still incrementing as expected.  If a variable \r
225 stops incrementing then it is likely that its associate task has stalled. */\r
226 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;\r
227 \r
228 /*-----------------------------------------------------------*/\r
229 \r
230 void main(void)\r
231 {\r
232 extern void HardwareSetup( void );\r
233 \r
234         /* Renesas provided CPU configuration routine.  The clocks are configured in\r
235         here. */\r
236         HardwareSetup();\r
237 \r
238         /* Turn all LEDs off. */\r
239         vParTestInitialise();\r
240 \r
241         /* Start the reg test tasks which test the context switching mechanism. */\r
242         xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
243         xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
244 \r
245         /* Start the check task as described at the top of this file. */\r
246         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
247 \r
248         /* Create the standard demo tasks. */\r
249         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
250         vCreateBlockTimeTasks();\r
251         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
252         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
253         vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
254         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
255         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
256         vStartQueuePeekTasks();\r
257         vStartRecursiveMutexTasks();\r
258         vStartInterruptQueueTasks();\r
259         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
260 \r
261         /* The suicide tasks must be created last as they need to know how many\r
262         tasks were running prior to their creation in order to ascertain whether\r
263         or not the correct/expected number of tasks are running at any given time. */\r
264         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
265 \r
266         /* Start the tasks running. */\r
267         vTaskStartScheduler();\r
268         \r
269         /* If all is well we will never reach here as the scheduler will now be\r
270         running.  If we do reach here then it is likely that there was insufficient\r
271         heap available for the idle task to be created. */\r
272         for( ;; );\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 static void prvCheckTask( void *pvParameters )\r
277 {\r
278 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;\r
279 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;\r
280 extern void vSetupHighFrequencyTimer( void );\r
281 extern volatile unsigned short usMaxJitter;\r
282 volatile unsigned long ulActualJitter = 0;\r
283 static char cErrorText[ 100 ];\r
284 \r
285         /* If this is being executed then the kernel has been started.  Start the high\r
286         frequency timer test as described at the top of this file.  This is only \r
287         included in the optimised build configuration - otherwise it takes up too much\r
288         CPU time. */\r
289         #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST\r
290                 vSetupHighFrequencyTimer();\r
291         #endif\r
292 \r
293         /* Initialise xNextWakeTime - this only needs to be done once. */\r
294         xNextWakeTime = xTaskGetTickCount();\r
295 \r
296         for( ;; )\r
297         {\r
298                 /* Place this task in the blocked state until it is time to run again. */\r
299                 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
300 \r
301                 /* Check the standard demo tasks are running without error. */\r
302                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
303                 {\r
304                         /* Increase the rate at which this task cycles, which will increase the\r
305                         rate at which mainCHECK_LED flashes to give visual feedback that an error\r
306                         has occurred. */\r
307                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
308                         strcpy( cErrorText, "Error: GenQueue" );\r
309                 }\r
310                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
311                 {\r
312                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
313                         strcpy( cErrorText, "Error: QueuePeek" );\r
314                 }\r
315                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
316                 {\r
317                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
318                         strcpy( cErrorText, "Error: BlockQueue" );\r
319                 }\r
320                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
321                 {\r
322                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
323                         strcpy( cErrorText, "Error: BlockTime" );\r
324                 }\r
325             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
326             {\r
327                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
328                         strcpy( cErrorText, "Error: SemTest" );\r
329             }\r
330             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
331             {\r
332                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
333                         strcpy( cErrorText, "Error: PollQueue" );\r
334             }\r
335             else if( xIsCreateTaskStillRunning() != pdTRUE )\r
336             {\r
337                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
338                         strcpy( cErrorText, "Error: Death" );\r
339             }\r
340             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
341             {\r
342                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
343                         strcpy( cErrorText, "Error: IntMath" );\r
344             }\r
345             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
346             {\r
347                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
348                         strcpy( cErrorText, "Error: RecMutex" );\r
349             }\r
350                 else if( xAreIntQueueTasksStillRunning() != pdPASS )\r
351                 {\r
352                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
353                         strcpy( cErrorText, "Error: IntQueue" );\r
354                 }\r
355                 else if( xAreMathsTaskStillRunning() != pdPASS )\r
356                 {\r
357                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
358                         strcpy( cErrorText, "Error: Flop" );\r
359                 }\r
360 \r
361                 /* Check the reg test tasks are still cycling.  They will stop incrementing\r
362                 their loop counters if they encounter an error. */\r
363                 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )\r
364                 {\r
365                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
366                         strcpy( cErrorText, "Error: RegTest1" );\r
367                 }\r
368 \r
369                 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )\r
370                 {\r
371                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
372                         strcpy( cErrorText, "Error: RegTest2" );\r
373                 }\r
374                 \r
375                 ulLastRegTest1CycleCount = ulRegTest1CycleCount;\r
376                 ulLastRegTest2CycleCount = ulRegTest2CycleCount;\r
377                 \r
378                 /* Toggle the check LED to give an indication of the system status.  If \r
379                 the LED toggles every 5 seconds then everything is ok.  A faster toggle \r
380                 indicates an error. */\r
381                 vParTestToggleLED( mainCHECK_LED );\r
382                 \r
383                 /* Calculate the maximum jitter experienced by the high frequency timer \r
384                 test and print it out.  It is ok to use printf without worrying about \r
385                 mutual exclusion as it is not used anywhere else in this demo. */\r
386                 //sprintf( cTempBuf, "%s [%fns]\n", "Max Jitter = ", ( ( float ) usMaxJitter ) * mainNS_PER_CLOCK );\r
387                 ulActualJitter = ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK;\r
388                 \r
389                 if( xCycleFrequency == mainERROR_CYCLE_TIME )\r
390                 {\r
391                         /* Just for break point. */\r
392                         portNOP();\r
393                 }\r
394         }\r
395 }\r
396 /*-----------------------------------------------------------*/\r
397 \r
398 /* The RX port uses this callback function to configure its tick interrupt.  \r
399 This allows the application to choose the tick interrupt source. */\r
400 void vApplicationSetupTimerInterrupt( void )\r
401 {\r
402         /* Enable compare match timer 0. */\r
403         MSTP( CMT0 ) = 0;\r
404         \r
405         /* Interrupt on compare match. */\r
406         CMT0.CMCR.BIT.CMIE = 1;\r
407         \r
408         /* Set the compare match value. */\r
409         CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );\r
410         \r
411         /* Divide the PCLK by 8. */\r
412         CMT0.CMCR.BIT.CKS = 0;\r
413         \r
414         /* Enable the interrupt... */\r
415         _IEN( _CMT0_CMI0 ) = 1;\r
416         \r
417         /* ...and set its priority to the application defined kernel priority. */\r
418         _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;\r
419         \r
420         /* Start the timer. */\r
421         CMT.CMSTR0.BIT.STR0 = 1;\r
422 }\r
423 /*-----------------------------------------------------------*/\r
424 \r
425 /* This function is explained by the comments above its prototype at the top\r
426 of this file. */\r
427 void vApplicationMallocFailedHook( void )\r
428 {\r
429         for( ;; );\r
430 }\r
431 /*-----------------------------------------------------------*/\r
432 \r
433 /* This function is explained by the comments above its prototype at the top\r
434 of this file. */\r
435 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
436 {\r
437         for( ;; );\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 /* This function is explained by the comments above its prototype at the top\r
442 of this file. */\r
443 void vApplicationIdleHook( void )\r
444 {\r
445 static volatile unsigned long ulIdleLoopCount = 0UL;\r
446 \r
447         ulIdleLoopCount++;\r
448 }\r
449 /*-----------------------------------------------------------*/\r
450 \r
451 /* This function is explained in the comments at the top of this file. */\r
452 static void prvRegTest1Task( void *pvParameters )\r
453 {\r
454         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )\r
455         {\r
456                 /* The parameter did not contain the expected value. */\r
457                 for( ;; )\r
458                 {\r
459                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
460                         taskDISABLE_INTERRUPTS();\r
461                 }\r
462         }\r
463         \r
464         /* This is an asm function that never returns. */\r
465         prvRegTest1Implementation();            \r
466 }\r
467 /*-----------------------------------------------------------*/\r
468 \r
469 /* This function is explained in the comments at the top of this file. */\r
470 static void prvRegTest2Task( void *pvParameters )\r
471 {\r
472         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )\r
473         {\r
474                 /* The parameter did not contain the expected value. */\r
475                 for( ;; )\r
476                 {\r
477                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
478                         taskDISABLE_INTERRUPTS();\r
479                 }\r
480         }\r
481 \r
482         /* This is an asm function that never returns. */\r
483         prvRegTest2Implementation();\r
484 }\r
485 /*-----------------------------------------------------------*/\r
486 \r
487 \r
488 \r
489 \r
490 \r