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