这个例程展示了怎样执行一个USB设备CDC,通讯设备类是一个通用的方法来进行各种各样的通讯,可以同数字电话,模拟调制解调器,ADSL或电缆调制解调器。
这个程序就是虚拟串口形成之后往PC机端发字母A。
主要配置在conf_usb.h
#ifndef _CONF_USB_H_
#define _CONF_USB_H_
#include "compiler.h"
#include "board.h"
/**
* USB Device Configuration
* @{
*/
//! Device definition (mandatory)
#define USB_DEVICE_VENDOR_ID USB_VID_ATMEL
#if BOARD == UC3B_BOARD_CONTROLLER
# define USB_DEVICE_PRODUCT_ID USB_PID_ATMEL_UC3_CDC_DEBUG
#else
# define USB_DEVICE_PRODUCT_ID USB_PID_ATMEL_ASF_CDC
#endif
#define USB_DEVICE_MAJOR_VERSION 1
#define USB_DEVICE_MINOR_VERSION 0
#define USB_DEVICE_POWER 100 // Consumption on Vbus line (mA)
#define USB_DEVICE_ATTR \
(USB_CONFIG_ATTR_SELF_POWERED)
// (USB_CONFIG_ATTR_BUS_POWERED)
// (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_SELF_POWERED)
// (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_BUS_POWERED)
//! USB Device string definitions (Optional)
#define USB_DEVICE_MANUFACTURE_NAME "ATMEL ASF"
#define USB_DEVICE_PRODUCT_NAME "CDC Virtual Com"
// #define USB_DEVICE_SERIAL_NAME "12...EF"
/**
* Device speeds support
* Low speed not supported by CDC
* @{
*/
//! To authorize the High speed
#if (UC3A3||UC3A4)
#define USB_DEVICE_HS_SUPPORT
#elif (SAM3XA||SAM3U)
#define USB_DEVICE_HS_SUPPORT
#endif
//@}
/**
* USB Device Callbacks definitions (Optional)
* @{
*/
#define UDC_VBUS_EVENT(b_vbus_high)
#define UDC_SOF_EVENT() main_sof_action()
#define UDC_SUSPEND_EVENT() main_suspend_action()
#define UDC_RESUME_EVENT() main_resume_action()
//! Mandatory when USB_DEVICE_ATTR authorizes remote wakeup feature
// #define UDC_REMOTEWAKEUP_ENABLE() user_callback_remotewakeup_enable()
// extern void user_callback_remotewakeup_enable(void);
// #define UDC_REMOTEWAKEUP_DISABLE() user_callback_remotewakeup_disable()
// extern void user_callback_remotewakeup_disable(void);
#ifdef USB_DEVICE_LPM_SUPPORT
#define UDC_SUSPEND_LPM_EVENT() main_suspend_lpm_action()
#define UDC_REMOTEWAKEUP_LPM_ENABLE() main_remotewakeup_lpm_enable()
#define UDC_REMOTEWAKEUP_LPM_DISABLE() main_remotewakeup_lpm_disable()
#endif
//! When a extra string descriptor must be supported
//! other than manufacturer, product and serial string
// #define UDC_GET_EXTRA_STRING()
//@}
//@}
/**
* USB Interface Configuration
* @{
*/
/**
* Configuration of CDC interface
* @{
*/
//! Define two USB communication ports
#define UDI_CDC_PORT_NB 1
//! Interface callback definition
#define UDI_CDC_ENABLE_EXT(port) main_cdc_enable(port)
#define UDI_CDC_DISABLE_EXT(port) main_cdc_disable(port)
#define UDI_CDC_RX_NOTIFY(port) uart_rx_notify(port)
#define UDI_CDC_TX_EMPTY_NOTIFY(port)
#define UDI_CDC_SET_CODING_EXT(port,cfg) uart_config(port,cfg)
#define UDI_CDC_SET_DTR_EXT(port,set) main_cdc_set_dtr(port,set)
#define UDI_CDC_SET_RTS_EXT(port,set)
//! Define it when the transfer CDC Device to Host is a low rate (<512000 bauds)
//! to reduce CDC buffers size
//#define UDI_CDC_LOW_RATE
//! Default configuration of communication port
#if BOARD == UC3B_BOARD_CONTROLLER
# define UDI_CDC_DEFAULT_RATE 57600
#else
# define UDI_CDC_DEFAULT_RATE 115200
#endif
#define UDI_CDC_DEFAULT_STOPBITS CDC_STOP_BITS_1
#define UDI_CDC_DEFAULT_PARITY CDC_PAR_NONE
#define UDI_CDC_DEFAULT_DATABITS 8
//@}
//@}
/**
* USB Device Driver Configuration
* @{
*/
//@}
//! The includes of classes and other headers must be done at the end of this file to avoid compile error
#include "udi_cdc_conf.h"
#include "uart.h"
#include "main.h"
#endif // _CONF_USB_H_
而主程序只是初始化一下,只发”A“
而USB 的处理是靠中断函数来完成的。以下是主程序:
#include <asf.h>
#include "conf_usb.h"
#include "ui.h"
#include "uart.h"
static volatile bool main_b_cdc_enable = false;
/*! \brief Main function. Execution starts here.
*/
int main(void)
{
irq_initialize_vectors();
cpu_irq_enable();
// Initialize the sleep manager
sleepmgr_init();
#if !SAM0
sysclk_init();
board_init();
#else
system_init();
#endif
ui_init();
ui_powerdown();
// Start USB stack to authorize VBus monitoring
udc_start();
// The main loop manages only the power mode
// because the USB management is done by interrupt
while (true) {
udi_cdc_putc('A');
sleepmgr_enter_sleep();
}
}
void main_suspend_action(void)
{
ui_powerdown();
}
void main_resume_action(void)
{
ui_wakeup();
}
void main_sof_action(void)
{
if (!main_b_cdc_enable)
return;
ui_process(udd_get_frame_number());
}
#ifdef USB_DEVICE_LPM_SUPPORT
void main_suspend_lpm_action(void)
{
ui_powerdown();
}
void main_remotewakeup_lpm_disable(void)
{
ui_wakeup_disable();
}
void main_remotewakeup_lpm_enable(void)
{
ui_wakeup_enable();
}
#endif
bool main_cdc_enable(uint8_t port)
{
main_b_cdc_enable = true;
// Open communication
uart_open(port);
return true;
}
void main_cdc_disable(uint8_t port)
{
main_b_cdc_enable = false;
// Close communication
uart_close(port);
}
void main_cdc_set_dtr(uint8_t port, bool b_enable)
{
if (b_enable) {
// Host terminal has open COM
ui_com_open(port);
}else{
// Host terminal has close COM
ui_com_close(port);
}
}
运行结果:
|