]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2106_GCC/main.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / ARM7_LPC2106_GCC / main.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
68         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
69         called.  The demo applications included in the FreeRTOS.org download switch\r
70         to supervisor mode prior to main being called.  If you are not using one of\r
71         these demo application projects then ensure Supervisor mode is used.\r
72 */\r
73 \r
74 \r
75 /*\r
76  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
77  * documentation provides more details of the demo application tasks.\r
78  *\r
79  * Main.c also creates a task called "Check".  This only executes every three\r
80  * seconds but has the highest priority so is guaranteed to get processor time.\r
81  * Its main function is to check that all the other tasks are still operational.\r
82  * Each task (other than the "flash" tasks) maintains a unique count that is\r
83  * incremented each time the task successfully completes its function.  Should\r
84  * any error occur within such a task the count is permanently halted.  The\r
85  * check task inspects the count of each task to ensure it has changed since\r
86  * the last time the check task executed.  If all the count variables have\r
87  * changed all the tasks are still executing error free, and the check task\r
88  * toggles the onboard LED.  Should any task contain an error at any time\r
89  * the LED toggle rate will change from 3 seconds to 500ms.\r
90  *\r
91  * To check the operation of the memory allocator the check task also\r
92  * dynamically creates a task before delaying, and deletes it again when it\r
93  * wakes.  If memory cannot be allocated for the new task the call to xTaskCreate\r
94  * will fail and an error is signalled.  The dynamically created task itself\r
95  * allocates and frees memory just to give the allocator a bit more exercise.\r
96  *\r
97  */\r
98 \r
99 /*\r
100         Changes from V2.4.2\r
101 \r
102         + The vErrorChecks() task now dynamically creates then deletes a task each\r
103           cycle.  This tests the operation of the memory allocator.\r
104 \r
105         Changes from V2.5.2\r
106 \r
107         + vParTestInitialise() is called during initialisation to ensure all the\r
108           LED's start off.\r
109 */\r
110 \r
111 \r
112 /* Standard includes. */\r
113 #include <stdlib.h>\r
114 #include <string.h>\r
115 \r
116 /* Scheduler includes. */\r
117 #include "FreeRTOS.h"\r
118 #include "task.h"\r
119 \r
120 /* Demo application includes. */\r
121 #include "partest.h"\r
122 #include "flash.h"\r
123 #include "integer.h"\r
124 #include "PollQ.h"\r
125 #include "comtest2.h"\r
126 #include "semtest.h"\r
127 #include "flop.h"\r
128 #include "dynamic.h"\r
129 #include "BlockQ.h"\r
130 #include "serial.h"\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 /* Constants to setup I/O. */\r
135 #define mainTX_ENABLE   ( ( unsigned long ) 0x0001 )\r
136 #define mainRX_ENABLE   ( ( unsigned long ) 0x0004 )\r
137 #define mainP0_14               ( ( unsigned long ) 0x4000 )\r
138 #define mainJTAG_PORT   ( ( unsigned long ) 0x3E0000UL )\r
139 \r
140 /* Constants to setup the PLL. */\r
141 #define mainPLL_MUL_4           ( ( unsigned char ) 0x0003 )\r
142 #define mainPLL_DIV_1           ( ( unsigned char ) 0x0000 )\r
143 #define mainPLL_ENABLE          ( ( unsigned char ) 0x0001 )\r
144 #define mainPLL_CONNECT         ( ( unsigned char ) 0x0003 )\r
145 #define mainPLL_FEED_BYTE1      ( ( unsigned char ) 0xaa )\r
146 #define mainPLL_FEED_BYTE2      ( ( unsigned char ) 0x55 )\r
147 #define mainPLL_LOCK            ( ( unsigned long ) 0x0400 )\r
148 \r
149 /* Constants to setup the MAM. */\r
150 #define mainMAM_TIM_3           ( ( unsigned char ) 0x03 )\r
151 #define mainMAM_MODE_FULL       ( ( unsigned char ) 0x02 )\r
152 \r
153 /* Constants to setup the peripheral bus. */\r
154 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
155 \r
156 /* Constants for the ComTest tasks. */\r
157 #define mainCOM_TEST_BAUD_RATE  ( ( unsigned long ) 115200 )\r
158 #define mainCOM_TEST_LED                ( 3 )\r
159 \r
160 /* Priorities for the demo application tasks. */\r
161 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
162 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
163 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 0 )\r
164 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
165 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 0 )\r
166 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
167 \r
168 /* The rate at which the on board LED will toggle when there is/is not an\r
169 error. */\r
170 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
171 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
172 #define mainON_BOARD_LED_BIT            ( ( unsigned long ) 0x80 )\r
173 \r
174 /* Constants used by the vMemCheckTask() task. */\r
175 #define mainCOUNT_INITIAL_VALUE         ( ( unsigned long ) 0 )\r
176 #define mainNO_TASK                                     ( 0 )\r
177 \r
178 /* The size of the memory blocks allocated by the vMemCheckTask() task. */\r
179 #define mainMEM_CHECK_SIZE_1            ( ( size_t ) 51 )\r
180 #define mainMEM_CHECK_SIZE_2            ( ( size_t ) 52 )\r
181 #define mainMEM_CHECK_SIZE_3            ( ( size_t ) 151 )\r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /*\r
186  * The Olimex demo board has a single built in LED.  This function simply\r
187  * toggles its state.\r
188  */\r
189 void prvToggleOnBoardLED( void );\r
190 \r
191 /*\r
192  * Checks that all the demo application tasks are still executing without error\r
193  * - as described at the top of the file.\r
194  */\r
195 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );\r
196 \r
197 /*\r
198  * The task that executes at the highest priority and calls\r
199  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
200  * of the file.\r
201  */\r
202 static void vErrorChecks( void *pvParameters );\r
203 \r
204 /*\r
205  * Dynamically created and deleted during each cycle of the vErrorChecks()\r
206  * task.  This is done to check the operation of the memory allocator.\r
207  * See the top of vErrorChecks for more details.\r
208  */\r
209 static void vMemCheckTask( void *pvParameters );\r
210 \r
211 /*\r
212  * Configure the processor for use with the Olimex demo board.  This includes\r
213  * setup for the I/O, system clock, and access timings.\r
214  */\r
215 static void prvSetupHardware( void );\r
216 \r
217 /*-----------------------------------------------------------*/\r
218 \r
219 /*\r
220  * Starts all the other tasks, then starts the scheduler.\r
221  */\r
222 int main( void )\r
223 {\r
224         /* Setup the hardware for use with the Olimex demo board. */\r
225         prvSetupHardware();\r
226 \r
227         /* Start the demo/test application tasks. */\r
228         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
229         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
230         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
231         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
232         vStartMathTasks( tskIDLE_PRIORITY );\r
233         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
234         vStartDynamicPriorityTasks();\r
235         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
236 \r
237         /* Start the check task - which is defined in this file. */\r
238         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
239 \r
240         /* Now all the tasks have been started - start the scheduler.\r
241 \r
242         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
243         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
244         called.  The demo applications included in the FreeRTOS.org download switch\r
245         to supervisor mode prior to main being called.  If you are not using one of\r
246         these demo application projects then ensure Supervisor mode is used here. */\r
247         vTaskStartScheduler();\r
248 \r
249         /* Should never reach here! */\r
250         return 0;\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 static void vErrorChecks( void *pvParameters )\r
255 {\r
256 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
257 unsigned long ulMemCheckTaskRunningCount;\r
258 TaskHandle_t xCreatedTask;\r
259 \r
260         /* The parameters are not used in this function. */\r
261         ( void ) pvParameters;\r
262 \r
263         /* Cycle for ever, delaying then checking all the other tasks are still\r
264         operating without error.  If an error is detected then the delay period\r
265         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
266         the on board LED flash rate will increase.\r
267 \r
268         In addition to the standard tests the memory allocator is tested through\r
269         the dynamic creation and deletion of a task each cycle.  Each time the\r
270         task is created memory must be allocated for its stack.  When the task is\r
271         deleted this memory is returned to the heap.  If the task cannot be created\r
272         then it is likely that the memory allocation failed. */\r
273 \r
274         for( ;; )\r
275         {\r
276                 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a\r
277                 parameter. */\r
278                 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;\r
279                 xCreatedTask = mainNO_TASK;\r
280 \r
281                 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )\r
282                 {\r
283                         /* Could not create the task - we have probably run out of heap. */\r
284                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
285                 }\r
286 \r
287                 /* Delay until it is time to execute again. */\r
288                 vTaskDelay( xDelayPeriod );\r
289 \r
290                 /* Delete the dynamically created task. */\r
291                 if( xCreatedTask != mainNO_TASK )\r
292                 {\r
293                         vTaskDelete( xCreatedTask );\r
294                 }\r
295 \r
296                 /* Check all the standard demo application tasks are executing without\r
297                 error.  ulMemCheckTaskRunningCount is checked to ensure it was\r
298                 modified by the task just deleted. */\r
299                 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )\r
300                 {\r
301                         /* An error has been detected in one of the tasks - flash faster. */\r
302                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
303                 }\r
304 \r
305                 prvToggleOnBoardLED();\r
306         }\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 static void prvSetupHardware( void )\r
311 {\r
312         #ifdef RUN_FROM_RAM\r
313                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
314                 SCB_MEMMAP = 2;\r
315         #endif\r
316 \r
317         /* Configure the RS2332 pins.  All other pins remain at their default of 0. */\r
318         PCB_PINSEL0 |= mainTX_ENABLE;\r
319         PCB_PINSEL0 |= mainRX_ENABLE;\r
320 \r
321         /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.\r
322         The JTAG pins are left as input as I'm not sure what will happen if the\r
323         Wiggler is connected after powerup - not that it would be a good idea to\r
324         do that anyway. */\r
325         GPIO_IODIR = ~( mainP0_14 + mainJTAG_PORT );\r
326 \r
327         /* Setup the PLL to multiply the XTAL input by 4. */\r
328         SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );\r
329 \r
330         /* Activate the PLL by turning it on then feeding the correct sequence of\r
331         bytes. */\r
332         SCB_PLLCON = mainPLL_ENABLE;\r
333         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
334         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
335 \r
336         /* Wait for the PLL to lock... */\r
337         while( !( SCB_PLLSTAT & mainPLL_LOCK ) );\r
338 \r
339         /* ...before connecting it using the feed sequence again. */\r
340         SCB_PLLCON = mainPLL_CONNECT;\r
341         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
342         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
343 \r
344         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
345         PLL used.  It is possible faster overall performance could be obtained by\r
346         tuning the MAM and PLL settings. */\r
347         MAM_TIM = mainMAM_TIM_3;\r
348         MAM_CR = mainMAM_MODE_FULL;\r
349 \r
350         /* Setup the peripheral bus to be the same as the PLL output. */\r
351         SCB_VPBDIV = mainBUS_CLK_FULL;\r
352 \r
353         /* Initialise LED outputs. */\r
354         vParTestInitialise();\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 void prvToggleOnBoardLED( void )\r
359 {\r
360 unsigned long ulState;\r
361 \r
362         ulState = GPIO0_IOPIN;\r
363         if( ulState & mainON_BOARD_LED_BIT )\r
364         {\r
365                 GPIO_IOCLR = mainON_BOARD_LED_BIT;\r
366         }\r
367         else\r
368         {\r
369                 GPIO_IOSET = mainON_BOARD_LED_BIT;\r
370         }\r
371 }\r
372 /*-----------------------------------------------------------*/\r
373 \r
374 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )\r
375 {\r
376 long lReturn = ( long ) pdPASS;\r
377 \r
378         /* Check all the demo tasks (other than the flash tasks) to ensure\r
379         that they are all still running, and that none of them have detected\r
380         an error. */\r
381 \r
382         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
383         {\r
384                 lReturn = ( long ) pdFAIL;\r
385         }\r
386 \r
387         if( xAreComTestTasksStillRunning() != pdTRUE )\r
388         {\r
389                 lReturn = ( long ) pdFAIL;\r
390         }\r
391 \r
392         if( xArePollingQueuesStillRunning() != pdTRUE )\r
393         {\r
394                 lReturn = ( long ) pdFAIL;\r
395         }\r
396 \r
397         if( xAreMathsTaskStillRunning() != pdTRUE )\r
398         {\r
399                 lReturn = ( long ) pdFAIL;\r
400         }\r
401 \r
402         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
403         {\r
404                 lReturn = ( long ) pdFAIL;\r
405         }\r
406 \r
407         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
408         {\r
409                 lReturn = ( long ) pdFAIL;\r
410         }\r
411 \r
412         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
413         {\r
414                 lReturn = ( long ) pdFAIL;\r
415         }\r
416 \r
417         if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )\r
418         {\r
419                 /* The vMemCheckTask did not increment the counter - it must\r
420                 have failed. */\r
421                 lReturn = ( long ) pdFAIL;\r
422         }\r
423 \r
424         return lReturn;\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 static void vMemCheckTask( void *pvParameters )\r
429 {\r
430 unsigned long *pulMemCheckTaskRunningCounter;\r
431 void *pvMem1, *pvMem2, *pvMem3;\r
432 static long lErrorOccurred = pdFALSE;\r
433 \r
434         /* This task is dynamically created then deleted during each cycle of the\r
435         vErrorChecks task to check the operation of the memory allocator.  Each time\r
436         the task is created memory is allocated for the stack and TCB.  Each time\r
437         the task is deleted this memory is returned to the heap.  This task itself\r
438         exercises the allocator by allocating and freeing blocks.\r
439 \r
440         The task executes at the idle priority so does not require a delay.\r
441 \r
442         pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the\r
443         vErrorChecks() task that this task is still executing without error. */\r
444 \r
445         pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;\r
446 \r
447         for( ;; )\r
448         {\r
449                 if( lErrorOccurred == pdFALSE )\r
450                 {\r
451                         /* We have never seen an error so increment the counter. */\r
452                         ( *pulMemCheckTaskRunningCounter )++;\r
453                 }\r
454 \r
455                 /* Allocate some memory - just to give the allocator some extra\r
456                 exercise.  This has to be in a critical section to ensure the\r
457                 task does not get deleted while it has memory allocated. */\r
458                 vTaskSuspendAll();\r
459                 {\r
460                         pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );\r
461                         if( pvMem1 == NULL )\r
462                         {\r
463                                 lErrorOccurred = pdTRUE;\r
464                         }\r
465                         else\r
466                         {\r
467                                 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );\r
468                                 vPortFree( pvMem1 );\r
469                         }\r
470                 }\r
471                 xTaskResumeAll();\r
472 \r
473                 /* Again - with a different size block. */\r
474                 vTaskSuspendAll();\r
475                 {\r
476                         pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );\r
477                         if( pvMem2 == NULL )\r
478                         {\r
479                                 lErrorOccurred = pdTRUE;\r
480                         }\r
481                         else\r
482                         {\r
483                                 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );\r
484                                 vPortFree( pvMem2 );\r
485                         }\r
486                 }\r
487                 xTaskResumeAll();\r
488 \r
489                 /* Again - with a different size block. */\r
490                 vTaskSuspendAll();\r
491                 {\r
492                         pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );\r
493                         if( pvMem3 == NULL )\r
494                         {\r
495                                 lErrorOccurred = pdTRUE;\r
496                         }\r
497                         else\r
498                         {\r
499                                 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );\r
500                                 vPortFree( pvMem3 );\r
501                         }\r
502                 }\r
503                 xTaskResumeAll();\r
504         }\r
505 }\r
506 \r
507 \r
508 \r