2 FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * The comms test Rx and Tx task and co-routine. See the comments at the top
\r
56 * of main.c for full information.
\r
60 /* Scheduler include files. */
\r
61 #include "FreeRTOS.h"
\r
64 #include "croutine.h"
\r
66 /* Demo application include files. */
\r
67 #include "partest.h"
\r
69 /* Library include files. */
\r
70 #include "DriverLib.h"
\r
72 /* The LED's toggled by the various tasks. */
\r
73 #define commsFAIL_LED ( 7 )
\r
74 #define commsRX_LED ( 6 )
\r
75 #define commsTX_LED ( 5 )
\r
77 /* The length of the queue used to pass received characters to the Comms Rx
\r
79 #define commsRX_QUEUE_LEN ( 5 )
\r
81 /* The baud rate used by the UART comms tasks/co-routine. */
\r
82 #define commsBAUD_RATE ( 57600 )
\r
84 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
\r
85 #define commsFIFO_SET ( 0x10 )
\r
87 /* The string that is transmitted on the UART contains sequentially the
\r
88 characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */
\r
89 #define commsFIRST_TX_CHAR '0'
\r
90 #define commsLAST_TX_CHAR 'z'
\r
92 /* Just used to walk through the program memory in order that some random data
\r
93 can be generated. */
\r
94 #define commsTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )
\r
95 #define commsFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )
\r
97 /* The time between transmissions of the string on UART 0. This is pseudo
\r
98 random in order to generate a bit or randomness to when the interrupts occur.*/
\r
99 #define commsMIN_TX_DELAY ( 40 / portTICK_RATE_MS )
\r
100 #define commsMAX_TX_DELAY ( ( portTickType ) 0x7f )
\r
101 #define commsOFFSET_TIME ( ( portTickType ) 3 )
\r
103 /* The time the Comms Rx task should wait to receive a character. This should
\r
104 be slightly longer than the time between transmissions. If we do not receive
\r
105 a character after this time then there must be an error in the transmission or
\r
106 the timing of the transmission. */
\r
107 #define commsRX_DELAY ( commsMAX_TX_DELAY + 20 )
\r
110 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;
\r
112 /* The queue used to pass characters out of the ISR. */
\r
113 static xQueueHandle xCommsQueue;
\r
115 /* The next character to transmit. */
\r
116 static char cNextChar;
\r
118 /*-----------------------------------------------------------*/
\r
120 void vSerialInit( void )
\r
122 /* Create the queue used to communicate between the UART ISR and the Comms
\r
124 xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );
\r
126 /* Enable the UART. GPIOA has already been initialised. */
\r
127 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
129 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
131 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\r
133 /* Configure the UART for 8-N-1 operation. */
\r
134 UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
\r
136 /* We dont want to use the fifo. This is for test purposes to generate
\r
137 as many interrupts as possible. */
\r
138 HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;
\r
140 /* Enable both Rx and Tx interrupts. */
\r
141 HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
\r
142 IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );
\r
143 IntEnable( INT_UART0 );
\r
145 /*-----------------------------------------------------------*/
\r
147 void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
149 portTickType xDelayPeriod;
\r
150 static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
152 /* Co-routine MUST start with a call to crSTART. */
\r
153 crSTART( xHandle );
\r
157 /* Was the previously transmitted string received correctly? */
\r
158 if( uxCommsErrorStatus != pdPASS )
\r
160 /* An error was encountered so set the error LED. */
\r
161 vParTestSetLED( commsFAIL_LED, pdTRUE );
\r
164 /* The next character to Tx is the first in the string. */
\r
165 cNextChar = commsFIRST_TX_CHAR;
\r
167 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
169 /* Send the first character. */
\r
170 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
172 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
175 /* Move the variable to the char to Tx on so the ISR transmits
\r
176 the next character in the string once this one has completed. */
\r
179 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
181 /* Toggle the LED to show a new string is being transmitted. */
\r
182 vParTestToggleLED( commsTX_LED );
\r
184 /* Delay before we start the string off again. A pseudo-random delay
\r
185 is used as this will provide a better test. */
\r
186 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
\r
189 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )
\r
191 pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
194 /* Make sure we don't wait too long... */
\r
195 xDelayPeriod &= commsMAX_TX_DELAY;
\r
197 /* ...but we do want to wait. */
\r
198 if( xDelayPeriod < commsMIN_TX_DELAY )
\r
200 xDelayPeriod = commsMIN_TX_DELAY;
\r
203 /* Block for the random(ish) time. */
\r
204 crDELAY( xHandle, xDelayPeriod );
\r
207 /* Co-routine MUST end with a call to crEND. */
\r
210 /*-----------------------------------------------------------*/
\r
212 void vUART_ISR( void )
\r
214 unsigned long ulStatus;
\r
216 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
218 /* What caused the interrupt. */
\r
219 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
221 /* Clear the interrupt. */
\r
222 UARTIntClear( UART0_BASE, ulStatus );
\r
224 /* Was an Rx interrpt pending? */
\r
225 if( ulStatus & UART_INT_RX )
\r
227 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
\r
229 /* Get the char from the buffer and post it onto the queue of
\r
230 Rxed chars. Posting the character should wake the task that is
\r
231 blocked on the queue waiting for characters. */
\r
232 cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );
\r
233 xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );
\r
237 /* Was a Tx interrupt pending? */
\r
238 if( ulStatus & UART_INT_TX )
\r
240 /* Send the next character in the string. We are not using the FIFO. */
\r
241 if( cNextChar <= commsLAST_TX_CHAR )
\r
243 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
245 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
251 /* If a task was woken by the character being received then we force
\r
252 a context switch to occur in case the task is of higher priority than
\r
253 the currently executing task (i.e. the task that this interrupt
\r
255 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
257 /*-----------------------------------------------------------*/
\r
259 void vCommsRxTask( void * pvParameters )
\r
261 static char cRxedChar, cExpectedChar;
\r
263 /* Set the char we expect to receive to the start of the string. */
\r
264 cExpectedChar = commsFIRST_TX_CHAR;
\r
268 /* Wait for a character to be received. */
\r
269 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );
\r
271 /* Was the character recived (if any) the expected character. */
\r
272 if( cRxedChar != cExpectedChar )
\r
274 /* Got an unexpected character. This can sometimes occur when
\r
275 reseting the system using the debugger leaving characters already
\r
276 in the UART regsters. */
\r
277 uxCommsErrorStatus = pdFAIL;
\r
279 /* Resync by waiting for the end of the current string. */
\r
280 while( cRxedChar != commsLAST_TX_CHAR )
\r
282 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
\r
285 /* The next expected character is the start of the string again. */
\r
286 cExpectedChar = commsFIRST_TX_CHAR;
\r
290 if( cExpectedChar == commsLAST_TX_CHAR )
\r
292 /* We have reached the end of the string - we now expect to
\r
293 receive the first character in the string again. The LED is
\r
294 toggled to indicate that the entire string was received without
\r
296 vParTestToggleLED( commsRX_LED );
\r
297 cExpectedChar = commsFIRST_TX_CHAR;
\r
301 /* We got the expected character, we now expect to receive the
\r
302 next character in the string. */
\r
308 /*-----------------------------------------------------------*/
\r
310 unsigned portBASE_TYPE uxGetCommsStatus( void )
\r
312 return uxCommsErrorStatus;
\r