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 |
|
|
/* FIXME, entering, exiting ciritical section around
|
109 |
|
|
xQueueReceiveFromISR is not work */
|
110 |
|
|
#if 0
|
111 |
584 |
jeremybenn |
portENTER_CRITICAL();
|
112 |
|
|
retstatus = xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken );
|
113 |
|
|
portEXIT_CRITICAL();
|
114 |
636 |
filepang |
#else
|
115 |
|
|
retstatus = xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken );
|
116 |
|
|
#endif
|
117 |
584 |
jeremybenn |
|
118 |
|
|
if (retstatus == pdTRUE)
|
119 |
|
|
{
|
120 |
|
|
/* A character was retrieved from the queue so can be sent to the
|
121 |
|
|
THR now. */
|
122 |
636 |
filepang |
uart_putc_noblock(0, cChar);
|
123 |
584 |
jeremybenn |
}
|
124 |
|
|
else
|
125 |
|
|
{
|
126 |
|
|
/* Queue empty, nothing to send so turn off the Tx interrupt. */
|
127 |
636 |
filepang |
uart_txint_disable(0);
|
128 |
584 |
jeremybenn |
}
|
129 |
|
|
}
|
130 |
|
|
|
131 |
636 |
filepang |
// RX RADY INTERRUPT
|
132 |
|
|
if (ulStatus & UART_IIR_RDI)
|
133 |
584 |
jeremybenn |
{
|
134 |
|
|
/* The interrupt was caused by the receiver getting data. */
|
135 |
636 |
filepang |
cChar = uart_getc_noblock(0);
|
136 |
584 |
jeremybenn |
|
137 |
|
|
/* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
|
138 |
|
|
calls in a critical section . */
|
139 |
|
|
portENTER_CRITICAL();
|
140 |
|
|
xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);
|
141 |
|
|
portEXIT_CRITICAL();
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/* The return value will be used by portEXIT_SWITCHING_ISR() to know if it
|
145 |
|
|
should perform a vTaskSwitchContext(). */
|
146 |
636 |
filepang |
// return ( xHigherPriorityTaskWoken );
|
147 |
584 |
jeremybenn |
}
|
148 |
|
|
/*-----------------------------------------------------------*/
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
/*
|
152 |
|
|
* Init the serial port for the Minimal implementation.
|
153 |
|
|
*/
|
154 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
155 |
|
|
{
|
156 |
|
|
xComPortHandle xReturn = serHANDLE;
|
157 |
|
|
|
158 |
|
|
/* Create the rx and tx queues. */
|
159 |
|
|
vprvSerialCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );
|
160 |
|
|
|
161 |
|
|
/* Configure USART. */
|
162 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) &&
|
163 |
|
|
( xCharsForTx != serINVALID_QUEUE ) &&
|
164 |
|
|
( ulWantedBaud != ( unsigned portLONG ) 0 ) )
|
165 |
|
|
{
|
166 |
|
|
portENTER_CRITICAL();
|
167 |
|
|
{
|
168 |
636 |
filepang |
// register interrupt handler
|
169 |
|
|
int_add(UART0_IRQ, vUSART_ISR, 0x0);
|
170 |
584 |
jeremybenn |
}
|
171 |
|
|
portEXIT_CRITICAL();
|
172 |
|
|
}
|
173 |
|
|
else
|
174 |
|
|
{
|
175 |
|
|
xReturn = serINVALID_COMPORT_HANDLER;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
return xReturn;
|
179 |
|
|
}
|
180 |
|
|
/*-----------------------------------------------------------*/
|
181 |
|
|
|
182 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
|
183 |
|
|
{
|
184 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
185 |
|
|
( void ) pxPort;
|
186 |
|
|
|
187 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
188 |
|
|
are available, or arrive before xBlockTime expires. */
|
189 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
190 |
|
|
{
|
191 |
|
|
return pdTRUE;
|
192 |
|
|
}
|
193 |
|
|
else
|
194 |
|
|
{
|
195 |
|
|
return pdFALSE;
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
/*-----------------------------------------------------------*/
|
199 |
|
|
|
200 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
|
201 |
|
|
{
|
202 |
636 |
filepang |
usStringLength = usStringLength;
|
203 |
584 |
jeremybenn |
signed portCHAR *pxNext;
|
204 |
|
|
|
205 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
206 |
|
|
block time is used! */
|
207 |
|
|
|
208 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
209 |
|
|
( void ) pxPort;
|
210 |
|
|
|
211 |
|
|
/* Send each character in the string, one at a time. */
|
212 |
|
|
pxNext = ( signed portCHAR * ) pcString;
|
213 |
|
|
while( *pxNext )
|
214 |
|
|
{
|
215 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
216 |
|
|
pxNext++;
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
|
|
/*-----------------------------------------------------------*/
|
220 |
|
|
|
221 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
|
222 |
|
|
{
|
223 |
636 |
filepang |
/* The port handle is not required as this driver only supports UART0. */
|
224 |
|
|
( void ) pxPort;
|
225 |
584 |
jeremybenn |
|
226 |
|
|
/* Place the character in the queue of characters to be transmitted. */
|
227 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
228 |
|
|
{
|
229 |
|
|
return pdFAIL;
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
/* Turn on the Tx interrupt so the ISR will remove the character from the
|
233 |
|
|
queue and send it. This does not need to be in a critical section as
|
234 |
|
|
if the interrupt has already removed the character the next interrupt
|
235 |
|
|
will simply turn off the Tx interrupt again. */
|
236 |
636 |
filepang |
uart_txint_enable(0);
|
237 |
584 |
jeremybenn |
|
238 |
|
|
return pdPASS;
|
239 |
|
|
}
|
240 |
|
|
/*-----------------------------------------------------------*/
|
241 |
|
|
|
242 |
|
|
void vSerialClose( xComPortHandle xPort )
|
243 |
|
|
{
|
244 |
|
|
/* Not supported as not required by the demo application. */
|
245 |
636 |
filepang |
xPort = xPort; // prevent compiler warning
|
246 |
584 |
jeremybenn |
}
|
247 |
|
|
/*-----------------------------------------------------------*/
|
248 |
|
|
|
249 |
|
|
/*###########################################################*/
|
250 |
|
|
|
251 |
|
|
/*
|
252 |
|
|
* Create the rx and tx queues.
|
253 |
|
|
*/
|
254 |
|
|
static void vprvSerialCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx )
|
255 |
|
|
{
|
256 |
|
|
/* Create the queues used to hold Rx and Tx characters. */
|
257 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
258 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
259 |
|
|
|
260 |
|
|
/* Pass back a reference to the queues so the serial API file can
|
261 |
|
|
post/receive characters. */
|
262 |
|
|
*pxRxedChars = xRxedChars;
|
263 |
|
|
*pxCharsForTx = xCharsForTx;
|
264 |
|
|
}
|
265 |
|
|
/*-----------------------------------------------------------*/
|