微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 请教stm32 usb虚拟串口程序移植到gd32的问题

请教stm32 usb虚拟串口程序移植到gd32的问题

时间:10-02 整理:3721RD 点击:
使用的是 libopencm3的库 和 它的例子, 就是个简单的usb虚拟串口, 能够回显输入的内容。
把其中的 rcc_clock_setup_in_hse_8mhz_out_72mhz(); 改成 rcc_clock_setup_in_hse_12mhz_out_72mhz(); 就能识别设备了, 但用putty打开后一开始输入的一些字符能回显, 但是很快就不再显示了。 同样的程序在stm32上面正常。 请教这是什么原因呢? 多谢了

https://raw.githubusercontent.com/libopencm3/libopencm3-examples/master/examples/stm32/f1/stm32-maple/usb_cdcacm/cdcacm.c

  1. /*
  2. * This file is part of the libopencm3 project.
  3. *
  4. * Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
  5. *
  6. * This library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this library.  If not, see <http://www.gnu.org/licenses/>.
  18. */

  19. #include <stdlib.h>
  20. #include <libopencm3/stm32/rcc.h>
  21. #include <libopencm3/stm32/gpio.h>
  22. #include <libopencm3/usb/usbd.h>
  23. #include <libopencm3/usb/cdc.h>
  24. #include <libopencm3/cm3/scb.h>

  25. static const struct usb_device_descriptor dev = {
  26.         .bLength = USB_DT_DEVICE_SIZE,
  27.         .bDescriptorType = USB_DT_DEVICE,
  28.         .bcdUSB = 0x0200,
  29.         .bDeviceClass = USB_CLASS_CDC,
  30.         .bDeviceSubClass = 0,
  31.         .bDeviceProtocol = 0,
  32.         .bMaxPacketSize0 = 64,
  33.         .idVendor = 0x0483,
  34.         .idProduct = 0x5740,
  35.         .bcdDevice = 0x0200,
  36.         .iManufacturer = 1,
  37.         .iProduct = 2,
  38.         .iSerialNumber = 3,
  39.         .bNumConfigurations = 1,
  40. };

  41. /*
  42. * This notification endpoint isn't implemented. According to CDC spec its
  43. * optional, but its absence causes a NULL pointer dereference in Linux
  44. * cdc_acm driver.
  45. */
  46. static const struct usb_endpoint_descriptor comm_endp[] = {{
  47.         .bLength = USB_DT_ENDPOINT_SIZE,
  48.         .bDescriptorType = USB_DT_ENDPOINT,
  49.         .bEndpointAddress = 0x83,
  50.         .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
  51.         .wMaxPacketSize = 16,
  52.         .bInterval = 255,
  53. }};

  54. static const struct usb_endpoint_descriptor data_endp[] = {{
  55.         .bLength = USB_DT_ENDPOINT_SIZE,
  56.         .bDescriptorType = USB_DT_ENDPOINT,
  57.         .bEndpointAddress = 0x01,
  58.         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
  59.         .wMaxPacketSize = 64,
  60.         .bInterval = 1,
  61. }, {
  62.         .bLength = USB_DT_ENDPOINT_SIZE,
  63.         .bDescriptorType = USB_DT_ENDPOINT,
  64.         .bEndpointAddress = 0x82,
  65.         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
  66.         .wMaxPacketSize = 64,
  67.         .bInterval = 1,
  68. }};

  69. static const struct {
  70.         struct usb_cdc_header_descriptor header;
  71.         struct usb_cdc_call_management_descriptor call_mgmt;
  72.         struct usb_cdc_acm_descriptor acm;
  73.         struct usb_cdc_union_descriptor cdc_union;
  74. } __attribute__((packed)) cdcacm_functional_descriptors = {
  75.         .header = {
  76.                 .bFunctionLength = sizeof(struct usb_cdc_header_descriptor),
  77.                 .bDescriptorType = CS_INTERFACE,
  78.                 .bDescriptorSubtype = USB_CDC_TYPE_HEADER,
  79.                 .bcdCDC = 0x0110,
  80.         },
  81.         .call_mgmt = {
  82.                 .bFunctionLength =
  83.                         sizeof(struct usb_cdc_call_management_descriptor),
  84.                 .bDescriptorType = CS_INTERFACE,
  85.                 .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT,
  86.                 .bmCapabilities = 0,
  87.                 .bDataInterface = 1,
  88.         },
  89.         .acm = {
  90.                 .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor),
  91.                 .bDescriptorType = CS_INTERFACE,
  92.                 .bDescriptorSubtype = USB_CDC_TYPE_ACM,
  93.                 .bmCapabilities = 0,
  94.         },
  95.         .cdc_union = {
  96.                 .bFunctionLength = sizeof(struct usb_cdc_union_descriptor),
  97.                 .bDescriptorType = CS_INTERFACE,
  98.                 .bDescriptorSubtype = USB_CDC_TYPE_UNION,
  99.                 .bControlInterface = 0,
  100.                 .bSubordinateInterface0 = 1,
  101.          },
  102. };

  103. static const struct usb_interface_descriptor comm_iface[] = {{
  104.         .bLength = USB_DT_INTERFACE_SIZE,
  105.         .bDescriptorType = USB_DT_INTERFACE,
  106.         .bInterfaceNumber = 0,
  107.         .bAlternateSetting = 0,
  108.         .bNumEndpoints = 1,
  109.         .bInterfaceClass = USB_CLASS_CDC,
  110.         .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  111.         .bInterfaceProtocol = USB_CDC_PROTOCOL_AT,
  112.         .iInterface = 0,

  113.         .endpoint = comm_endp,

  114.         .extra = &cdcacm_functional_descriptors,
  115.         .extralen = sizeof(cdcacm_functional_descriptors),
  116. }};

  117. static const struct usb_interface_descriptor data_iface[] = {{
  118.         .bLength = USB_DT_INTERFACE_SIZE,
  119.         .bDescriptorType = USB_DT_INTERFACE,
  120.         .bInterfaceNumber = 1,
  121.         .bAlternateSetting = 0,
  122.         .bNumEndpoints = 2,
  123.         .bInterfaceClass = USB_CLASS_DATA,
  124.         .bInterfaceSubClass = 0,
  125.         .bInterfaceProtocol = 0,
  126.         .iInterface = 0,

  127.         .endpoint = data_endp,
  128. }};

  129. static const struct usb_interface ifaces[] = {{
  130.         .num_altsetting = 1,
  131.         .altsetting = comm_iface,
  132. }, {
  133.         .num_altsetting = 1,
  134.         .altsetting = data_iface,
  135. }};

  136. static const struct usb_config_descriptor config = {
  137.         .bLength = USB_DT_CONFIGURATION_SIZE,
  138.         .bDescriptorType = USB_DT_CONFIGURATION,
  139.         .wTotalLength = 0,
  140.         .bNumInterfaces = 2,
  141.         .bConfigurationValue = 1,
  142.         .iConfiguration = 0,
  143.         .bmAttributes = 0x80,
  144.         .bMaxPower = 0x32,

  145.         .interface = ifaces,
  146. };

  147. static const char *usb_strings[] = {
  148.         "Black Sphere Technologies",
  149.         "CDC-ACM Demo",
  150.         "DEMO",
  151. };

  152. /* Buffer to be used for control requests. */
  153. uint8_t usbd_control_buffer[128];

  154. static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf,
  155.                 uint16_t *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
  156. {
  157.         (void)complete;
  158.         (void)buf;
  159.         (void)usbd_dev;

  160.         switch (req->bRequest) {
  161.         case USB_CDC_REQ_SET_CONTROL_LINE_STATE: {
  162.                 /*
  163.                  * This Linux cdc_acm driver requires this to be implemented
  164.                  * even though it's optional in the CDC spec, and we don't
  165.                  * advertise it in the ACM functional descriptor.
  166.                  */
  167.                 char local_buf[10];
  168.                 struct usb_cdc_notification *notif = (void *)local_buf;

  169.                 /* We echo signals back to host as notification. */
  170.                 notif->bmRequestType = 0xA1;
  171.                 notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
  172.                 notif->wValue = 0;
  173.                 notif->wIndex = 0;
  174.                 notif->wLength = 2;
  175.                 local_buf[8] = req->wValue & 3;
  176.                 local_buf[9] = 0;
  177.                 // usbd_ep_write_packet(0x83, buf, 10);
  178.                 return 1;
  179.                 }
  180.         case USB_CDC_REQ_SET_LINE_CODING:
  181.                 if (*len < sizeof(struct usb_cdc_line_coding))
  182.                         return 0;
  183.                 return 1;
  184.         }
  185.         return 0;
  186. }

  187. static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
  188. {
  189.         (void)ep;
  190.         (void)usbd_dev;

  191.         char buf[64];
  192.         int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64);

  193.         if (len) {
  194.                 usbd_ep_write_packet(usbd_dev, 0x82, buf, len);
  195.                 buf[len] = 0;
  196.         }
  197. }

  198. static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue)
  199. {
  200.         (void)wValue;
  201.         (void)usbd_dev;

  202.         usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb);
  203.         usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL);
  204.         usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);

  205.         usbd_register_control_callback(
  206.                                 usbd_dev,
  207.                                 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
  208.                                 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
  209.                                 cdcacm_control_request);
  210. }

  211. int main(void)
  212. {
  213.         int i;

  214.         usbd_device *usbd_dev;

  215.         SCB_VTOR = (uint32_t) 0x08005000;

  216.         rcc_clock_setup_in_hse_8mhz_out_72mhz();

  217.         rcc_periph_clock_enable(RCC_GPIOA);
  218.         rcc_periph_clock_enable(RCC_GPIOC);

  219.         /* Setup GPIOC Pin 12 to pull up the D+ high, so autodect works
  220.          * with the bootloader.  The circuit is active low. */
  221.         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
  222.                       GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12);
  223.         gpio_clear(GPIOC, GPIO12);

  224.         /* Setup GPIOA Pin 5 for the LED */
  225.         gpio_set(GPIOA, GPIO5);
  226.         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
  227.                       GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);

  228.         usbd_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer));
  229.         usbd_register_set_config_callback(usbd_dev, cdcacm_set_config);

  230.         for (i = 0; i < 0x800000; i++)
  231.                 __asm__("nop");
  232.         gpio_clear(GPIOA, GPIO5);

  233.         while (1)
  234.                 usbd_poll(usbd_dev);
  235. }

复制代码


没人知道么,看来还是不能用gd的产品啊

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top