1 |
584 |
jeremybenn |
//--------------------------------------------------------------------------//
|
2 |
|
|
//----------------------- COPIED FROM AVR32 EXAMPLE ------------------------//
|
3 |
|
|
//--------------------------------------------------------------------------//
|
4 |
|
|
|
5 |
|
|
/*This file has been prepared for Doxygen automatic documentation generation.*/
|
6 |
|
|
/*! \file *********************************************************************
|
7 |
|
|
*
|
8 |
|
|
* \brief FreeRTOS Serial Port management example for AVR32 UC3.
|
9 |
|
|
*
|
10 |
|
|
* - Compiler: IAR EWAVR32 and GNU GCC for AVR32
|
11 |
|
|
* - Supported devices: All AVR32 devices can be used.
|
12 |
|
|
* - AppNote:
|
13 |
|
|
*
|
14 |
|
|
* \author Atmel Corporation: http://www.atmel.com \n
|
15 |
|
|
* Support and FAQ: http://support.atmel.no/
|
16 |
|
|
*
|
17 |
|
|
*****************************************************************************/
|
18 |
|
|
|
19 |
|
|
/* Copyright (c) 2007, Atmel Corporation All rights reserved.
|
20 |
|
|
*
|
21 |
|
|
* Redistribution and use in source and binary forms, with or without
|
22 |
|
|
* modification, are permitted provided that the following conditions are met:
|
23 |
|
|
*
|
24 |
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
25 |
|
|
* this list of conditions and the following disclaimer.
|
26 |
|
|
*
|
27 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
28 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
29 |
|
|
* and/or other materials provided with the distribution.
|
30 |
|
|
*
|
31 |
|
|
* 3. The name of ATMEL may not be used to endorse or promote products derived
|
32 |
|
|
* from this software without specific prior written permission.
|
33 |
|
|
*
|
34 |
|
|
* THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
35 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
36 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
|
37 |
|
|
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
|
38 |
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
39 |
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
40 |
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
41 |
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
42 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
43 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
44 |
|
|
*/
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
/*
|
48 |
|
|
BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART.
|
49 |
|
|
*/
|
50 |
|
|
|
51 |
|
|
/* Scheduler includes. */
|
52 |
|
|
#include "FreeRTOS.h"
|
53 |
|
|
#include "queue.h"
|
54 |
|
|
#include "task.h"
|
55 |
|
|
|
56 |
|
|
/* Demo application includes. */
|
57 |
|
|
#include "serial.h"
|
58 |
636 |
filepang |
|
59 |
|
|
/* bsp includes. */
|
60 |
|
|
#include "support.h"
|
61 |
|
|
#include "spr_defs.h"
|
62 |
584 |
jeremybenn |
#include "uart.h"
|
63 |
636 |
filepang |
#include "interrupts.h"
|
64 |
584 |
jeremybenn |
|
65 |
|
|
/*-----------------------------------------------------------*/
|
66 |
|
|
|
67 |
|
|
/* Constants to setup and access the USART. */
|
68 |
|
|
#define serINVALID_COMPORT_HANDLER ( ( xComPortHandle ) 0 )
|
69 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
70 |
|
|
#define serHANDLE ( ( xComPortHandle ) 1 )
|
71 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
72 |
|
|
|
73 |
|
|
/*-----------------------------------------------------------*/
|
74 |
|
|
|
75 |
|
|
/* Queues used to hold received characters, and characters waiting to be
|
76 |
|
|
transmitted. */
|
77 |
|
|
static xQueueHandle xRxedChars;
|
78 |
|
|
static xQueueHandle xCharsForTx;
|
79 |
|
|
|
80 |
|
|
/*-----------------------------------------------------------*/
|
81 |
|
|
|
82 |
|
|
/* Forward declaration. */
|
83 |
|
|
static void vprvSerialCreateQueues( unsigned portBASE_TYPE uxQueueLength,
|
84 |
|
|
xQueueHandle *pxRxedChars,
|
85 |
|
|
xQueueHandle *pxCharsForTx );
|
86 |
|
|
|
87 |
|
|
/*-----------------------------------------------------------*/
|
88 |
636 |
filepang |
static void vUSART_ISR( void *arg )
|
89 |
584 |
jeremybenn |
{
|
90 |
|
|
/* Now we can declare the local variables. */
|
91 |
636 |
filepang |
arg = arg;
|
92 |
|
|
signed portCHAR cChar;
|
93 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
94 |
|
|
unsigned portLONG ulStatus;
|
95 |
584 |
jeremybenn |
portBASE_TYPE retstatus;
|
96 |
|
|
|
97 |
|
|
/* What caused the interrupt? */
|
98 |
636 |
filepang |
ulStatus = uart_get_iir(0);
|
99 |
584 |
jeremybenn |
|
100 |
636 |
filepang |
// TX RADY INTERRUPT
|
101 |
|
|
if (ulStatus & UART_IIR_THRI)
|
102 |
584 |
jeremybenn |
{
|
103 |
|
|
/* The interrupt was caused by the THR becoming empty. Are there any
|
104 |
|
|
more characters to transmit?
|
105 |
|
|
Because FreeRTOS is not supposed to run with nested interrupts, put all OS
|
106 |
|
|
calls in a critical section . */
|
107 |
636 |
filepang |
|
108 |
649 |
filepang |
/* entering, exiting ciritical section around xQueueReceiveFromISR is not
|
109 |
|
|
required. OpenRISC automaticaly disable interrupt when expection occurs */
|
110 |
636 |
filepang |
retstatus = xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken );
|
111 |
584 |
jeremybenn |
|
112 |
|
|
if (retstatus == pdTRUE)
|
113 |
|
|
{
|
114 |
|
|
/* A character was retrieved from the queue so can be sent to the
|
115 |
|
|
THR now. */
|
116 |
636 |
filepang |
uart_putc_noblock(0, cChar);
|
117 |
584 |
jeremybenn |
}
|
118 |
|
|
else
|
119 |
|
|
{
|
120 |
|
|
/* Queue empty, nothing to send so turn off the Tx interrupt. */
|
121 |
636 |
filepang |
uart_txint_disable(0);
|
122 |
584 |
jeremybenn |
}
|
123 |
|
|
}
|
124 |
|
|
|
125 |
636 |
filepang |
// RX RADY INTERRUPT
|
126 |
|
|
if (ulStatus & UART_IIR_RDI)
|
127 |
584 |
jeremybenn |
{
|
128 |
|
|
/* The interrupt was caused by the receiver getting data. */
|
129 |
636 |
filepang |
cChar = uart_getc_noblock(0);
|
130 |
584 |
jeremybenn |
|
131 |
|
|
/* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
|
132 |
649 |
filepang |
calls in a critical section . but in case of OpenRISC, it is not required. Tick
|
133 |
|
|
, External interrupt are automaticaly disabled. */
|
134 |
|
|
xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);
|
135 |
584 |
jeremybenn |
}
|
136 |
|
|
|
137 |
|
|
/* The return value will be used by portEXIT_SWITCHING_ISR() to know if it
|
138 |
|
|
should perform a vTaskSwitchContext(). */
|
139 |
636 |
filepang |
// return ( xHigherPriorityTaskWoken );
|
140 |
584 |
jeremybenn |
}
|
141 |
|
|
/*-----------------------------------------------------------*/
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
/*
|
145 |
|
|
* Init the serial port for the Minimal implementation.
|
146 |
|
|
*/
|
147 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
148 |
|
|
{
|
149 |
|
|
xComPortHandle xReturn = serHANDLE;
|
150 |
|
|
|
151 |
|
|
/* Create the rx and tx queues. */
|
152 |
|
|
vprvSerialCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );
|
153 |
|
|
|
154 |
|
|
/* Configure USART. */
|
155 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) &&
|
156 |
|
|
( xCharsForTx != serINVALID_QUEUE ) &&
|
157 |
|
|
( ulWantedBaud != ( unsigned portLONG ) 0 ) )
|
158 |
|
|
{
|
159 |
|
|
portENTER_CRITICAL();
|
160 |
|
|
{
|
161 |
636 |
filepang |
// register interrupt handler
|
162 |
|
|
int_add(UART0_IRQ, vUSART_ISR, 0x0);
|
163 |
584 |
jeremybenn |
}
|
164 |
|
|
portEXIT_CRITICAL();
|
165 |
|
|
}
|
166 |
|
|
else
|
167 |
|
|
{
|
168 |
|
|
xReturn = serINVALID_COMPORT_HANDLER;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
return xReturn;
|
172 |
|
|
}
|
173 |
|
|
/*-----------------------------------------------------------*/
|
174 |
|
|
|
175 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
|
176 |
|
|
{
|
177 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
178 |
|
|
( void ) pxPort;
|
179 |
|
|
|
180 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
181 |
|
|
are available, or arrive before xBlockTime expires. */
|
182 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
183 |
|
|
{
|
184 |
|
|
return pdTRUE;
|
185 |
|
|
}
|
186 |
|
|
else
|
187 |
|
|
{
|
188 |
|
|
return pdFALSE;
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
/*-----------------------------------------------------------*/
|
192 |
|
|
|
193 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
|
194 |
|
|
{
|
195 |
636 |
filepang |
usStringLength = usStringLength;
|
196 |
584 |
jeremybenn |
signed portCHAR *pxNext;
|
197 |
|
|
|
198 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
199 |
|
|
block time is used! */
|
200 |
|
|
|
201 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
202 |
|
|
( void ) pxPort;
|
203 |
|
|
|
204 |
|
|
/* Send each character in the string, one at a time. */
|
205 |
|
|
pxNext = ( signed portCHAR * ) pcString;
|
206 |
|
|
while( *pxNext )
|
207 |
|
|
{
|
208 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
209 |
|
|
pxNext++;
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
/*-----------------------------------------------------------*/
|
213 |
|
|
|
214 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
|
215 |
|
|
{
|
216 |
636 |
filepang |
/* The port handle is not required as this driver only supports UART0. */
|
217 |
|
|
( void ) pxPort;
|
218 |
584 |
jeremybenn |
|
219 |
|
|
/* Place the character in the queue of characters to be transmitted. */
|
220 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
221 |
|
|
{
|
222 |
|
|
return pdFAIL;
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
/* Turn on the Tx interrupt so the ISR will remove the character from the
|
226 |
|
|
queue and send it. This does not need to be in a critical section as
|
227 |
|
|
if the interrupt has already removed the character the next interrupt
|
228 |
|
|
will simply turn off the Tx interrupt again. */
|
229 |
636 |
filepang |
uart_txint_enable(0);
|
230 |
584 |
jeremybenn |
|
231 |
|
|
return pdPASS;
|
232 |
|
|
}
|
233 |
|
|
/*-----------------------------------------------------------*/
|
234 |
|
|
|
235 |
|
|
void vSerialClose( xComPortHandle xPort )
|
236 |
|
|
{
|
237 |
|
|
/* Not supported as not required by the demo application. */
|
238 |
636 |
filepang |
xPort = xPort; // prevent compiler warning
|
239 |
584 |
jeremybenn |
}
|
240 |
|
|
/*-----------------------------------------------------------*/
|
241 |
|
|
|
242 |
|
|
/*###########################################################*/
|
243 |
|
|
|
244 |
|
|
/*
|
245 |
|
|
* Create the rx and tx queues.
|
246 |
|
|
*/
|
247 |
|
|
static void vprvSerialCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx )
|
248 |
|
|
{
|
249 |
|
|
/* Create the queues used to hold Rx and Tx characters. */
|
250 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
251 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
252 |
|
|
|
253 |
|
|
/* Pass back a reference to the queues so the serial API file can
|
254 |
|
|
post/receive characters. */
|
255 |
|
|
*pxRxedChars = xRxedChars;
|
256 |
|
|
*pxCharsForTx = xCharsForTx;
|
257 |
|
|
}
|
258 |
|
|
/*-----------------------------------------------------------*/
|