2 FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 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
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
38 * The comms test Rx and Tx task and co-routine. See the comments at the top
\r
39 * of main.c for full information.
\r
43 /* Scheduler include files. */
\r
44 #include "FreeRTOS.h"
\r
47 #include "croutine.h"
\r
49 /* Demo application include files. */
\r
50 #include "partest.h"
\r
52 /* Library include files. */
\r
53 #include "DriverLib.h"
\r
55 /* The LED's toggled by the various tasks. */
\r
56 #define commsFAIL_LED ( 7 )
\r
57 #define commsRX_LED ( 6 )
\r
58 #define commsTX_LED ( 5 )
\r
60 /* The length of the queue used to pass received characters to the Comms Rx
\r
62 #define commsRX_QUEUE_LEN ( 5 )
\r
64 /* The baud rate used by the UART comms tasks/co-routine. */
\r
65 #define commsBAUD_RATE ( 57600 )
\r
67 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
\r
68 #define commsFIFO_SET ( 0x10 )
\r
70 /* The string that is transmitted on the UART contains sequentially the
\r
71 characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */
\r
72 #define commsFIRST_TX_CHAR '0'
\r
73 #define commsLAST_TX_CHAR 'z'
\r
75 /* Just used to walk through the program memory in order that some random data
\r
76 can be generated. */
\r
77 #define commsTOTAL_PROGRAM_MEMORY ( ( unsigned portLONG * ) ( 8 * 1024 ) )
\r
78 #define commsFIRST_PROGRAM_BYTES ( ( unsigned portLONG * ) 4 )
\r
80 /* The time between transmissions of the string on UART 0. This is pseudo
\r
81 random in order to generate a bit or randomness to when the interrupts occur.*/
\r
82 #define commsMIN_TX_DELAY ( 40 / portTICK_RATE_MS )
\r
83 #define commsMAX_TX_DELAY ( ( portTickType ) 0x7f )
\r
84 #define commsOFFSET_TIME ( ( portTickType ) 3 )
\r
86 /* The time the Comms Rx task should wait to receive a character. This should
\r
87 be slightly longer than the time between transmissions. If we do not receive
\r
88 a character after this time then there must be an error in the transmission or
\r
89 the timing of the transmission. */
\r
90 #define commsRX_DELAY ( commsMAX_TX_DELAY + 20 )
\r
93 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;
\r
95 /* The queue used to pass characters out of the ISR. */
\r
96 static xQueueHandle xCommsQueue;
\r
98 /* The next character to transmit. */
\r
99 static portCHAR cNextChar;
\r
101 /*-----------------------------------------------------------*/
\r
103 void vSerialInit( void )
\r
105 /* Create the queue used to communicate between the UART ISR and the Comms
\r
107 xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( portCHAR ) );
\r
109 /* Enable the UART. GPIOA has already been initialised. */
\r
110 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
112 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
114 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\r
116 /* Configure the UART for 8-N-1 operation. */
\r
117 UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
\r
119 /* We dont want to use the fifo. This is for test purposes to generate
\r
120 as many interrupts as possible. */
\r
121 HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;
\r
123 /* Enable both Rx and Tx interrupts. */
\r
124 HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
\r
125 IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );
\r
126 IntEnable( INT_UART0 );
\r
128 /*-----------------------------------------------------------*/
\r
130 void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
132 portTickType xDelayPeriod;
\r
133 static unsigned portLONG *pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
135 /* Co-routine MUST start with a call to crSTART. */
\r
136 crSTART( xHandle );
\r
140 /* Was the previously transmitted string received correctly? */
\r
141 if( uxCommsErrorStatus != pdPASS )
\r
143 /* An error was encountered so set the error LED. */
\r
144 vParTestSetLED( commsFAIL_LED, pdTRUE );
\r
147 /* The next character to Tx is the first in the string. */
\r
148 cNextChar = commsFIRST_TX_CHAR;
\r
150 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
152 /* Send the first character. */
\r
153 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
155 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
158 /* Move the variable to the char to Tx on so the ISR transmits
\r
159 the next character in the string once this one has completed. */
\r
162 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
164 /* Toggle the LED to show a new string is being transmitted. */
\r
165 vParTestToggleLED( commsTX_LED );
\r
167 /* Delay before we start the string off again. A pseudo-random delay
\r
168 is used as this will provide a better test. */
\r
169 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
\r
172 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )
\r
174 pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
177 /* Make sure we don't wait too long... */
\r
178 xDelayPeriod &= commsMAX_TX_DELAY;
\r
180 /* ...but we do want to wait. */
\r
181 if( xDelayPeriod < commsMIN_TX_DELAY )
\r
183 xDelayPeriod = commsMIN_TX_DELAY;
\r
186 /* Block for the random(ish) time. */
\r
187 crDELAY( xHandle, xDelayPeriod );
\r
190 /* Co-routine MUST end with a call to crEND. */
\r
193 /*-----------------------------------------------------------*/
\r
195 void vUART_ISR( void )
\r
197 unsigned portLONG ulStatus;
\r
198 portCHAR cRxedChar;
\r
199 portBASE_TYPE xTaskWokenByPost = pdFALSE;
\r
201 /* What caused the interrupt. */
\r
202 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
204 /* Clear the interrupt. */
\r
205 UARTIntClear( UART0_BASE, ulStatus );
\r
207 /* Was an Rx interrpt pending? */
\r
208 if( ulStatus & UART_INT_RX )
\r
210 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
\r
212 /* Get the char from the buffer and post it onto the queue of
\r
213 Rxed chars. Posting the character should wake the task that is
\r
214 blocked on the queue waiting for characters. */
\r
215 cRxedChar = ( portCHAR ) HWREG( UART0_BASE + UART_O_DR );
\r
216 xTaskWokenByPost = xQueueSendFromISR( xCommsQueue, &cRxedChar, xTaskWokenByPost );
\r
220 /* Was a Tx interrupt pending? */
\r
221 if( ulStatus & UART_INT_TX )
\r
223 /* Send the next character in the string. We are not using the FIFO. */
\r
224 if( cNextChar <= commsLAST_TX_CHAR )
\r
226 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
228 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
234 if( xTaskWokenByPost )
\r
236 /* If a task was woken by the character being received then we force
\r
237 a context switch to occur in case the task is of higher priority than
\r
238 the currently executing task (i.e. the task that this interrupt
\r
240 portEND_SWITCHING_ISR( xTaskWokenByPost );
\r
243 /*-----------------------------------------------------------*/
\r
245 void vCommsRxTask( void * pvParameters )
\r
247 static portCHAR cRxedChar, cExpectedChar;
\r
249 /* Set the char we expect to receive to the start of the string. */
\r
250 cExpectedChar = commsFIRST_TX_CHAR;
\r
254 /* Wait for a character to be received. */
\r
255 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );
\r
257 /* Was the character recived (if any) the expected character. */
\r
258 if( cRxedChar != cExpectedChar )
\r
260 /* Got an unexpected character. This can sometimes occur when
\r
261 reseting the system using the debugger leaving characters already
\r
262 in the UART regsters. */
\r
263 uxCommsErrorStatus = pdFAIL;
\r
265 /* Resync by waiting for the end of the current string. */
\r
266 while( cRxedChar != commsLAST_TX_CHAR )
\r
268 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
\r
271 /* The next expected character is the start of the string again. */
\r
272 cExpectedChar = commsFIRST_TX_CHAR;
\r
276 if( cExpectedChar == commsLAST_TX_CHAR )
\r
278 /* We have reached the end of the string - we now expect to
\r
279 receive the first character in the string again. The LED is
\r
280 toggled to indicate that the entire string was received without
\r
282 vParTestToggleLED( commsRX_LED );
\r
283 cExpectedChar = commsFIRST_TX_CHAR;
\r
287 /* We got the expected character, we now expect to receive the
\r
288 next character in the string. */
\r
294 /*-----------------------------------------------------------*/
\r
296 unsigned portBASE_TYPE uxGetCommsStatus( void )
\r
298 return uxCommsErrorStatus;
\r