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