serial.c File Reference


Detailed Description

FreeRTOS Serial Port management example for AVR32 UC3.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file serial.c.

#include <avr32/io.h>
#include "board.h"
#include "gpio.h"
#include "usart.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#include "serial.h"

Go to the source code of this file.

Defines

#define serHANDLE   ( ( xComPortHandle ) 1 )
#define serINVALID_COMPORT_HANDLER   ( ( xComPortHandle ) 0 )
#define serINVALID_QUEUE   ( ( xQueueHandle ) 0 )
#define serNO_BLOCK   ( ( portTickType ) 0 )

Functions

static portBASE_TYPE prvUSART_ISR_NonNakedBehaviour (void)
static void vprvSerialCreateQueues (unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx)
void vSerialClose (xComPortHandle xPort)
void vSerialPutString (xComPortHandle pxPort, const signed portCHAR *const pcString, unsigned portSHORT usStringLength)
static void vUSART_ISR (void)
signed portBASE_TYPE xSerialGetChar (xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime)
xComPortHandle xSerialPortInitMinimal (unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength)
signed portBASE_TYPE xSerialPutChar (xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime)

Variables

static xQueueHandle xCharsForTx
static xQueueHandle xRxedChars


Define Documentation

#define serHANDLE   ( ( xComPortHandle ) 1 )

Definition at line 68 of file serial.c.

Referenced by xSerialPortInitMinimal().

#define serINVALID_COMPORT_HANDLER   ( ( xComPortHandle ) 0 )

Definition at line 66 of file serial.c.

Referenced by xSerialPortInitMinimal().

#define serINVALID_QUEUE   ( ( xQueueHandle ) 0 )

Definition at line 67 of file serial.c.

Referenced by xSerialPortInitMinimal().

#define serNO_BLOCK   ( ( portTickType ) 0 )

Definition at line 69 of file serial.c.

Referenced by vSerialPutString().


Function Documentation

static portBASE_TYPE prvUSART_ISR_NonNakedBehaviour ( void   )  [static]

Definition at line 93 of file serial.c.

References pdFALSE, pdTRUE, portBASE_TYPE, portCHAR, portENTER_CRITICAL, portEXIT_CRITICAL, portLONG, serialPORT_USART, xCharsForTx, xQueueReceiveFromISR(), xQueueSendFromISR, and xRxedChars.

Referenced by vUSART_ISR().

00094 {
00095     /* Now we can declare the local variables. */
00096     signed portCHAR     cChar;
00097     portBASE_TYPE     xHigherPriorityTaskWoken = pdFALSE;
00098     unsigned portLONG     ulStatus;
00099     volatile avr32_usart_t  *usart = serialPORT_USART;
00100     portBASE_TYPE retstatus;
00101 
00102     /* What caused the interrupt? */
00103     ulStatus = usart->csr & usart->imr;
00104 
00105     if (ulStatus & AVR32_USART_CSR_TXRDY_MASK)
00106     {
00107         /* The interrupt was caused by the THR becoming empty.  Are there any
00108         more characters to transmit?
00109         Because FreeRTOS is not supposed to run with nested interrupts, put all OS
00110         calls in a critical section . */
00111         portENTER_CRITICAL();
00112             retstatus = xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken );
00113         portEXIT_CRITICAL();
00114 
00115         if (retstatus == pdTRUE)
00116         {
00117             /* A character was retrieved from the queue so can be sent to the
00118              THR now. */
00119             usart->thr = cChar;
00120         }
00121         else
00122         {
00123             /* Queue empty, nothing to send so turn off the Tx interrupt. */
00124             usart->idr = AVR32_USART_IDR_TXRDY_MASK;
00125         }
00126     }
00127 
00128     if (ulStatus & AVR32_USART_CSR_RXRDY_MASK)
00129     {
00130         /* The interrupt was caused by the receiver getting data. */
00131         cChar = usart->rhr; //TODO
00132 
00133         /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
00134         calls in a critical section . */
00135         portENTER_CRITICAL();
00136             xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);
00137         portEXIT_CRITICAL();
00138     }
00139 
00140     /* The return value will be used by portEXIT_SWITCHING_ISR() to know if it
00141     should perform a vTaskSwitchContext(). */
00142     return ( xHigherPriorityTaskWoken );
00143 }

