blob: ad2944108ab7de39062533ea4d9718fe804a8671 [file] [log] [blame]
Richard Barry3dcbed52006-06-22 17:18:31 +00001/*
Richard Barry16b6baf2008-07-30 20:04:29 +00002 FreeRTOS.org V5.0.3 - Copyright (C) 2003-2008 Richard Barry.
Richard Barry3dcbed52006-06-22 17:18:31 +00003
4 This file is part of the FreeRTOS distribution.
5
6 FreeRTOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 FreeRTOS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with FreeRTOS; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 A special exception to the GPL can be applied should you wish to distribute
21 a combined work that includes FreeRTOS, without being obliged to provide
22 the source code for any proprietary components. See the licensing section
23 of http://www.FreeRTOS.org for full details of how and when the exception
24 can be applied.
25
Richard Barry9596b042008-03-26 13:04:38 +000026 ***************************************************************************
27 ***************************************************************************
28 * *
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
30 * and even write all or part of your application on your behalf. *
31 * See http://www.OpenRTOS.com for details of the services we provide to *
32 * expedite your project. *
33 * *
34 ***************************************************************************
35 ***************************************************************************
Richard Barry0a6d59a2007-04-01 20:47:49 +000036
Richard Barry527fb6a2008-03-25 21:22:13 +000037 Please ensure to read the configuration and relevant port sections of the
Richard Barryc86dcf72008-02-03 19:45:58 +000038 online documentation.
39
Richard Barry527fb6a2008-03-25 21:22:13 +000040 http://www.FreeRTOS.org - Documentation, latest information, license and
41 contact details.
Richard Barryc86dcf72008-02-03 19:45:58 +000042
Richard Barry527fb6a2008-03-25 21:22:13 +000043 http://www.SafeRTOS.com - A version that is certified for use in safety
44 critical systems.
Richard Barryc86dcf72008-02-03 19:45:58 +000045
Richard Barry527fb6a2008-03-25 21:22:13 +000046 http://www.OpenRTOS.com - Commercial support, development, porting,
47 licensing and training services.
Richard Barry3dcbed52006-06-22 17:18:31 +000048*/
49
50/*
51 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
52 The processor MUST be in supervisor mode when vTaskStartScheduler is
53 called. The demo applications included in the FreeRTOS.org download switch
54 to supervisor mode prior to main being called. If you are not using one of
55 these demo application projects then ensure Supervisor mode is used.
56*/
57
58/*
59 * Creates all the demo application tasks, then starts the scheduler. The WEB
60 * documentation provides more details of the demo application tasks.
61 *
62 * A few tasks are created that are not part of the standard demo. These are
Richard Barry98a99592007-09-17 10:07:48 +000063 * the 'LCD' task, the 'LCD Message' task, a WEB server task and the 'Check'
Richard Barry3dcbed52006-06-22 17:18:31 +000064 * task.
65 *
66 * The LCD task is the only task that accesses the LCD directly, so mutual
67 * exclusion is ensured. Any task wishing to display text sends the LCD task
68 * a message containing a pointer to the string that should be displayed.
Richard Barry98a99592007-09-17 10:07:48 +000069 * The LCD task itself just blocks on a queue waiting for such a message to
Richard Barry3dcbed52006-06-22 17:18:31 +000070 * arrive - processing each in turn.
71 *
72 * The LCD Message task does nothing other than periodically send messages to
Richard Barry98a99592007-09-17 10:07:48 +000073 * the LCD task. The messages originating from the LCD Message task are
Richard Barry3dcbed52006-06-22 17:18:31 +000074 * displayed on the top row of the LCD.
75 *
Richard Barry98a99592007-09-17 10:07:48 +000076 * The Check task only executes every three seconds but has the highest
77 * priority so is guaranteed to get processor time. Its main function is to
78 * check that all the other tasks are still operational. Most tasks maintain
79 * a unique count that is incremented each time the task successfully completes
80 * a cycle of its function. Should any error occur within such a task the
Richard Barry3dcbed52006-06-22 17:18:31 +000081 * count is permanently halted. The check task sets a bit in an error status
82 * flag should it find any counter variable at a value that indicates an
Richard Barry98a99592007-09-17 10:07:48 +000083 * error has occurred. The error flag value is converted to a string and sent
Richard Barry3dcbed52006-06-22 17:18:31 +000084 * to the LCD task for display on the bottom row on the LCD.
85 */
86
87/* Standard includes. */
88#include <stdio.h>
89
90/* Library includes. */
91#include "91x_lib.h"
92
93/* Scheduler includes. */
94#include "FreeRTOS.h"
95#include "task.h"
96#include "queue.h"
97
98/* Demo application includes. */
99#include "lcd.h"
100#include "flash.h"
101#include "integer.h"
102#include "PollQ.h"
103#include "BlockQ.h"
104#include "semtest.h"
105#include "dynamic.h"
106#include "partest.h"
107#include "flop.h"
108#include "comtest2.h"
109#include "serial.h"
Richard Barry98a99592007-09-17 10:07:48 +0000110#include "GenQTest.h"
111#include "QPeek.h"
112
113#ifdef STACK_LWIP
114 #include "BasicWEB.h"
115 #include "sys.h"
116#endif
Richard Barry3dcbed52006-06-22 17:18:31 +0000117
118/* Priorities for the demo application tasks. */
119#define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
120#define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
121#define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
122#define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
123#define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
124#define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
125#define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
126#define mainMSG_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
Richard Barry98a99592007-09-17 10:07:48 +0000127#define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
Richard Barry3dcbed52006-06-22 17:18:31 +0000128
129/* Delays used by the various tasks defined in this file. */
130#define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
131#define mainSTRING_WRITE_DELAY ( 500 / portTICK_RATE_MS )
132#define mainLCD_DELAY ( 20 / portTICK_RATE_MS )
133
134/* Constants for the ComTest tasks. */
135#define mainCOM_TEST_BAUD_RATE ( ( unsigned portLONG ) 115200 )
136#define mainCOM_TEST_LED ( 3 )
137
138/* The maximum number of messages that can be pending to be written to the LCD. */
139#define mainLCD_QUEUE_LEN ( 6 )
140
141/* Dimension the buffer used to write the error flag string. */
142#define mainMAX_FLAG_STRING_LEN ( 32 )
143
144/* The structure that is passed on the LCD message queue. */
145typedef struct
146{
147 portCHAR **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */
148 portBASE_TYPE xRow; /*<< The row on which the message should be displayed. */
149} xLCDMessage;
150/*-----------------------------------------------------------*/
151
152/*
153 * The task that executes at the highest priority and calls
154 * prvCheckOtherTasksAreStillRunning(). See the description at the top
155 * of the file.
156 */
157static void vErrorChecks( void *pvParameters );
158
159/*
160 * Configure the processor clock and ports.
161 */
162static void prvSetupHardware( void );
163
164/*
165 * Checks that all the demo application tasks are still executing without error
166 * - as described at the top of the file. Called by vErrorChecks().
167 */
168static void prvCheckOtherTasksAreStillRunning( void );
169
Richard Barry98a99592007-09-17 10:07:48 +0000170#ifdef STACK_UIP
171 /*
172 * The WEB server task prototype. The task is created in this file but defined
173 * elsewhere. STACK_UIP is defined when the uIP stack is used in preference
174 * to the lwIP stack.
175 */
176 extern void vuIP_Task(void *pvParameters);
177#endif
Richard Barry3dcbed52006-06-22 17:18:31 +0000178
179/*
180 * The task that displays text on the LCD.
181 */
182static void prvLCDTask( void * pvParameters );
183
184/*
185 * The task that sends messages to be displayed on the top row of the LCD.
186 */
187static void prvLCDMessageTask( void * pvParameters );
188
189/*-----------------------------------------------------------*/
190
191/* The queue used to pass messages to the LCD task. */
192static xQueueHandle xLCDQueue;
193
194/* Error status flag. */
195static unsigned portLONG ulErrorFlags = 0;
196
197/*-----------------------------------------------------------*/
198
199/*
200 * Starts all the other tasks, then starts the scheduler.
201 */
202void main( void )
203{
204 #ifdef DEBUG
205 debug();
206 #endif
207
208 /* Setup any hardware that has not already been configured by the low
209 level init routines. */
210 prvSetupHardware();
Richard Barry3dcbed52006-06-22 17:18:31 +0000211 /* Create the queue used to send data to the LCD task. */
212 xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );
Richard Barry98a99592007-09-17 10:07:48 +0000213
Richard Barry3dcbed52006-06-22 17:18:31 +0000214 /* Start all the standard demo application tasks. */
215 vStartIntegerMathTasks( tskIDLE_PRIORITY );
216 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
217 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
218 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
219 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
220 vStartDynamicPriorityTasks();
221 vStartMathTasks( tskIDLE_PRIORITY );
222 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
Richard Barry98a99592007-09-17 10:07:48 +0000223 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
224 vStartQueuePeekTasks();
Richard Barry3dcbed52006-06-22 17:18:31 +0000225
226 /* Start the tasks which are defined in this file. */
227 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
228 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );
229 xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );
230
Richard Barry98a99592007-09-17 10:07:48 +0000231 /* Start either the uIP TCP/IP stack or the lwIP TCP/IP stack. */
232 #ifdef STACK_UIP
233 /* Finally, create the WEB server task. */
234 xTaskCreate( vuIP_Task, "uIP", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );
235 #endif
236
237 #ifdef STACK_LWIP
238 /* Create the lwIP task. This uses the lwIP RTOS abstraction layer.*/
239 vlwIPInit();
240 sys_set_state( ( signed portCHAR * ) "httpd", lwipBASIC_SERVER_STACK_SIZE );
241 sys_thread_new( vBasicWEBServer, ( void * ) NULL, basicwebWEBSERVER_PRIORITY );
242 sys_set_default_state();
243 #endif
Richard Barry3dcbed52006-06-22 17:18:31 +0000244
245 /* Start the scheduler.
246
247 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
248 The processor MUST be in supervisor mode when vTaskStartScheduler is
249 called. The demo applications included in the FreeRTOS.org download switch
250 to supervisor mode prior to main being called. If you are not using one of
251 these demo application projects then ensure Supervisor mode is used here. */
252
253 vTaskStartScheduler();
254
255 /* We should never get here as control is now taken by the scheduler. */
256 for( ;; );
257}
258/*-----------------------------------------------------------*/
259
260static void prvSetupHardware( void )
261{
262 /* Configuration taken from the ST code.
263
264 Set Flash banks size & address */
Richard Barry98a99592007-09-17 10:07:48 +0000265 FMI_BankRemapConfig( 4, 2, 0, 0x80000 );
Richard Barry3dcbed52006-06-22 17:18:31 +0000266
267 /* FMI Waite States */
Richard Barry98a99592007-09-17 10:07:48 +0000268 FMI_Config( FMI_READ_WAIT_STATE_2, FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE, FMI_LVD_ENABLE, FMI_FREQ_HIGH );
Richard Barry3dcbed52006-06-22 17:18:31 +0000269
Richard Barry430893f2006-08-10 20:15:54 +0000270 /* Configure the FPLL = 96MHz, and APB to 48MHz. */
271 SCU_PCLKDivisorConfig( SCU_PCLK_Div2 );
Richard Barry98a99592007-09-17 10:07:48 +0000272 SCU_PLLFactorsConfig( 192, 25, 2 );
Richard Barry3dcbed52006-06-22 17:18:31 +0000273 SCU_PLLCmd( ENABLE );
274 SCU_MCLKSourceConfig( SCU_MCLK_PLL );
275
276 WDG_Cmd( DISABLE );
277 VIC_DeInit();
278
279 /* GPIO8 clock source enable, used by the LCD. */
280 SCU_APBPeriphClockConfig(__GPIO8, ENABLE);
281 GPIO_DeInit(GPIO8);
282
283 /* GPIO 9 clock source enable, used by the LCD. */
284 SCU_APBPeriphClockConfig(__GPIO9, ENABLE);
285 GPIO_DeInit(GPIO9);
286
287 /* Enable VIC clock */
288 SCU_AHBPeriphClockConfig(__VIC, ENABLE);
289 SCU_AHBPeriphReset(__VIC, DISABLE);
290
291 /* Peripheral initialisation. */
Richard Barry3dcbed52006-06-22 17:18:31 +0000292 vParTestInitialise();
293}
294/*-----------------------------------------------------------*/
295
296static void vErrorChecks( void *pvParameters )
297{
298static portCHAR cCheckVal[ mainMAX_FLAG_STRING_LEN ];
299portCHAR *pcFlagString;
300xLCDMessage xMessageToSend;
301portTickType xLastWakeTime;
302portCHAR *pcStringsToDisplay[] = {
303 "Check status flag"
304 };
305
306 /* The parameters are not used in this task. */
307 ( void ) pvParameters;
Richard Barry98a99592007-09-17 10:07:48 +0000308
Richard Barry3dcbed52006-06-22 17:18:31 +0000309 pcFlagString = &cCheckVal[ 0 ];
310
Richard Barry3dcbed52006-06-22 17:18:31 +0000311 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
312 functions correctly. */
313 xLastWakeTime = xTaskGetTickCount();
314
315 /* Cycle for ever, delaying then checking all the other tasks are still
316 operating without error. */
Richard Barry3dcbed52006-06-22 17:18:31 +0000317 for( ;; )
318 {
319 /* Delay until it is time to execute again. */
320 vTaskDelayUntil( &xLastWakeTime, mainCHECK_PERIOD );
321
322 /* Check all the other tasks to see if the error flag needs updating. */
323 prvCheckOtherTasksAreStillRunning();
324
325 /* Create a string indicating the error flag status. */
326 sprintf( cCheckVal, "equals 0x%x ", ulErrorFlags );
327 xMessageToSend.xRow = Line2;
328
329 /* Send the first part of the message to the LCD task. */
330 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ 0 ];
331 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );
332 vTaskDelay( mainSTRING_WRITE_DELAY );
333
334 /* Send the second part of the message to the LCD task. */
335 xMessageToSend.ppcMessageToDisplay = &pcFlagString;
336 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );
337 }
338}
339/*-----------------------------------------------------------*/
340
341static void prvCheckOtherTasksAreStillRunning( void )
342{
343 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
344 {
345 ulErrorFlags |= 0x01;
346 }
347
348 if( xArePollingQueuesStillRunning() != pdTRUE )
349 {
350 ulErrorFlags |= 0x02;
351 }
352
353 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
354 {
355 ulErrorFlags |= 0x04;
356 }
357
358 if( xAreBlockingQueuesStillRunning() != pdTRUE )
359 {
360 ulErrorFlags |= 0x08;
361 }
362
363 if( xAreComTestTasksStillRunning() != pdTRUE )
364 {
365 ulErrorFlags |= 0x10;
366 }
367
368 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
369 {
370 ulErrorFlags |= 0x20;
371 }
372
373 if( xAreMathsTaskStillRunning() != pdTRUE )
374 {
375 ulErrorFlags |= 0x40;
376 }
Richard Barry98a99592007-09-17 10:07:48 +0000377
378 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
379 {
380 ulErrorFlags |= 0x80;
381 }
382
383 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
384 {
385 ulErrorFlags |= 0x100;
386 }
387
Richard Barry3dcbed52006-06-22 17:18:31 +0000388}
389/*-----------------------------------------------------------*/
390
391static void prvLCDMessageTask( void * pvParameters )
392{
393xQueueHandle *pxLCDQueue;
394xLCDMessage xMessageToSend;
395portBASE_TYPE xIndex = 0;
396
397/* The strings that are written to the LCD. */
398portCHAR *pcStringsToDisplay[] = {
399 "IAR ",
400 "STR912 ",
401 "Demo ",
402 "www.FreeRTOS.org",
403 ""
404 };
405
406
407 /* To test the parameter passing mechanism, the queue on which messages are
408 posted is passed in as a parameter even though it is available as a file
409 scope variable anyway. */
410 pxLCDQueue = ( xQueueHandle * ) pvParameters;
411
412 for( ;; )
413 {
414 /* Wait until it is time to move onto the next string. */
415 vTaskDelay( mainSTRING_WRITE_DELAY );
416
417 /* Configure the message object to send to the LCD task. */
418 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];
419 xMessageToSend.xRow = Line1;
420
421 /* Post the message to be displayed. */
422 xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 );
423
424 /* Move onto the next message, wrapping when necessary. */
425 xIndex++;
426 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )
427 {
428 xIndex = 0;
429
430 /* Delay longer before going back to the start of the messages. */
431 vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
432 }
433 }
434}
435/*-----------------------------------------------------------*/
436
437void prvLCDTask( void * pvParameters )
438{
439xQueueHandle *pxLCDQueue;
440xLCDMessage xReceivedMessage;
441portCHAR *pcString;
442
443 /* To test the parameter passing mechanism, the queue on which messages are
444 received is passed in as a parameter even though it is available as a file
445 scope variable anyway. */
446 pxLCDQueue = ( xQueueHandle * ) pvParameters;
447
Richard Barry5d413a02007-03-08 21:08:52 +0000448 LCD_Init();
449
Richard Barry98a99592007-09-17 10:07:48 +0000450 for( ;; )
Richard Barry3dcbed52006-06-22 17:18:31 +0000451 {
452 /* Wait for a message to arrive. */
453 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )
454 {
455 /* Where is the string we are going to display? */
456 pcString = *xReceivedMessage.ppcMessageToDisplay;
457 LCD_DisplayString(xReceivedMessage.xRow, pcString, BlackText);
458
459 /* The delay here is just to ensure the LCD task does not starve
Richard Barry98a99592007-09-17 10:07:48 +0000460 out lower priority tasks as writing to the LCD can take a long
Richard Barry3dcbed52006-06-22 17:18:31 +0000461 time. */
462 vTaskDelay( mainLCD_DELAY );
463 }
464 }
465}
466/*-----------------------------------------------------------*/