]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4_RM48_TMS570_CCS5/main_full.c
5ccbe9c497aad55c2319b78a5e1796664a431569
[freertos] / FreeRTOS / Demo / CORTEX_R4_RM48_TMS570_CCS5 / main_full.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /******************************************************************************\r
76  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
77  * project, and a more comprehensive test and demo application.  The\r
78  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
79  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
80  * in main.c.  This file implements the comprehensive test and demo version.\r
81  *\r
82  * NOTE 2:  This file only contains the source code that is specific to the\r
83  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
84  * required to configure the hardware, are defined in main.c.\r
85  ******************************************************************************\r
86  *\r
87  * main_full() creates all the demo application tasks and software timers,\r
88  * then starts the scheduler.  The web documentation provides more details of\r
89  * the standard demo application tasks, which provide no particular\r
90  * functionality, but do provide a good example of how to use the FreeRTOS API.\r
91  *\r
92  * In addition to the standard demo tasks, the following tasks and tests are\r
93  * defined and/or created within this file:\r
94  *\r
95  * "Check" timer - The check software timer period is set to three seconds.\r
96  * The callback function associated with the check software timer checks that\r
97  * all the standard demo tasks are not only still executing, but are executing\r
98  * without reporting any errors.  If the check software timer discovers that a\r
99  * task has either stalled, or reported an error, then the error is logged and\r
100  * the check software timer toggles the red LEDs.  If an error has never been\r
101  * latched, the check software timer toggles the green LEDs.  Therefore, if the\r
102  * system is executing correctly, the green LEDs will toggle every three\r
103  * seconds, and if an error has ever been detected, the red LEDs will toggle\r
104  * every three seconds.\r
105  *\r
106  * "Reg test" tasks - These fill both the core and floating point registers\r
107  * with known values, then check that each register maintains its expected\r
108  * value for the lifetime of the tasks.  Each task uses a different set of\r
109  * values.  The reg test tasks execute with a very low priority, so get\r
110  * preempted very frequently.  A register containing an unexpected value is\r
111  * indicative of an error in the context switching mechanism.\r
112  *\r
113  * "LED" software timer - The callback function associated with the LED\r
114  * software time maintains a pattern of spinning white LEDs.\r
115  *\r
116  * See the documentation page for this demo on the FreeRTOS.org web site for\r
117  * full information, including hardware setup requirements. \r
118  */\r
119 \r
120 /* Standard includes. */\r
121 #include <stdio.h>\r
122 \r
123 /* Kernel includes. */\r
124 #include "FreeRTOS.h"\r
125 #include "task.h"\r
126 #include "timers.h"\r
127 #include "semphr.h"\r
128 \r
129 /* Standard demo application includes. */\r
130 #include "integer.h"\r
131 #include "PollQ.h"\r
132 #include "semtest.h"\r
133 #include "dynamic.h"\r
134 #include "BlockQ.h"\r
135 #include "blocktim.h"\r
136 #include "countsem.h"\r
137 #include "GenQTest.h"\r
138 #include "recmutex.h"\r
139 #include "death.h"\r
140 #include "partest.h"\r
141 #include "flop.h"\r
142 #include "serial.h"\r
143 #include "comtest.h"\r
144 \r
145 /* Priorities for the demo application tasks. */\r
146 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
147 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
148 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
149 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
150 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
151 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
152 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
153 \r
154 /* A block time of zero simply means "don't block". */\r
155 #define mainDONT_BLOCK                                          ( 0UL )\r
156 \r
157 /* The period after which the check timer will expire, converted to ticks. */\r
158 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
159 \r
160 /* The period after which the LED timer will expire, converted to ticks. */\r
161 #define mainLED_TIMER_PERIOD_MS                         ( 75UL / portTICK_RATE_MS )\r
162 \r
163 /* Constants for the ComTest tasks. */\r
164 #define mainCOM_TEST_BAUD_RATE                          ( ( unsigned long ) 19200 )\r
165 #define mainCOM_TEST_LED                                        ( 100 )\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /*\r
170  * The check timer callback function, as described at the top of this file.\r
171  */\r
172 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
173 \r
174 /*\r
175  * The LED timer callback function, as described at the top of this file.\r
176  */\r
177 static void prvLEDTimerCallback( xTimerHandle xTimer );\r
178 \r
179 /*\r
180  * The reg test tasks, as described at the top of this file.\r
181  */\r
182 extern void vRegTestTask1( void *pvParameters );\r
183 extern void vRegTestTask2( void *pvParameters );\r
184 \r
185 /*-----------------------------------------------------------*/\r
186 \r
187 /* Variables that are incremented on each iteration of the reg test tasks -\r
188 provided the tasks have not reported any errors.  The check task inspects these\r
189 variables to ensure they are still incrementing as expected.  If a variable\r
190 stops incrementing then it is likely that its associate task has stalled. */\r
191 volatile unsigned long ulRegTest1Counter = 0, ulRegTest2Counter = 0;\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void main_full( void )\r
196 {\r
197 xTimerHandle xTimer = NULL;\r
198 \r
199         /* Start all the standard demo/test tasks.  These have not particular\r
200         functionality, but do demonstrate how to use the FreeRTOS API, and test the\r
201         kernel port. */\r
202         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
203         vStartDynamicPriorityTasks();\r
204         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
205         vCreateBlockTimeTasks();\r
206         vStartCountingSemaphoreTasks();\r
207         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
208         vStartRecursiveMutexTasks();\r
209         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
210         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
211         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
212         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
213 \r
214         /* Create the register test tasks, as described at the top of this file. */\r
215         xTaskCreate( vRegTestTask1, ( const signed char * ) "Reg1...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
216         xTaskCreate( vRegTestTask2, ( const signed char * ) "Reg2...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
217         \r
218 \r
219         /* Create the software timer that performs the 'check' functionality,\r
220         as described at the top of this file. */\r
221         xTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
222                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
223                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
224                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
225                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
226                                                  );\r
227         \r
228         if( xTimer != NULL )\r
229         {\r
230                 xTimerStart( xTimer, mainDONT_BLOCK );\r
231         }\r
232 \r
233         /* Create the software timer that performs the 'LED spin' functionality,\r
234         as described at the top of this file. */\r
235         xTimer = xTimerCreate( ( const signed char * ) "LEDTimer",      /* A text name, purely to help debugging. */\r
236                                                         ( mainLED_TIMER_PERIOD_MS ),            /* The timer period, in this case 75ms. */\r
237                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
238                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
239                                                         prvLEDTimerCallback                                     /* The callback function that toggles the white LEDs. */\r
240                                                  );\r
241 \r
242         if( xTimer != NULL )\r
243         {\r
244                 xTimerStart( xTimer, mainDONT_BLOCK );\r
245         }\r
246 \r
247         /* The set of tasks created by the following function call have to be \r
248         created last as they keep account of the number of tasks they expect to see \r
249         running. */\r
250         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
251 \r
252         /* Start the scheduler. */\r
253         vTaskStartScheduler();\r
254         \r
255         /* If all is well, the scheduler will now be running, and the following line\r
256         will never be reached.  If the following line does execute, then there was\r
257         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
258         to be created.  See the memory management section on the FreeRTOS web site\r
259         for more details. */\r
260         for( ;; );      \r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
265 {\r
266 static long lChangeToRedLEDsAlready = pdFALSE;\r
267 static unsigned long ulLastRegTest1Counter = 0, ulLastRegTest2Counter = 0;\r
268 unsigned long ulErrorFound = pdFALSE;\r
269 /* LEDs are defaulted to use the Green LEDs.  The Red LEDs are used if an error\r
270 is found. */\r
271 static unsigned long ulLED1 = 8, ulLED2 = 11;\r
272 const unsigned long ulRedLED1 = 6, ulRedLED2 = 9;\r
273 \r
274         /* Check all the demo tasks (other than the flash tasks) to ensure\r
275         they are all still running, and that none have detected an error. */\r
276 \r
277         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
278         {\r
279                 ulErrorFound = pdTRUE;\r
280         }\r
281 \r
282         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
283         {\r
284                 ulErrorFound = pdTRUE;\r
285         }\r
286 \r
287         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
288         {\r
289                 ulErrorFound = pdTRUE;\r
290         }\r
291 \r
292         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
293         {\r
294                 ulErrorFound = pdTRUE;\r
295         }\r
296 \r
297         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
298         {\r
299                 ulErrorFound = pdTRUE;\r
300         }\r
301 \r
302         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
303         {\r
304                 ulErrorFound = pdTRUE;\r
305         }\r
306 \r
307         if( xIsCreateTaskStillRunning() != pdTRUE )\r
308         {\r
309                 ulErrorFound = pdTRUE;\r
310         }\r
311 \r
312         if( xArePollingQueuesStillRunning() != pdTRUE )\r
313         {\r
314                 ulErrorFound = pdTRUE;\r
315         }\r
316 \r
317         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
318         {\r
319                 ulErrorFound = pdTRUE;\r
320         }\r
321 \r
322         if( xAreMathsTaskStillRunning() != pdTRUE )\r
323         {\r
324                 ulErrorFound = pdTRUE;\r
325         }\r
326 \r
327         if( xAreComTestTasksStillRunning() != pdTRUE )\r
328         {\r
329                 ulErrorFound = pdTRUE;\r
330         }\r
331 \r
332         /* Check the reg test tasks are still cycling.  They will stop\r
333         incrementing their loop counters if they encounter an error. */\r
334         if( ulRegTest1Counter == ulLastRegTest1Counter )\r
335         {\r
336                 ulErrorFound = pdTRUE;\r
337         }\r
338 \r
339         if( ulRegTest2Counter == ulLastRegTest2Counter )\r
340         {\r
341                 ulErrorFound = pdTRUE;\r
342         }\r
343 \r
344         ulLastRegTest1Counter = ulRegTest1Counter;\r
345         ulLastRegTest2Counter = ulRegTest2Counter;\r
346 \r
347         /* Toggle the check LEDs to give an indication of the system status.  If\r
348         the green LEDs are toggling, then no errors have been detected.  If the red\r
349         LEDs are toggling, then an error has been reported in at least one task. */\r
350         vParTestToggleLED( ulLED1 );\r
351         vParTestToggleLED( ulLED2 );\r
352         \r
353         /* Have any errors been latch in ulErrorFound?  If so, ensure the gree LEDs\r
354         are off, then switch to using the red LEDs. */\r
355         if( ulErrorFound != pdFALSE )\r
356         {\r
357                 if( lChangeToRedLEDsAlready == pdFALSE )\r
358                 {\r
359                         lChangeToRedLEDsAlready = pdTRUE;\r
360                         \r
361                         /* An error has been found.  Switch to use the red LEDs. */\r
362                         vParTestSetLED( ulLED1, pdFALSE );\r
363                         vParTestSetLED( ulLED2, pdFALSE );\r
364                         ulLED1 = ulRedLED1;\r
365                         ulLED2 = ulRedLED2;\r
366                 }\r
367         }\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 static void prvLEDTimerCallback( xTimerHandle xTimer )\r
372 {\r
373 const unsigned long ulNumWhiteLEDs = 6;\r
374 static unsigned long ulLit1 = 2, ulLit2 = 1;\r
375 \r
376         vParTestSetLED( ulLit2, pdFALSE );\r
377 \r
378         ulLit2 = ulLit1;\r
379         ulLit1++;\r
380 \r
381         if( ulLit1 >= ulNumWhiteLEDs )\r
382         {\r
383                 ulLit1 = 0;\r
384         }\r
385 \r
386         vParTestSetLED( ulLit1, pdTRUE );\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r