static void vprvSerialCreateQueues ( unsigned portBASE_TYPE  uxQueueLength,
xQueueHandle pxRxedChars,
xQueueHandle pxCharsForTx 
) [static]

Definition at line 309 of file serial.c.

References portBASE_TYPE, portCHAR, xCharsForTx, xQueueCreate(), and xRxedChars.

Referenced by xSerialPortInitMinimal().

00310 {
00311     /* Create the queues used to hold Rx and Tx characters. */
00312     xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
00313     xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
00314 
00315     /* Pass back a reference to the queues so the serial API file can
00316     post/receive characters. */
00317     *pxRxedChars = xRxedChars;
00318     *pxCharsForTx = xCharsForTx;
00319 }

void vSerialClose ( xComPortHandle  xPort  ) 

Definition at line 298 of file serial.c.

00299 {
00300   /* Not supported as not required by the demo application. */
00301 }

void vSerialPutString ( xComPortHandle  pxPort,
const signed portCHAR *const   pcString,
unsigned portSHORT  usStringLength 
)

Definition at line 258 of file serial.c.

References portCHAR, serNO_BLOCK, and xSerialPutChar().

00259 {
00260 signed portCHAR *pxNext;
00261 
00262     /* NOTE: This implementation does not handle the queue being full as no
00263     block time is used! */
00264 
00265     /* The port handle is not required as this driver only supports UART0. */
00266     ( void ) pxPort;
00267 
00268     /* Send each character in the string, one at a time. */
00269     pxNext = ( signed portCHAR * ) pcString;
00270     while( *pxNext )
00271     {
00272         xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
00273         pxNext++;
00274     }
00275 }

static void vUSART_ISR ( void   )  [static]

Definition at line 155 of file serial.c.

References portENTER_SWITCHING_ISR, portEXIT_SWITCHING_ISR, and prvUSART_ISR_NonNakedBehaviour().

Referenced by xSerialPortInitMinimal().

00156 {
00157     /* This ISR can cause a context switch, so the first statement must be a
00158     call to the portENTER_SWITCHING_ISR() macro.  This must be BEFORE any
00159     variable declarations. */
00160     portENTER_SWITCHING_ISR();
00161 
00162     prvUSART_ISR_NonNakedBehaviour();
00163 
00164     /* Exit the ISR.  If a task was woken by either a character being received
00165     or transmitted then a context switch will occur. */
00166     portEXIT_SWITCHING_ISR();
00167 }

signed portBASE_TYPE xSerialGetChar ( xComPortHandle  pxPort,
signed portCHAR *  pcRxedChar,
portTickType  xBlockTime 
)

Definition at line 240 of file serial.c.

References pdFALSE, pdTRUE, xQueueReceive, and xRxedChars.

Referenced by portTASK_FUNCTION().

00241 {
00242     /* The port handle is not required as this driver only supports UART0. */
00243     ( void ) pxPort;
00244 
00245     /* Get the next character from the buffer.  Return false if no characters
00246     are available, or arrive before xBlockTime expires. */
00247     if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
00248     {
00249         return pdTRUE;
00250     }
00251     else
00252     {
00253         return pdFALSE;
00254     }
00255 }

xComPortHandle xSerialPortInitMinimal ( unsigned portLONG  ulWantedBaud,
unsigned portBASE_TYPE  uxQueueLength 
)

Configure USART.

Definition at line 174 of file serial.c.

References configPBA_CLOCK_HZ, portENTER_CRITICAL, portEXIT_CRITICAL, portLONG, serHANDLE, serialPORT_USART, serialPORT_USART_IRQ, serialPORT_USART_RX_FUNCTION, serialPORT_USART_RX_PIN, serialPORT_USART_TX_FUNCTION, serialPORT_USART_TX_PIN, serINVALID_COMPORT_HANDLER, serINVALID_QUEUE, vprvSerialCreateQueues(), vUSART_ISR(), xCharsForTx, and xRxedChars.

