]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_LPC2138_Rowley/main.c
there was an extra ')'... caused build to fail
[cmsis-freertos] / Demo / ARM7_LPC2138_Rowley / main.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 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  * This file contains a demo created to execute on the Rowley Associates
30  * LPC2138 CrossFire development board.
31  *
32  * main() creates all the demo application tasks, then starts the scheduler.
33  * The WEB documentation provides more details of the standard demo application
34  * tasks.
35  *
36  * Main.c also creates a task called "Check".  This only executes every few
37  * seconds but has a high priority so is guaranteed to get processor time.
38  * Its function is to check that all the other tasks are still operational.
39  * Each standard demo task maintains a unique count that is incremented each
40  * time the task successfully completes its function.  Should any error occur
41  * within such a task the count is permanently halted.  The check task inspects
42  * the count of each task to ensure it has changed since the last time the
43  * check task executed.  If all the count variables have changed all the tasks
44  * are still executing error free, and the check task writes "PASS" to the
45  * CrossStudio terminal IO window.  Should any task contain an error at any time
46  * the error is latched and "FAIL" written to the terminal IO window.
47  *
48  * Finally, main() sets up an interrupt service routine and task to handle
49  * pushes of the button that is built into the CrossFire board.  When the button
50  * is pushed the ISR wakes the button task - which generates a table of task
51  * status information which is also displayed on the terminal IO window.
52  *
53  * A print task is defined to ensure exclusive and consistent access to the
54  * terminal IO.  This is the only task that is allowed to access the terminal.
55  * The check and button task therefore do not access the terminal directly but
56  * instead pass a pointer to the message they wish to display to the print task.
57  */
58
59 /* Standard includes. */
60 #include <__cross_studio_io.h>
61
62 /* Scheduler includes. */
63 #include "FreeRTOS.h"
64 #include "task.h"
65 #include "queue.h"
66 #include "semphr.h"
67
68 /* Demo app includes. */
69 #include "BlockQ.h"
70 #include "death.h"
71 #include "dynamic.h"
72 #include "integer.h"
73 #include "PollQ.h"
74 #include "blocktim.h"
75 #include "recmutex.h"
76 #include "semtest.h"
77
78 /* Hardware configuration definitions. */
79 #define mainBUS_CLK_FULL                                        ( ( unsigned char ) 0x01 )
80 #define mainLED_BIT                                                     0x80000000
81 #define mainP0_14__EINT_1                                       ( 2 << 28 )
82 #define mainEINT_1_EDGE_SENSITIVE                       2
83 #define mainEINT_1_FALLING_EDGE_SENSITIVE       0
84 #define mainEINT_1_CHANNEL                                      15
85 #define mainEINT_1_VIC_CHANNEL_BIT                      ( 1 << mainEINT_1_CHANNEL )
86 #define mainEINT_1_ENABLE_BIT                           ( 1 << 5 )
87
88 /* Demo application definitions. */
89 #define mainQUEUE_SIZE                                          ( 3 )
90 #define mainLED_DELAY                                           ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
91 #define mainERROR_LED_DELAY                                     ( ( TickType_t ) 50 / portTICK_PERIOD_MS )
92 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
93 #define mainLIST_BUFFER_SIZE                            2048
94 #define mainNO_DELAY                                            ( 0 )
95 #define mainSHORT_DELAY                                         ( 150 / portTICK_PERIOD_MS )
96
97 /* Task priorities. */
98 #define mainLED_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 2 )
99 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
100 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
101 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
102 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
103 #define mainPRINT_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 0 )
104
105 /*-----------------------------------------------------------*/
106
107 /* The semaphore used to wake the button task from within the external interrupt
108 handler. */
109 SemaphoreHandle_t xButtonSemaphore;
110
111 /* The queue that is used to send message to vPrintTask for display in the
112 terminal output window. */
113 QueueHandle_t xPrintQueue;
114
115 /* The rate at which the LED will toggle.  The toggle rate increases if an
116 error is detected in any task. */
117 static TickType_t xLED_Delay = mainLED_DELAY;
118 /*-----------------------------------------------------------*/
119
120 /*
121  * Simply flashes the on board LED every mainLED_DELAY milliseconds.
122  */
123 static void vLEDTask( void *pvParameters );
124
125 /*
126  * Checks the status of all the demo tasks then prints a message to the
127  * CrossStudio terminal IO windows.  The message will be either PASS or FAIL
128  * depending on the status of the demo applications tasks.  A FAIL status will
129  * be latched.
130  *
131  * Messages are not written directly to the terminal, but passed to vPrintTask
132  * via a queue.
133  */
134 static void vCheckTask( void *pvParameters );
135
136 /*
137  * Controls all terminal output.  If a task wants to send a message to the
138  * terminal IO it posts a pointer to the text to vPrintTask via a queue.  This
139  * ensures serial access to the terminal IO.
140  */
141 static void vPrintTask( void *pvParameter );
142
143 /*
144  * Simply waits for an interrupt to be generated from the built in button, then
145  * generates a table of tasks states that is then written by vPrintTask to the
146  * terminal output window within CrossStudio.
147  */
148 static void vButtonHandlerTask( void *pvParameters );
149
150 /*-----------------------------------------------------------*/
151
152 int main( void )
153 {
154         /* Setup the peripheral bus to be the same as the PLL output. */
155         VPBDIV = mainBUS_CLK_FULL;
156
157         /* Create the queue used to pass message to vPrintTask. */
158         xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
159
160         /* Create the semaphore used to wake vButtonHandlerTask(). */
161         vSemaphoreCreateBinary( xButtonSemaphore );
162         xSemaphoreTake( xButtonSemaphore, 0 );
163
164         /* Start the standard demo tasks. */
165         vStartIntegerMathTasks( tskIDLE_PRIORITY );
166         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
167         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
168         vStartDynamicPriorityTasks();
169         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
170
171         #if configUSE_PREEMPTION == 1
172         {
173                 /* The timing of console output when not using the preemptive
174                 scheduler causes the block time tests to detect a timing problem. */
175                 vCreateBlockTimeTasks();
176         }
177         #endif
178
179     vStartRecursiveMutexTasks();
180
181         /* Start the tasks defined within this file. */
182         xTaskCreate( vLEDTask, "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
183     xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
184     xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
185     xTaskCreate( vButtonHandlerTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
186
187         /* Start the scheduler. */
188         vTaskStartScheduler();
189
190         /* The scheduler should now be running, so we will only ever reach here if we
191         ran out of heap space. */
192
193         return 0;
194 }
195 /*-----------------------------------------------------------*/
196
197 static void vLEDTask( void *pvParameters )
198 {
199         /* Just to remove compiler warnings. */
200         ( void ) pvParameters;
201
202         /* Configure IO. */
203         IO0DIR |= mainLED_BIT;
204         IO0SET = mainLED_BIT;
205
206         for( ;; )
207         {
208                 /* Not very exiting - just delay... */
209                 vTaskDelay( xLED_Delay );
210
211                 /* ...set the IO ... */
212         IO0CLR = mainLED_BIT;
213
214                 /* ...delay again... */
215                 vTaskDelay( xLED_Delay );
216
217                 /* ...then clear the IO. */
218                 IO0SET = mainLED_BIT;
219         }
220 }
221 /*-----------------------------------------------------------*/
222
223 static void vCheckTask( void *pvParameters )
224 {
225 portBASE_TYPE xErrorOccurred = pdFALSE;
226 TickType_t xLastExecutionTime;
227 const char * const pcPassMessage = "PASS\n";
228 const char * const pcFailMessage = "FAIL\n";
229
230         /* Just to remove compiler warnings. */
231         ( void ) pvParameters;
232
233         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
234         works correctly. */
235         xLastExecutionTime = xTaskGetTickCount();
236
237         for( ;; )
238         {
239                 /* Perform this check every mainCHECK_DELAY milliseconds. */
240                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
241
242                 /* Has an error been found in any task? */
243
244                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
245                 {
246                         xErrorOccurred = pdTRUE;
247                 }
248
249                 if( xArePollingQueuesStillRunning() != pdTRUE )
250                 {
251                         xErrorOccurred = pdTRUE;
252                 }
253
254                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
255                 {
256                         xErrorOccurred = pdTRUE;
257                 }
258
259                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
260                 {
261                         xErrorOccurred = pdTRUE;
262                 }
263
264                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
265                 {
266                         xErrorOccurred = pdTRUE;
267                 }
268
269                 #if configUSE_PREEMPTION == 1
270                 {
271                         /* The timing of console output when not using the preemptive
272                         scheduler causes the block time tests to detect a timing problem. */
273                         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
274                         {
275                                 xErrorOccurred = pdTRUE;
276                         }
277                 }
278                 #endif
279
280                 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
281                 {
282                         xErrorOccurred = pdTRUE;
283                 }
284
285                 /* Send either a pass or fail message.  If an error is found it is
286                 never cleared again. */
287                 if( xErrorOccurred == pdTRUE )
288                 {
289                         xLED_Delay = mainERROR_LED_DELAY;
290                         xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
291                 }
292                 else
293                 {
294                         xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
295                 }
296         }
297 }
298 /*-----------------------------------------------------------*/
299
300 static void vPrintTask( void *pvParameters )
301 {
302 char *pcMessage;
303
304         /* Just to stop compiler warnings. */
305         ( void ) pvParameters;
306
307         for( ;; )
308         {
309                 /* Wait for a message to arrive. */
310                 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
311
312                 /* Write the message to the terminal IO. */
313                 #ifndef NDEBUG
314                         debug_printf( "%s", pcMessage );
315                 #endif
316         }
317 }
318 /*-----------------------------------------------------------*/
319
320 static void vButtonHandlerTask( void *pvParameters )
321 {
322 static char cListBuffer[ mainLIST_BUFFER_SIZE ];
323 const char *pcList = &( cListBuffer[ 0 ] );
324 const char * const pcHeader = "\nTask          State  Priority  Stack   #\n************************************************";
325 extern void (vButtonISRWrapper) ( void );
326
327         /* Just to stop compiler warnings. */
328         ( void ) pvParameters;
329
330         /* Configure the interrupt. */
331         portENTER_CRITICAL();
332         {
333                 /* Configure P0.14 to generate interrupts. */
334                 PINSEL0 |= mainP0_14__EINT_1;
335                 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
336                 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
337
338                 /* Setup the VIC for EINT 1. */
339                 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
340                 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
341                 VICVectAddr1 = ( long ) vButtonISRWrapper;
342                 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
343         }
344         portEXIT_CRITICAL();
345
346         for( ;; )
347         {
348                 /* For debouncing, wait a while then clear the semaphore. */
349                 vTaskDelay( mainSHORT_DELAY );
350                 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
351
352                 /* Wait for an interrupt. */
353                 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
354
355                 /* Send the column headers to the print task for display. */
356                 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
357
358                 /* Create the list of task states. */
359                 vTaskList( cListBuffer );
360
361                 /* Send the task status information to the print task for display. */
362                 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
363         }
364 }
365 /*-----------------------------------------------------------*/
366
367 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
368 {
369         /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
370         if pcTaskName has itself been corrupted. */
371         ( void ) pxTask;
372         ( void ) pcTaskName;
373         for( ;; );
374 }
375
376
377
378
379
380