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