Xmega Application Note


uart_usb_lib.c File Reference


Detailed Description

This file controls the UART USB functions.

Application note:
AVR1907: Xplain Evaluation Board
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
3122
Date
2010-01-13 13:26:22 +0100 (on, 13 jan 2010)

Copyright (c) 2010, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file uart_usb_lib.c.

#include "config.h"
#include "usb_drv.h"
#include "usb_descriptors.h"
#include "uart_usb_lib.h"

Include dependency graph for uart_usb_lib.c:

Go to the source code of this file.

Functions

void uart_usb_flush (void)
 This function sends the data stored in the USB transmit buffer.
uint8_t uart_usb_getchar (void)
 This function reads one byte from the USB bus.
void uart_usb_init (void)
 Initializes the uart_usb library.
int uart_usb_putchar (int data_to_send)
 This function fills the USB transmit buffer with the new data. This buffer is sent if complete. To flush this buffer before waiting full, launch the uart_usb_flush() function.
uint8_t uart_usb_test_hit (void)
 This function checks if a character has been received on the USB bus.
uint8_t uart_usb_tx_ready (void)
 This function checks if the USB emission buffer is ready to accept at at least 1 byte.

Variables

uint8_t rx_counter
uint8_t tx_counter


Function Documentation

void uart_usb_flush ( void   ) 

This function sends the data stored in the USB transmit buffer.

This function does nothing if there is no data in the buffer.

Definition at line 160 of file uart_usb_lib.c.

References tx_counter, TX_EP, Usb_select_endpoint, and Usb_send_in.

Referenced by cdc_task(), and uart_usb_putchar().

00161 {
00162         Usb_select_endpoint(TX_EP);
00163         Usb_send_in();
00164         tx_counter = 0;
00165         
00166 }

uint8_t uart_usb_getchar ( void   ) 

This function reads one byte from the USB bus.

If one byte is present in the USB fifo, this byte is returned. If no data is present in the USB fifo, this function waits for USB data.

Returns:
byte received

Definition at line 101 of file uart_usb_lib.c.

References rx_counter, RX_EP, uart_usb_test_hit(), Usb_ack_receive_out, Usb_read_byte, and Usb_select_endpoint.

Referenced by cdc_task().

00102 {
00103         register uint8_t data_rx;
00104         
00105         Usb_select_endpoint(RX_EP);
00106         if (!rx_counter){
00107                 while (!uart_usb_test_hit());
00108         }
00109         
00110         data_rx = Usb_read_byte();
00111         rx_counter--;
00112         
00113         if (!rx_counter){
00114                 Usb_ack_receive_out();
00115         }
00116         
00117         return data_rx;
00118 }

Here is the call graph for this function:

void uart_usb_init ( void   ) 

Initializes the uart_usb library.

Definition at line 58 of file uart_usb_lib.c.

References rx_counter, and tx_counter.

00059 {
00060         tx_counter = 0;
00061         rx_counter = 0;
00062 }

int uart_usb_putchar ( int  data_to_send  ) 

This function fills the USB transmit buffer with the new data. This buffer is sent if complete. To flush this buffer before waiting full, launch the uart_usb_flush() function.

Parameters:
data_to_send 
Returns:

Definition at line 142 of file uart_usb_lib.c.

References Is_usb_write_enabled, tx_counter, TX_EP, uart_usb_flush(), uart_usb_tx_ready(), Usb_select_endpoint, and Usb_write_byte.

Referenced by cdc_task().

00143 {
00144         Usb_select_endpoint(TX_EP);
00145         while(!uart_usb_tx_ready()); /* Wait Endpoint ready */
00146         Usb_write_byte(data_to_send);
00147         tx_counter++;
00148         if(!Is_usb_write_enabled()) /* If Endpoint full -> flush */
00149         {
00150                 uart_usb_flush();
00151         }
00152         return data_to_send;
00153 }

Here is the call graph for this function:

uint8_t uart_usb_test_hit ( void   ) 

This function checks if a character has been received on the USB bus.

Returns:
bit (true if a byte is ready to be read)

Definition at line 68 of file uart_usb_lib.c.

References Is_usb_receive_out, rx_counter, RX_EP, Usb_ack_receive_out, Usb_byte_counter, and Usb_select_endpoint.

Referenced by cdc_task(), and uart_usb_getchar().

00069 {
00070         if (!rx_counter)
00071         {
00072                 Usb_select_endpoint(RX_EP);
00073                 if (Is_usb_receive_out())
00074                 {
00075                         
00076 #ifdef __ICCAVR__
00077 #pragma diag_suppress=Pa082 /* Suppress warning for undefined order of volatile access. */
00078 #endif
00079                         rx_counter = Usb_byte_counter();
00080 #ifdef __ICCAVR__
00081 #pragma diag_default=Pa082 /* Back to default.*/
00082 #endif
00083 
00084                         if (!rx_counter)
00085                         {
00086                                 Usb_ack_receive_out();
00087                         }
00088                 }
00089         }
00090         return (rx_counter!=0);
00091 }

uint8_t uart_usb_tx_ready ( void   ) 

This function checks if the USB emission buffer is ready to accept at at least 1 byte.

Returns:
TRUE if the firmware can write a new byte to transmit.

Definition at line 125 of file uart_usb_lib.c.

References Is_usb_write_enabled.

Referenced by uart_usb_putchar().

00126 {
00127         if (!Is_usb_write_enabled())
00128         {
00129                 return false;
00130         }
00131         return true;
00132 }


Variable Documentation

uint8_t rx_counter

Definition at line 54 of file uart_usb_lib.c.

Referenced by uart_usb_getchar(), uart_usb_init(), and uart_usb_test_hit().

uint8_t tx_counter

Definition at line 53 of file uart_usb_lib.c.

Referenced by cdc_task(), uart_usb_flush(), uart_usb_init(), and uart_usb_putchar().

@DOC_TITLE@
Generated on Mon Jan 18 09:26:11 2010 for AVR1907 Xplain USB Gateway by doxygen 1.5.5