2 FreeRTOS V9.0.0 - Copyright (C) 2016 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 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * This demo application creates eight co-routines and four tasks (five
\r
72 * including the idle task). The co-routines execute as part of the idle task
\r
73 * hook. The application is limited in size to allow its compilation using
\r
74 * the KickStart version of the IAR compiler.
\r
76 * Six of the created co-routines are the standard 'co-routine flash'
\r
77 * co-routines contained within the Demo/Common/Minimal/crflash.c file and
\r
78 * documented on the FreeRTOS.org WEB site.
\r
80 * The 'LCD Task' waits on a message queue for messages informing it what and
\r
81 * where to display text. This is the only task that accesses the LCD
\r
82 * so mutual exclusion is guaranteed.
\r
84 * The 'LCD Message Task' periodically sends strings to the LCD Task using
\r
85 * the message queue. The strings are rotated to form a short message and
\r
86 * are written to the top row of the LCD.
\r
88 * The 'ADC Co-routine' periodically reads the ADC input that is connected to
\r
89 * the light sensor, forms a short message from the value, and then sends this
\r
90 * message to the LCD Task using the same message queue. The ADC readings are
\r
91 * displayed on the bottom row of the LCD.
\r
93 * The eighth co-routine and final task control the transmission and reception
\r
94 * of a string to UART 0. The co-routine periodically sends the first
\r
95 * character of the string to the UART, with the UART's TxEnd interrupt being
\r
96 * used to transmit the remaining characters. The UART's RxEnd interrupt
\r
97 * receives the characters and places them on a queue to be processed by the
\r
98 * 'COMs Rx' task. An error is latched should an unexpected character be
\r
99 * received, or any character be received out of sequence.
\r
101 * A loopback connector is required to ensure that each character transmitted
\r
102 * on the UART is also received on the same UART. For test purposes the UART
\r
103 * FIFO's are not utalised in order to maximise the interrupt overhead. Also
\r
104 * a pseudo random interval is used between the start of each transmission in
\r
105 * order that the resultant interrupts are more randomly distributed and
\r
106 * therefore more likely to highlight any problems.
\r
108 * The flash co-routines control LED's zero to four. LED five is toggled each
\r
109 * time the string is transmitted on the UART. LED six is toggled each time
\r
110 * the string is CORRECTLY received on the UART. LED seven is latched on
\r
111 * should an error be detected in any task or co-routine.
\r
113 * In addition the idle task makes repetitive calls to
\r
114 * vSetAndCheckRegisters(). This simply loads the general purpose registers
\r
115 * with a known value, then checks each register to ensure the held value is
\r
116 * still correct. As a low priority task this checking routine is likely to
\r
117 * get repeatedly swapped in and out. A register being found to contain an
\r
118 * incorrect value is therefore indicative of an error in the task switching
\r
123 /* standard include files. */
\r
126 /* Scheduler include files. */
\r
127 #include "FreeRTOS.h"
\r
130 #include "croutine.h"
\r
132 /* Demo application include files. */
\r
133 #include "partest.h"
\r
134 #include "crflash.h"
\r
135 #include "commstest.h"
\r
137 /* Library include files. */
\r
138 #include "DriverLib.h"
\r
140 /* The time to delay between writing each character to the LCD. */
\r
141 #define mainCHAR_WRITE_DELAY ( 2 / portTICK_PERIOD_MS )
\r
143 /* The time to delay between writing each string to the LCD. */
\r
144 #define mainSTRING_WRITE_DELAY ( 400 / portTICK_PERIOD_MS )
\r
146 #define mainADC_DELAY ( 200 / portTICK_PERIOD_MS )
\r
148 /* The number of flash co-routines to create. */
\r
149 #define mainNUM_FLASH_CO_ROUTINES ( 5 )
\r
151 /* The length of the queue used to send messages to the LCD task. */
\r
152 #define mainLCD_QUEUE_LEN ( 3 )
\r
154 /* The priority of the co-routine used to initiate the transmission of the
\r
155 string on UART 0. */
\r
156 #define mainTX_CO_ROUTINE_PRIORITY ( 1 )
\r
157 #define mainADC_CO_ROUTINE_PRIORITY ( 2 )
\r
159 /* Only one of each co-routine is created so its index is not important. */
\r
160 #define mainTX_CO_ROUTINE_INDEX ( 0 )
\r
161 #define mainADC_CO_ROUTINE_INDEX ( 0 )
\r
163 /* The task priorities. */
\r
164 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
165 #define mainMSG_TASK_PRIORITY ( mainLCD_TASK_PRIORITY - 1 )
\r
166 #define mainCOMMS_RX_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
168 /* The LCD had two rows. */
\r
169 #define mainTOP_ROW 0
\r
170 #define mainBOTTOM_ROW 1
\r
172 /* Dimension for the buffer into which the ADC value string is written. */
\r
173 #define mainMAX_ADC_STRING_LEN 20
\r
175 /* The LED that is lit should an error be detected in any of the tasks or
\r
177 #define mainFAIL_LED ( 7 )
\r
179 /*-----------------------------------------------------------*/
\r
182 * The task that displays text on the LCD.
\r
184 static void prvLCDTask( void * pvParameters );
\r
187 * The task that sends messages to be displayed on the top row of the LCD.
\r
189 static void prvLCDMessageTask( void * pvParameters );
\r
192 * The co-routine that reads the ADC and sends messages for display on the
\r
193 * bottom row of the LCD.
\r
195 static void prvADCCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
198 * Function to simply set a known value into the general purpose registers
\r
199 * then read them back to ensure they remain set correctly. An incorrect value
\r
200 * being indicative of an error in the task switching mechanism.
\r
202 extern void vSetAndCheckRegisters( void );
\r
205 * Latch the LED that indicates that an error has occurred.
\r
207 void vSetErrorLED( void );
\r
210 * Thread safe write to the PDC.
\r
212 static void prvPDCWrite( char cAddress, char cData );
\r
215 * Sets up the hardware used by the demo.
\r
217 static void prvSetupHardware( void );
\r
220 /*-----------------------------------------------------------*/
\r
222 /* The structure that is passed on the LCD message queue. */
\r
225 char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */
\r
226 portBASE_TYPE xRow; /*<< The row on which the message should be displayed. */
\r
229 /* Error flag set to pdFAIL if an error is encountered in the tasks/co-routines
\r
230 defined within this file. */
\r
231 unsigned portBASE_TYPE uxErrorStatus = pdPASS;
\r
233 /* The queue used to transmit messages to the LCD task. */
\r
234 static QueueHandle_t xLCDQueue;
\r
236 /*-----------------------------------------------------------*/
\r
239 * Setup the hardware, create the tasks/co-routines, then start the scheduler.
\r
243 /* Create the queue used by tasks wanting to write to the LCD. */
\r
244 xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );
\r
246 /* Setup the ports used by the demo and the clock. */
\r
247 prvSetupHardware();
\r
249 /* Create the co-routines that flash the LED's. */
\r
250 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
252 /* Create the co-routine that initiates the transmission of characters
\r
253 on the UART and the task that receives them, as described at the top of
\r
255 xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );
\r
256 xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );
\r
258 /* Create the task that waits for messages to display on the LCD, plus the
\r
259 task and co-routine that send messages for display (as described at the top
\r
261 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );
\r
262 xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );
\r
263 xCoRoutineCreate( prvADCCoRoutine, mainADC_CO_ROUTINE_PRIORITY, mainADC_CO_ROUTINE_INDEX );
\r
265 /* Start the scheduler running the tasks and co-routines just created. */
\r
266 vTaskStartScheduler();
\r
268 /* Should not get here unless we did not have enough memory to start the
\r
272 /*-----------------------------------------------------------*/
\r
274 static void prvLCDMessageTask( void * pvParameters )
\r
276 /* The strings that are written to the LCD. */
\r
277 char *pcStringsToDisplay[] = {
\r
281 "www.FreeRTOS.org",
\r
285 QueueHandle_t *pxLCDQueue;
\r
286 xLCDMessage xMessageToSend;
\r
287 portBASE_TYPE xIndex = 0;
\r
289 /* To test the parameter passing mechanism, the queue on which messages are
\r
290 posted is passed in as a parameter even though it is available as a file
\r
291 scope variable anyway. */
\r
292 pxLCDQueue = ( QueueHandle_t * ) pvParameters;
\r
296 /* Wait until it is time to move onto the next string. */
\r
297 vTaskDelay( mainSTRING_WRITE_DELAY );
\r
299 /* Create the message object to send to the LCD task. */
\r
300 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];
\r
301 xMessageToSend.xRow = mainTOP_ROW;
\r
303 /* Post the message to be displayed. */
\r
304 if( !xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 ) )
\r
306 uxErrorStatus = pdFAIL;
\r
309 /* Move onto the next message, wrapping when necessary. */
\r
311 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )
\r
315 /* Delay longer before going back to the start of the messages. */
\r
316 vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
\r
320 /*-----------------------------------------------------------*/
\r
322 void prvLCDTask( void * pvParameters )
\r
324 unsigned portBASE_TYPE uxIndex;
\r
325 QueueHandle_t *pxLCDQueue;
\r
326 xLCDMessage xReceivedMessage;
\r
328 const unsigned char ucCFGData[] = {
\r
329 0x30, /* Set data bus to 8-bits. */
\r
332 0x3C, /* Number of lines/font. */
\r
333 0x08, /* Display off. */
\r
334 0x01, /* Display clear. */
\r
335 0x06, /* Entry mode [cursor dir][shift]. */
\r
336 0x0C /* Display on [display on][curson on][blinking on]. */
\r
339 /* To test the parameter passing mechanism, the queue on which messages are
\r
340 received is passed in as a parameter even though it is available as a file
\r
341 scope variable anyway. */
\r
342 pxLCDQueue = ( QueueHandle_t * ) pvParameters;
\r
344 /* Configure the LCD. */
\r
346 while( uxIndex < sizeof( ucCFGData ) )
\r
348 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
\r
350 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
353 /* Turn the LCD Backlight on. */
\r
354 prvPDCWrite( PDC_CSR, 0x01 );
\r
356 /* Clear display. */
\r
357 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
358 prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR );
\r
363 /* Wait for a message to arrive. */
\r
364 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )
\r
366 /* Which row does the received message say to write to? */
\r
367 PDCLCDSetPos( 0, xReceivedMessage.xRow );
\r
369 /* Where is the string we are going to display? */
\r
370 pcString = *xReceivedMessage.ppcMessageToDisplay;
\r
374 /* Don't write out the string too quickly as LCD's are usually
\r
375 pretty slow devices. */
\r
376 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
377 prvPDCWrite( PDC_LCD_RAM, *pcString );
\r
383 /*-----------------------------------------------------------*/
\r
385 static void prvADCCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
387 static unsigned long ulADCValue;
\r
388 static char cMessageBuffer[ mainMAX_ADC_STRING_LEN ];
\r
389 static char *pcMessage;
\r
390 static xLCDMessage xMessageToSend;
\r
392 /* Co-routines MUST start with a call to crSTART(). */
\r
393 crSTART( xHandle );
\r
397 /* Start an ADC conversion. */
\r
398 ADCProcessorTrigger( ADC_BASE, 0 );
\r
400 /* Simply delay - when we unblock the result should be available */
\r
401 crDELAY( xHandle, mainADC_DELAY );
\r
403 /* Get the ADC result. */
\r
404 ADCSequenceDataGet( ADC_BASE, 0, &ulADCValue );
\r
406 /* Create a string with the result. */
\r
407 sprintf( cMessageBuffer, "ADC = %d ", ulADCValue );
\r
408 pcMessage = cMessageBuffer;
\r
410 /* Configure the message we are going to send for display. */
\r
411 xMessageToSend.ppcMessageToDisplay = ( char** ) &pcMessage;
\r
412 xMessageToSend.xRow = mainBOTTOM_ROW;
\r
414 /* Send the string to the LCD task for display. We are sending
\r
415 on a task queue so do not have the option to block. */
\r
416 if( !xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 ) )
\r
418 uxErrorStatus = pdFAIL;
\r
422 /* Co-routines MUST end with a call to crEND(). */
\r
425 /*-----------------------------------------------------------*/
\r
427 static void prvSetupHardware( void )
\r
429 /* Setup the PLL. */
\r
430 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
432 /* Initialise the hardware used to talk to the LCD, LED's and UART. */
\r
434 vParTestInitialise();
\r
437 /* The ADC is used to read the light sensor. */
\r
438 SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
\r
439 ADCSequenceConfigure( ADC_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
\r
440 ADCSequenceStepConfigure( ADC_BASE, 0, 0, ADC_CTL_CH0 | ADC_CTL_END );
\r
441 ADCSequenceEnable( ADC_BASE, 0 );
\r
444 /*-----------------------------------------------------------*/
\r
446 static void prvPDCWrite( char cAddress, char cData )
\r
450 PDCWrite( cAddress, cData );
\r
454 /*-----------------------------------------------------------*/
\r
456 void vSetErrorLED( void )
\r
458 vParTestSetLED( mainFAIL_LED, pdTRUE );
\r
460 /*-----------------------------------------------------------*/
\r
462 void vApplicationIdleHook( void )
\r
464 /* The co-routines are executed in the idle task using the idle task
\r
468 /* Schedule the co-routines. */
\r
469 vCoRoutineSchedule();
\r
471 /* Run the register check function between each co-routine. */
\r
472 vSetAndCheckRegisters();
\r
474 /* See if the comms task and co-routine has found any errors. */
\r
475 if( uxGetCommsStatus() != pdPASS )
\r
477 vParTestSetLED( mainFAIL_LED, pdTRUE );
\r
481 /*-----------------------------------------------------------*/
\r