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