]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_LPC2138_Rowley/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / ARM7_LPC2138_Rowley / main.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /*
71  * This file contains a demo created to execute on the Rowley Associates
72  * LPC2138 CrossFire development board.
73  *
74  * main() creates all the demo application tasks, then starts the scheduler.
75  * The WEB documentation provides more details of the standard demo application
76  * tasks.
77  *
78  * Main.c also creates a task called "Check".  This only executes every few
79  * seconds but has a high priority so is guaranteed to get processor time.
80  * Its function is to check that all the other tasks are still operational.
81  * Each standard demo task maintains a unique count that is incremented each
82  * time the task successfully completes its function.  Should any error occur
83  * within such a task the count is permanently halted.  The check task inspects
84  * the count of each task to ensure it has changed since the last time the
85  * check task executed.  If all the count variables have changed all the tasks
86  * are still executing error free, and the check task writes "PASS" to the
87  * CrossStudio terminal IO window.  Should any task contain an error at any time
88  * the error is latched and "FAIL" written to the terminal IO window.
89  *
90  * Finally, main() sets up an interrupt service routine and task to handle
91  * pushes of the button that is built into the CrossFire board.  When the button
92  * is pushed the ISR wakes the button task - which generates a table of task
93  * status information which is also displayed on the terminal IO window.
94  *
95  * A print task is defined to ensure exclusive and consistent access to the
96  * terminal IO.  This is the only task that is allowed to access the terminal.
97  * The check and button task therefore do not access the terminal directly but
98  * instead pass a pointer to the message they wish to display to the print task.
99  */
100
101 /* Standard includes. */
102 #include <__cross_studio_io.h>
103
104 /* Scheduler includes. */
105 #include "FreeRTOS.h"
106 #include "task.h"
107 #include "queue.h"
108 #include "semphr.h"
109
110 /* Demo app includes. */
111 #include "BlockQ.h"
112 #include "death.h"
113 #include "dynamic.h"
114 #include "integer.h"
115 #include "PollQ.h"
116 #include "blocktim.h"
117 #include "recmutex.h"
118 #include "semtest.h"
119
120 /* Hardware configuration definitions. */
121 #define mainBUS_CLK_FULL                                        ( ( unsigned char ) 0x01 )
122 #define mainLED_BIT                                                     0x80000000
123 #define mainP0_14__EINT_1                                       ( 2 << 28 )
124 #define mainEINT_1_EDGE_SENSITIVE                       2
125 #define mainEINT_1_FALLING_EDGE_SENSITIVE       0
126 #define mainEINT_1_CHANNEL                                      15
127 #define mainEINT_1_VIC_CHANNEL_BIT                      ( 1 << mainEINT_1_CHANNEL )
128 #define mainEINT_1_ENABLE_BIT                           ( 1 << 5 )
129
130 /* Demo application definitions. */
131 #define mainQUEUE_SIZE                                          ( 3 )
132 #define mainLED_DELAY                                           ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
133 #define mainERROR_LED_DELAY                                     ( ( TickType_t ) 50 / portTICK_PERIOD_MS )
134 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
135 #define mainLIST_BUFFER_SIZE                            2048
136 #define mainNO_DELAY                                            ( 0 )
137 #define mainSHORT_DELAY                                         ( 150 / portTICK_PERIOD_MS )
138
139 /* Task priorities. */
140 #define mainLED_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 2 )
141 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
142 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
143 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
144 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
145 #define mainPRINT_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 0 )
146
147 /*-----------------------------------------------------------*/
148
149 /* The semaphore used to wake the button task from within the external interrupt
150 handler. */
151 SemaphoreHandle_t xButtonSemaphore;
152
153 /* The queue that is used to send message to vPrintTask for display in the
154 terminal output window. */
155 QueueHandle_t xPrintQueue;
156
157 /* The rate at which the LED will toggle.  The toggle rate increases if an
158 error is detected in any task. */
159 static TickType_t xLED_Delay = mainLED_DELAY;
160 /*-----------------------------------------------------------*/
161
162 /*
163  * Simply flashes the on board LED every mainLED_DELAY milliseconds.
164  */
165 static void vLEDTask( void *pvParameters );
166
167 /*
168  * Checks the status of all the demo tasks then prints a message to the
169  * CrossStudio terminal IO windows.  The message will be either PASS or FAIL
170  * depending on the status of the demo applications tasks.  A FAIL status will
171  * be latched.
172  *
173  * Messages are not written directly to the terminal, but passed to vPrintTask
174  * via a queue.
175  */
176 static void vCheckTask( void *pvParameters );
177
178 /*
179  * Controls all terminal output.  If a task wants to send a message to the
180  * terminal IO it posts a pointer to the text to vPrintTask via a queue.  This
181  * ensures serial access to the terminal IO.
182  */
183 static void vPrintTask( void *pvParameter );
184
185 /*
186  * Simply waits for an interrupt to be generated from the built in button, then
187  * generates a table of tasks states that is then written by vPrintTask to the
188  * terminal output window within CrossStudio.
189  */
190 static void vButtonHandlerTask( void *pvParameters );
191
192 /*-----------------------------------------------------------*/
193
194 int main( void )
195 {
196         /* Setup the peripheral bus to be the same as the PLL output. */
197         VPBDIV = mainBUS_CLK_FULL;
198
199         /* Create the queue used to pass message to vPrintTask. */
200         xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
201
202         /* Create the semaphore used to wake vButtonHandlerTask(). */
203         vSemaphoreCreateBinary( xButtonSemaphore );
204         xSemaphoreTake( xButtonSemaphore, 0 );
205
206         /* Start the standard demo tasks. */
207         vStartIntegerMathTasks( tskIDLE_PRIORITY );
208         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
209         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
210         vStartDynamicPriorityTasks();
211         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
212
213         #if configUSE_PREEMPTION == 1
214         {
215                 /* The timing of console output when not using the preemptive
216                 scheduler causes the block time tests to detect a timing problem. */
217                 vCreateBlockTimeTasks();
218         }
219         #endif
220
221     vStartRecursiveMutexTasks();
222
223         /* Start the tasks defined within this file. */
224         xTaskCreate( vLEDTask, "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
225     xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
226     xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
227     xTaskCreate( vButtonHandlerTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
228
229         /* Start the scheduler. */
230         vTaskStartScheduler();
231
232         /* The scheduler should now be running, so we will only ever reach here if we
233         ran out of heap space. */
234
235         return 0;
236 }
237 /*-----------------------------------------------------------*/
238
239 static void vLEDTask( void *pvParameters )
240 {
241         /* Just to remove compiler warnings. */
242         ( void ) pvParameters;
243
244         /* Configure IO. */
245         IO0DIR |= mainLED_BIT;
246         IO0SET = mainLED_BIT;
247
248         for( ;; )
249         {
250                 /* Not very exiting - just delay... */
251                 vTaskDelay( xLED_Delay );
252
253                 /* ...set the IO ... */
254         IO0CLR = mainLED_BIT;
255
256                 /* ...delay again... */
257                 vTaskDelay( xLED_Delay );
258
259                 /* ...then clear the IO. */
260                 IO0SET = mainLED_BIT;
261         }
262 }
263 /*-----------------------------------------------------------*/
264
265 static void vCheckTask( void *pvParameters )
266 {
267 portBASE_TYPE xErrorOccurred = pdFALSE;
268 TickType_t xLastExecutionTime;
269 const char * const pcPassMessage = "PASS\n";
270 const char * const pcFailMessage = "FAIL\n";
271
272         /* Just to remove compiler warnings. */
273         ( void ) pvParameters;
274
275         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
276         works correctly. */
277         xLastExecutionTime = xTaskGetTickCount();
278
279         for( ;; )
280         {
281                 /* Perform this check every mainCHECK_DELAY milliseconds. */
282                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
283
284                 /* Has an error been found in any task? */
285
286                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
287                 {
288                         xErrorOccurred = pdTRUE;
289                 }
290
291                 if( xArePollingQueuesStillRunning() != pdTRUE )
292                 {
293                         xErrorOccurred = pdTRUE;
294                 }
295
296                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
297                 {
298                         xErrorOccurred = pdTRUE;
299                 }
300
301                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
302                 {
303                         xErrorOccurred = pdTRUE;
304                 }
305
306                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
307                 {
308                         xErrorOccurred = pdTRUE;
309                 }
310
311                 #if configUSE_PREEMPTION == 1
312                 {
313                         /* The timing of console output when not using the preemptive
314                         scheduler causes the block time tests to detect a timing problem. */
315                         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
316                         {
317                                 xErrorOccurred = pdTRUE;
318                         }
319                 }
320                 #endif
321
322                 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
323                 {
324                         xErrorOccurred = pdTRUE;
325                 }
326
327                 /* Send either a pass or fail message.  If an error is found it is
328                 never cleared again. */
329                 if( xErrorOccurred == pdTRUE )
330                 {
331                         xLED_Delay = mainERROR_LED_DELAY;
332                         xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
333                 }
334                 else
335                 {
336                         xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
337                 }
338         }
339 }
340 /*-----------------------------------------------------------*/
341
342 static void vPrintTask( void *pvParameters )
343 {
344 char *pcMessage;
345
346         /* Just to stop compiler warnings. */
347         ( void ) pvParameters;
348
349         for( ;; )
350         {
351                 /* Wait for a message to arrive. */
352                 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
353
354                 /* Write the message to the terminal IO. */
355                 #ifndef NDEBUG
356                         debug_printf( "%s", pcMessage );
357                 #endif
358         }
359 }
360 /*-----------------------------------------------------------*/
361
362 static void vButtonHandlerTask( void *pvParameters )
363 {
364 static char cListBuffer[ mainLIST_BUFFER_SIZE ];
365 const char *pcList = &( cListBuffer[ 0 ] );
366 const char * const pcHeader = "\nTask          State  Priority  Stack   #\n************************************************";
367 extern void (vButtonISRWrapper) ( void );
368
369         /* Just to stop compiler warnings. */
370         ( void ) pvParameters;
371
372         /* Configure the interrupt. */
373         portENTER_CRITICAL();
374         {
375                 /* Configure P0.14 to generate interrupts. */
376                 PINSEL0 |= mainP0_14__EINT_1;
377                 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
378                 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
379
380                 /* Setup the VIC for EINT 1. */
381                 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
382                 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
383                 VICVectAddr1 = ( long ) vButtonISRWrapper;
384                 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
385         }
386         portEXIT_CRITICAL();
387
388         for( ;; )
389         {
390                 /* For debouncing, wait a while then clear the semaphore. */
391                 vTaskDelay( mainSHORT_DELAY );
392                 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
393
394                 /* Wait for an interrupt. */
395                 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
396
397                 /* Send the column headers to the print task for display. */
398                 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
399
400                 /* Create the list of task states. */
401                 vTaskList( cListBuffer );
402
403                 /* Send the task status information to the print task for display. */
404                 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
405         }
406 }
407 /*-----------------------------------------------------------*/
408
409 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
410 {
411         /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
412         if pcTaskName has itself been corrupted. */
413         ( void ) pxTask;
414         ( void ) pcTaskName;
415         for( ;; );
416 }
417
418
419
420
421
422