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