2 FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* NOTE: This is just a test file and not intended to be a generic
\r
57 #include "altera_avalon_uart.h"
\r
58 #include "altera_avalon_uart_regs.h"
\r
59 #include "sys/alt_irq.h"
\r
61 #include "FreeRTOS.h"
\r
65 /*---------------------------------------------------------------------------*/
\r
67 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
68 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
69 /*---------------------------------------------------------------------------*/
\r
71 static xQueueHandle xRxedChars;
\r
72 static xQueueHandle xCharsForTx;
\r
74 alt_u32 uartControl;
\r
75 /*---------------------------------------------------------------------------*/
\r
77 static void vUARTInterruptHandler( void* context, alt_u32 id );
\r
78 static void vUARTReceiveHandler( alt_u32 status );
\r
79 static void vUARTTransmitHandler( alt_u32 status );
\r
80 /*---------------------------------------------------------------------------*/
\r
82 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
84 /* Create the queues used to hold Rx and Tx characters. */
\r
85 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
86 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
88 /* If the queues were created correctly then setup the serial port hardware. */
\r
89 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
91 portENTER_CRITICAL();
\r
93 uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;
\r
94 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
96 /* register the interrupt handler */
\r
97 alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );
\r
99 portEXIT_CRITICAL();
\r
103 return ( xComPortHandle ) 0;
\r
105 return ( xComPortHandle ) 1;
\r
107 /*---------------------------------------------------------------------------*/
\r
109 void vSerialClose( xComPortHandle xPort )
\r
113 /*---------------------------------------------------------------------------*/
\r
115 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
117 /* The port handle is not required as this driver only supports one port. */
\r
121 /* Get the next character from the buffer. Return false if no characters
\r
122 are available, or arrive before xBlockTime expires. */
\r
123 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
129 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
130 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
134 /*---------------------------------------------------------------------------*/
\r
136 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
138 signed portBASE_TYPE lReturn = pdPASS;
\r
140 /* Place the character in the queue of characters to be transmitted. */
\r
141 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
143 /*Triggers an interrupt on every character or (down) when queue is full. */
\r
144 uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
145 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
154 /*---------------------------------------------------------------------------*/
\r
156 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
158 signed char *pxNext;
\r
160 /* A couple of parameters that this port does not use. */
\r
161 ( void ) usStringLength;
\r
164 /* NOTE: This implementation does not handle the queue being full as no block time is used! */
\r
166 /* The port handle is not required as this driver only supports UART0. */
\r
169 /* Send each character in the string, one at a time. */
\r
170 pxNext = ( signed char * ) pcString;
\r
173 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
177 /*-----------------------------------------------------------*/
\r
179 static void vUARTInterruptHandler( void* context, alt_u32 id )
\r
183 /* Read the status register in order to determine the cause of the
\r
185 status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );
\r
187 /* Clear any error flags set at the device */
\r
188 IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );
\r
190 /* process a read irq */
\r
191 if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )
\r
193 vUARTReceiveHandler( status );
\r
196 /* process a write irq */
\r
197 if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK ) )
\r
199 vUARTTransmitHandler( status );
\r
202 /*---------------------------------------------------------------------------*/
\r
204 static void vUARTReceiveHandler( alt_u32 status )
\r
207 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
209 /* If there was an error, discard the data */
\r
210 if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )
\r
216 /* Transfer data from the device to the circular buffer */
\r
217 cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );
\r
218 if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )
\r
220 /* If the circular buffer was full, disable interrupts. Interrupts will
\r
221 be re-enabled when data is removed from the buffer. */
\r
222 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
223 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
226 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
228 /*---------------------------------------------------------------------------*/
\r
230 static void vUARTTransmitHandler( alt_u32 status )
\r
233 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
234 /* Transfer data if there is some ready to be transferred */
\r
235 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
237 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );
\r
241 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
244 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
245 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
247 /*---------------------------------------------------------------------------*/
\r