📄 usb_vuart_lib.c
字号:
/*C**************************************************************************
* NAME: usb_vuart_lib.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE: c5131-usb-cdc-1_0_1
* REVISION: 1.2
*----------------------------------------------------------------------------
* PURPOSE:
* This firmware library file provides USB "Virtual UART" functions
* which allow the USB interface to resemble a UART to higher-level firmware.
* These functions require lower-level USB firmware library modules:
* "usb_cdc_enum.c" and "usb_drv.c".
*
* The USB host requires standard 'CDC' driver software which provides
* a "virtual" COM port to the host PC application.
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "usb_config.h"
#include "a89c5131.h"
#include "c5131_drv.h"
#include "usb_drv.h"
#include "usb_cdc_enum.h"
#include "usb_vuart_lib.h"
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
Uchar tx_counter;
Uchar rx_counter;
/*_____ D E C L A R A T I O N ______________________________________________*/
/*F**************************************************************************
* NAME: usb_vuart_init
*----------------------------------------------------------------------------
* PARAMS:
* delay: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* This function initializes the UART USB library.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_vuart_init(void)
{
tx_counter = 0;
rx_counter = 0;
}
/*F**************************************************************************
* NAME: usb_vuart_rx_data_avail
*----------------------------------------------------------------------------
* PARAMS:
* delay: none
* return: boolean TRUE if a byte is ready to be read, FALSE otherwise
*----------------------------------------------------------------------------
* PURPOSE:
* This function checks if a character has been received on the USB bus.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit usb_vuart_rx_data_avail(void)
{
if (!rx_counter)
{
Usb_select_ep(RX_EP);
if (Usb_rx_complete())
{ rx_counter = usb_get_nb_byte(); if (!rx_counter) Usb_clear_rx(); }
}
return (rx_counter!=0);
}
/*F**************************************************************************
* NAME: usb_vuart_getchar
*----------------------------------------------------------------------------
* PARAMS:
* delay: none
* return: Uchar, read data from USB
*----------------------------------------------------------------------------
* PURPOSE:
* This function reads one byte from the USB bus
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* 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.
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
char usb_vuart_getchar(void)
{
register Uchar data_rx;
Usb_select_ep(RX_EP);
if (!rx_counter) while (!usb_vuart_rx_data_avail());
data_rx=Usb_read_byte();
rx_counter--;
if (!rx_counter) Usb_clear_rx();
return data_rx;
}
/*F**************************************************************************
* NAME: usb_vuart_tx_ready
*----------------------------------------------------------------------------
* PARAMS:
* delay: none
* return: Boolean. TRUE if the firmware can write a new byte to transmit.
* FALSE oterwise.
*----------------------------------------------------------------------------
* PURPOSE:
* This function checks if the USB emission buffer is ready to accept at
* at least 1 byte
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit usb_vuart_tx_ready(void)
{
if ((Usb_tx_ready())||(Usb_tx_complete())) { return FALSE; }
return TRUE;
}
/*F**************************************************************************
* NAME: usb_vuart_putchar
*----------------------------------------------------------------------------
* PARAMS:
* Uchar: data to send through USB
* delay: none
* return: data to send
*----------------------------------------------------------------------------
* PURPOSE:
* This function fills the USB transmit buffer with the new data. This buffer
* is sent if complete. To flush this buffer before waiting until full, launch
* the usb_vuart_flush() function.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
char usb_vuart_putchar(Uchar data_to_send)
{
Usb_select_ep(TX_EP);
if ((Usb_tx_ready())||(Usb_tx_complete()))
{ while(!Usb_tx_complete()); Usb_clear_tx_complete(); }
Usb_write_byte(data_to_send);
tx_counter++;
if (tx_counter >= TX_EP_SIZE) usb_vuart_flush();
return data_to_send;
}
/*F**************************************************************************
* NAME: usb_vuart_flush
*----------------------------------------------------------------------------
* PARAMS:
* delay: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* This function sends the data stored in the USB transmit buffer.
* This function does nothing if there is no data in the buffer.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_vuart_flush (void)
{
if (tx_counter)
{
Usb_select_ep(TX_EP);
Usb_set_tx_ready();
tx_counter = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -