2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* NOTE: This is just a test file and not intended to be a generic
\r
69 #include "altera_avalon_uart.h"
\r
70 #include "altera_avalon_uart_regs.h"
\r
71 #include "sys/alt_irq.h"
\r
73 #include "FreeRTOS.h"
\r
77 /*---------------------------------------------------------------------------*/
\r
79 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
80 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
81 /*---------------------------------------------------------------------------*/
\r
83 static QueueHandle_t xRxedChars;
\r
84 static QueueHandle_t xCharsForTx;
\r
86 alt_u32 uartControl;
\r
87 /*---------------------------------------------------------------------------*/
\r
89 static void vUARTInterruptHandler( void* context, alt_u32 id );
\r
90 static void vUARTReceiveHandler( alt_u32 status );
\r
91 static void vUARTTransmitHandler( alt_u32 status );
\r
92 /*---------------------------------------------------------------------------*/
\r
94 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
96 /* Create the queues used to hold Rx and Tx characters. */
\r
97 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
98 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
100 /* If the queues were created correctly then setup the serial port hardware. */
\r
101 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
103 portENTER_CRITICAL();
\r
105 uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;
\r
106 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
108 /* register the interrupt handler */
\r
109 alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );
\r
111 portEXIT_CRITICAL();
\r
115 return ( xComPortHandle ) 0;
\r
117 return ( xComPortHandle ) 1;
\r
119 /*---------------------------------------------------------------------------*/
\r
121 void vSerialClose( xComPortHandle xPort )
\r
125 /*---------------------------------------------------------------------------*/
\r
127 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
129 /* The port handle is not required as this driver only supports one port. */
\r
133 /* Get the next character from the buffer. Return false if no characters
\r
134 are available, or arrive before xBlockTime expires. */
\r
135 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
141 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
142 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
146 /*---------------------------------------------------------------------------*/
\r
148 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
150 signed portBASE_TYPE lReturn = pdPASS;
\r
152 /* Place the character in the queue of characters to be transmitted. */
\r
153 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
155 /*Triggers an interrupt on every character or (down) when queue is full. */
\r
156 uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
157 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
166 /*---------------------------------------------------------------------------*/
\r
168 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
170 signed char *pxNext;
\r
172 /* A couple of parameters that this port does not use. */
\r
173 ( void ) usStringLength;
\r
176 /* NOTE: This implementation does not handle the queue being full as no block time is used! */
\r
178 /* The port handle is not required as this driver only supports UART0. */
\r
181 /* Send each character in the string, one at a time. */
\r
182 pxNext = ( signed char * ) pcString;
\r
185 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
189 /*-----------------------------------------------------------*/
\r
191 static void vUARTInterruptHandler( void* context, alt_u32 id )
\r
195 /* Read the status register in order to determine the cause of the
\r
197 status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );
\r
199 /* Clear any error flags set at the device */
\r
200 IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );
\r
202 /* process a read irq */
\r
203 if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )
\r
205 vUARTReceiveHandler( status );
\r
208 /* process a write irq */
\r
209 if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK ) )
\r
211 vUARTTransmitHandler( status );
\r
214 /*---------------------------------------------------------------------------*/
\r
216 static void vUARTReceiveHandler( alt_u32 status )
\r
219 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
221 /* If there was an error, discard the data */
\r
222 if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )
\r
228 /* Transfer data from the device to the circular buffer */
\r
229 cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );
\r
230 if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )
\r
232 /* If the circular buffer was full, disable interrupts. Interrupts will
\r
233 be re-enabled when data is removed from the buffer. */
\r
234 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
\r
235 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
238 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
240 /*---------------------------------------------------------------------------*/
\r
242 static void vUARTTransmitHandler( alt_u32 status )
\r
245 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
246 /* Transfer data if there is some ready to be transferred */
\r
247 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
249 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );
\r
253 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
\r
256 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
\r
257 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
259 /*---------------------------------------------------------------------------*/
\r