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