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