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