2 FreeRTOS V5.4.2 - 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 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
48 /* NOTE: This is just a test file and not intended to be a generic
\r
51 #include "altera_avalon_uart.h"
\r
52 #include "altera_avalon_uart_regs.h"
\r
53 #include "sys/alt_irq.h"
\r
55 #include "FreeRTOS.h"
\r
59 /*---------------------------------------------------------------------------*/
\r
61 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
62 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
63 /*---------------------------------------------------------------------------*/
\r
65 static xQueueHandle xRxedChars;
\r
66 static xQueueHandle xCharsForTx;
\r
68 alt_u32 uartControl;
\r
69 /*---------------------------------------------------------------------------*/
\r
71 static void vUARTInterruptHandler( void* context, alt_u32 id );
\r
72 static void vUARTReceiveHandler( alt_u32 status );
\r
73 static void vUARTTransmitHandler( alt_u32 status );
\r
74 /*---------------------------------------------------------------------------*/
\r
76 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
78 /* Create the queues used to hold Rx and Tx characters. */
\r
79 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
80 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
82 /* If the queues were created correctly then setup the serial port hardware. */
\r
83 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
85 portENTER_CRITICAL();
\r
87 uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;
\r
88 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
90 /* register the interrupt handler */
\r
91 alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );
\r
93 portEXIT_CRITICAL();
\r
97 return ( xComPortHandle ) 0;
\r
99 return ( xComPortHandle ) 1;
\r
101 /*---------------------------------------------------------------------------*/
\r
103 void vSerialClose( xComPortHandle xPort )
\r
107 /*---------------------------------------------------------------------------*/
\r
109 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
111 /* The port handle is not required as this driver only supports one port. */
\r
115 /* Get the next character from the buffer. Return false if no characters
\r
116 are available, or arrive before xBlockTime expires. */
\r
117 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
123 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
124 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
128 /*---------------------------------------------------------------------------*/
\r
130 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
132 signed portBASE_TYPE lReturn = pdPASS;
\r
134 /* Place the character in the queue of characters to be transmitted. */
\r
135 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
137 /*Triggers an interrupt on every character or (down) when queue is full. */
\r
138 uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
139 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
148 /*---------------------------------------------------------------------------*/
\r
150 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
152 signed portCHAR *pxNext;
\r
154 /* A couple of parameters that this port does not use. */
\r
155 ( void ) usStringLength;
\r
158 /* NOTE: This implementation does not handle the queue being full as no block time is used! */
\r
160 /* The port handle is not required as this driver only supports UART0. */
\r
163 /* Send each character in the string, one at a time. */
\r
164 pxNext = ( signed portCHAR * ) pcString;
\r
167 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
171 /*-----------------------------------------------------------*/
\r
173 static void vUARTInterruptHandler( void* context, alt_u32 id )
\r
177 /* Read the status register in order to determine the cause of the
\r
179 status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );
\r
181 /* Clear any error flags set at the device */
\r
182 IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );
\r
184 /* process a read irq */
\r
185 if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )
\r
187 vUARTReceiveHandler( status );
\r
190 /* process a write irq */
\r
191 if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK ) )
\r
193 vUARTTransmitHandler( status );
\r
196 /*---------------------------------------------------------------------------*/
\r
198 static void vUARTReceiveHandler( alt_u32 status )
\r
200 signed portCHAR cChar;
\r
201 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
203 /* If there was an error, discard the data */
\r
204 if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )
\r
210 /* Transfer data from the device to the circular buffer */
\r
211 cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );
\r
212 if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )
\r
214 /* If the circular buffer was full, disable interrupts. Interrupts will
\r
215 be re-enabled when data is removed from the buffer. */
\r
216 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
217 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
220 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
222 /*---------------------------------------------------------------------------*/
\r
224 static void vUARTTransmitHandler( alt_u32 status )
\r
226 signed portCHAR cChar;
\r
227 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
228 /* Transfer data if there is some ready to be transferred */
\r
229 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
231 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );
\r
235 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
238 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
239 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
241 /*---------------------------------------------------------------------------*/
\r