Referenced by vAltStartComTestTasks().

00175 {
00176 static const gpio_map_t USART_GPIO_MAP =
00177 {
00178     { serialPORT_USART_RX_PIN, serialPORT_USART_RX_FUNCTION },
00179     { serialPORT_USART_TX_PIN, serialPORT_USART_TX_FUNCTION }
00180 };
00181 
00182 xComPortHandle    xReturn = serHANDLE;
00183 volatile avr32_usart_t  *usart = serialPORT_USART;
00184 
00185     // USART options.
00186     usart_options_t USART_OPTIONS =
00187     {
00188         .baudrate     = 57600,
00189         .charlength   = 8,
00190         .paritytype   = USART_NO_PARITY,
00191         .stopbits     = USART_1_STOPBIT,
00192         .channelmode  = USART_NORMAL_CHMODE
00193     };
00194 
00195     USART_OPTIONS.baudrate = ulWantedBaud;
00196 
00197     /* Create the rx and tx queues. */
00198     vprvSerialCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );
00199 
00200     /* Configure USART. */
00201     if( ( xRxedChars != serINVALID_QUEUE ) &&
00202       ( xCharsForTx != serINVALID_QUEUE ) &&
00203       ( ulWantedBaud != ( unsigned portLONG ) 0 ) )
00204     {
00205         portENTER_CRITICAL();
00206         {
00210             /* Enable USART RXD & TXD pins. */
00211             gpio_enable_module( USART_GPIO_MAP, sizeof( USART_GPIO_MAP ) / sizeof( USART_GPIO_MAP[0] ) );
00212 
00213             // Initialize USART in RS232 mode.
00214             usart_init_rs232(usart, &USART_OPTIONS, configPBA_CLOCK_HZ);
00215 
00216             /* We're not fully done yet: disable receiver and transmitter. */
00217             usart->cr |= AVR32_USART_CR_RXDIS_MASK | AVR32_USART_CR_TXDIS_MASK;
00218 
00219             /* Register the USART interrupt handler to the interrupt controller and
00220              enable the USART interrupt. */
00221             INTC_register_interrupt((__int_handler)&vUSART_ISR, serialPORT_USART_IRQ, AVR32_INTC_INT1);
00222 
00223             /* Enable USART interrupt sources (but not Tx for now)... */
00224             usart->ier = AVR32_USART_IER_RXRDY_MASK;
00225 
00226             /* Enable receiver and transmitter... */
00227             usart->cr |= AVR32_USART_CR_TXEN_MASK | AVR32_USART_CR_RXEN_MASK;
00228         }
00229         portEXIT_CRITICAL();
00230     }
00231     else
00232     {
00233         xReturn = serINVALID_COMPORT_HANDLER;
00234     }
00235 
00236     return xReturn;
00237 }

signed portBASE_TYPE xSerialPutChar ( xComPortHandle  pxPort,
signed portCHAR  cOutChar,
portTickType  xBlockTime 
)

Definition at line 278 of file serial.c.

References pdFAIL, pdPASS, serialPORT_USART, xCharsForTx, and xQueueSend.

Referenced by portTASK_FUNCTION(), and vSerialPutString().

00279 {
00280 volatile avr32_usart_t  *usart = serialPORT_USART;
00281 
00282     /* Place the character in the queue of characters to be transmitted. */
00283     if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
00284     {
00285         return pdFAIL;
00286     }
00287 
00288     /* Turn on the Tx interrupt so the ISR will remove the character from the
00289     queue and send it.   This does not need to be in a critical section as
00290     if the interrupt has already removed the character the next interrupt
00291     will simply turn off the Tx interrupt again. */
00292     usart->ier = (1 << AVR32_USART_IER_TXRDY_OFFSET);
00293 
00294     return pdPASS;
00295 }


Variable Documentation


Generated on Thu Dec 17 20:02:01 2009 for AVR32 UC3 - FreeRTOS Real Time Kernel by  doxygen 1.5.5