2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * This demo application creates eight co-routines and four tasks (five
\r
69 * including the idle task). The co-routines execute as part of the idle task
\r
70 * hook. The application is limited in size to allow its compilation using
\r
71 * the KickStart version of the IAR compiler.
\r
73 * Six of the created co-routines are the standard 'co-routine flash'
\r
74 * co-routines contained within the Demo/Common/Minimal/crflash.c file and
\r
75 * documented on the FreeRTOS.org WEB site.
\r
77 * The 'LCD Task' waits on a message queue for messages informing it what and
\r
78 * where to display text. This is the only task that accesses the LCD
\r
79 * so mutual exclusion is guaranteed.
\r
81 * The 'LCD Message Task' periodically sends strings to the LCD Task using
\r
82 * the message queue. The strings are rotated to form a short message and
\r
83 * are written to the top row of the LCD.
\r
85 * The 'ADC Co-routine' periodically reads the ADC input that is connected to
\r
86 * the light sensor, forms a short message from the value, and then sends this
\r
87 * message to the LCD Task using the same message queue. The ADC readings are
\r
88 * displayed on the bottom row of the LCD.
\r
90 * The eighth co-routine and final task control the transmission and reception
\r
91 * of a string to UART 0. The co-routine periodically sends the first
\r
92 * character of the string to the UART, with the UART's TxEnd interrupt being
\r
93 * used to transmit the remaining characters. The UART's RxEnd interrupt
\r
94 * receives the characters and places them on a queue to be processed by the
\r
95 * 'COMs Rx' task. An error is latched should an unexpected character be
\r
96 * received, or any character be received out of sequence.
\r
98 * A loopback connector is required to ensure that each character transmitted
\r
99 * on the UART is also received on the same UART. For test purposes the UART
\r
100 * FIFO's are not utalised in order to maximise the interrupt overhead. Also
\r
101 * a pseudo random interval is used between the start of each transmission in
\r
102 * order that the resultant interrupts are more randomly distributed and
\r
103 * therefore more likely to highlight any problems.
\r
105 * The flash co-routines control LED's zero to four. LED five is toggled each
\r
106 * time the string is transmitted on the UART. LED six is toggled each time
\r
107 * the string is CORRECTLY received on the UART. LED seven is latched on
\r
108 * should an error be detected in any task or co-routine.
\r
110 * In addition the idle task makes repetitive calls to
\r
111 * vSetAndCheckRegisters(). This simply loads the general purpose registers
\r
112 * with a known value, then checks each register to ensure the held value is
\r
113 * still correct. As a low priority task this checking routine is likely to
\r
114 * get repeatedly swapped in and out. A register being found to contain an
\r
115 * incorrect value is therefore indicative of an error in the task switching
\r
120 /* standard include files. */
\r
123 /* Scheduler include files. */
\r
124 #include "FreeRTOS.h"
\r
127 #include "croutine.h"
\r
129 /* Demo application include files. */
\r
130 #include "partest.h"
\r
131 #include "crflash.h"
\r
132 #include "commstest.h"
\r
134 /* Library include files. */
\r
135 #include "DriverLib.h"
\r
137 /* The time to delay between writing each character to the LCD. */
\r
138 #define mainCHAR_WRITE_DELAY ( 2 / portTICK_RATE_MS )
\r
140 /* The time to delay between writing each string to the LCD. */
\r
141 #define mainSTRING_WRITE_DELAY ( 400 / portTICK_RATE_MS )
\r
143 #define mainADC_DELAY ( 200 / portTICK_RATE_MS )
\r
145 /* The number of flash co-routines to create. */
\r
146 #define mainNUM_FLASH_CO_ROUTINES ( 5 )
\r
148 /* The length of the queue used to send messages to the LCD task. */
\r
149 #define mainLCD_QUEUE_LEN ( 3 )
\r
151 /* The priority of the co-routine used to initiate the transmission of the
\r
152 string on UART 0. */
\r
153 #define mainTX_CO_ROUTINE_PRIORITY ( 1 )
\r
154 #define mainADC_CO_ROUTINE_PRIORITY ( 2 )
\r
156 /* Only one of each co-routine is created so its index is not important. */
\r
157 #define mainTX_CO_ROUTINE_INDEX ( 0 )
\r
158 #define mainADC_CO_ROUTINE_INDEX ( 0 )
\r
160 /* The task priorities. */
\r
161 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
162 #define mainMSG_TASK_PRIORITY ( mainLCD_TASK_PRIORITY - 1 )
\r
163 #define mainCOMMS_RX_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
165 /* The LCD had two rows. */
\r
166 #define mainTOP_ROW 0
\r
167 #define mainBOTTOM_ROW 1
\r
169 /* Dimension for the buffer into which the ADC value string is written. */
\r
170 #define mainMAX_ADC_STRING_LEN 20
\r
172 /* The LED that is lit should an error be detected in any of the tasks or
\r
174 #define mainFAIL_LED ( 7 )
\r
176 /*-----------------------------------------------------------*/
\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
189 * The co-routine that reads the ADC and sends messages for display on the
\r
190 * bottom row of the LCD.
\r
192 static void prvADCCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
195 * Function to simply set a known value into the general purpose registers
\r
196 * then read them back to ensure they remain set correctly. An incorrect value
\r
197 * being indicative of an error in the task switching mechanism.
\r
199 extern void vSetAndCheckRegisters( void );
\r
202 * Latch the LED that indicates that an error has occurred.
\r
204 void vSetErrorLED( void );
\r
207 * Thread safe write to the PDC.
\r
209 static void prvPDCWrite( char cAddress, char cData );
\r
212 * Sets up the hardware used by the demo.
\r
214 static void prvSetupHardware( void );
\r
217 /*-----------------------------------------------------------*/
\r
219 /* The structure that is passed on the LCD message queue. */
\r
222 char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */
\r
223 portBASE_TYPE xRow; /*<< The row on which the message should be displayed. */
\r
226 /* Error flag set to pdFAIL if an error is encountered in the tasks/co-routines
\r
227 defined within this file. */
\r
228 unsigned portBASE_TYPE uxErrorStatus = pdPASS;
\r
230 /* The queue used to transmit messages to the LCD task. */
\r
231 static xQueueHandle xLCDQueue;
\r
233 /*-----------------------------------------------------------*/
\r
236 * Setup the hardware, create the tasks/co-routines, then start the scheduler.
\r
240 /* Create the queue used by tasks wanting to write to the LCD. */
\r
241 xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );
\r
243 /* Setup the ports used by the demo and the clock. */
\r
244 prvSetupHardware();
\r
246 /* Create the co-routines that flash the LED's. */
\r
247 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
249 /* Create the co-routine that initiates the transmission of characters
\r
250 on the UART and the task that receives them, as described at the top of
\r
252 xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );
\r
253 xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );
\r
255 /* Create the task that waits for messages to display on the LCD, plus the
\r
256 task and co-routine that send messages for display (as described at the top
\r
258 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );
\r
259 xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );
\r
260 xCoRoutineCreate( prvADCCoRoutine, mainADC_CO_ROUTINE_PRIORITY, mainADC_CO_ROUTINE_INDEX );
\r
262 /* Start the scheduler running the tasks and co-routines just created. */
\r
263 vTaskStartScheduler();
\r
265 /* Should not get here unless we did not have enough memory to start the
\r
269 /*-----------------------------------------------------------*/
\r
271 static void prvLCDMessageTask( void * pvParameters )
\r
273 /* The strings that are written to the LCD. */
\r
274 char *pcStringsToDisplay[] = {
\r
278 "www.FreeRTOS.org",
\r
282 xQueueHandle *pxLCDQueue;
\r
283 xLCDMessage xMessageToSend;
\r
284 portBASE_TYPE xIndex = 0;
\r
286 /* To test the parameter passing mechanism, the queue on which messages are
\r
287 posted is passed in as a parameter even though it is available as a file
\r
288 scope variable anyway. */
\r
289 pxLCDQueue = ( xQueueHandle * ) pvParameters;
\r
293 /* Wait until it is time to move onto the next string. */
\r
294 vTaskDelay( mainSTRING_WRITE_DELAY );
\r
296 /* Create the message object to send to the LCD task. */
\r
297 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];
\r
298 xMessageToSend.xRow = mainTOP_ROW;
\r
300 /* Post the message to be displayed. */
\r
301 if( !xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 ) )
\r
303 uxErrorStatus = pdFAIL;
\r
306 /* Move onto the next message, wrapping when necessary. */
\r
308 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )
\r
312 /* Delay longer before going back to the start of the messages. */
\r
313 vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
\r
317 /*-----------------------------------------------------------*/
\r
319 void prvLCDTask( void * pvParameters )
\r
321 unsigned portBASE_TYPE uxIndex;
\r
322 xQueueHandle *pxLCDQueue;
\r
323 xLCDMessage xReceivedMessage;
\r
325 const unsigned char ucCFGData[] = {
\r
326 0x30, /* Set data bus to 8-bits. */
\r
329 0x3C, /* Number of lines/font. */
\r
330 0x08, /* Display off. */
\r
331 0x01, /* Display clear. */
\r
332 0x06, /* Entry mode [cursor dir][shift]. */
\r
333 0x0C /* Display on [display on][curson on][blinking on]. */
\r
336 /* To test the parameter passing mechanism, the queue on which messages are
\r
337 received is passed in as a parameter even though it is available as a file
\r
338 scope variable anyway. */
\r
339 pxLCDQueue = ( xQueueHandle * ) pvParameters;
\r
341 /* Configure the LCD. */
\r
343 while( uxIndex < sizeof( ucCFGData ) )
\r
345 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
\r
347 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
350 /* Turn the LCD Backlight on. */
\r
351 prvPDCWrite( PDC_CSR, 0x01 );
\r
353 /* Clear display. */
\r
354 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
355 prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR );
\r
360 /* Wait for a message to arrive. */
\r
361 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )
\r
363 /* Which row does the received message say to write to? */
\r
364 PDCLCDSetPos( 0, xReceivedMessage.xRow );
\r
366 /* Where is the string we are going to display? */
\r
367 pcString = *xReceivedMessage.ppcMessageToDisplay;
\r
371 /* Don't write out the string too quickly as LCD's are usually
\r
372 pretty slow devices. */
\r
373 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
374 prvPDCWrite( PDC_LCD_RAM, *pcString );
\r
380 /*-----------------------------------------------------------*/
\r
382 static void prvADCCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
384 static unsigned long ulADCValue;
\r
385 static char cMessageBuffer[ mainMAX_ADC_STRING_LEN ];
\r
386 static char *pcMessage;
\r
387 static xLCDMessage xMessageToSend;
\r
389 /* Co-routines MUST start with a call to crSTART(). */
\r
390 crSTART( xHandle );
\r
394 /* Start an ADC conversion. */
\r
395 ADCProcessorTrigger( ADC_BASE, 0 );
\r
397 /* Simply delay - when we unblock the result should be available */
\r
398 crDELAY( xHandle, mainADC_DELAY );
\r
400 /* Get the ADC result. */
\r
401 ADCSequenceDataGet( ADC_BASE, 0, &ulADCValue );
\r
403 /* Create a string with the result. */
\r
404 sprintf( cMessageBuffer, "ADC = %d ", ulADCValue );
\r
405 pcMessage = cMessageBuffer;
\r
407 /* Configure the message we are going to send for display. */
\r
408 xMessageToSend.ppcMessageToDisplay = ( char** ) &pcMessage;
\r
409 xMessageToSend.xRow = mainBOTTOM_ROW;
\r
411 /* Send the string to the LCD task for display. We are sending
\r
412 on a task queue so do not have the option to block. */
\r
413 if( !xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 ) )
\r
415 uxErrorStatus = pdFAIL;
\r
419 /* Co-routines MUST end with a call to crEND(). */
\r
422 /*-----------------------------------------------------------*/
\r
424 static void prvSetupHardware( void )
\r
426 /* Setup the PLL. */
\r
427 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
429 /* Initialise the hardware used to talk to the LCD, LED's and UART. */
\r
431 vParTestInitialise();
\r
434 /* The ADC is used to read the light sensor. */
\r
435 SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
\r
436 ADCSequenceConfigure( ADC_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
\r
437 ADCSequenceStepConfigure( ADC_BASE, 0, 0, ADC_CTL_CH0 | ADC_CTL_END );
\r
438 ADCSequenceEnable( ADC_BASE, 0 );
\r
441 /*-----------------------------------------------------------*/
\r
443 static void prvPDCWrite( char cAddress, char cData )
\r
447 PDCWrite( cAddress, cData );
\r
451 /*-----------------------------------------------------------*/
\r
453 void vSetErrorLED( void )
\r
455 vParTestSetLED( mainFAIL_LED, pdTRUE );
\r
457 /*-----------------------------------------------------------*/
\r
459 void vApplicationIdleHook( void )
\r
461 /* The co-routines are executed in the idle task using the idle task
\r
465 /* Schedule the co-routines. */
\r
466 vCoRoutineSchedule();
\r
468 /* Run the register check function between each co-routine. */
\r
469 vSetAndCheckRegisters();
\r
471 /* See if the comms task and co-routine has found any errors. */
\r
472 if( uxGetCommsStatus() != pdPASS )
\r
474 vParTestSetLED( mainFAIL_LED, pdTRUE );
\r
478 /*-----------------------------------------------------------*/
\r