2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
49 /* NOTE: This is just a test file and not intended to be a generic
\r
52 #include "altera_avalon_uart.h"
\r
53 #include "altera_avalon_uart_regs.h"
\r
54 #include "sys/alt_irq.h"
\r
56 #include "FreeRTOS.h"
\r
60 /*---------------------------------------------------------------------------*/
\r
62 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
63 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
64 /*---------------------------------------------------------------------------*/
\r
66 static xQueueHandle xRxedChars;
\r
67 static xQueueHandle xCharsForTx;
\r
69 alt_u32 uartControl;
\r
70 /*---------------------------------------------------------------------------*/
\r
72 static void vUARTInterruptHandler( void* context, alt_u32 id );
\r
73 static void vUARTReceiveHandler( alt_u32 status );
\r
74 static void vUARTTransmitHandler( alt_u32 status );
\r
75 /*---------------------------------------------------------------------------*/
\r
77 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
79 /* Create the queues used to hold Rx and Tx characters. */
\r
80 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
81 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
83 /* If the queues were created correctly then setup the serial port hardware. */
\r
84 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
86 portENTER_CRITICAL();
\r
88 uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;
\r
89 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
91 /* register the interrupt handler */
\r
92 alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );
\r
94 portEXIT_CRITICAL();
\r
98 return ( xComPortHandle ) 0;
\r
100 return ( xComPortHandle ) 1;
\r
102 /*---------------------------------------------------------------------------*/
\r
104 void vSerialClose( xComPortHandle xPort )
\r
108 /*---------------------------------------------------------------------------*/
\r
110 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
112 /* The port handle is not required as this driver only supports one port. */
\r
116 /* Get the next character from the buffer. Return false if no characters
\r
117 are available, or arrive before xBlockTime expires. */
\r
118 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
124 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
125 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
129 /*---------------------------------------------------------------------------*/
\r
131 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
133 signed portBASE_TYPE lReturn = pdPASS;
\r
135 /* Place the character in the queue of characters to be transmitted. */
\r
136 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
138 /*Triggers an interrupt on every character or (down) when queue is full. */
\r
139 uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
140 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
149 /*---------------------------------------------------------------------------*/
\r
151 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
153 signed char *pxNext;
\r
155 /* A couple of parameters that this port does not use. */
\r
156 ( void ) usStringLength;
\r
159 /* NOTE: This implementation does not handle the queue being full as no block time is used! */
\r
161 /* The port handle is not required as this driver only supports UART0. */
\r
164 /* Send each character in the string, one at a time. */
\r
165 pxNext = ( signed char * ) pcString;
\r
168 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
172 /*-----------------------------------------------------------*/
\r
174 static void vUARTInterruptHandler( void* context, alt_u32 id )
\r
178 /* Read the status register in order to determine the cause of the
\r
180 status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );
\r
182 /* Clear any error flags set at the device */
\r
183 IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );
\r
185 /* process a read irq */
\r
186 if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )
\r
188 vUARTReceiveHandler( status );
\r
191 /* process a write irq */
\r
192 if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK ) )
\r
194 vUARTTransmitHandler( status );
\r
197 /*---------------------------------------------------------------------------*/
\r
199 static void vUARTReceiveHandler( alt_u32 status )
\r
202 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
204 /* If there was an error, discard the data */
\r
205 if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )
\r
211 /* Transfer data from the device to the circular buffer */
\r
212 cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );
\r
213 if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )
\r
215 /* If the circular buffer was full, disable interrupts. Interrupts will
\r
216 be re-enabled when data is removed from the buffer. */
\r
217 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
218 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
221 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
223 /*---------------------------------------------------------------------------*/
\r
225 static void vUARTTransmitHandler( alt_u32 status )
\r
228 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
229 /* Transfer data if there is some ready to be transferred */
\r
230 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
232 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );
\r
236 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
239 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
240 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
242 /*---------------------------------------------------------------------------*/
\r