2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * The comms test Rx and Tx task and co-routine. See the comments at the top
\r
67 * of main.c for full information.
\r
71 /* Scheduler include files. */
\r
72 #include "FreeRTOS.h"
\r
75 #include "croutine.h"
\r
77 /* Demo application include files. */
\r
78 #include "partest.h"
\r
80 /* Library include files. */
\r
81 #include "DriverLib.h"
\r
83 /* The LED's toggled by the various tasks. */
\r
84 #define commsFAIL_LED ( 7 )
\r
85 #define commsRX_LED ( 6 )
\r
86 #define commsTX_LED ( 5 )
\r
88 /* The length of the queue used to pass received characters to the Comms Rx
\r
90 #define commsRX_QUEUE_LEN ( 5 )
\r
92 /* The baud rate used by the UART comms tasks/co-routine. */
\r
93 #define commsBAUD_RATE ( 57600 )
\r
95 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
\r
96 #define commsFIFO_SET ( 0x10 )
\r
98 /* The string that is transmitted on the UART contains sequentially the
\r
99 characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */
\r
100 #define commsFIRST_TX_CHAR '0'
\r
101 #define commsLAST_TX_CHAR 'z'
\r
103 /* Just used to walk through the program memory in order that some random data
\r
104 can be generated. */
\r
105 #define commsTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )
\r
106 #define commsFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )
\r
108 /* The time between transmissions of the string on UART 0. This is pseudo
\r
109 random in order to generate a bit or randomness to when the interrupts occur.*/
\r
110 #define commsMIN_TX_DELAY ( 40 / portTICK_RATE_MS )
\r
111 #define commsMAX_TX_DELAY ( ( portTickType ) 0x7f )
\r
112 #define commsOFFSET_TIME ( ( portTickType ) 3 )
\r
114 /* The time the Comms Rx task should wait to receive a character. This should
\r
115 be slightly longer than the time between transmissions. If we do not receive
\r
116 a character after this time then there must be an error in the transmission or
\r
117 the timing of the transmission. */
\r
118 #define commsRX_DELAY ( commsMAX_TX_DELAY + 20 )
\r
121 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;
\r
123 /* The queue used to pass characters out of the ISR. */
\r
124 static xQueueHandle xCommsQueue;
\r
126 /* The next character to transmit. */
\r
127 static char cNextChar;
\r
129 /*-----------------------------------------------------------*/
\r
131 void vSerialInit( void )
\r
133 /* Create the queue used to communicate between the UART ISR and the Comms
\r
135 xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );
\r
137 /* Enable the UART. GPIOA has already been initialised. */
\r
138 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
140 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
142 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\r
144 /* Configure the UART for 8-N-1 operation. */
\r
145 UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
\r
147 /* We dont want to use the fifo. This is for test purposes to generate
\r
148 as many interrupts as possible. */
\r
149 HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;
\r
151 /* Enable both Rx and Tx interrupts. */
\r
152 HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
\r
153 IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );
\r
154 IntEnable( INT_UART0 );
\r
156 /*-----------------------------------------------------------*/
\r
158 void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
160 portTickType xDelayPeriod;
\r
161 static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
163 /* Co-routine MUST start with a call to crSTART. */
\r
164 crSTART( xHandle );
\r
168 /* Was the previously transmitted string received correctly? */
\r
169 if( uxCommsErrorStatus != pdPASS )
\r
171 /* An error was encountered so set the error LED. */
\r
172 vParTestSetLED( commsFAIL_LED, pdTRUE );
\r
175 /* The next character to Tx is the first in the string. */
\r
176 cNextChar = commsFIRST_TX_CHAR;
\r
178 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
180 /* Send the first character. */
\r
181 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
183 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
186 /* Move the variable to the char to Tx on so the ISR transmits
\r
187 the next character in the string once this one has completed. */
\r
190 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
192 /* Toggle the LED to show a new string is being transmitted. */
\r
193 vParTestToggleLED( commsTX_LED );
\r
195 /* Delay before we start the string off again. A pseudo-random delay
\r
196 is used as this will provide a better test. */
\r
197 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
\r
200 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )
\r
202 pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
205 /* Make sure we don't wait too long... */
\r
206 xDelayPeriod &= commsMAX_TX_DELAY;
\r
208 /* ...but we do want to wait. */
\r
209 if( xDelayPeriod < commsMIN_TX_DELAY )
\r
211 xDelayPeriod = commsMIN_TX_DELAY;
\r
214 /* Block for the random(ish) time. */
\r
215 crDELAY( xHandle, xDelayPeriod );
\r
218 /* Co-routine MUST end with a call to crEND. */
\r
221 /*-----------------------------------------------------------*/
\r
223 void vUART_ISR( void )
\r
225 unsigned long ulStatus;
\r
227 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
229 /* What caused the interrupt. */
\r
230 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
232 /* Clear the interrupt. */
\r
233 UARTIntClear( UART0_BASE, ulStatus );
\r
235 /* Was an Rx interrpt pending? */
\r
236 if( ulStatus & UART_INT_RX )
\r
238 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
\r
240 /* Get the char from the buffer and post it onto the queue of
\r
241 Rxed chars. Posting the character should wake the task that is
\r
242 blocked on the queue waiting for characters. */
\r
243 cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );
\r
244 xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );
\r
248 /* Was a Tx interrupt pending? */
\r
249 if( ulStatus & UART_INT_TX )
\r
251 /* Send the next character in the string. We are not using the FIFO. */
\r
252 if( cNextChar <= commsLAST_TX_CHAR )
\r
254 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
256 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
262 /* If a task was woken by the character being received then we force
\r
263 a context switch to occur in case the task is of higher priority than
\r
264 the currently executing task (i.e. the task that this interrupt
\r
266 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
268 /*-----------------------------------------------------------*/
\r
270 void vCommsRxTask( void * pvParameters )
\r
272 static char cRxedChar, cExpectedChar;
\r
274 /* Set the char we expect to receive to the start of the string. */
\r
275 cExpectedChar = commsFIRST_TX_CHAR;
\r
279 /* Wait for a character to be received. */
\r
280 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );
\r
282 /* Was the character recived (if any) the expected character. */
\r
283 if( cRxedChar != cExpectedChar )
\r
285 /* Got an unexpected character. This can sometimes occur when
\r
286 reseting the system using the debugger leaving characters already
\r
287 in the UART regsters. */
\r
288 uxCommsErrorStatus = pdFAIL;
\r
290 /* Resync by waiting for the end of the current string. */
\r
291 while( cRxedChar != commsLAST_TX_CHAR )
\r
293 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
\r
296 /* The next expected character is the start of the string again. */
\r
297 cExpectedChar = commsFIRST_TX_CHAR;
\r
301 if( cExpectedChar == commsLAST_TX_CHAR )
\r
303 /* We have reached the end of the string - we now expect to
\r
304 receive the first character in the string again. The LED is
\r
305 toggled to indicate that the entire string was received without
\r
307 vParTestToggleLED( commsRX_LED );
\r
308 cExpectedChar = commsFIRST_TX_CHAR;
\r
312 /* We got the expected character, we now expect to receive the
\r
313 next character in the string. */
\r
319 /*-----------------------------------------------------------*/
\r
321 unsigned portBASE_TYPE uxGetCommsStatus( void )
\r
323 return uxCommsErrorStatus;
\r