2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * This demo application creates eight co-routines and four tasks (five
\r
98 * including the idle task). The co-routines execute as part of the idle task
\r
99 * hook. The application is limited in size to allow its compilation using
\r
100 * the KickStart version of the IAR compiler.
\r
102 * Six of the created co-routines are the standard 'co-routine flash'
\r
103 * co-routines contained within the Demo/Common/Minimal/crflash.c file and
\r
104 * documented on the FreeRTOS.org WEB site.
\r
106 * The 'LCD Task' waits on a message queue for messages informing it what and
\r
107 * where to display text. This is the only task that accesses the LCD
\r
108 * so mutual exclusion is guaranteed.
\r
110 * The 'LCD Message Task' periodically sends strings to the LCD Task using
\r
111 * the message queue. The strings are rotated to form a short message and
\r
112 * are written to the top row of the LCD.
\r
114 * The 'ADC Co-routine' periodically reads the ADC input that is connected to
\r
115 * the light sensor, forms a short message from the value, and then sends this
\r
116 * message to the LCD Task using the same message queue. The ADC readings are
\r
117 * displayed on the bottom row of the LCD.
\r
119 * The eighth co-routine and final task control the transmission and reception
\r
120 * of a string to UART 0. The co-routine periodically sends the first
\r
121 * character of the string to the UART, with the UART's TxEnd interrupt being
\r
122 * used to transmit the remaining characters. The UART's RxEnd interrupt
\r
123 * receives the characters and places them on a queue to be processed by the
\r
124 * 'COMs Rx' task. An error is latched should an unexpected character be
\r
125 * received, or any character be received out of sequence.
\r
127 * A loopback connector is required to ensure that each character transmitted
\r
128 * on the UART is also received on the same UART. For test purposes the UART
\r
129 * FIFO's are not utalised in order to maximise the interrupt overhead. Also
\r
130 * a pseudo random interval is used between the start of each transmission in
\r
131 * order that the resultant interrupts are more randomly distributed and
\r
132 * therefore more likely to highlight any problems.
\r
134 * The flash co-routines control LED's zero to four. LED five is toggled each
\r
135 * time the string is transmitted on the UART. LED six is toggled each time
\r
136 * the string is CORRECTLY received on the UART. LED seven is latched on
\r
137 * should an error be detected in any task or co-routine.
\r
139 * In addition the idle task makes repetitive calls to
\r
140 * vSetAndCheckRegisters(). This simply loads the general purpose registers
\r
141 * with a known value, then checks each register to ensure the held value is
\r
142 * still correct. As a low priority task this checking routine is likely to
\r
143 * get repeatedly swapped in and out. A register being found to contain an
\r
144 * incorrect value is therefore indicative of an error in the task switching
\r
149 /* standard include files. */
\r
152 /* Scheduler include files. */
\r
153 #include "FreeRTOS.h"
\r
156 #include "croutine.h"
\r
158 /* Demo application include files. */
\r
159 #include "partest.h"
\r
160 #include "crflash.h"
\r
161 #include "commstest.h"
\r
163 /* Library include files. */
\r
164 #include "DriverLib.h"
\r
166 /* The time to delay between writing each character to the LCD. */
\r
167 #define mainCHAR_WRITE_DELAY ( 2 / portTICK_PERIOD_MS )
\r
169 /* The time to delay between writing each string to the LCD. */
\r
170 #define mainSTRING_WRITE_DELAY ( 400 / portTICK_PERIOD_MS )
\r
172 #define mainADC_DELAY ( 200 / portTICK_PERIOD_MS )
\r
174 /* The number of flash co-routines to create. */
\r
175 #define mainNUM_FLASH_CO_ROUTINES ( 5 )
\r
177 /* The length of the queue used to send messages to the LCD task. */
\r
178 #define mainLCD_QUEUE_LEN ( 3 )
\r
180 /* The priority of the co-routine used to initiate the transmission of the
\r
181 string on UART 0. */
\r
182 #define mainTX_CO_ROUTINE_PRIORITY ( 1 )
\r
183 #define mainADC_CO_ROUTINE_PRIORITY ( 2 )
\r
185 /* Only one of each co-routine is created so its index is not important. */
\r
186 #define mainTX_CO_ROUTINE_INDEX ( 0 )
\r
187 #define mainADC_CO_ROUTINE_INDEX ( 0 )
\r
189 /* The task priorities. */
\r
190 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
191 #define mainMSG_TASK_PRIORITY ( mainLCD_TASK_PRIORITY - 1 )
\r
192 #define mainCOMMS_RX_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
194 /* The LCD had two rows. */
\r
195 #define mainTOP_ROW 0
\r
196 #define mainBOTTOM_ROW 1
\r
198 /* Dimension for the buffer into which the ADC value string is written. */
\r
199 #define mainMAX_ADC_STRING_LEN 20
\r
201 /* The LED that is lit should an error be detected in any of the tasks or
\r
203 #define mainFAIL_LED ( 7 )
\r
205 /*-----------------------------------------------------------*/
\r
208 * The task that displays text on the LCD.
\r
210 static void prvLCDTask( void * pvParameters );
\r
213 * The task that sends messages to be displayed on the top row of the LCD.
\r
215 static void prvLCDMessageTask( void * pvParameters );
\r
218 * The co-routine that reads the ADC and sends messages for display on the
\r
219 * bottom row of the LCD.
\r
221 static void prvADCCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
224 * Function to simply set a known value into the general purpose registers
\r
225 * then read them back to ensure they remain set correctly. An incorrect value
\r
226 * being indicative of an error in the task switching mechanism.
\r
228 extern void vSetAndCheckRegisters( void );
\r
231 * Latch the LED that indicates that an error has occurred.
\r
233 void vSetErrorLED( void );
\r
236 * Thread safe write to the PDC.
\r
238 static void prvPDCWrite( char cAddress, char cData );
\r
241 * Sets up the hardware used by the demo.
\r
243 static void prvSetupHardware( void );
\r
246 /*-----------------------------------------------------------*/
\r
248 /* The structure that is passed on the LCD message queue. */
\r
251 char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */
\r
252 portBASE_TYPE xRow; /*<< The row on which the message should be displayed. */
\r
255 /* Error flag set to pdFAIL if an error is encountered in the tasks/co-routines
\r
256 defined within this file. */
\r
257 unsigned portBASE_TYPE uxErrorStatus = pdPASS;
\r
259 /* The queue used to transmit messages to the LCD task. */
\r
260 static QueueHandle_t xLCDQueue;
\r
262 /*-----------------------------------------------------------*/
\r
265 * Setup the hardware, create the tasks/co-routines, then start the scheduler.
\r
269 /* Create the queue used by tasks wanting to write to the LCD. */
\r
270 xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );
\r
272 /* Setup the ports used by the demo and the clock. */
\r
273 prvSetupHardware();
\r
275 /* Create the co-routines that flash the LED's. */
\r
276 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
278 /* Create the co-routine that initiates the transmission of characters
\r
279 on the UART and the task that receives them, as described at the top of
\r
281 xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );
\r
282 xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );
\r
284 /* Create the task that waits for messages to display on the LCD, plus the
\r
285 task and co-routine that send messages for display (as described at the top
\r
287 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );
\r
288 xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );
\r
289 xCoRoutineCreate( prvADCCoRoutine, mainADC_CO_ROUTINE_PRIORITY, mainADC_CO_ROUTINE_INDEX );
\r
291 /* Start the scheduler running the tasks and co-routines just created. */
\r
292 vTaskStartScheduler();
\r
294 /* Should not get here unless we did not have enough memory to start the
\r
298 /*-----------------------------------------------------------*/
\r
300 static void prvLCDMessageTask( void * pvParameters )
\r
302 /* The strings that are written to the LCD. */
\r
303 char *pcStringsToDisplay[] = {
\r
307 "www.FreeRTOS.org",
\r
311 QueueHandle_t *pxLCDQueue;
\r
312 xLCDMessage xMessageToSend;
\r
313 portBASE_TYPE xIndex = 0;
\r
315 /* To test the parameter passing mechanism, the queue on which messages are
\r
316 posted is passed in as a parameter even though it is available as a file
\r
317 scope variable anyway. */
\r
318 pxLCDQueue = ( QueueHandle_t * ) pvParameters;
\r
322 /* Wait until it is time to move onto the next string. */
\r
323 vTaskDelay( mainSTRING_WRITE_DELAY );
\r
325 /* Create the message object to send to the LCD task. */
\r
326 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];
\r
327 xMessageToSend.xRow = mainTOP_ROW;
\r
329 /* Post the message to be displayed. */
\r
330 if( !xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 ) )
\r
332 uxErrorStatus = pdFAIL;
\r
335 /* Move onto the next message, wrapping when necessary. */
\r
337 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )
\r
341 /* Delay longer before going back to the start of the messages. */
\r
342 vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
\r
346 /*-----------------------------------------------------------*/
\r
348 void prvLCDTask( void * pvParameters )
\r
350 unsigned portBASE_TYPE uxIndex;
\r
351 QueueHandle_t *pxLCDQueue;
\r
352 xLCDMessage xReceivedMessage;
\r
354 const unsigned char ucCFGData[] = {
\r
355 0x30, /* Set data bus to 8-bits. */
\r
358 0x3C, /* Number of lines/font. */
\r
359 0x08, /* Display off. */
\r
360 0x01, /* Display clear. */
\r
361 0x06, /* Entry mode [cursor dir][shift]. */
\r
362 0x0C /* Display on [display on][curson on][blinking on]. */
\r
365 /* To test the parameter passing mechanism, the queue on which messages are
\r
366 received is passed in as a parameter even though it is available as a file
\r
367 scope variable anyway. */
\r
368 pxLCDQueue = ( QueueHandle_t * ) pvParameters;
\r
370 /* Configure the LCD. */
\r
372 while( uxIndex < sizeof( ucCFGData ) )
\r
374 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
\r
376 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
379 /* Turn the LCD Backlight on. */
\r
380 prvPDCWrite( PDC_CSR, 0x01 );
\r
382 /* Clear display. */
\r
383 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
384 prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR );
\r
389 /* Wait for a message to arrive. */
\r
390 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )
\r
392 /* Which row does the received message say to write to? */
\r
393 PDCLCDSetPos( 0, xReceivedMessage.xRow );
\r
395 /* Where is the string we are going to display? */
\r
396 pcString = *xReceivedMessage.ppcMessageToDisplay;
\r
400 /* Don't write out the string too quickly as LCD's are usually
\r
401 pretty slow devices. */
\r
402 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
403 prvPDCWrite( PDC_LCD_RAM, *pcString );
\r
409 /*-----------------------------------------------------------*/
\r
411 static void prvADCCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
413 static unsigned long ulADCValue;
\r
414 static char cMessageBuffer[ mainMAX_ADC_STRING_LEN ];
\r
415 static char *pcMessage;
\r
416 static xLCDMessage xMessageToSend;
\r
418 /* Co-routines MUST start with a call to crSTART(). */
\r
419 crSTART( xHandle );
\r
423 /* Start an ADC conversion. */
\r
424 ADCProcessorTrigger( ADC_BASE, 0 );
\r
426 /* Simply delay - when we unblock the result should be available */
\r
427 crDELAY( xHandle, mainADC_DELAY );
\r
429 /* Get the ADC result. */
\r
430 ADCSequenceDataGet( ADC_BASE, 0, &ulADCValue );
\r
432 /* Create a string with the result. */
\r
433 sprintf( cMessageBuffer, "ADC = %d ", ulADCValue );
\r
434 pcMessage = cMessageBuffer;
\r
436 /* Configure the message we are going to send for display. */
\r
437 xMessageToSend.ppcMessageToDisplay = ( char** ) &pcMessage;
\r
438 xMessageToSend.xRow = mainBOTTOM_ROW;
\r
440 /* Send the string to the LCD task for display. We are sending
\r
441 on a task queue so do not have the option to block. */
\r
442 if( !xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 ) )
\r
444 uxErrorStatus = pdFAIL;
\r
448 /* Co-routines MUST end with a call to crEND(). */
\r
451 /*-----------------------------------------------------------*/
\r
453 static void prvSetupHardware( void )
\r
455 /* Setup the PLL. */
\r
456 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
458 /* Initialise the hardware used to talk to the LCD, LED's and UART. */
\r
460 vParTestInitialise();
\r
463 /* The ADC is used to read the light sensor. */
\r
464 SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
\r
465 ADCSequenceConfigure( ADC_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
\r
466 ADCSequenceStepConfigure( ADC_BASE, 0, 0, ADC_CTL_CH0 | ADC_CTL_END );
\r
467 ADCSequenceEnable( ADC_BASE, 0 );
\r
470 /*-----------------------------------------------------------*/
\r
472 static void prvPDCWrite( char cAddress, char cData )
\r
476 PDCWrite( cAddress, cData );
\r
480 /*-----------------------------------------------------------*/
\r
482 void vSetErrorLED( void )
\r
484 vParTestSetLED( mainFAIL_LED, pdTRUE );
\r
486 /*-----------------------------------------------------------*/
\r
488 void vApplicationIdleHook( void )
\r
490 /* The co-routines are executed in the idle task using the idle task
\r
494 /* Schedule the co-routines. */
\r
495 vCoRoutineSchedule();
\r
497 /* Run the register check function between each co-routine. */
\r
498 vSetAndCheckRegisters();
\r
500 /* See if the comms task and co-routine has found any errors. */
\r
501 if( uxGetCommsStatus() != pdPASS )
\r
503 vParTestSetLED( mainFAIL_LED, pdTRUE );
\r
507 /*-----------------------------------------------------------*/
\r