[Atmel] 用SAM-BA或JLINK跑ATSAM4E16的程序(18)ASF USB CDC

[复制链接]
1513|0
 楼主| ddllxxrr 发表于 2015-11-25 21:29 | 显示全部楼层 |阅读模式
这个例程展示了怎样执行一个USB设备CDC,通讯设备类是一个通用的方法来进行各种各样的通讯,可以同数字电话,模拟调制解调器,ADSL或电缆调制解调器。

这个程序就是虚拟串口形成之后往PC机端发字母A。

主要配置在conf_usb.h


  1. #ifndef _CONF_USB_H_
  2. #define _CONF_USB_H_

  3. #include "compiler.h"
  4. #include "board.h"

  5. /**
  6. * USB Device Configuration
  7. * @{
  8. */

  9. //! Device definition (mandatory)
  10. #define  USB_DEVICE_VENDOR_ID             USB_VID_ATMEL
  11. #if BOARD == UC3B_BOARD_CONTROLLER
  12. # define  USB_DEVICE_PRODUCT_ID            USB_PID_ATMEL_UC3_CDC_DEBUG
  13. #else
  14. # define  USB_DEVICE_PRODUCT_ID            USB_PID_ATMEL_ASF_CDC
  15. #endif
  16. #define  USB_DEVICE_MAJOR_VERSION         1
  17. #define  USB_DEVICE_MINOR_VERSION         0
  18. #define  USB_DEVICE_POWER                 100 // Consumption on Vbus line (mA)
  19. #define  USB_DEVICE_ATTR                  \
  20.         (USB_CONFIG_ATTR_SELF_POWERED)
  21. // (USB_CONFIG_ATTR_BUS_POWERED)
  22. //        (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_SELF_POWERED)
  23. //        (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_BUS_POWERED)

  24. //! USB Device string definitions (Optional)
  25. #define  USB_DEVICE_MANUFACTURE_NAME      "ATMEL ASF"
  26. #define  USB_DEVICE_PRODUCT_NAME          "CDC Virtual Com"
  27. // #define  USB_DEVICE_SERIAL_NAME           "12...EF"


  28. /**
  29. * Device speeds support
  30. * Low speed not supported by CDC
  31. * @{
  32. */
  33. //! To authorize the High speed
  34. #if (UC3A3||UC3A4)
  35. #define  USB_DEVICE_HS_SUPPORT
  36. #elif (SAM3XA||SAM3U)
  37. #define  USB_DEVICE_HS_SUPPORT
  38. #endif
  39. //@}


  40. /**
  41. * USB Device Callbacks definitions (Optional)
  42. * @{
  43. */
  44. #define  UDC_VBUS_EVENT(b_vbus_high)
  45. #define  UDC_SOF_EVENT()                  main_sof_action()
  46. #define  UDC_SUSPEND_EVENT()              main_suspend_action()
  47. #define  UDC_RESUME_EVENT()               main_resume_action()
  48. //! Mandatory when USB_DEVICE_ATTR authorizes remote wakeup feature
  49. // #define  UDC_REMOTEWAKEUP_ENABLE()        user_callback_remotewakeup_enable()
  50. // extern void user_callback_remotewakeup_enable(void);
  51. // #define  UDC_REMOTEWAKEUP_DISABLE()       user_callback_remotewakeup_disable()
  52. // extern void user_callback_remotewakeup_disable(void);
  53. #ifdef USB_DEVICE_LPM_SUPPORT
  54. #define  UDC_SUSPEND_LPM_EVENT()          main_suspend_lpm_action()
  55. #define  UDC_REMOTEWAKEUP_LPM_ENABLE()    main_remotewakeup_lpm_enable()
  56. #define  UDC_REMOTEWAKEUP_LPM_DISABLE()   main_remotewakeup_lpm_disable()
  57. #endif
  58. //! When a extra string descriptor must be supported
  59. //! other than manufacturer, product and serial string
  60. // #define  UDC_GET_EXTRA_STRING()
  61. //@}

  62. //@}


  63. /**
  64. * USB Interface Configuration
  65. * @{
  66. */
  67. /**
  68. * Configuration of CDC interface
  69. * @{
  70. */

  71. //! Define two USB communication ports
  72. #define  UDI_CDC_PORT_NB 1

  73. //! Interface callback definition
  74. #define  UDI_CDC_ENABLE_EXT(port)         main_cdc_enable(port)
  75. #define  UDI_CDC_DISABLE_EXT(port)        main_cdc_disable(port)
  76. #define  UDI_CDC_RX_NOTIFY(port)          uart_rx_notify(port)
  77. #define  UDI_CDC_TX_EMPTY_NOTIFY(port)
  78. #define  UDI_CDC_SET_CODING_EXT(port,cfg) uart_config(port,cfg)
  79. #define  UDI_CDC_SET_DTR_EXT(port,set)    main_cdc_set_dtr(port,set)
  80. #define  UDI_CDC_SET_RTS_EXT(port,set)

  81. //! Define it when the transfer CDC Device to Host is a low rate (<512000 bauds)
  82. //! to reduce CDC buffers size
  83. //#define  UDI_CDC_LOW_RATE

  84. //! Default configuration of communication port
  85. #if BOARD == UC3B_BOARD_CONTROLLER
  86. # define  UDI_CDC_DEFAULT_RATE             57600
  87. #else
  88. # define  UDI_CDC_DEFAULT_RATE             115200
  89. #endif
  90. #define  UDI_CDC_DEFAULT_STOPBITS         CDC_STOP_BITS_1
  91. #define  UDI_CDC_DEFAULT_PARITY           CDC_PAR_NONE
  92. #define  UDI_CDC_DEFAULT_DATABITS         8
  93. //@}
  94. //@}


  95. /**
  96. * USB Device Driver Configuration
  97. * @{
  98. */
  99. //@}

  100. //! The includes of classes and other headers must be done at the end of this file to avoid compile error
  101. #include "udi_cdc_conf.h"
  102. #include "uart.h"
  103. #include "main.h"

  104. #endif // _CONF_USB_H_

