2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* NOTE: This is just a test file and not intended to be a generic
\r
68 #include "altera_avalon_uart.h"
\r
69 #include "altera_avalon_uart_regs.h"
\r
70 #include "sys/alt_irq.h"
\r
72 #include "FreeRTOS.h"
\r
76 /*---------------------------------------------------------------------------*/
\r
78 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
79 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
80 /*---------------------------------------------------------------------------*/
\r
82 static xQueueHandle xRxedChars;
\r
83 static xQueueHandle xCharsForTx;
\r
85 alt_u32 uartControl;
\r
86 /*---------------------------------------------------------------------------*/
\r
88 static void vUARTInterruptHandler( void* context, alt_u32 id );
\r
89 static void vUARTReceiveHandler( alt_u32 status );
\r
90 static void vUARTTransmitHandler( alt_u32 status );
\r
91 /*---------------------------------------------------------------------------*/
\r
93 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
95 /* Create the queues used to hold Rx and Tx characters. */
\r
96 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
97 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
99 /* If the queues were created correctly then setup the serial port hardware. */
\r
100 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
102 portENTER_CRITICAL();
\r
104 uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;
\r
105 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
107 /* register the interrupt handler */
\r
108 alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );
\r
110 portEXIT_CRITICAL();
\r
114 return ( xComPortHandle ) 0;
\r
116 return ( xComPortHandle ) 1;
\r
118 /*---------------------------------------------------------------------------*/
\r
120 void vSerialClose( xComPortHandle xPort )
\r
124 /*---------------------------------------------------------------------------*/
\r
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
128 /* The port handle is not required as this driver only supports one port. */
\r
132 /* Get the next character from the buffer. Return false if no characters
\r
133 are available, or arrive before xBlockTime expires. */
\r
134 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
140 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
141 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
145 /*---------------------------------------------------------------------------*/
\r
147 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
149 signed portBASE_TYPE lReturn = pdPASS;
\r
151 /* Place the character in the queue of characters to be transmitted. */
\r
152 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
154 /*Triggers an interrupt on every character or (down) when queue is full. */
\r
155 uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
156 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
165 /*---------------------------------------------------------------------------*/
\r
167 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
169 signed char *pxNext;
\r
171 /* A couple of parameters that this port does not use. */
\r
172 ( void ) usStringLength;
\r
175 /* NOTE: This implementation does not handle the queue being full as no block time is used! */
\r
177 /* The port handle is not required as this driver only supports UART0. */
\r
180 /* Send each character in the string, one at a time. */
\r
181 pxNext = ( signed char * ) pcString;
\r
184 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
188 /*-----------------------------------------------------------*/
\r
190 static void vUARTInterruptHandler( void* context, alt_u32 id )
\r
194 /* Read the status register in order to determine the cause of the
\r
196 status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );
\r
198 /* Clear any error flags set at the device */
\r
199 IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );
\r
201 /* process a read irq */
\r
202 if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )
\r
204 vUARTReceiveHandler( status );
\r
207 /* process a write irq */
\r
208 if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK ) )
\r
210 vUARTTransmitHandler( status );
\r
213 /*---------------------------------------------------------------------------*/
\r
215 static void vUARTReceiveHandler( alt_u32 status )
\r
218 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
220 /* If there was an error, discard the data */
\r
221 if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )
\r
227 /* Transfer data from the device to the circular buffer */
\r
228 cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );
\r
229 if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )
\r
231 /* If the circular buffer was full, disable interrupts. Interrupts will
\r
232 be re-enabled when data is removed from the buffer. */
\r
233 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
234 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
237 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
239 /*---------------------------------------------------------------------------*/
\r
241 static void vUARTTransmitHandler( alt_u32 status )
\r
244 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
245 /* Transfer data if there is some ready to be transferred */
\r
246 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
248 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );
\r
252 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
255 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
256 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
258 /*---------------------------------------------------------------------------*/
\r