1 |
584 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/* NOTE: This is just a test file and not intended to be a generic
|
55 |
|
|
COM driver. */
|
56 |
|
|
|
57 |
|
|
#include "altera_avalon_uart.h"
|
58 |
|
|
#include "altera_avalon_uart_regs.h"
|
59 |
|
|
#include "sys/alt_irq.h"
|
60 |
|
|
|
61 |
|
|
#include "FreeRTOS.h"
|
62 |
|
|
#include "queue.h"
|
63 |
|
|
#include "system.h"
|
64 |
|
|
#include "Serial.h"
|
65 |
|
|
/*---------------------------------------------------------------------------*/
|
66 |
|
|
|
67 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
68 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
69 |
|
|
/*---------------------------------------------------------------------------*/
|
70 |
|
|
|
71 |
|
|
static xQueueHandle xRxedChars;
|
72 |
|
|
static xQueueHandle xCharsForTx;
|
73 |
|
|
|
74 |
|
|
alt_u32 uartControl;
|
75 |
|
|
/*---------------------------------------------------------------------------*/
|
76 |
|
|
|
77 |
|
|
static void vUARTInterruptHandler( void* context, alt_u32 id );
|
78 |
|
|
static void vUARTReceiveHandler( alt_u32 status );
|
79 |
|
|
static void vUARTTransmitHandler( alt_u32 status );
|
80 |
|
|
/*---------------------------------------------------------------------------*/
|
81 |
|
|
|
82 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
83 |
|
|
{
|
84 |
|
|
/* Create the queues used to hold Rx and Tx characters. */
|
85 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
86 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
87 |
|
|
|
88 |
|
|
/* If the queues were created correctly then setup the serial port hardware. */
|
89 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
|
90 |
|
|
{
|
91 |
|
|
portENTER_CRITICAL();
|
92 |
|
|
{
|
93 |
|
|
uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;
|
94 |
|
|
IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
|
95 |
|
|
|
96 |
|
|
/* register the interrupt handler */
|
97 |
|
|
alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );
|
98 |
|
|
}
|
99 |
|
|
portEXIT_CRITICAL();
|
100 |
|
|
}
|
101 |
|
|
else
|
102 |
|
|
{
|
103 |
|
|
return ( xComPortHandle ) 0;
|
104 |
|
|
}
|
105 |
|
|
return ( xComPortHandle ) 1;
|
106 |
|
|
}
|
107 |
|
|
/*---------------------------------------------------------------------------*/
|
108 |
|
|
|
109 |
|
|
void vSerialClose( xComPortHandle xPort )
|
110 |
|
|
{
|
111 |
|
|
/* Never used. */
|
112 |
|
|
}
|
113 |
|
|
/*---------------------------------------------------------------------------*/
|
114 |
|
|
|
115 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
116 |
|
|
{
|
117 |
|
|
/* The port handle is not required as this driver only supports one port. */
|
118 |
|
|
( void ) pxPort;
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
122 |
|
|
are available, or arrive before xBlockTime expires. */
|
123 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
124 |
|
|
{
|
125 |
|
|
return pdTRUE;
|
126 |
|
|
}
|
127 |
|
|
else
|
128 |
|
|
{
|
129 |
|
|
uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
|
130 |
|
|
IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
|
131 |
|
|
return pdFALSE;
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
/*---------------------------------------------------------------------------*/
|
135 |
|
|
|
136 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
137 |
|
|
{
|
138 |
|
|
signed portBASE_TYPE lReturn = pdPASS;
|
139 |
|
|
|
140 |
|
|
/* Place the character in the queue of characters to be transmitted. */
|
141 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
|
142 |
|
|
{
|
143 |
|
|
/*Triggers an interrupt on every character or (down) when queue is full. */
|
144 |
|
|
uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
|
145 |
|
|
IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
|
146 |
|
|
lReturn = pdPASS;
|
147 |
|
|
}
|
148 |
|
|
else
|
149 |
|
|
{
|
150 |
|
|
lReturn = pdFAIL;
|
151 |
|
|
}
|
152 |
|
|
return lReturn;
|
153 |
|
|
}
|
154 |
|
|
/*---------------------------------------------------------------------------*/
|
155 |
|
|
|
156 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
|
157 |
|
|
{
|
158 |
|
|
signed char *pxNext;
|
159 |
|
|
|
160 |
|
|
/* A couple of parameters that this port does not use. */
|
161 |
|
|
( void ) usStringLength;
|
162 |
|
|
( void ) pxPort;
|
163 |
|
|
|
164 |
|
|
/* NOTE: This implementation does not handle the queue being full as no block time is used! */
|
165 |
|
|
|
166 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
167 |
|
|
( void ) pxPort;
|
168 |
|
|
|
169 |
|
|
/* Send each character in the string, one at a time. */
|
170 |
|
|
pxNext = ( signed char * ) pcString;
|
171 |
|
|
while( *pxNext )
|
172 |
|
|
{
|
173 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
174 |
|
|
pxNext++;
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
/*-----------------------------------------------------------*/
|
178 |
|
|
|
179 |
|
|
static void vUARTInterruptHandler( void* context, alt_u32 id )
|
180 |
|
|
{
|
181 |
|
|
alt_u32 status;
|
182 |
|
|
|
183 |
|
|
/* Read the status register in order to determine the cause of the
|
184 |
|
|
interrupt. */
|
185 |
|
|
status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );
|
186 |
|
|
|
187 |
|
|
/* Clear any error flags set at the device */
|
188 |
|
|
IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );
|
189 |
|
|
|
190 |
|
|
/* process a read irq */
|
191 |
|
|
if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )
|
192 |
|
|
{
|
193 |
|
|
vUARTReceiveHandler( status );
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
/* process a write irq */
|
197 |
|
|
if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK ) )
|
198 |
|
|
{
|
199 |
|
|
vUARTTransmitHandler( status );
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
/*---------------------------------------------------------------------------*/
|
203 |
|
|
|
204 |
|
|
static void vUARTReceiveHandler( alt_u32 status )
|
205 |
|
|
{
|
206 |
|
|
signed char cChar;
|
207 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
208 |
|
|
|
209 |
|
|
/* If there was an error, discard the data */
|
210 |
|
|
if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )
|
211 |
|
|
{
|
212 |
|
|
asm("break");
|
213 |
|
|
return;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
/* Transfer data from the device to the circular buffer */
|
217 |
|
|
cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );
|
218 |
|
|
if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )
|
219 |
|
|
{
|
220 |
|
|
/* If the circular buffer was full, disable interrupts. Interrupts will
|
221 |
|
|
be re-enabled when data is removed from the buffer. */
|
222 |
|
|
uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
|
223 |
|
|
IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
227 |
|
|
}
|
228 |
|
|
/*---------------------------------------------------------------------------*/
|
229 |
|
|
|
230 |
|
|
static void vUARTTransmitHandler( alt_u32 status )
|
231 |
|
|
{
|
232 |
|
|
signed char cChar;
|
233 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
234 |
|
|
/* Transfer data if there is some ready to be transferred */
|
235 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
|
236 |
|
|
{
|
237 |
|
|
IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );
|
238 |
|
|
}
|
239 |
|
|
else
|
240 |
|
|
{
|
241 |
|
|
uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );
|
245 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
246 |
|
|
}
|
247 |
|
|
/*---------------------------------------------------------------------------*/
|