2 FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * This demo application creates six co-routines and two tasks (three including
\r
54 * the idle task). The co-routines execute as part of the idle task hook.
\r
56 * Five of the created co-routines are the standard 'co-routine flash'
\r
57 * co-routines contained within the Demo/Common/Minimal/crflash.c file and
\r
58 * documented on the FreeRTOS.org WEB site.
\r
60 * The 'LCD Task' rotates a string on the LCD, delaying between each character
\r
61 * as necessitated by the slow interface, and delaying between each string just
\r
62 * long enough to enable the text to be read.
\r
64 * The sixth co-routine and final task control the transmission and reception
\r
65 * of a string to UART 0. The co-routine periodically sends the first
\r
66 * character of the string to the UART, with the UART's TxEnd interrupt being
\r
67 * used to transmit the remaining characters. The UART's RxEnd interrupt
\r
68 * receives the characters and places them on a queue to be processed by the
\r
69 * 'COMs Rx' task. An error is latched should an unexpected character be
\r
70 * received, or any character be received out of sequence.
\r
72 * A loopback connector is required to ensure that each character transmitted
\r
73 * on the UART is also received on the same UART. For test purposes the UART
\r
74 * FIFO's are not utalised in order to maximise the interrupt overhead. Also
\r
75 * a pseudo random interval is used between the start of each transmission in
\r
76 * order that the resultant interrupts are more randomly distributed and
\r
77 * therefore more likely to highlight any problems.
\r
79 * The flash co-routines control LED's zero to four. LED five is toggled each
\r
80 * time the string is transmitted on the UART. LED six is toggled each time
\r
81 * the string is CORRECTLY received on the UART. LED seven is latched on should
\r
82 * an error be detected in any task or co-routine.
\r
84 * In addition the idle task makes repetative calls to
\r
85 * prvSetAndCheckRegisters(). This simply loads the general purpose registers
\r
86 * with a known value, then checks each register to ensure the held value is
\r
87 * still correct. As a low priority task this checking routine is likely to
\r
88 * get repeatedly swapped in and out. A register being found to contain an
\r
89 * incorrect value is therefore indicative of an error in the task switching
\r
94 /* Scheduler include files. */
\r
95 #include "FreeRTOS.h"
\r
98 #include "croutine.h"
\r
100 /* Demo application include files. */
\r
101 #include "partest.h"
\r
102 #include "crflash.h"
\r
104 /* Library include files. */
\r
105 #include "DriverLib.h"
\r
107 /* The time to delay between writing each character to the LCD. */
\r
108 #define mainCHAR_WRITE_DELAY ( 2 / portTICK_RATE_MS )
\r
110 /* The time to delay between writing each string to the LCD. */
\r
111 #define mainSTRING_WRITE_DELAY ( 400 / portTICK_RATE_MS )
\r
113 /* The number of flash co-routines to create. */
\r
114 #define mainNUM_FLASH_CO_ROUTINES ( 5 )
\r
116 /* The length of the queue used to pass received characters to the Comms Rx
\r
118 #define mainRX_QUEUE_LEN ( 5 )
\r
120 /* The priority of the co-routine used to initiate the transmission of the
\r
121 string on UART 0. */
\r
122 #define mainTX_CO_ROUTINE_PRIORITY ( 1 )
\r
124 /* Only one co-routine is created so its index is not important. */
\r
125 #define mainTX_CO_ROUTINE_INDEX ( 0 )
\r
127 /* The time between transmissions of the string on UART 0. This is pseudo
\r
128 random in order to generate a bit or randomness to when the interrupts occur.*/
\r
129 #define mainMIN_TX_DELAY ( 40 / portTICK_RATE_MS )
\r
130 #define mainMAX_TX_DELAY ( ( portTickType ) 0x7f )
\r
131 #define mainOFFSET_TIME ( ( portTickType ) 3 )
\r
133 /* The time the Comms Rx task should wait to receive a character. This should
\r
134 be slightly longer than the time between transmissions. If we do not receive
\r
135 a character after this time then there must be an error in the transmission or
\r
136 the timing of the transmission. */
\r
137 #define mainCOMMS_RX_DELAY ( mainMAX_TX_DELAY + 20 )
\r
139 /* The task priorites. */
\r
140 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
141 #define mainCOMMS_RX_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
143 /* The LED's toggled by the various tasks. */
\r
144 #define mainCOMMS_FAIL_LED ( 7 )
\r
145 #define mainCOMMS_RX_LED ( 6 )
\r
146 #define mainCOMMS_TX_LED ( 5 )
\r
148 /* The baud rate used by the UART comms tasks/co-routine. */
\r
149 #define mainBAUD_RATE ( 57600 )
\r
151 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
\r
152 #define mainFIFO_SET ( 0x10 )
\r
154 /* The string that is transmitted on the UART contains sequentially the
\r
155 characters from mainFIRST_TX_CHAR to mainLAST_TX_CHAR. */
\r
156 #define mainFIRST_TX_CHAR '0'
\r
157 #define mainLAST_TX_CHAR 'z'
\r
159 /* Just used to walk through the program memory in order that some random data
\r
160 can be generated. */
\r
161 #define mainTOTAL_PROGRAM_MEMORY ( ( unsigned portLONG * ) ( 8 * 1024 ) )
\r
162 #define mainFIRST_PROGRAM_BYTES ( ( unsigned portLONG * ) 4 )
\r
164 /* The error routine that is called if the driver library encounters an error. */
\r
167 __error__(char *pcFilename, unsigned long ulLine)
\r
172 /*-----------------------------------------------------------*/
\r
175 * The task that rotates text on the LCD.
\r
177 static void vLCDTask( void * pvParameters );
\r
180 * The task that receives the characters from UART 0.
\r
182 static void vCommsRxTask( void * pvParameters );
\r
185 * The co-routine that periodically initiates the transmission of the string on
\r
188 static void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
191 * Writes a string the the LCD.
\r
193 static void prvWriteString( const portCHAR *pcString );
\r
196 * Initialisation routine for the UART.
\r
198 static void vSerialInit( void );
\r
201 * Thread safe write to the PDC.
\r
203 static void prvPDCWrite( portCHAR cAddress, portCHAR cData );
\r
206 * Function to simply set a known value into the general purpose registers
\r
207 * then read them back to ensure they remain set correctly. An incorrect value
\r
208 * being indicative of an error in the task switching mechanism.
\r
210 void prvSetAndCheckRegisters( void );
\r
213 * Latch the LED that indicates that an error has occurred.
\r
215 void vSetErrorLED( void );
\r
218 * Sets up the PLL and ports used by the demo.
\r
220 static void prvSetupHardware( void );
\r
222 /*-----------------------------------------------------------*/
\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 next character to transmit. */
\r
229 static portCHAR cNextChar;
\r
231 /* The queue used to transmit characters from the interrupt to the Comms Rx
\r
233 static xQueueHandle xCommsQueue;
\r
235 /*-----------------------------------------------------------*/
\r
239 /* Create the queue used to communicate between the UART ISR and the Comms
\r
241 xCommsQueue = xQueueCreate( mainRX_QUEUE_LEN, sizeof( portCHAR ) );
\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
251 xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );
\r
253 /* Create the LCD and Comms Rx tasks. */
\r
254 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );
\r
255 xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );
\r
257 /* Start the scheduler running the tasks and co-routines just created. */
\r
258 vTaskStartScheduler();
\r
260 /* Should not get here unless we did not have enough memory to start the
\r
264 /*-----------------------------------------------------------*/
\r
266 static void prvSetupHardware( void )
\r
268 /* Setup the PLL. */
\r
269 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\r
271 /* Initialise the hardware used to talk to the LCD, LED's and UART. */
\r
273 vParTestInitialise();
\r
276 /*-----------------------------------------------------------*/
\r
278 void vApplicationIdleHook( void )
\r
280 /* The co-routines are executed in the idle task using the idle task
\r
284 /* Schedule the co-routines. */
\r
285 vCoRoutineSchedule();
\r
287 /* Run the register check function between each co-routine. */
\r
288 prvSetAndCheckRegisters();
\r
291 /*-----------------------------------------------------------*/
\r
293 static void prvWriteString( const portCHAR *pcString )
\r
295 /* Write pcString to the LED, pausing between each character. */
\r
296 prvPDCWrite(PDC_LCD_CSR, LCD_CLEAR);
\r
299 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
300 prvPDCWrite( PDC_LCD_RAM, *pcString );
\r
304 /*-----------------------------------------------------------*/
\r
306 void vLCDTask( void * pvParameters )
\r
308 unsigned portBASE_TYPE uxIndex;
\r
309 const unsigned portCHAR ucCFGData[] = {
\r
310 0x30, /* Set data bus to 8-bits. */
\r
313 0x3C, /* Number of lines/font. */
\r
314 0x08, /* Display off. */
\r
315 0x01, /* Display clear. */
\r
316 0x06, /* Entry mode [cursor dir][shift]. */
\r
317 0x0C /* Display on [display on][curson on][blinking on]. */
\r
320 /* The strings that are written to the LCD. */
\r
321 const portCHAR *pcStringsToDisplay[] = {
\r
325 "www.FreeRTOS.org",
\r
329 /* Configure the LCD. */
\r
331 while( uxIndex < sizeof( ucCFGData ) )
\r
333 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
\r
335 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
338 /* Turn the LCD Backlight on. */
\r
339 prvPDCWrite( PDC_CSR, 0x01 );
\r
341 /* Clear display. */
\r
342 vTaskDelay( mainCHAR_WRITE_DELAY );
\r
343 prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR );
\r
348 /* Display the string on the LCD. */
\r
349 prvWriteString( pcStringsToDisplay[ uxIndex ] );
\r
351 /* Move on to the next string - wrapping if necessary. */
\r
353 if( *( pcStringsToDisplay[ uxIndex ] ) == 0x00 )
\r
356 /* Longer pause on the last string to be sent. */
\r
357 vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
\r
360 /* Wait until it is time to move onto the next string. */
\r
361 vTaskDelay( mainSTRING_WRITE_DELAY );
\r
364 /*-----------------------------------------------------------*/
\r
366 static void vCommsRxTask( void * pvParameters )
\r
368 static portCHAR cRxedChar, cExpectedChar;
\r
370 /* Set the char we expect to receive to the start of the string. */
\r
371 cExpectedChar = mainFIRST_TX_CHAR;
\r
375 /* Wait for a character to be received. */
\r
376 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, mainCOMMS_RX_DELAY );
\r
378 /* Was the character recived (if any) the expected character. */
\r
379 if( cRxedChar != cExpectedChar )
\r
381 /* Got an unexpected character. This can sometimes occur when
\r
382 reseting the system using the debugger leaving characters already
\r
383 in the UART regsters. */
\r
384 uxErrorStatus = pdFAIL;
\r
386 /* Resync by waiting for the end of the current string. */
\r
387 while( cRxedChar != mainLAST_TX_CHAR )
\r
389 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
\r
392 /* The next expected character is the start of the string again. */
\r
393 cExpectedChar = mainFIRST_TX_CHAR;
\r
397 if( cExpectedChar == mainLAST_TX_CHAR )
\r
399 /* We have reached the end of the string - we now expect to
\r
400 receive the first character in the string again. The LED is
\r
401 toggled to indicate that the entire string was received without
\r
403 vParTestToggleLED( mainCOMMS_RX_LED );
\r
404 cExpectedChar = mainFIRST_TX_CHAR;
\r
408 /* We got the expected character, we now expect to receive the
\r
409 next character in the string. */
\r
415 /*-----------------------------------------------------------*/
\r
417 static void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
419 portTickType xDelayPeriod;
\r
420 static unsigned portLONG *pulRandomBytes = mainFIRST_PROGRAM_BYTES;
\r
422 /* Co-routine MUST start with a call to crSTART. */
\r
423 crSTART( xHandle );
\r
427 /* Was the previously transmitted string received correctly? */
\r
428 if( uxErrorStatus != pdPASS )
\r
430 /* An error was encountered so set the error LED. */
\r
434 /* The next character to Tx is the first in the string. */
\r
435 cNextChar = mainFIRST_TX_CHAR;
\r
437 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
439 /* Send the first character. */
\r
440 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
442 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
445 /* Move the variable to the char to Tx on so the ISR transmits
\r
446 the next character in the string once this one has completed. */
\r
449 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
451 /* Toggle the LED to show a new string is being transmitted. */
\r
452 vParTestToggleLED( mainCOMMS_TX_LED );
\r
454 /* Delay before we start the string off again. A pseudo-random delay
\r
455 is used as this will provide a better test. */
\r
456 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
\r
459 if( pulRandomBytes > mainTOTAL_PROGRAM_MEMORY )
\r
461 pulRandomBytes = mainFIRST_PROGRAM_BYTES;
\r
464 /* Make sure we don't wait too long... */
\r
465 xDelayPeriod &= mainMAX_TX_DELAY;
\r
467 /* ...but we do want to wait. */
\r
468 if( xDelayPeriod < mainMIN_TX_DELAY )
\r
470 xDelayPeriod = mainMIN_TX_DELAY;
\r
473 /* Block for the random(ish) time. */
\r
474 crDELAY( xHandle, xDelayPeriod );
\r
477 /* Co-routine MUST end with a call to crEND. */
\r
480 /*-----------------------------------------------------------*/
\r
482 static void vSerialInit( void )
\r
484 /* Enable the UART. GPIOA has already been initialised. */
\r
485 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
487 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
489 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\r
491 /* Configure the UART for 8-N-1 operation. */
\r
492 UARTConfigSet( UART0_BASE, mainBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
\r
494 /* We dont want to use the fifo. This is for test purposes to generate
\r
495 as many interrupts as possible. */
\r
496 HWREG( UART0_BASE + UART_O_LCR_H ) &= ~mainFIFO_SET;
\r
498 /* Enable both Rx and Tx interrupts. */
\r
499 HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
\r
500 IntEnable( INT_UART0 );
\r
502 /*-----------------------------------------------------------*/
\r
504 void vUART_ISR(void)
\r
506 unsigned portLONG ulStatus;
\r
507 portCHAR cRxedChar;
\r
508 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
510 /* What caused the interrupt. */
\r
511 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
513 /* Clear the interrupt. */
\r
514 UARTIntClear( UART0_BASE, ulStatus );
\r
516 /* Was an Rx interrpt pending? */
\r
517 if( ulStatus & UART_INT_RX )
\r
519 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
\r
521 /* Get the char from the buffer and post it onto the queue of
\r
522 Rxed chars. Posting the character should wake the task that is
\r
523 blocked on the queue waiting for characters. */
\r
524 cRxedChar = ( portCHAR ) HWREG( UART0_BASE + UART_O_DR );
\r
525 xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );
\r
529 /* Was a Tx interrupt pending? */
\r
530 if( ulStatus & UART_INT_TX )
\r
532 /* Send the next character in the string. We are not using the FIFO. */
\r
533 if( cNextChar <= mainLAST_TX_CHAR )
\r
535 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
537 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
543 /* If a task was woken by the character being received then we force
\r
544 a context switch to occur in case the task is of higher priority than
\r
545 the currently executing task (i.e. the task that this interrupt
\r
547 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
549 /*-----------------------------------------------------------*/
\r
551 static void prvPDCWrite( portCHAR cAddress, portCHAR cData )
\r
555 PDCWrite( cAddress, cData );
\r
559 /*-----------------------------------------------------------*/
\r
561 void vSetErrorLED( void )
\r
563 vParTestSetLED( mainCOMMS_FAIL_LED, pdTRUE );
\r
565 /*-----------------------------------------------------------*/
\r
567 void prvSetAndCheckRegisters( void )
\r
569 /* Fill the general purpose registers with known values. */
\r
570 __asm volatile( " mov r11, #10\n"
\r
571 " add r0, r11, #1\n"
\r
572 " add r1, r11, #2\n"
\r
573 " add r2, r11, #3\n"
\r
574 " add r3, r11, #4\n"
\r
575 " add r4, r11, #5\n"
\r
576 " add r5, r11, #6\n"
\r
577 " add r6, r11, #7\n"
\r
578 " add r7, r11, #8\n"
\r
579 " add r8, r11, #9\n"
\r
580 " add r9, r11, #10\n"
\r
581 " add r10, r11, #11\n"
\r
582 " add r12, r11, #12" );
\r
584 /* Check the values are as expected. */
\r
585 __asm volatile( " cmp r11, #10\n"
\r
586 " bne set_error_led\n"
\r
588 " bne set_error_led\n"
\r
590 " bne set_error_led\n"
\r
592 " bne set_error_led\n"
\r
594 " bne set_error_led\n"
\r
596 " bne set_error_led\n"
\r
598 " bne set_error_led\n"
\r
600 " bne set_error_led\n"
\r
602 " bne set_error_led\n"
\r
604 " bne set_error_led\n"
\r
606 " bne set_error_led\n"
\r
608 " bne set_error_led\n"
\r
610 " bne set_error_led\n"
\r
613 __asm volatile( "set_error_led:\n"
\r
615 " ldr r1, =vSetErrorLED\n"
\r
620 /*-----------------------------------------------------------*/
\r