2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
51 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
52 called. The demo applications included in the FreeRTOS.org download switch
\r
53 to supervisor mode prior to main being called. If you are not using one of
\r
54 these demo application projects then ensure Supervisor mode is used.
\r
58 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
59 * documentation provides more details of the demo application tasks.
\r
61 * A few tasks are created that are not part of the standard demo. These are
\r
62 * the 'LCD' task, the 'LCD Message' task, a WEB server task and the 'Check'
\r
65 * The LCD task is the only task that accesses the LCD directly, so mutual
\r
66 * exclusion is ensured. Any task wishing to display text sends the LCD task
\r
67 * a message containing a pointer to the string that should be displayed.
\r
68 * The LCD task itself just blocks on a queue waiting for such a message to
\r
69 * arrive - processing each in turn.
\r
71 * The LCD Message task does nothing other than periodically send messages to
\r
72 * the LCD task. The messages originating from the LCD Message task are
\r
73 * displayed on the top row of the LCD.
\r
75 * The Check task only executes every three seconds but has the highest
\r
76 * priority so is guaranteed to get processor time. Its main function is to
\r
77 * check that all the other tasks are still operational. Most tasks maintain
\r
78 * a unique count that is incremented each time the task successfully completes
\r
79 * a cycle of its function. Should any error occur within such a task the
\r
80 * count is permanently halted. The check task sets a bit in an error status
\r
81 * flag should it find any counter variable at a value that indicates an
\r
82 * error has occurred. The error flag value is converted to a string and sent
\r
83 * to the LCD task for display on the bottom row on the LCD.
\r
86 /* Standard includes. */
\r
89 /* Library includes. */
\r
90 #include "91x_lib.h"
\r
92 /* Scheduler includes. */
\r
93 #include "FreeRTOS.h"
\r
97 /* Demo application includes. */
\r
100 #include "integer.h"
\r
102 #include "BlockQ.h"
\r
103 #include "semtest.h"
\r
104 #include "dynamic.h"
\r
105 #include "partest.h"
\r
107 #include "comtest2.h"
\r
108 #include "serial.h"
\r
109 #include "GenQTest.h"
\r
113 #include "BasicWEB.h"
\r
117 /* Priorities for the demo application tasks. */
\r
118 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
119 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
120 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
121 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
122 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
123 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
124 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
125 #define mainMSG_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
126 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
128 /* Delays used by the various tasks defined in this file. */
\r
129 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
130 #define mainSTRING_WRITE_DELAY ( 500 / portTICK_RATE_MS )
\r
131 #define mainLCD_DELAY ( 20 / portTICK_RATE_MS )
\r
133 /* Constants for the ComTest tasks. */
\r
134 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
135 #define mainCOM_TEST_LED ( 3 )
\r
137 /* The maximum number of messages that can be pending to be written to the LCD. */
\r
138 #define mainLCD_QUEUE_LEN ( 6 )
\r
140 /* Dimension the buffer used to write the error flag string. */
\r
141 #define mainMAX_FLAG_STRING_LEN ( 32 )
\r
143 /* The structure that is passed on the LCD message queue. */
\r
146 char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */
\r
147 portBASE_TYPE xRow; /*<< The row on which the message should be displayed. */
\r
149 /*-----------------------------------------------------------*/
\r
152 * The task that executes at the highest priority and calls
\r
153 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
156 static void vErrorChecks( void *pvParameters );
\r
159 * Configure the processor clock and ports.
\r
161 static void prvSetupHardware( void );
\r
164 * Checks that all the demo application tasks are still executing without error
\r
165 * - as described at the top of the file. Called by vErrorChecks().
\r
167 static void prvCheckOtherTasksAreStillRunning( void );
\r
171 * The WEB server task prototype. The task is created in this file but defined
\r
172 * elsewhere. STACK_UIP is defined when the uIP stack is used in preference
\r
173 * to the lwIP stack.
\r
175 extern void vuIP_Task(void *pvParameters);
\r
179 * The task that displays text on the LCD.
\r
181 static void prvLCDTask( void * pvParameters );
\r
184 * The task that sends messages to be displayed on the top row of the LCD.
\r
186 static void prvLCDMessageTask( void * pvParameters );
\r
188 /*-----------------------------------------------------------*/
\r
190 /* The queue used to pass messages to the LCD task. */
\r
191 static xQueueHandle xLCDQueue;
\r
193 /* Error status flag. */
\r
194 static unsigned long ulErrorFlags = 0;
\r
196 /*-----------------------------------------------------------*/
\r
199 * Starts all the other tasks, then starts the scheduler.
\r
207 /* Setup any hardware that has not already been configured by the low
\r
208 level init routines. */
\r
209 prvSetupHardware();
\r
210 /* Create the queue used to send data to the LCD task. */
\r
211 xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );
\r
213 /* Start all the standard demo application tasks. */
\r
214 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
215 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
216 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
217 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
218 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
219 vStartDynamicPriorityTasks();
\r
220 vStartMathTasks( tskIDLE_PRIORITY );
\r
221 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
222 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
223 vStartQueuePeekTasks();
\r
225 /* Start the tasks which are defined in this file. */
\r
226 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
227 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );
\r
228 xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );
\r
230 /* Start either the uIP TCP/IP stack or the lwIP TCP/IP stack. */
\r
232 /* Finally, create the WEB server task. */
\r
233 xTaskCreate( vuIP_Task, "uIP", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );
\r
237 /* Create the lwIP task. This uses the lwIP RTOS abstraction layer.*/
\r
239 sys_set_state( ( signed char * ) "httpd", lwipBASIC_SERVER_STACK_SIZE );
\r
240 sys_thread_new( vBasicWEBServer, ( void * ) NULL, basicwebWEBSERVER_PRIORITY );
\r
241 sys_set_default_state();
\r
244 /* Start the scheduler.
\r
246 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
247 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
248 called. The demo applications included in the FreeRTOS.org download switch
\r
249 to supervisor mode prior to main being called. If you are not using one of
\r
250 these demo application projects then ensure Supervisor mode is used here. */
\r
252 vTaskStartScheduler();
\r
254 /* We should never get here as control is now taken by the scheduler. */
\r
257 /*-----------------------------------------------------------*/
\r
259 static void prvSetupHardware( void )
\r
261 /* Configuration taken from the ST code.
\r
263 Set Flash banks size & address */
\r
264 FMI_BankRemapConfig( 4, 2, 0, 0x80000 );
\r
266 /* FMI Waite States */
\r
267 FMI_Config( FMI_READ_WAIT_STATE_2, FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE, FMI_LVD_ENABLE, FMI_FREQ_HIGH );
\r
269 /* Configure the FPLL = 96MHz, and APB to 48MHz. */
\r
270 SCU_PCLKDivisorConfig( SCU_PCLK_Div2 );
\r
271 SCU_PLLFactorsConfig( 192, 25, 2 );
\r
272 SCU_PLLCmd( ENABLE );
\r
273 SCU_MCLKSourceConfig( SCU_MCLK_PLL );
\r
275 WDG_Cmd( DISABLE );
\r
278 /* GPIO8 clock source enable, used by the LCD. */
\r
279 SCU_APBPeriphClockConfig(__GPIO8, ENABLE);
\r
280 GPIO_DeInit(GPIO8);
\r
282 /* GPIO 9 clock source enable, used by the LCD. */
\r
283 SCU_APBPeriphClockConfig(__GPIO9, ENABLE);
\r
284 GPIO_DeInit(GPIO9);
\r
286 /* Enable VIC clock */
\r
287 SCU_AHBPeriphClockConfig(__VIC, ENABLE);
\r
288 SCU_AHBPeriphReset(__VIC, DISABLE);
\r
290 /* Peripheral initialisation. */
\r
291 vParTestInitialise();
\r
293 /*-----------------------------------------------------------*/
\r
295 static void vErrorChecks( void *pvParameters )
\r
297 static char cCheckVal[ mainMAX_FLAG_STRING_LEN ];
\r
298 char *pcFlagString;
\r
299 xLCDMessage xMessageToSend;
\r
300 portTickType xLastWakeTime;
\r
301 char *pcStringsToDisplay[] = {
\r
302 "Check status flag"
\r
305 /* The parameters are not used in this task. */
\r
306 ( void ) pvParameters;
\r
308 pcFlagString = &cCheckVal[ 0 ];
\r
310 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
311 functions correctly. */
\r
312 xLastWakeTime = xTaskGetTickCount();
\r
314 /* Cycle for ever, delaying then checking all the other tasks are still
\r
315 operating without error. */
\r
318 /* Delay until it is time to execute again. */
\r
319 vTaskDelayUntil( &xLastWakeTime, mainCHECK_PERIOD );
\r
321 /* Check all the other tasks to see if the error flag needs updating. */
\r
322 prvCheckOtherTasksAreStillRunning();
\r
324 /* Create a string indicating the error flag status. */
\r
325 sprintf( cCheckVal, "equals 0x%x ", ulErrorFlags );
\r
326 xMessageToSend.xRow = Line2;
\r
328 /* Send the first part of the message to the LCD task. */
\r
329 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ 0 ];
\r
330 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );
\r
331 vTaskDelay( mainSTRING_WRITE_DELAY );
\r
333 /* Send the second part of the message to the LCD task. */
\r
334 xMessageToSend.ppcMessageToDisplay = &pcFlagString;
\r
335 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );
\r
338 /*-----------------------------------------------------------*/
\r
340 static void prvCheckOtherTasksAreStillRunning( void )
\r
342 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
344 ulErrorFlags |= 0x01;
\r
347 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
349 ulErrorFlags |= 0x02;
\r
352 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
354 ulErrorFlags |= 0x04;
\r
357 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
359 ulErrorFlags |= 0x08;
\r
362 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
364 ulErrorFlags |= 0x10;
\r
367 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
369 ulErrorFlags |= 0x20;
\r
372 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
374 ulErrorFlags |= 0x40;
\r
377 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
379 ulErrorFlags |= 0x80;
\r
382 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
384 ulErrorFlags |= 0x100;
\r
388 /*-----------------------------------------------------------*/
\r
390 static void prvLCDMessageTask( void * pvParameters )
\r
392 xQueueHandle *pxLCDQueue;
\r
393 xLCDMessage xMessageToSend;
\r
394 portBASE_TYPE xIndex = 0;
\r
396 /* The strings that are written to the LCD. */
\r
397 char *pcStringsToDisplay[] = {
\r
401 "www.FreeRTOS.org",
\r
406 /* To test the parameter passing mechanism, the queue on which messages are
\r
407 posted is passed in as a parameter even though it is available as a file
\r
408 scope variable anyway. */
\r
409 pxLCDQueue = ( xQueueHandle * ) pvParameters;
\r
413 /* Wait until it is time to move onto the next string. */
\r
414 vTaskDelay( mainSTRING_WRITE_DELAY );
\r
416 /* Configure the message object to send to the LCD task. */
\r
417 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];
\r
418 xMessageToSend.xRow = Line1;
\r
420 /* Post the message to be displayed. */
\r
421 xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 );
\r
423 /* Move onto the next message, wrapping when necessary. */
\r
425 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )
\r
429 /* Delay longer before going back to the start of the messages. */
\r
430 vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
\r
434 /*-----------------------------------------------------------*/
\r
436 void prvLCDTask( void * pvParameters )
\r
438 xQueueHandle *pxLCDQueue;
\r
439 xLCDMessage xReceivedMessage;
\r
442 /* To test the parameter passing mechanism, the queue on which messages are
\r
443 received is passed in as a parameter even though it is available as a file
\r
444 scope variable anyway. */
\r
445 pxLCDQueue = ( xQueueHandle * ) pvParameters;
\r
451 /* Wait for a message to arrive. */
\r
452 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )
\r
454 /* Where is the string we are going to display? */
\r
455 pcString = *xReceivedMessage.ppcMessageToDisplay;
\r
456 LCD_DisplayString(xReceivedMessage.xRow, pcString, BlackText);
\r
458 /* The delay here is just to ensure the LCD task does not starve
\r
459 out lower priority tasks as writing to the LCD can take a long
\r
461 vTaskDelay( mainLCD_DELAY );
\r
465 /*-----------------------------------------------------------*/
\r