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 six co-routines and two tasks (three including
72 * the idle task). The co-routines execute as part of the idle task hook.
74 * Five of the created co-routines are the standard 'co-routine flash'
75 * co-routines contained within the Demo/Common/Minimal/crflash.c file and
76 * documented on the FreeRTOS.org WEB site.
78 * The 'LCD Task' rotates a string on the LCD, delaying between each character
79 * as necessitated by the slow interface, and delaying between each string just
80 * long enough to enable the text to be read.
82 * The sixth co-routine and final task control the transmission and reception
83 * of a string to UART 0. The co-routine periodically sends the first
84 * character of the string to the UART, with the UART's TxEnd interrupt being
85 * used to transmit the remaining characters. The UART's RxEnd interrupt
86 * receives the characters and places them on a queue to be processed by the
87 * 'COMs Rx' task. An error is latched should an unexpected character be
88 * received, or any character be received out of sequence.
90 * A loopback connector is required to ensure that each character transmitted
91 * on the UART is also received on the same UART. For test purposes the UART
92 * FIFO's are not utalised in order to maximise the interrupt overhead. Also
93 * a pseudo random interval is used between the start of each transmission in
94 * order that the resultant interrupts are more randomly distributed and
95 * therefore more likely to highlight any problems.
97 * The flash co-routines control LED's zero to four. LED five is toggled each
98 * time the string is transmitted on the UART. LED six is toggled each time
99 * the string is CORRECTLY received on the UART. LED seven is latched on should
100 * an error be detected in any task or co-routine.
102 * In addition the idle task makes repetative calls to
103 * prvSetAndCheckRegisters(). This simply loads the general purpose registers
104 * with a known value, then checks each register to ensure the held value is
105 * still correct. As a low priority task this checking routine is likely to
106 * get repeatedly swapped in and out. A register being found to contain an
107 * incorrect value is therefore indicative of an error in the task switching
112 /* Scheduler include files. */
113 #include "FreeRTOS.h"
116 #include "croutine.h"
118 /* Demo application include files. */
122 /* Library include files. */
123 #include "DriverLib.h"
125 /* The time to delay between writing each character to the LCD. */
126 #define mainCHAR_WRITE_DELAY ( 2 / portTICK_PERIOD_MS )
128 /* The time to delay between writing each string to the LCD. */
129 #define mainSTRING_WRITE_DELAY ( 400 / portTICK_PERIOD_MS )
131 /* The number of flash co-routines to create. */
132 #define mainNUM_FLASH_CO_ROUTINES ( 5 )
134 /* The length of the queue used to pass received characters to the Comms Rx
136 #define mainRX_QUEUE_LEN ( 5 )
138 /* The priority of the co-routine used to initiate the transmission of the
140 #define mainTX_CO_ROUTINE_PRIORITY ( 1 )
142 /* Only one co-routine is created so its index is not important. */
143 #define mainTX_CO_ROUTINE_INDEX ( 0 )
145 /* The time between transmissions of the string on UART 0. This is pseudo
146 random in order to generate a bit or randomness to when the interrupts occur.*/
147 #define mainMIN_TX_DELAY ( 40 / portTICK_PERIOD_MS )
148 #define mainMAX_TX_DELAY ( ( TickType_t ) 0x7f )
149 #define mainOFFSET_TIME ( ( TickType_t ) 3 )
151 /* The time the Comms Rx task should wait to receive a character. This should
152 be slightly longer than the time between transmissions. If we do not receive
153 a character after this time then there must be an error in the transmission or
154 the timing of the transmission. */
155 #define mainCOMMS_RX_DELAY ( mainMAX_TX_DELAY + 20 )
157 /* The task priorites. */
158 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY )
159 #define mainCOMMS_RX_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
161 /* The LED's toggled by the various tasks. */
162 #define mainCOMMS_FAIL_LED ( 7 )
163 #define mainCOMMS_RX_LED ( 6 )
164 #define mainCOMMS_TX_LED ( 5 )
166 /* The baud rate used by the UART comms tasks/co-routine. */
167 #define mainBAUD_RATE ( 57600 )
169 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
170 #define mainFIFO_SET ( 0x10 )
172 /* The string that is transmitted on the UART contains sequentially the
173 characters from mainFIRST_TX_CHAR to mainLAST_TX_CHAR. */
174 #define mainFIRST_TX_CHAR '0'
175 #define mainLAST_TX_CHAR 'z'
177 /* Just used to walk through the program memory in order that some random data
179 #define mainTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )
180 #define mainFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )
182 /* The error routine that is called if the driver library encounters an error. */
185 __error__(char *pcFilename, unsigned long ulLine)
190 /*-----------------------------------------------------------*/
193 * The task that rotates text on the LCD.
195 static void vLCDTask( void * pvParameters );
198 * The task that receives the characters from UART 0.
200 static void vCommsRxTask( void * pvParameters );
203 * The co-routine that periodically initiates the transmission of the string on
206 static void vSerialTxCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
209 * Writes a string the the LCD.
211 static void prvWriteString( const char *pcString );
214 * Initialisation routine for the UART.
216 static void vSerialInit( void );
219 * Thread safe write to the PDC.
221 static void prvPDCWrite( char cAddress, char cData );
224 * Function to simply set a known value into the general purpose registers
225 * then read them back to ensure they remain set correctly. An incorrect value
226 * being indicative of an error in the task switching mechanism.
228 void prvSetAndCheckRegisters( void );
231 * Latch the LED that indicates that an error has occurred.
233 void vSetErrorLED( void );
236 * Sets up the PLL and ports used by the demo.
238 static void prvSetupHardware( void );
240 /*-----------------------------------------------------------*/
242 /* Error flag set to pdFAIL if an error is encountered in the tasks/co-routines
243 defined within this file. */
244 unsigned portBASE_TYPE uxErrorStatus = pdPASS;
246 /* The next character to transmit. */
247 static char cNextChar;
249 /* The queue used to transmit characters from the interrupt to the Comms Rx
251 static QueueHandle_t xCommsQueue;
253 /*-----------------------------------------------------------*/
257 /* Create the queue used to communicate between the UART ISR and the Comms
259 xCommsQueue = xQueueCreate( mainRX_QUEUE_LEN, sizeof( char ) );
261 /* Setup the ports used by the demo and the clock. */
264 /* Create the co-routines that flash the LED's. */
265 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
267 /* Create the co-routine that initiates the transmission of characters
269 xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );
271 /* Create the LCD and Comms Rx tasks. */
272 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );
273 xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );
275 /* Start the scheduler running the tasks and co-routines just created. */
276 vTaskStartScheduler();
278 /* Should not get here unless we did not have enough memory to start the
282 /*-----------------------------------------------------------*/
284 static void prvSetupHardware( void )
287 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
289 /* Initialise the hardware used to talk to the LCD, LED's and UART. */
291 vParTestInitialise();
294 /*-----------------------------------------------------------*/
296 void vApplicationIdleHook( void )
298 /* The co-routines are executed in the idle task using the idle task
302 /* Schedule the co-routines. */
303 vCoRoutineSchedule();
305 /* Run the register check function between each co-routine. */
306 prvSetAndCheckRegisters();
309 /*-----------------------------------------------------------*/
311 static void prvWriteString( const char *pcString )
313 /* Write pcString to the LED, pausing between each character. */
314 prvPDCWrite(PDC_LCD_CSR, LCD_CLEAR);
317 vTaskDelay( mainCHAR_WRITE_DELAY );
318 prvPDCWrite( PDC_LCD_RAM, *pcString );
322 /*-----------------------------------------------------------*/
324 void vLCDTask( void * pvParameters )
326 unsigned portBASE_TYPE uxIndex;
327 const unsigned char ucCFGData[] = {
328 0x30, /* Set data bus to 8-bits. */
331 0x3C, /* Number of lines/font. */
332 0x08, /* Display off. */
333 0x01, /* Display clear. */
334 0x06, /* Entry mode [cursor dir][shift]. */
335 0x0C /* Display on [display on][curson on][blinking on]. */
338 /* The strings that are written to the LCD. */
339 const char *pcStringsToDisplay[] = {
347 /* Configure the LCD. */
349 while( uxIndex < sizeof( ucCFGData ) )
351 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
353 vTaskDelay( mainCHAR_WRITE_DELAY );
356 /* Turn the LCD Backlight on. */
357 prvPDCWrite( PDC_CSR, 0x01 );
360 vTaskDelay( mainCHAR_WRITE_DELAY );
361 prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR );
366 /* Display the string on the LCD. */
367 prvWriteString( pcStringsToDisplay[ uxIndex ] );
369 /* Move on to the next string - wrapping if necessary. */
371 if( *( pcStringsToDisplay[ uxIndex ] ) == 0x00 )
374 /* Longer pause on the last string to be sent. */
375 vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
378 /* Wait until it is time to move onto the next string. */
379 vTaskDelay( mainSTRING_WRITE_DELAY );
382 /*-----------------------------------------------------------*/
384 static void vCommsRxTask( void * pvParameters )
386 static char cRxedChar, cExpectedChar;
388 /* Set the char we expect to receive to the start of the string. */
389 cExpectedChar = mainFIRST_TX_CHAR;
393 /* Wait for a character to be received. */
394 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, mainCOMMS_RX_DELAY );
396 /* Was the character recived (if any) the expected character. */
397 if( cRxedChar != cExpectedChar )
399 /* Got an unexpected character. This can sometimes occur when
400 reseting the system using the debugger leaving characters already
401 in the UART regsters. */
402 uxErrorStatus = pdFAIL;
404 /* Resync by waiting for the end of the current string. */
405 while( cRxedChar != mainLAST_TX_CHAR )
407 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
410 /* The next expected character is the start of the string again. */
411 cExpectedChar = mainFIRST_TX_CHAR;
415 if( cExpectedChar == mainLAST_TX_CHAR )
417 /* We have reached the end of the string - we now expect to
418 receive the first character in the string again. The LED is
419 toggled to indicate that the entire string was received without
421 vParTestToggleLED( mainCOMMS_RX_LED );
422 cExpectedChar = mainFIRST_TX_CHAR;
426 /* We got the expected character, we now expect to receive the
427 next character in the string. */
433 /*-----------------------------------------------------------*/
435 static void vSerialTxCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
437 TickType_t xDelayPeriod;
438 static unsigned long *pulRandomBytes = mainFIRST_PROGRAM_BYTES;
440 /* Co-routine MUST start with a call to crSTART. */
445 /* Was the previously transmitted string received correctly? */
446 if( uxErrorStatus != pdPASS )
448 /* An error was encountered so set the error LED. */
452 /* The next character to Tx is the first in the string. */
453 cNextChar = mainFIRST_TX_CHAR;
455 UARTIntDisable( UART0_BASE, UART_INT_TX );
457 /* Send the first character. */
458 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
460 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
463 /* Move the variable to the char to Tx on so the ISR transmits
464 the next character in the string once this one has completed. */
467 UARTIntEnable(UART0_BASE, UART_INT_TX);
469 /* Toggle the LED to show a new string is being transmitted. */
470 vParTestToggleLED( mainCOMMS_TX_LED );
472 /* Delay before we start the string off again. A pseudo-random delay
473 is used as this will provide a better test. */
474 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
477 if( pulRandomBytes > mainTOTAL_PROGRAM_MEMORY )
479 pulRandomBytes = mainFIRST_PROGRAM_BYTES;
482 /* Make sure we don't wait too long... */
483 xDelayPeriod &= mainMAX_TX_DELAY;
485 /* ...but we do want to wait. */
486 if( xDelayPeriod < mainMIN_TX_DELAY )
488 xDelayPeriod = mainMIN_TX_DELAY;
491 /* Block for the random(ish) time. */
492 crDELAY( xHandle, xDelayPeriod );
495 /* Co-routine MUST end with a call to crEND. */
498 /*-----------------------------------------------------------*/
500 static void vSerialInit( void )
502 /* Enable the UART. GPIOA has already been initialised. */
503 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
505 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
507 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
509 /* Configure the UART for 8-N-1 operation. */
510 UARTConfigSet( UART0_BASE, mainBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
512 /* We dont want to use the fifo. This is for test purposes to generate
513 as many interrupts as possible. */
514 HWREG( UART0_BASE + UART_O_LCR_H ) &= ~mainFIFO_SET;
516 /* Enable both Rx and Tx interrupts. */
517 HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
518 IntEnable( INT_UART0 );
520 /*-----------------------------------------------------------*/
524 unsigned long ulStatus;
526 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
528 /* What caused the interrupt. */
529 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
531 /* Clear the interrupt. */
532 UARTIntClear( UART0_BASE, ulStatus );
534 /* Was an Rx interrpt pending? */
535 if( ulStatus & UART_INT_RX )
537 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
539 /* Get the char from the buffer and post it onto the queue of
540 Rxed chars. Posting the character should wake the task that is
541 blocked on the queue waiting for characters. */
542 cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );
543 xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );
547 /* Was a Tx interrupt pending? */
548 if( ulStatus & UART_INT_TX )
550 /* Send the next character in the string. We are not using the FIFO. */
551 if( cNextChar <= mainLAST_TX_CHAR )
553 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
555 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
561 /* If a task was woken by the character being received then we force
562 a context switch to occur in case the task is of higher priority than
563 the currently executing task (i.e. the task that this interrupt
565 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
567 /*-----------------------------------------------------------*/
569 static void prvPDCWrite( char cAddress, char cData )
573 PDCWrite( cAddress, cData );
577 /*-----------------------------------------------------------*/
579 void vSetErrorLED( void )
581 vParTestSetLED( mainCOMMS_FAIL_LED, pdTRUE );
583 /*-----------------------------------------------------------*/
585 void prvSetAndCheckRegisters( void )
587 /* Fill the general purpose registers with known values. */
588 __asm volatile( " mov r11, #10\n"
598 " add r9, r11, #10\n"
599 " add r10, r11, #11\n"
600 " add r12, r11, #12" );
602 /* Check the values are as expected. */
603 __asm volatile( " cmp r11, #10\n"
604 " bne set_error_led\n"
606 " bne set_error_led\n"
608 " bne set_error_led\n"
610 " bne set_error_led\n"
612 " bne set_error_led\n"
614 " bne set_error_led\n"
616 " bne set_error_led\n"
618 " bne set_error_led\n"
620 " bne set_error_led\n"
622 " bne set_error_led\n"
624 " bne set_error_led\n"
626 " bne set_error_led\n"
628 " bne set_error_led\n"
631 __asm volatile( "set_error_led:\n"
633 " ldr r1, =vSetErrorLED\n"
638 /*-----------------------------------------------------------*/