2 FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * The comms test Rx and Tx task and co-routine. See the comments at the top
\r
68 * of main.c for full information.
\r
72 /* Scheduler include files. */
\r
73 #include "FreeRTOS.h"
\r
76 #include "croutine.h"
\r
78 /* Demo application include files. */
\r
79 #include "partest.h"
\r
81 /* Library include files. */
\r
82 #include "DriverLib.h"
\r
84 /* The LED's toggled by the various tasks. */
\r
85 #define commsFAIL_LED ( 7 )
\r
86 #define commsRX_LED ( 6 )
\r
87 #define commsTX_LED ( 5 )
\r
89 /* The length of the queue used to pass received characters to the Comms Rx
\r
91 #define commsRX_QUEUE_LEN ( 5 )
\r
93 /* The baud rate used by the UART comms tasks/co-routine. */
\r
94 #define commsBAUD_RATE ( 57600 )
\r
96 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
\r
97 #define commsFIFO_SET ( 0x10 )
\r
99 /* The string that is transmitted on the UART contains sequentially the
\r
100 characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */
\r
101 #define commsFIRST_TX_CHAR '0'
\r
102 #define commsLAST_TX_CHAR 'z'
\r
104 /* Just used to walk through the program memory in order that some random data
\r
105 can be generated. */
\r
106 #define commsTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )
\r
107 #define commsFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )
\r
109 /* The time between transmissions of the string on UART 0. This is pseudo
\r
110 random in order to generate a bit or randomness to when the interrupts occur.*/
\r
111 #define commsMIN_TX_DELAY ( 40 / portTICK_RATE_MS )
\r
112 #define commsMAX_TX_DELAY ( ( portTickType ) 0x7f )
\r
113 #define commsOFFSET_TIME ( ( portTickType ) 3 )
\r
115 /* The time the Comms Rx task should wait to receive a character. This should
\r
116 be slightly longer than the time between transmissions. If we do not receive
\r
117 a character after this time then there must be an error in the transmission or
\r
118 the timing of the transmission. */
\r
119 #define commsRX_DELAY ( commsMAX_TX_DELAY + 20 )
\r
122 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;
\r
124 /* The queue used to pass characters out of the ISR. */
\r
125 static xQueueHandle xCommsQueue;
\r
127 /* The next character to transmit. */
\r
128 static char cNextChar;
\r
130 /*-----------------------------------------------------------*/
\r
132 void vSerialInit( void )
\r
134 /* Create the queue used to communicate between the UART ISR and the Comms
\r
136 xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );
\r
138 /* Enable the UART. GPIOA has already been initialised. */
\r
139 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
141 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
143 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\r
145 /* Configure the UART for 8-N-1 operation. */
\r
146 UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
\r
148 /* We dont want to use the fifo. This is for test purposes to generate
\r
149 as many interrupts as possible. */
\r
150 HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;
\r
152 /* Enable both Rx and Tx interrupts. */
\r
153 HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
\r
154 IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );
\r
155 IntEnable( INT_UART0 );
\r
157 /*-----------------------------------------------------------*/
\r
159 void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
161 portTickType xDelayPeriod;
\r
162 static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
164 /* Co-routine MUST start with a call to crSTART. */
\r
165 crSTART( xHandle );
\r
169 /* Was the previously transmitted string received correctly? */
\r
170 if( uxCommsErrorStatus != pdPASS )
\r
172 /* An error was encountered so set the error LED. */
\r
173 vParTestSetLED( commsFAIL_LED, pdTRUE );
\r
176 /* The next character to Tx is the first in the string. */
\r
177 cNextChar = commsFIRST_TX_CHAR;
\r
179 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
181 /* Send the first character. */
\r
182 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
184 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
187 /* Move the variable to the char to Tx on so the ISR transmits
\r
188 the next character in the string once this one has completed. */
\r
191 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
193 /* Toggle the LED to show a new string is being transmitted. */
\r
194 vParTestToggleLED( commsTX_LED );
\r
196 /* Delay before we start the string off again. A pseudo-random delay
\r
197 is used as this will provide a better test. */
\r
198 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
\r
201 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )
\r
203 pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
206 /* Make sure we don't wait too long... */
\r
207 xDelayPeriod &= commsMAX_TX_DELAY;
\r
209 /* ...but we do want to wait. */
\r
210 if( xDelayPeriod < commsMIN_TX_DELAY )
\r
212 xDelayPeriod = commsMIN_TX_DELAY;
\r
215 /* Block for the random(ish) time. */
\r
216 crDELAY( xHandle, xDelayPeriod );
\r
219 /* Co-routine MUST end with a call to crEND. */
\r
222 /*-----------------------------------------------------------*/
\r
224 void vUART_ISR( void )
\r
226 unsigned long ulStatus;
\r
228 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
230 /* What caused the interrupt. */
\r
231 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
233 /* Clear the interrupt. */
\r
234 UARTIntClear( UART0_BASE, ulStatus );
\r
236 /* Was an Rx interrpt pending? */
\r
237 if( ulStatus & UART_INT_RX )
\r
239 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
\r
241 /* Get the char from the buffer and post it onto the queue of
\r
242 Rxed chars. Posting the character should wake the task that is
\r
243 blocked on the queue waiting for characters. */
\r
244 cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );
\r
245 xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );
\r
249 /* Was a Tx interrupt pending? */
\r
250 if( ulStatus & UART_INT_TX )
\r
252 /* Send the next character in the string. We are not using the FIFO. */
\r
253 if( cNextChar <= commsLAST_TX_CHAR )
\r
255 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
257 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\r
263 /* If a task was woken by the character being received then we force
\r
264 a context switch to occur in case the task is of higher priority than
\r
265 the currently executing task (i.e. the task that this interrupt
\r
267 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
269 /*-----------------------------------------------------------*/
\r
271 void vCommsRxTask( void * pvParameters )
\r
273 static char cRxedChar, cExpectedChar;
\r
275 /* Set the char we expect to receive to the start of the string. */
\r
276 cExpectedChar = commsFIRST_TX_CHAR;
\r
280 /* Wait for a character to be received. */
\r
281 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );
\r
283 /* Was the character recived (if any) the expected character. */
\r
284 if( cRxedChar != cExpectedChar )
\r
286 /* Got an unexpected character. This can sometimes occur when
\r
287 reseting the system using the debugger leaving characters already
\r
288 in the UART regsters. */
\r
289 uxCommsErrorStatus = pdFAIL;
\r
291 /* Resync by waiting for the end of the current string. */
\r
292 while( cRxedChar != commsLAST_TX_CHAR )
\r
294 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
\r
297 /* The next expected character is the start of the string again. */
\r
298 cExpectedChar = commsFIRST_TX_CHAR;
\r
302 if( cExpectedChar == commsLAST_TX_CHAR )
\r
304 /* We have reached the end of the string - we now expect to
\r
305 receive the first character in the string again. The LED is
\r
306 toggled to indicate that the entire string was received without
\r
308 vParTestToggleLED( commsRX_LED );
\r
309 cExpectedChar = commsFIRST_TX_CHAR;
\r
313 /* We got the expected character, we now expect to receive the
\r
314 next character in the string. */
\r
320 /*-----------------------------------------------------------*/
\r
322 unsigned portBASE_TYPE uxGetCommsStatus( void )
\r
324 return uxCommsErrorStatus;
\r