]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91FR40008_GCC/main.c
b0cf90702ceb3458eb98f1ea89f0ecd364ab9945
[freertos] / FreeRTOS / Demo / ARM7_AT91FR40008_GCC / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\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.\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 /* Standard includes. */\r
62 #include <stdlib.h>\r
63 #include <string.h>\r
64 \r
65 /* Scheduler includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /* Demo application includes. */\r
70 #include "partest.h"\r
71 #include "flash.h"\r
72 #include "integer.h"\r
73 #include "PollQ.h"\r
74 #include "comtest2.h"\r
75 #include "semtest.h"\r
76 #include "flop.h"\r
77 #include "dynamic.h"\r
78 #include "BlockQ.h"\r
79 #include "serial.h"\r
80 \r
81 /* Hardware specific definitions. */\r
82 #include "aic.h"\r
83 #include "ebi.h"\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* Constants for the ComTest tasks. */\r
88 #define mainCOM_TEST_BAUD_RATE  ( ( unsigned long ) 115200 )\r
89 #define mainCOM_TEST_LED                ( 5 )\r
90 \r
91 /* Priorities for the demo application tasks. */\r
92 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
93 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
94 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
95 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
96 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
97 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
98 \r
99 /* The rate at which the on board LED will toggle when there is/is not an\r
100 error. */\r
101 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
102 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
103 #define mainON_BOARD_LED_BIT            ( ( unsigned long ) 7 )\r
104 \r
105 /* Constants used by the vMemCheckTask() task. */\r
106 #define mainCOUNT_INITIAL_VALUE         ( ( unsigned long ) 0 )\r
107 #define mainNO_TASK                                     ( 0 )\r
108 \r
109 /* The size of the memory blocks allocated by the vMemCheckTask() task. */\r
110 #define mainMEM_CHECK_SIZE_1            ( ( size_t ) 51 )\r
111 #define mainMEM_CHECK_SIZE_2            ( ( size_t ) 52 )\r
112 #define mainMEM_CHECK_SIZE_3            ( ( size_t ) 151 )\r
113 \r
114 #define MAX_WAIT_STATES  8\r
115 static const unsigned long ululCSRWaitValues[ MAX_WAIT_STATES + 1 ] =\r
116 {\r
117         WaitState1,/* There is no "zero wait state" value, so use one wait state */\r
118         WaitState1,\r
119         WaitState2,\r
120         WaitState3,\r
121         WaitState4,\r
122         WaitState5,\r
123         WaitState6,\r
124         WaitState7,\r
125         WaitState8\r
126 };\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 /*\r
130  * Checks that all the demo application tasks are still executing without error\r
131  * - as described at the top of the file.\r
132  */\r
133 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );\r
134 \r
135 /*\r
136  * The task that executes at the highest priority and calls\r
137  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
138  * of the file.\r
139  */\r
140 static void vErrorChecks( void *pvParameters );\r
141 \r
142 /*\r
143  * Dynamically created and deleted during each cycle of the vErrorChecks()\r
144  * task.  This is done to check the operation of the memory allocator.\r
145  * See the top of vErrorChecks for more details.\r
146  */\r
147 static void vMemCheckTask( void *pvParameters );\r
148 \r
149 /*\r
150  * Configure the processor for use with the Olimex demo board.  This includes\r
151  * setup for the I/O, system clock, and access timings.\r
152  */\r
153 static void prvSetupHardware( void );\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /*\r
158  * Starts all the other tasks, then starts the scheduler.\r
159  */\r
160 int main( void )\r
161 {\r
162         /* Setup the hardware for use with the Olimex demo board. */\r
163         prvSetupHardware();\r
164 \r
165         /* Start the demo/test application tasks. */\r
166         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
167         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
168         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
169         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
170         vStartMathTasks( tskIDLE_PRIORITY );\r
171         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
172         vStartDynamicPriorityTasks();\r
173         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
174 \r
175         /* Start the check task - which is defined in this file. */\r
176         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
177 \r
178         /* Now all the tasks have been started - start the scheduler.\r
179 \r
180         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
181         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
182         called.  The demo applications included in the FreeRTOS.org download switch\r
183         to supervisor mode prior to main being called.  If you are not using one of\r
184         these demo application projects then ensure Supervisor mode is used here. */\r
185         vTaskStartScheduler();\r
186 \r
187         /* Should never reach here! */\r
188         return 0;\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void vErrorChecks( void *pvParameters )\r
193 {\r
194 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
195 unsigned long ulMemCheckTaskRunningCount;\r
196 TaskHandle_t xCreatedTask;\r
197 \r
198         /* Just to stop compiler warnings. */\r
199         ( void ) pvParameters;\r
200 \r
201         /* Cycle for ever, delaying then checking all the other tasks are still\r
202         operating without error.  If an error is detected then the delay period\r
203         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
204         the on board LED flash rate will increase.\r
205 \r
206         In addition to the standard tests the memory allocator is tested through\r
207         the dynamic creation and deletion of a task each cycle.  Each time the\r
208         task is created memory must be allocated for its stack.  When the task is\r
209         deleted this memory is returned to the heap.  If the task cannot be created\r
210         then it is likely that the memory allocation failed. */\r
211 \r
212         for( ;; )\r
213         {\r
214                 /* Reset xCreatedTask.  This is modified by the task about to be\r
215                 created so we can tell if it is executing correctly or not. */\r
216                 xCreatedTask = mainNO_TASK;\r
217 \r
218                 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a\r
219                 parameter. */\r
220                 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;\r
221                 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )\r
222                 {\r
223                         /* Could not create the task - we have probably run out of heap. */\r
224                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
225                 }\r
226 \r
227                 /* Delay until it is time to execute again. */\r
228                 vTaskDelay( xDelayPeriod );\r
229 \r
230                 /* Delete the dynamically created task. */\r
231                 if( xCreatedTask != mainNO_TASK )\r
232                 {\r
233                         vTaskDelete( xCreatedTask );\r
234                 }\r
235 \r
236                 /* Check all the standard demo application tasks are executing without\r
237                 error.  ulMemCheckTaskRunningCount is checked to ensure it was\r
238                 modified by the task just deleted. */\r
239                 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )\r
240                 {\r
241                         /* An error has been detected in one of the tasks - flash faster. */\r
242                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
243                 }\r
244 \r
245                 /* The toggle rate of the LED depends on how long this task delays for.\r
246                 An error reduces the delay period and so increases the toggle rate. */\r
247                 vParTestToggleLED( mainON_BOARD_LED_BIT );\r
248         }\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 static void prvSetupHardware( void )\r
253 {\r
254 long lCount;\r
255 \r
256         #ifdef RUN_FROM_ROM\r
257         {\r
258         portFLOAT nsecsPerClockTick;\r
259         long lNumWaitStates;\r
260         unsigned long ulCSRWaitValue;\r
261 \r
262                 /* We are compiling to run from ROM (either on-chip or off-chip flash).\r
263                 Leave the RAM/flash mapped the way they are on reset\r
264                 (flash @ 0x00000000, RAM @ 0x00300000), and set up the\r
265                 proper flash wait states (starts out at the maximum number\r
266                 of wait states on reset, so we should be able to reduce it).\r
267                 Most of this code will probably get removed by the compiler\r
268                 if optimization is enabled, since these calculations are\r
269                 based on constants.  But the compiler should still produce\r
270                 a correct wait state register value. */\r
271                 nsecsPerClockTick = ( portFLOAT ) 1000000000 / configCPU_CLOCK_HZ;\r
272                 lNumWaitStates = ( long )( ( configFLASH_SPEED_NSEC / nsecsPerClockTick ) + 0.5 ) - 1;\r
273 \r
274                 if( lNumWaitStates < 0 )\r
275                 {\r
276                         lNumWaitStates = 0;\r
277                 }\r
278 \r
279                 if( lNumWaitStates > MAX_WAIT_STATES )\r
280                 {\r
281                         lNumWaitStates = MAX_WAIT_STATES;\r
282                 }\r
283 \r
284                 ulCSRWaitValue = ululCSRWaitValues[ lNumWaitStates ];\r
285                 ulCSRWaitValue = WaitState5;\r
286 \r
287                 AT91C_BASE_EBI->EBI_CSR[ 0 ] = ulCSRWaitValue | DataBus16 | WaitStateEnable\r
288                                                                            | PageSize1M | tDF_0cycle\r
289                                                                            | ByteWriteAccessType | CSEnable\r
290                                                                            | 0x00000000 /* Base Address */;\r
291         }\r
292         #else  /* else we are compiling to run from on-chip RAM */\r
293         {\r
294                 /* If compiling to run from RAM, we expect the on-chip RAM to already\r
295                 be mapped at 0x00000000.  This is typically done with an initialization\r
296                 script for the JTAG emulator you are using to download and run the\r
297                 demo application. So there is nothing to do here in this case. */\r
298         }\r
299         #endif\r
300 \r
301         /* Disable all interrupts at the AIC level initially... */\r
302         AT91C_BASE_AIC->AIC_IDCR = 0xFFFFFFFF;\r
303 \r
304         /* Set all SVR and SMR entries to default values (start with a clean slate)... */\r
305         for( lCount = 0; lCount < 32; lCount++ )\r
306         {\r
307                 AT91C_BASE_AIC->AIC_SVR[ lCount ] = (unsigned long) 0;\r
308                 AT91C_BASE_AIC->AIC_SMR[ lCount ] = AIC_SRCTYPE_INT_EDGE_TRIGGERED;\r
309         }\r
310 \r
311         /* Disable clocks to all peripherals initially... */\r
312         AT91C_BASE_PS->PS_PCDR = 0xFFFFFFFF;\r
313 \r
314         /* Clear all interrupts at the AIC level initially... */\r
315         AT91C_BASE_AIC->AIC_ICCR = 0xFFFFFFFF;\r
316 \r
317         /* Perform 8 "End Of Interrupt" cmds to make sure AIC will not Lock out\r
318         nIRQ */\r
319         for( lCount = 0; lCount < 8; lCount++ )\r
320         {\r
321                 AT91C_BASE_AIC->AIC_EOICR = 0;\r
322         }\r
323 \r
324         /* Initialise LED outputs. */\r
325         vParTestInitialise();\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )\r
330 {\r
331 long lReturn = ( long ) pdPASS;\r
332 \r
333         /* Check all the demo tasks (other than the flash tasks) to ensure\r
334         that they are all still running, and that none of them have detected\r
335         an error. */\r
336 \r
337         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
338         {\r
339                 lReturn = ( long ) pdFAIL;\r
340         }\r
341 \r
342         if( xAreComTestTasksStillRunning() != pdTRUE )\r
343         {\r
344                 lReturn = ( long ) pdFAIL;\r
345         }\r
346 \r
347         if( xArePollingQueuesStillRunning() != pdTRUE )\r
348         {\r
349                 lReturn = ( long ) pdFAIL;\r
350         }\r
351 \r
352         if( xAreMathsTaskStillRunning() != pdTRUE )\r
353         {\r
354                 lReturn = ( long ) pdFAIL;\r
355         }\r
356 \r
357         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
358         {\r
359                 lReturn = ( long ) pdFAIL;\r
360         }\r
361 \r
362         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
363         {\r
364                 lReturn = ( long ) pdFAIL;\r
365         }\r
366 \r
367         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
368         {\r
369                 lReturn = ( long ) pdFAIL;\r
370         }\r
371 \r
372         if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )\r
373         {\r
374                 /* The vMemCheckTask did not increment the counter - it must\r
375                 have failed. */\r
376                 lReturn = ( long ) pdFAIL;\r
377         }\r
378 \r
379         return lReturn;\r
380 }\r
381 /*-----------------------------------------------------------*/\r
382 \r
383 static void vMemCheckTask( void *pvParameters )\r
384 {\r
385 unsigned long *pulMemCheckTaskRunningCounter;\r
386 void *pvMem1, *pvMem2, *pvMem3;\r
387 static long lErrorOccurred = pdFALSE;\r
388 \r
389         /* This task is dynamically created then deleted during each cycle of the\r
390         vErrorChecks task to check the operation of the memory allocator.  Each time\r
391         the task is created memory is allocated for the stack and TCB.  Each time\r
392         the task is deleted this memory is returned to the heap.  This task itself\r
393         exercises the allocator by allocating and freeing blocks.\r
394 \r
395         The task executes at the idle priority so does not require a delay.\r
396 \r
397         pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the\r
398         vErrorChecks() task that this task is still executing without error. */\r
399 \r
400         pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;\r
401 \r
402         for( ;; )\r
403         {\r
404                 if( lErrorOccurred == pdFALSE )\r
405                 {\r
406                         /* We have never seen an error so increment the counter. */\r
407                         ( *pulMemCheckTaskRunningCounter )++;\r
408                 }\r
409                 else\r
410                 {\r
411                         /* There has been an error so reset the counter so the check task\r
412                         can tell that an error occurred. */\r
413                         *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;\r
414                 }\r
415 \r
416                 /* Allocate some memory - just to give the allocator some extra\r
417                 exercise.  This has to be in a critical section to ensure the\r
418                 task does not get deleted while it has memory allocated. */\r
419                 vTaskSuspendAll();\r
420                 {\r
421                         pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );\r
422                         if( pvMem1 == NULL )\r
423                         {\r
424                                 lErrorOccurred = pdTRUE;\r
425                         }\r
426                         else\r
427                         {\r
428                                 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );\r
429                                 vPortFree( pvMem1 );\r
430                         }\r
431                 }\r
432                 xTaskResumeAll();\r
433 \r
434                 /* Again - with a different size block. */\r
435                 vTaskSuspendAll();\r
436                 {\r
437                         pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );\r
438                         if( pvMem2 == NULL )\r
439                         {\r
440                                 lErrorOccurred = pdTRUE;\r
441                         }\r
442                         else\r
443                         {\r
444                                 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );\r
445                                 vPortFree( pvMem2 );\r
446                         }\r
447                 }\r
448                 xTaskResumeAll();\r
449 \r
450                 /* Again - with a different size block. */\r
451                 vTaskSuspendAll();\r
452                 {\r
453                         pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );\r
454                         if( pvMem3 == NULL )\r
455                         {\r
456                                 lErrorOccurred = pdTRUE;\r
457                         }\r
458                         else\r
459                         {\r
460                                 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );\r
461                                 vPortFree( pvMem3 );\r
462                         }\r
463                 }\r
464                 xTaskResumeAll();\r
465         }\r
466 }\r
467 \r