而主程序只是初始化一下,只发”A“
而USB 的处理是靠中断函数来完成的。以下是主程序:

  1. #include <asf.h>
  2. #include "conf_usb.h"
  3. #include "ui.h"
  4. #include "uart.h"

  5. static volatile bool main_b_cdc_enable = false;

  6. /*! \brief Main function. Execution starts here.
  7. */
  8. int main(void)
  9. {

  10.         irq_initialize_vectors();
  11.         cpu_irq_enable();

  12.         // Initialize the sleep manager
  13.         sleepmgr_init();

  14. #if !SAM0
  15.         sysclk_init();
  16.         board_init();
  17. #else
  18.         system_init();
  19. #endif
  20.         ui_init();
  21.         ui_powerdown();

  22.         // Start USB stack to authorize VBus monitoring
  23.         udc_start();

  24.         // The main loop manages only the power mode
  25.         // because the USB management is done by interrupt
  26.         while (true) {
  27.                 udi_cdc_putc('A');
  28.                 sleepmgr_enter_sleep();
  29.         }
  30. }

  31. void main_suspend_action(void)
  32. {
  33.         ui_powerdown();
  34. }

  35. void main_resume_action(void)
  36. {
  37.         ui_wakeup();
  38. }

  39. void main_sof_action(void)
  40. {
  41.         if (!main_b_cdc_enable)
  42.                 return;
  43.         ui_process(udd_get_frame_number());
  44. }

  45. #ifdef USB_DEVICE_LPM_SUPPORT
  46. void main_suspend_lpm_action(void)
  47. {
  48.         ui_powerdown();
  49. }

  50. void main_remotewakeup_lpm_disable(void)
  51. {
  52.         ui_wakeup_disable();
  53. }

  54. void main_remotewakeup_lpm_enable(void)
  55. {
  56.         ui_wakeup_enable();
  57. }
  58. #endif

  59. bool main_cdc_enable(uint8_t port)
  60. {
  61.         main_b_cdc_enable = true;
  62.         // Open communication
  63.         uart_open(port);
  64.         return true;
  65. }

  66. void main_cdc_disable(uint8_t port)
  67. {
  68.         main_b_cdc_enable = false;
  69.         // Close communication
  70.         uart_close(port);
  71. }

  72. void main_cdc_set_dtr(uint8_t port, bool b_enable)
  73. {
  74.         if (b_enable) {
  75.                 // Host terminal has open COM
  76.                 ui_com_open(port);
  77.         }else{
  78.                 // Host terminal has close COM
  79.                 ui_com_close(port);
  80.         }
  81. }
运行结果:



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部