2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * The comms test Rx and Tx task and co-routine. See the comments at the top
\r
71 * of main.c for full information.
\r
75 /* Scheduler include files. */
\r
76 #include "FreeRTOS.h"
\r
79 #include "croutine.h"
\r
81 /* Demo application include files. */
\r
82 #include "partest.h"
\r
84 /* Library include files. */
\r
85 #include "DriverLib.h"
\r
87 /* The LED's toggled by the various tasks. */
\r
88 #define commsFAIL_LED ( 7 )
\r
89 #define commsRX_LED ( 6 )
\r
90 #define commsTX_LED ( 5 )
\r
92 /* The length of the queue used to pass received characters to the Comms Rx
\r
94 #define commsRX_QUEUE_LEN ( 5 )
\r
96 /* The baud rate used by the UART comms tasks/co-routine. */
\r
97 #define commsBAUD_RATE ( 57600 )
\r
99 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
\r
100 #define commsFIFO_SET ( 0x10 )
\r
102 /* The string that is transmitted on the UART contains sequentially the
\r
103 characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */
\r
104 #define commsFIRST_TX_CHAR '0'
\r
105 #define commsLAST_TX_CHAR 'z'
\r
107 /* Just used to walk through the program memory in order that some random data
\r
108 can be generated. */
\r
109 #define commsTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )
\r
110 #define commsFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )
\r
112 /* The time between transmissions of the string on UART 0. This is pseudo
\r
113 random in order to generate a bit or randomness to when the interrupts occur.*/
\r
114 #define commsMIN_TX_DELAY ( 40 / portTICK_RATE_MS )
\r
115 #define commsMAX_TX_DELAY ( ( portTickType ) 0x7f )
\r
116 #define commsOFFSET_TIME ( ( portTickType ) 3 )
\r
118 /* The time the Comms Rx task should wait to receive a character. This should
\r
119 be slightly longer than the time between transmissions. If we do not receive
\r
120 a character after this time then there must be an error in the transmission or
\r
121 the timing of the transmission. */
\r
122 #define commsRX_DELAY ( commsMAX_TX_DELAY + 20 )
\r
125 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;
\r
127 /* The queue used to pass characters out of the ISR. */
\r
128 static xQueueHandle xCommsQueue;
\r
130 /* The next character to transmit. */
\r
131 static char cNextChar;
\r
133 /*-----------------------------------------------------------*/
\r
135 void vSerialInit( void )
\r
137 /* Create the queue used to communicate between the UART ISR and the Comms
\r
139 xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );
\r
141 /* Enable the UART. GPIOA has already been initialised. */
\r
142 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
144 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
146 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\r
148 /* Configure the UART for 8-N-1 operation. */
\r
149 UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
\r
151 /* We dont want to use the fifo. This is for test purposes to generate
\r
152 as many interrupts as possible. */
\r
153 HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;
\r
155 /* Enable both Rx and Tx interrupts. */
\r
156 HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
\r
157 IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );
\r
158 IntEnable( INT_UART0 );
\r
160 /*-----------------------------------------------------------*/
\r
162 void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
164 portTickType xDelayPeriod;
\r
165 static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
167 /* Co-routine MUST start with a call to crSTART. */
\r
168 crSTART( xHandle );
\r
172 /* Was the previously transmitted string received correctly? */
\r
173 if( uxCommsErrorStatus != pdPASS )
\r
175 /* An error was encountered so set the error LED. */
\r
176 vParTestSetLED( commsFAIL_LED, pdTRUE );
\r
179 /* The next character to Tx is the first in the string. */
\r
180 cNextChar = commsFIRST_TX_CHAR;
\r
182 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
184 /* Send the first character. */
\r
185 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
187 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
190 /* Move the variable to the char to Tx on so the ISR transmits
\r
191 the next character in the string once this one has completed. */
\r
194 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
196 /* Toggle the LED to show a new string is being transmitted. */
\r
197 vParTestToggleLED( commsTX_LED );
\r
199 /* Delay before we start the string off again. A pseudo-random delay
\r
200 is used as this will provide a better test. */
\r
201 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
\r
204 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )
\r
206 pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
209 /* Make sure we don't wait too long... */
\r
210 xDelayPeriod &= commsMAX_TX_DELAY;
\r
212 /* ...but we do want to wait. */
\r
213 if( xDelayPeriod < commsMIN_TX_DELAY )
\r
215 xDelayPeriod = commsMIN_TX_DELAY;
\r
218 /* Block for the random(ish) time. */
\r
219 crDELAY( xHandle, xDelayPeriod );
\r
222 /* Co-routine MUST end with a call to crEND. */
\r
225 /*-----------------------------------------------------------*/
\r
227 void vUART_ISR( void )
\r
229 unsigned long ulStatus;
\r
231 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
233 /* What caused the interrupt. */
\r
234 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
236 /* Clear the interrupt. */
\r
237 UARTIntClear( UART0_BASE, ulStatus );
\r
239 /* Was an Rx interrpt pending? */
\r
240 if( ulStatus & UART_INT_RX )
\r
242 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
\r
244 /* Get the char from the buffer and post it onto the queue of
\r
245 Rxed chars. Posting the character should wake the task that is
\r
246 blocked on the queue waiting for characters. */
\r
247 cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );
\r
248 xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );
\r
252 /* Was a Tx interrupt pending? */
\r
253 if( ulStatus & UART_INT_TX )
\r
255 /* Send the next character in the string. We are not using the FIFO. */
\r
256 if( cNextChar <= commsLAST_TX_CHAR )
\r
258 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
260 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
266 /* If a task was woken by the character being received then we force
\r
267 a context switch to occur in case the task is of higher priority than
\r
268 the currently executing task (i.e. the task that this interrupt
\r
270 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
272 /*-----------------------------------------------------------*/
\r
274 void vCommsRxTask( void * pvParameters )
\r
276 static char cRxedChar, cExpectedChar;
\r
278 /* Set the char we expect to receive to the start of the string. */
\r
279 cExpectedChar = commsFIRST_TX_CHAR;
\r
283 /* Wait for a character to be received. */
\r
284 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );
\r
286 /* Was the character recived (if any) the expected character. */
\r
287 if( cRxedChar != cExpectedChar )
\r
289 /* Got an unexpected character. This can sometimes occur when
\r
290 reseting the system using the debugger leaving characters already
\r
291 in the UART regsters. */
\r
292 uxCommsErrorStatus = pdFAIL;
\r
294 /* Resync by waiting for the end of the current string. */
\r
295 while( cRxedChar != commsLAST_TX_CHAR )
\r
297 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
\r
300 /* The next expected character is the start of the string again. */
\r
301 cExpectedChar = commsFIRST_TX_CHAR;
\r
305 if( cExpectedChar == commsLAST_TX_CHAR )
\r
307 /* We have reached the end of the string - we now expect to
\r
308 receive the first character in the string again. The LED is
\r
309 toggled to indicate that the entire string was received without
\r
311 vParTestToggleLED( commsRX_LED );
\r
312 cExpectedChar = commsFIRST_TX_CHAR;
\r
316 /* We got the expected character, we now expect to receive the
\r
317 next character in the string. */
\r
323 /*-----------------------------------------------------------*/
\r
325 unsigned portBASE_TYPE uxGetCommsStatus( void )
\r
327 return uxCommsErrorStatus;
\r