2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * This demo application creates eight co-routines and four tasks (five
\r
67 * including the idle task). The co-routines execute as part of the idle task
\r
68 * hook. The application is limited in size to allow its compilation using
\r
69 * the KickStart version of the IAR compiler.
\r
71 * Six of the created co-routines are the standard 'co-routine flash'
\r
72 * co-routines contained within the Demo/Common/Minimal/crflash.c file and
\r
73 * documented on the FreeRTOS.org WEB site.
\r
75 * The 'LCD Task' waits on a message queue for messages informing it what and
\r
76 * where to display text. This is the only task that accesses the LCD
\r
77 * so mutual exclusion is guaranteed.
\r
79 * The 'LCD Message Task' periodically sends strings to the LCD Task using
\r
80 * the message queue. The strings are rotated to form a short message and
\r
81 * are written to the top row of the LCD.
\r
83 * The 'ADC Co-routine' periodically reads the ADC input that is connected to
\r
84 * the light sensor, forms a short message from the value, and then sends this
\r
85 * message to the LCD Task using the same message queue. The ADC readings are
\r
86 * displayed on the bottom row of the LCD.
\r
88 * The eighth co-routine and final task control the transmission and reception
\r
89 * of a string to UART 0. The co-routine periodically sends the first
\r
90 * character of the string to the UART, with the UART's TxEnd interrupt being
\r
91 * used to transmit the remaining characters. The UART's RxEnd interrupt
\r
92 * receives the characters and places them on a queue to be processed by the
\r
93 * 'COMs Rx' task. An error is latched should an unexpected character be
\r
94 * received, or any character be received out of sequence.
\r
96 * A loopback connector is required to ensure that each character transmitted
\r
97 * on the UART is also received on the same UART. For test purposes the UART
\r
98 * FIFO's are not utalised in order to maximise the interrupt overhead. Also
\r
99 * a pseudo random interval is used between the start of each transmission in
\r
100 * order that the resultant interrupts are more randomly distributed and
\r
101 * therefore more likely to highlight any problems.
\r
103 * The flash co-routines control LED's zero to four. LED five is toggled each
\r
104 * time the string is transmitted on the UART. LED six is toggled each time
\r
105 * the string is CORRECTLY received on the UART. LED seven is latched on
\r
106 * should an error be detected in any task or co-routine.
\r
108 * In addition the idle task makes repetitive calls to
\r
109 * vSetAndCheckRegisters(). This simply loads the general purpose registers
\r
110 * with a known value, then checks each register to ensure the held value is
\r
111 * still correct. As a low priority task this checking routine is likely to
\r
112 * get repeatedly swapped in and out. A register being found to contain an
\r
113 * incorrect value is therefore indicative of an error in the task switching
\r
118 /* standard include files. */
\r
121 /* Scheduler include files. */
\r
122 #include "FreeRTOS.h"
\r
125 #include "croutine.h"
\r
127 /* Demo application include files. */
\r
128 #include "partest.h"
\r
129 #include "crflash.h"
\r
130 #include "commstest.h"
\r
132 /* Library include files. */
\r
133 #include "DriverLib.h"
\r
135 /* The time to delay between writing each character to the LCD. */
\r
136 #define mainCHAR_WRITE_DELAY ( 2 / portTICK_RATE_MS )
\r
138 /* The time to delay between writing each string to the LCD. */
\r
139 #define mainSTRING_WRITE_DELAY ( 400 / portTICK_RATE_MS )
\r
141 #define mainADC_DELAY ( 200 / portTICK_RATE_MS )
\r
143 /* The number of flash co-routines to create. */
\r
144 #define mainNUM_FLASH_CO_ROUTINES ( 5 )
\r
146 /* The length of the queue used to send messages to the LCD task. */
\r
147 #define mainLCD_QUEUE_LEN ( 3 )
\r
149 /* The priority of the co-routine used to initiate the transmission of the
\r
150 string on UART 0. */
\r
151 #define mainTX_CO_ROUTINE_PRIORITY ( 1 )
\r
152 #define mainADC_CO_ROUTINE_PRIORITY ( 2 )
\r
154 /* Only one of each co-routine is created so its index is not important. */
\r
155 #define mainTX_CO_ROUTINE_INDEX ( 0 )
\r
156 #define mainADC_CO_ROUTINE_INDEX ( 0 )
\r
158 /* The task priorities. */
\r
159 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
160 #define mainMSG_TASK_PRIORITY ( mainLCD_TASK_PRIORITY - 1 )
\r
161 #define mainCOMMS_RX_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
163 /* The LCD had two rows. */
\r
164 #define mainTOP_ROW 0
\r
165 #define mainBOTTOM_ROW 1
\r
167 /* Dimension for the buffer into which the ADC value string is written. */
\r
168 #define mainMAX_ADC_STRING_LEN 20
\r
170 /* The LED that is lit should an error be detected in any of the tasks or
\r
172 #define mainFAIL_LED ( 7 )
\r
174 /*-----------------------------------------------------------*/
\r
177 * The task that displays text on the LCD.
\r
179 static void prvLCDTask( void * pvParameters );
\r
182 * The task that sends messages to be displayed on the top row of the LCD.
\r
184 static void prvLCDMessageTask( void * pvParameters );
\r
187 * The co-routine that reads the ADC and sends messages for display on the
\r
188 * bottom row of the LCD.
\r
190 static void prvADCCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
193 * Function to simply set a known value into the general purpose registers
\r
194 * then read them back to ensure they remain set correctly. An incorrect value
\r
195 * being indicative of an error in the task switching mechanism.
\r
197 extern void vSetAndCheckRegisters( void );
\r
200 * Latch the LED that indicates that an error has occurred.
\r
202 void vSetErrorLED( void );
\r
205 * Thread safe write to the PDC.
\r
207 static void prvPDCWrite( char cAddress, char cData );
\r
210 * Sets up the hardware used by the demo.
\r
212 static void prvSetupHardware( void );
\r
215 /*-----------------------------------------------------------*/
\r
217 /* The structure that is passed on the LCD message queue. */
\r
220 char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */
\r
221 portBASE_TYPE xRow; /*<< The row on which the message should be displayed. */
\r
224 /* Error flag set to pdFAIL if an error is encountered in the tasks/co-routines
\r
225 defined within this file. */
\r
226 unsigned portBASE_TYPE uxErrorStatus = pdPASS;
\r
228 /* The queue used to transmit messages to the LCD task. */
\r
229 static xQueueHandle xLCDQueue;
\r
231 /*-----------------------------------------------------------*/
\r
234 * Setup the hardware, create the tasks/co-routines, then start the scheduler.
\r
238 /* Create the queue used by tasks wanting to write to the LCD. */
\r
239 xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );
\r
241 /* Setup the ports used by the demo and the clock. */
\r
242 prvSetupHardware();
\r
244 /* Create the co-routines that flash the LED's. */
\r
245 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
247 /* Create the co-routine that initiates the transmission of characters
\r
248 on the UART and the task that receives them, as described at the top of
\r
250 xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );
\r
251 xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );
\r
253 /* Create the task that waits for messages to display on the LCD, plus the
\r
254 task and co-routine that send messages for display (as described at the top
\r
256 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );
\r
257 xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );
\r
258 xCoRoutineCreate( prvADCCoRoutine, mainADC_CO_ROUTINE_PRIORITY, mainADC_CO_ROUTINE_INDEX );
\r
260 /* Start the scheduler running the tasks and co-routines just created. */
\r
261 vTaskStartScheduler();
\r
263 /* Should not get here unless we did not have enough memory to start the
\r
267 /*-----------------------------------------------------------*/
\r
269 static void prvLCDMessageTask( void * pvParameters )
\r
271 /* The strings that are written to the LCD. */
\r
272 char *pcStringsToDisplay[] = {
\r
276 "www.FreeRTOS.org",
\r
280 xQueueHandle *pxLCDQueue;
\r
281 xLCDMessage xMessageToSend;
\r
282 portBASE_TYPE xIndex = 0;
\r
284 /* To test the parameter passing mechanism, the queue on which messages are
\r
285 posted is passed in as a parameter even though it is available as a file
\r
286 scope variable anyway. */
\r
287 pxLCDQueue = ( xQueueHandle * ) pvParameters;
\r
291 /* Wait until it is time to move onto the next string. */
\r
292 vTaskDelay( mainSTRING_WRITE_DELAY );
\r
294 /* Create the message object to send to the LCD task. */
\r
295 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];
\r
296 xMessageToSend.xRow = mainTOP_ROW;
\r
298 /* Post the message to be displayed. */
\r
299 if( !xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 ) )
\r
301 uxErrorStatus = pdFAIL;
\r
304 /* Move onto the next message, wrapping when necessary. */
\r
306 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )
\r
310 /* Delay longer before going back to the start of the messages. */
\r
311 vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
\r
315 /*-----------------------------------------------------------*/
\r
317 void prvLCDTask( void * pvParameters )
\r
319 unsigned portBASE_TYPE uxIndex;
\r
320 xQueueHandle *pxLCDQueue;
\r
321 xLCDMessage xReceivedMessage;
\r
323 const unsigned char ucCFGData[] = {
\r
324 0x30, /* Set data bus to 8-bits. */
\r
327 0x3C, /* Number of lines/font. */
\r
328 0x08, /* Display off. */
\r
329 0x01, /* Display clear. */
\r
330 0x06, /* Entry mode [cursor dir][shift]. */
\r
331 0x0C /* Display on [display on][curson on][blinking on]. */
\r
334 /* To test the parameter passing mechanism, the queue on which messages are
\r
335 received is passed in as a parameter even though it is available as a file
\r
336 scope variable anyway. */
\r
337 pxLCDQueue = ( xQueueHandle * ) pvParameters;
\r
339 /* Configure the LCD. */
\r
341 while( uxIndex < sizeof( ucCFGData ) )
\r
343 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
\r
345 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
348 /* Turn the LCD Backlight on. */
\r
349 prvPDCWrite( PDC_CSR, 0x01 );
\r
351 /* Clear display. */
\r
352 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
353 prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR );
\r
358 /* Wait for a message to arrive. */
\r
359 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )
\r
361 /* Which row does the received message say to write to? */
\r
362 PDCLCDSetPos( 0, xReceivedMessage.xRow );
\r
364 /* Where is the string we are going to display? */
\r
365 pcString = *xReceivedMessage.ppcMessageToDisplay;
\r
369 /* Don't write out the string too quickly as LCD's are usually
\r
370 pretty slow devices. */
\r
371 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
372 prvPDCWrite( PDC_LCD_RAM, *pcString );
\r
378 /*-----------------------------------------------------------*/
\r
380 static void prvADCCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
382 static unsigned long ulADCValue;
\r
383 static char cMessageBuffer[ mainMAX_ADC_STRING_LEN ];
\r
384 static char *pcMessage;
\r
385 static xLCDMessage xMessageToSend;
\r
387 /* Co-routines MUST start with a call to crSTART(). */
\r
388 crSTART( xHandle );
\r
392 /* Start an ADC conversion. */
\r
393 ADCProcessorTrigger( ADC_BASE, 0 );
\r
395 /* Simply delay - when we unblock the result should be available */
\r
396 crDELAY( xHandle, mainADC_DELAY );
\r
398 /* Get the ADC result. */
\r
399 ADCSequenceDataGet( ADC_BASE, 0, &ulADCValue );
\r
401 /* Create a string with the result. */
\r
402 sprintf( cMessageBuffer, "ADC = %d ", ulADCValue );
\r
403 pcMessage = cMessageBuffer;
\r
405 /* Configure the message we are going to send for display. */
\r
406 xMessageToSend.ppcMessageToDisplay = ( char** ) &pcMessage;
\r
407 xMessageToSend.xRow = mainBOTTOM_ROW;
\r
409 /* Send the string to the LCD task for display. We are sending
\r
410 on a task queue so do not have the option to block. */
\r
411 if( !xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 ) )
\r
413 uxErrorStatus = pdFAIL;
\r
417 /* Co-routines MUST end with a call to crEND(). */
\r
420 /*-----------------------------------------------------------*/
\r
422 static void prvSetupHardware( void )
\r
424 /* Setup the PLL. */
\r
425 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
427 /* Initialise the hardware used to talk to the LCD, LED's and UART. */
\r
429 vParTestInitialise();
\r
432 /* The ADC is used to read the light sensor. */
\r
433 SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
\r
434 ADCSequenceConfigure( ADC_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
\r
435 ADCSequenceStepConfigure( ADC_BASE, 0, 0, ADC_CTL_CH0 | ADC_CTL_END );
\r
436 ADCSequenceEnable( ADC_BASE, 0 );
\r
439 /*-----------------------------------------------------------*/
\r
441 static void prvPDCWrite( char cAddress, char cData )
\r
445 PDCWrite( cAddress, cData );
\r
449 /*-----------------------------------------------------------*/
\r
451 void vSetErrorLED( void )
\r
453 vParTestSetLED( mainFAIL_LED, pdTRUE );
\r
455 /*-----------------------------------------------------------*/
\r
457 void vApplicationIdleHook( void )
\r
459 /* The co-routines are executed in the idle task using the idle task
\r
463 /* Schedule the co-routines. */
\r
464 vCoRoutineSchedule();
\r
466 /* Run the register check function between each co-routine. */
\r
467 vSetAndCheckRegisters();
\r
469 /* See if the comms task and co-routine has found any errors. */
\r
470 if( uxGetCommsStatus() != pdPASS )
\r
472 vParTestSetLED( mainFAIL_LED, pdTRUE );
\r
476 /*-----------------------------------------------------------*/
\r