2 FreeRTOS V7.4.2 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* NOTE: This is just a test file and not intended to be a generic
\r
78 #include "altera_avalon_uart.h"
\r
79 #include "altera_avalon_uart_regs.h"
\r
80 #include "sys/alt_irq.h"
\r
82 #include "FreeRTOS.h"
\r
86 /*---------------------------------------------------------------------------*/
\r
88 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
89 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
90 /*---------------------------------------------------------------------------*/
\r
92 static xQueueHandle xRxedChars;
\r
93 static xQueueHandle xCharsForTx;
\r
95 alt_u32 uartControl;
\r
96 /*---------------------------------------------------------------------------*/
\r
98 static void vUARTInterruptHandler( void* context, alt_u32 id );
\r
99 static void vUARTReceiveHandler( alt_u32 status );
\r
100 static void vUARTTransmitHandler( alt_u32 status );
\r
101 /*---------------------------------------------------------------------------*/
\r
103 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
105 /* Create the queues used to hold Rx and Tx characters. */
\r
106 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
107 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
109 /* If the queues were created correctly then setup the serial port hardware. */
\r
110 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
112 portENTER_CRITICAL();
\r
114 uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;
\r
115 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
117 /* register the interrupt handler */
\r
118 alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );
\r
120 portEXIT_CRITICAL();
\r
124 return ( xComPortHandle ) 0;
\r
126 return ( xComPortHandle ) 1;
\r
128 /*---------------------------------------------------------------------------*/
\r
130 void vSerialClose( xComPortHandle xPort )
\r
134 /*---------------------------------------------------------------------------*/
\r
136 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
138 /* The port handle is not required as this driver only supports one port. */
\r
142 /* Get the next character from the buffer. Return false if no characters
\r
143 are available, or arrive before xBlockTime expires. */
\r
144 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
150 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
151 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
155 /*---------------------------------------------------------------------------*/
\r
157 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
159 signed portBASE_TYPE lReturn = pdPASS;
\r
161 /* Place the character in the queue of characters to be transmitted. */
\r
162 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
164 /*Triggers an interrupt on every character or (down) when queue is full. */
\r
165 uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
166 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
175 /*---------------------------------------------------------------------------*/
\r
177 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
179 signed char *pxNext;
\r
181 /* A couple of parameters that this port does not use. */
\r
182 ( void ) usStringLength;
\r
185 /* NOTE: This implementation does not handle the queue being full as no block time is used! */
\r
187 /* The port handle is not required as this driver only supports UART0. */
\r
190 /* Send each character in the string, one at a time. */
\r
191 pxNext = ( signed char * ) pcString;
\r
194 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
198 /*-----------------------------------------------------------*/
\r
200 static void vUARTInterruptHandler( void* context, alt_u32 id )
\r
204 /* Read the status register in order to determine the cause of the
\r
206 status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );
\r
208 /* Clear any error flags set at the device */
\r
209 IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );
\r
211 /* process a read irq */
\r
212 if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )
\r
214 vUARTReceiveHandler( status );
\r
217 /* process a write irq */
\r
218 if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK ) )
\r
220 vUARTTransmitHandler( status );
\r
223 /*---------------------------------------------------------------------------*/
\r
225 static void vUARTReceiveHandler( alt_u32 status )
\r
228 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
230 /* If there was an error, discard the data */
\r
231 if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )
\r
237 /* Transfer data from the device to the circular buffer */
\r
238 cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );
\r
239 if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )
\r
241 /* If the circular buffer was full, disable interrupts. Interrupts will
\r
242 be re-enabled when data is removed from the buffer. */
\r
243 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
244 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
247 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
249 /*---------------------------------------------------------------------------*/
\r
251 static void vUARTTransmitHandler( alt_u32 status )
\r
254 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
255 /* Transfer data if there is some ready to be transferred */
\r
256 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
258 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );
\r
262 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
265 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
266 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
268 /*---------------------------------------------------------------------------*/
\r