这个例程展示了怎样执行一个USB设备CDC,通讯设备类是一个通用的方法来进行各种各样的通讯,可以同数字电话,模拟调制解调器,ADSL或电缆调制解调器。
这个程序就是虚拟串口形成之后往PC机端发字母A。
主要配置在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);
- }
- }
运行结果:
|