Tiny and portable usb host and device stack for mcu with usb ip

Overview

USB Stack

USB Stack 是一个跨平台的、用于嵌入式 MCU 的 USB 协议栈。其中 DEVICE 协议栈对标准设备请求、CLASS 请求、VENDOR 请求规范了一套统一的函数框架,从而对复合设备或者使用自定义设备类时,能够在极短的时间内进行添加和移植。同时提供了一套标准的 dcd porting 接口,供给不同的 MCU 使用,因此,通用性也非常高。此外在代码优美方面,以及内存占用方面也是相当出色。USB DEVICE 协议栈当前具有以下功能:

  • 支持 USB2.0 全速和高速设备
  • 支持端点中断注册功能,porting 给用户自己处理中断里的数据
  • 支持复合设备
  • 支持 Communication Class (CDC)
  • 支持 Human Interface Device (HID)
  • 支持 Custom human Interface Device (HID)
  • 支持 Mass Storage Class (MSC)
  • 支持 USB VIDEO CLASS (UVC)
  • 支持 USB AUDIO CLASS (UAC)
  • 支持 Device Firmware Upgrade CLASS (DFU)
  • 支持 USB MIDI CLASS (MIDI)
  • 支持 Test and Measurement CLASS (TMC)
  • 支持 Vendor 类 class
  • 支持 WINUSB1.0、WINUSB2.0

USB 参考手册

USB 协议栈实现

USB DEVICE 协议栈

USB DEVICE 协议栈的代码实现过程参考 https://www.bilibili.com/video/BV1Ef4y1t73d

USB HOST 协议栈

waiting....

USB DEVICE 协议栈 porting 接口

USB DEVICE 协议栈 porting 接口在 usb_stack/common/usb_dc.h 文件中声明,用户根据自己的 MCU 实现以下接口

USB DEVICE 控制器接口

用户需要实现 usb controller 相关寄存器初始化(可以命名为 usb_dc_init )以及在 USB 中断函数中,根据不同的中断标志调用 usbd_event_notify_handler

USB DEVICE 应用层接口

USB DEVICE 通用接口

usbd_desc_register

usbd_desc_register 用来注册 USB 描述符,描述符种类包括:设备描述符、配置描述符、接口描述符、字符串描述符、设备限定描述符。

void usbd_desc_register(const uint8_t *desc);
  • desc 描述符的句柄

usbd_msosv1_desc_register

usbd_msosv1_desc_register 用来注册一个 WINUSB 描述符,格式按照 struct usb_msosv1_descriptor

void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc);
  • desc 描述符句柄

usbd_class_add_interface

usbd_class_add_interface 用来给 USB 设备类增加接口,并将接口信息挂载在类的链表上。

void usbd_class_add_interface(usbd_class_t *class, usbd_interface_t *intf);
  • class USB 设备类的句柄
  • intf USB 设备接口的句柄

usbd_class_t 定义如下

typedef struct usbd_class {
    usb_slist_t list;
    const char *name;
    usb_slist_t intf_list;
} usbd_class_t;
  • list 类的链表节点
  • name 类的名称
  • intf_list 接口的链表节点

usbd_interface_t 定义如下

typedef struct usbd_interface {
    usb_slist_t list;
    /** Handler for USB Class specific Control (EP 0) communications */
    usbd_request_handler class_handler;
    /** Handler for USB Vendor specific commands */
    usbd_request_handler vendor_handler;
    /** Handler for USB custom specific commands */
    usbd_request_handler custom_handler;
    /** Handler for USB event notify commands */
    usbd_notify_handler notify_handler;
    uint8_t intf_num;
    usb_slist_t ep_list;
} usbd_interface_t;
  • list 接口的链表节点
  • class_handler class setup 请求回调函数
  • vendor_handler vendor setup 请求回调函数
  • custom_handler custom setup 请求回调函数
  • notify_handler 中断标志、协议栈相关状态回调函数
  • intf_num 当前接口偏移
  • ep_list 端点的链表节点

usbd_interface_add_endpoint

usbd_interface_add_endpoint 用来给 USB 接口增加端点,并将端点信息挂载在接口的链表上。

void usbd_interface_add_endpoint(usbd_interface_t *intf, usbd_endpoint_t *ep);
  • intf USB 设备接口的句柄
  • ep USB 设备端点的句柄

usbd_class_t 定义如下

typedef struct usbd_endpoint {
    usb_slist_t list;
    uint8_t ep_addr;
    usbd_endpoint_callback ep_cb;
} usbd_endpoint_t;
  • list 端点的链表节点
  • ep_addr 端点地址
  • ep_cb 端点中断回调函数

usb_device_is_configured

usb_device_is_configured 用来检查 USB 设备是否被配置(枚举)。

bool usb_device_is_configured(void);
  • return 配置状态, 0 表示未配置, 1 表示配置成功

USB Device CDC 类接口

usbd_cdc_add_acm_interface

usbd_cdc_add_acm_interface 用来给 USB CDC ACM 类添加接口,并重写该接口相关的函数。重写的函数包括 cdc_acm_class_request_handlercdc_notify_handler, 其中 cdc_acm_class_request_handler 用于处理 USB CDC ACM Setup 中断请求, cdc_notify_handler 用于实现 USB CDC 其他中断回调函数。

void usbd_cdc_add_acm_interface(usbd_class_t *class, usbd_interface_t *intf);
  • class 类的句柄
  • intf 接口句柄

usbd_cdc_acm_set_line_coding

usbd_cdc_acm_set_line_coding 用来对串口进行配置。该接口由用户实现,默认为空。

void usbd_cdc_acm_set_line_coding(uint32_t baudrate, uint8_t databits, uint8_t parity, uint8_t stopbits);
  • baudrate 波特率
  • databits 数据位
  • parity 校验位
  • stopbits 停止位

usbd_cdc_acm_set_dtr

usbd_cdc_acm_set_line_coding 用来控制串口 DTR。该接口由用户实现,默认为空。

void usbd_cdc_acm_set_dtr(bool dtr);
  • dtr dtr 为1表示拉低电平,为0表示拉高电平

usbd_cdc_acm_set_rts

usbd_cdc_acm_set_line_coding 用来控制串口 RTS。该接口由用户实现,默认为空。

void usbd_cdc_acm_set_rts(bool rts);
  • rts rts 为1表示拉低电平,为0表示拉高电平

USB Device MSC 类接口

usbd_msc_class_init

usbd_msc_class_init 用于初始化 USB MSC 类,注册 USB CDC ACM 设备并为其添加接口,且为接口添加 BLUK OUT 、BULK IN 端点及其回调函数。

void usbd_msc_class_init(uint8_t out_ep, uint8_t in_ep);
  • out_ep 输出端点的地址
  • in_ep 输入端点的地址

usbd_msc_get_cap

usbd_msc_get_cap 用来获取存储器的信息。该接口由用户实现,默认为空。

void usbd_msc_get_cap(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
  • lun 存储逻辑单元,暂时无用
  • block_num 存储扇区个数的指针
  • block_size 存储扇区大小的指针

usbd_msc_sector_read

usbd_msc_sector_read 用来对存储器某个扇区开始进行数据读取。该接口由用户实现,默认为空。

int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length);
  • sector 扇区偏移
  • buffer 存储读取的数据的指针
  • length 读取长度

usbd_msc_sector_write

usbd_msc_sector_write 用来对存储器某个扇区开始写入数据。该接口由用户实现,默认为空。

int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length);
  • sector 扇区偏移
  • buffer 写入数据指针
  • length 写入长度

USB Device HID 类接口

usbd_hid_add_interface

usbd_hid_add_interface 用来给 USB HID 类添加接口,并重写该接口相关的函数。重写的函数包括 hid_class_request_handlerhid_custom_request_handlerhid_notify_handler,其中 hid_class_request_handler 用来处理 USB HID 类的 Setup 中断请求, hid_custom_request_handler 用来处理 USB HID 获取描述符请求, hid_notify_handler 用来处理 USB HID 类的其他中断回调函数。

void usbd_hid_add_interface(usbd_class_t *class, usbd_interface_t *intf);
  • class 类的句柄
  • intf 接口句柄

usbd_hid_report_descriptor_register

usbd_hid_report_descriptor_register 用来对存储器某个扇区开始写入数据。该接口由用户实现,默认为空。

void usbd_hid_report_descriptor_register(uint8_t intf_num, const uint8_t *desc, uint32_t desc_len);
  • intf_num 当前 hid 报告描述符所在接口偏移
  • desc 报告描述符
  • desc_len 报告描述符长度

usbd_hid_set_request_callback

usbd_hid_set_request_callback 用来对存储器某个扇区开始写入数据。该接口由用户实现,默认为空。

void usbd_hid_set_request_callback( uint8_t intf_num,
                                    uint8_t (*get_report_callback)(uint8_t report_id, uint8_t report_type),
                                    void (*set_report_callback)(uint8_t report_id, uint8_t report_type, uint8_t *report, uint8_t report_len),
                                    uint8_t (*get_idle_callback)(uint8_t report_id),
                                    void (*set_idle_callback)(uint8_t report_id, uint8_t duration),
                                    void (*set_protocol_callback)(uint8_t protocol),
                                    uint8_t (*get_protocol_callback)(void));
  • intf_num 当前 hid 报告描述符所在接口偏移
  • get_report_callback get report命令处理回调函数
  • set_report_callback set report命令处理回调函数
  • get_idle_callback get idle命令处理回调函数
  • set_idle_callback set idle命令处理回调函数
  • set_protocol_callback set protocol命令处理回调函数
  • get_protocol_callback get protocol命令处理回调函数

USB Device AUDIO 类接口

usbd_audio_add_interface

usbd_audio_add_interface 用来给 USB Audio 类添加接口,并重写该接口相关的函数。重写的函数包括 audio_class_request_handleraudio_notify_handler

void usbd_audio_add_interface(usbd_class_t *class, usbd_interface_t *intf);
  • class 类的句柄
  • intf 接口句柄

USB Device VIDEO 类接口

usbd_video_add_interface

usbd_video_add_interface 用来给 USB Video 类添加接口,并重写该接口相关的函数。重写的函数包括 video_class_request_handlervideo_notify_handler

void usbd_video_add_interface(usbd_class_t *class, usbd_interface_t *intf);
  • class 类的句柄
  • intf 接口句柄

USB Device DFU 类接口

Comments
  • lsusb -v cdc device

    lsusb -v cdc device

    Every time I do this on Linux: lsusb -v

    Then a log appears on my device:

    [E/USB] descriptor <type:6,index:0> not found!
    [E/USB] standard request error
    [E/USB] standard request error
    

    On Linux, this is the output of my device:

    Bus 001 Device 061: ID 1a86:fe0c QinHeng Electronics Validator
    Device Descriptor:
      bLength                18
      bDescriptorType         1
      bcdUSB               2.00
      bDeviceClass          239 Miscellaneous Device
      bDeviceSubClass         2 
      bDeviceProtocol         1 Interface Association
      bMaxPacketSize0        64
      idVendor           0x1a86 QinHeng Electronics
      idProduct          0xfe0c 
      bcdDevice            1.00
      iManufacturer           1 TTT
      iProduct                2 TTTTTTTTT
      iSerial                 3 E339E33917A5383B2662B908
      bNumConfigurations      1
      Configuration Descriptor:
        bLength                 9
        bDescriptorType         2
        wTotalLength       0x004b
        bNumInterfaces          2
        bConfigurationValue     1
        iConfiguration          0 
        bmAttributes         0x80
          (Bus Powered)
        MaxPower              100mA
        Interface Association:
          bLength                 8
          bDescriptorType        11
          bFirstInterface         0
          bInterfaceCount         2
          bFunctionClass          2 Communications
          bFunctionSubClass       2 Abstract (modem)
          bFunctionProtocol       1 AT-commands (v.25ter)
          iFunction               0 
        Interface Descriptor:
          bLength                 9
          bDescriptorType         4
          bInterfaceNumber        0
          bAlternateSetting       0
          bNumEndpoints           1
          bInterfaceClass         2 Communications
          bInterfaceSubClass      2 Abstract (modem)
          bInterfaceProtocol      1 AT-commands (v.25ter)
          iInterface              2 Validator
          CDC Header:
            bcdCDC               1.10
          CDC Call Management:
            bmCapabilities       0x00
            bDataInterface          1
          CDC ACM:
            bmCapabilities       0x02
              line coding and serial state
          CDC Union:
            bMasterInterface        0
            bSlaveInterface         1 
          Endpoint Descriptor:
            bLength                 7
            bDescriptorType         5
            bEndpointAddress     0x83  EP 3 IN
            bmAttributes            3
              Transfer Type            Interrupt
              Synch Type               None
              Usage Type               Data
            wMaxPacketSize     0x0008  1x 8 bytes
            bInterval              16
        Interface Descriptor:
          bLength                 9
          bDescriptorType         4
          bInterfaceNumber        1
          bAlternateSetting       0
          bNumEndpoints           2
          bInterfaceClass        10 CDC Data
          bInterfaceSubClass      0 
          bInterfaceProtocol      0 
          iInterface              0 
          Endpoint Descriptor:
            bLength                 7
            bDescriptorType         5
            bEndpointAddress     0x02  EP 2 OUT
            bmAttributes            2
              Transfer Type            Bulk
              Synch Type               None
              Usage Type               Data
            wMaxPacketSize     0x0200  1x 512 bytes
            bInterval               0
          Endpoint Descriptor:
            bLength                 7
            bDescriptorType         5
            bEndpointAddress     0x81  EP 1 IN
            bmAttributes            2
              Transfer Type            Bulk
              Synch Type               None
              Usage Type               Data
            wMaxPacketSize     0x0200  1x 512 bytes
            bInterval               0
    can't get device qualifier: Resource temporarily unavailable
    can't get debug descriptor: Resource temporarily unavailable
    Device Status:     0x0000
      (Bus Powered)
    

    The project has a device qualifier descriptor and define CONFIG_USB_HS:

    #ifdef CONFIG_USB_HS
        ///////////////////////////////////////
        /// device qualifier descriptor
        ///////////////////////////////////////
        0x0a,
        USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
        0x00,
        0x02,
        0x02,
        0x02,
        0x01,
        0x40,
        0x01,
        0x00,
    #endif
        0x00
    

    In fact, I did not change anything in the descriptor, except for: vid, pid and 3 string . The sizes of string correctly counted

    opened by SeenTwo 10
  • usb hub 多次枚举导致 No memory to alloc hub class

    usb hub 多次枚举导致 No memory to alloc hub class

    测试STM32F407的USB主机模式的时候使用USB延长线连接hub,导致usb线非常的长,如果先将hid接入到hub再接入控制器中,不会有问题;如果先将hub接入控制器,再将hid接入hub,则会出现
    image 跟踪调试之后发现,在后一种情况下,hid接入hub时,再中断函数中检查 USB_OTG_HPRT 寄存器值为 0x00021008,之后唤醒 usb-hub线程进行事件处理,在 usb-hub 线程中再次读取 USB_OTG_HPRT 寄存器值为 0x00021401,状态再次变成上电,导致hub设备在没有调用 CLASS_DISCONNECT 的情况下再次调用 CLASS_CONNECT,引发 No memory to alloc hub class 错误,并且再次拔插USB也无法恢复,必须重启stm32

    opened by supf1994 8
  • Synopsys/EHCI/OHCI support

    Synopsys/EHCI/OHCI support

    @sakumisu 您好,有几个问题请教一下: 1)CherryUSB软件包是否是独立于RT-Thread原生自带的USB驱动框架,即单独打开软件包即可使用,不需要打开RT-Thread的components/drivers/usb? 2)CherryUSB中所支持的Synopsys IP,是DesignWare HS OTG Controller吗?若是,是否已支持高速host和高速device?(我目前在跑Linux,用的是原生的dwc2的驱动) 3)CherryUSB中已支持EHCI,但未支持OHCI,是否意味着类似全志的SoC中支持标准EHCI/OHCI的USB host暂时无法使用全速/低速设备(如USB鼠标/键盘等)?

    希望能得到解答,谢谢!

    opened by Devilliv 8
  • Supported BSP for USBD_Video

    Supported BSP for USBD_Video

    你好,

    请问 USB Device 模拟摄像头现在除了 BL706_AVB 还支持其他开发板吗?似乎 BL706_AVB 只能在论坛申请。

    https://dev.bouffalolab.com/media/doc/sdk/bl_mcu_sdk_zh/samples/advance%20samples/usb/usbd_video.html

    opened by wuhanstudio 7
  • provide a way to override default SCSI_inquiry name

    provide a way to override default SCSI_inquiry name

    here is the hardcoded define in usbd_msc.c:461

    static bool SCSI_inquiry(uint8_t **data, uint32_t *len)
    {
        //...some code
    
        uint8_t inquiry[SCSIRESP_INQUIRY_SIZEOF] = {
            /* 36 */
    
            /* LUN 0 */
            0x00,
            0x80,
            0x02,
            0x02,
            (SCSIRESP_INQUIRY_SIZEOF - 5),
            0x00,
            0x00,
            0x00,
            'B', 'o', 'u', 'f', 'f', 'a', 'l', 'o', /* Manufacturer : 8 bytes */
            'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product      : 16 Bytes */
            ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
            '0', '.', '0', '1' /* Version      : 4 Bytes */
        };
    
    

    Thanks.

    opened by QIANWC 7
  • [E/USB] ep:86 clear halt

    [E/USB] ep:86 clear halt

    stm32f103c8t6 demo\cdc_acm_hid_msc_template.c need modify usb_dc_fsdev.c USB_RAM_SIZE to 1024

    #ifndef USB_RAM_SIZE
    #define USB_RAM_SIZE 512
    #endif
    

    and hid endpoint get errors USB_REQUEST_CLEAR_FEATURE: [E/USB] ep:86 clear halt

    导致hid设备出现感叹号,这个应该如何解决

    opened by SmartElec 6
  • ch32v307 cdc aсm

    ch32v307 cdc aсm

    Tried your cdc acm example for my ch32v307 chip. On Linux, there are problems with registering a usb device:

    [597460.594304] usb 1-6: new high-speed USB device number 68 using xhci_hcd
    [597460.722349] usb 1-6: device descriptor read/64, error 18
    [597460.958271] usb 1-6: device descriptor read/64, error 18
    [597461.198248] usb 1-6: new high-speed USB device number 69 using xhci_hcd
    [597461.326338] usb 1-6: device descriptor read/64, error 18
    [597461.562305] usb 1-6: device descriptor read/64, error 18
    [597461.670409] usb usb1-port6: attempt power cycle
    [597462.086277] usb 1-6: new high-speed USB device number 70 using xhci_hcd
    [597462.110555] usb 1-6: device descriptor read/8, error -61
    [597462.242559] usb 1-6: device descriptor read/8, error -61
    

    If you use CherryUSB from this old repository, then everything works:

    [597604.323505] usb 1-6: new high-speed USB device number 72 using xhci_hcd
    [597604.471899] usb 1-6: New USB device found, idVendor=ffff, idProduct=ffff, bcdDevice= 1.00
    [597604.471905] usb 1-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    [597604.471909] usb 1-6: Product: CherryUSB CDC DEMO
    [597604.471912] usb 1-6: Manufacturer: CherryUSB
    [597604.471915] usb 1-6: SerialNumber: 2022123456
    [597604.477337] cdc_acm 1-6:1.0: ttyACM1: USB ACM device
    

    Why is this happening?

    opened by SeenTwo 5
  • ubuntu系统下,git clone后,直接git diff会提示CRLF 将被 LF 替换

    ubuntu系统下,git clone后,直接git diff会提示CRLF 将被 LF 替换

    warning: class/audio/usbd_audio.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/cdc/usbd_cdc.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/cdc/usbh_cdc_acm.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/cdc/usbh_cdc_acm.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/hid/usbd_hid.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/hid/usbh_hid.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/hid/usbh_hid.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/hub/usbh_hub.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/hub/usbh_hub.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/msc/usbd_msc.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/msc/usbh_msc.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/msc/usbh_msc.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: class/video/usbd_video.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: common/usb_dc.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: common/usb_def.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: common/usb_util.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: core/usbd_core.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: core/usbd_core.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: core/usbh_core.c 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: core/usbh_core.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。 warning: usb_config.h 中的 CRLF 将被 LF 替换。< 在工作区中该文件仍保持原有的换行符。

    opened by jianjunjiang 4
  • 修复./core/usbd_core.c以及./port/musb/usbd_dc_musb.c中的bug

    修复./core/usbd_core.c以及./port/musb/usbd_dc_musb.c中的bug

    1. 修复./core/usbd_core.c中bug
    • line 370,#ifdef CONFIG_USBDEV_TEST_MODE改为#if CONFIG_USBDEV_TEST_MODE
    • line897,添加memcpy(usbd_core_cfg.req_data, usbd_core_cfg.ep0_data_buf, nbytes)用于在循环读取数据时将数据备份到数组usbd_core_cfg.req_data中,以便数据读取结束后进行后续的请求处理。
    1. 修复./port/musb/usbd_dc_musb.c中的bug
    • 修复读8bit寄存器时按16bit读取的问题
    • 删除对只读寄存器的写操作
    • 优化中断程序的控制流程
    opened by fuyunezu 2
  • Very minor typo in directory name

    Very minor typo in directory name

    I don't know how to create a PR for this.

    In https://github.com/sakumisu/CherryUSB/tree/master/docs the first directory should probably be "assets" (resources belonging to a project" and not "asserts" (like a programmer checking that something is true).

    Looks like a cool project! Congrats on creating it.

    opened by robertlipe 2
  • 关于USB转换芯片MAX3421E的协议栈移植问题

    关于USB转换芯片MAX3421E的协议栈移植问题

    由于使用的芯片没有USB主机功能,增加了一个美信的MAX3421E芯片,将SPI转USB,但是该芯片不提供协议栈,只有基础的驱动程序。MAX3421E的寄存器和通常的MCU USB IP寄存器不一样,不知道能否移植这个协议栈使用?
    看了下这个协议栈的移植例程,似乎是需要自己实现usb_dc_xxx层的驱动,为usbh_core.c提供接口函数,那么是否可以在usb_dc_xxx中直接对MAX3421E驱动的USB传输函数进行二次封装,而不实现寄存器映射表?
    如果能提供一些建议,将不胜感激!
    
    opened by Yoomia 2
  • cmsis-dap_v2.1_tempate.c

    cmsis-dap_v2.1_tempate.c

    我正在stm32f103cb上制作一个daplink, 然后希望之后能平滑移植到其他mcu平台 我已经完成了cdc的功能, 已经可以连接msh 但是使用了cmsis-dap_v2.1_tempate.c之后出了一些问题 比如初始化的时候: [I/USB] Setup: bmRequestType 0x80, bRequest 0x06, wValue 0x0600, wIndex 0x0000, wLength 0x000a [I/USB] Open endpoint:0x81 type:2 mps:64 [I/USB] Open endpoint:0x2 type:2 mps:64 [I/USB] Open endpoint:0x85 type:3 mps:8 [E/USB] Ep addr 133 overflow [I/USB] Open endpoint:0x4 type:2 mps:64 [E/USB] Ep addr 4 overflow [I/USB] Open endpoint:0x83 type:2 mps:64 [I/USB] get Compat ID [I/USB] get Compat ID cdc我在使用cdc_acm_template.c时调试通过, 但是在使用daplink的cdc时,一旦pc连接串口,就会不停的上报错误: [E/USB] ep:85 clear halt [E/USB] ep:85 clear halt [E/USB] ep:85 clear halt [E/USB] ep:85 clear haltt 断开串口的连接错误终止, 不知道是我哪里的使用有问题? https://cherryusb.readthedocs.io/zh_CN/latest/index.html 在文档中也没有找到相关的介绍内容

    opened by myshowtogo 0
  • stm32f107 dwc2 rndis device

    stm32f107 dwc2 rndis device

    I tried to run rndis with lwip on stm32f107. The device is detected, windows receives an ip address and a gateway via dhcp, but network manager freezes and it is impossible to turn on the network interface. What am I doing wrong?

    • Im defined:
    #define CONFIG_USBDEV_RNDIS_USING_LWIP
    #define CONFIG_USBDEV_REQUEST_BUFFER_LEN 256
    
    #ifndef CONFIG_USBDEV_RNDIS_RESP_BUFFER_SIZE
    #define CONFIG_USBDEV_RNDIS_RESP_BUFFER_SIZE 128
    #endif
    
    #ifndef CONFIG_USBDEV_RNDIS_ETH_MAX_FRAME_SIZE
    #define CONFIG_USBDEV_RNDIS_ETH_MAX_FRAME_SIZE 1536
    #endif
    
    #ifndef CONFIG_USBDEV_RNDIS_VENDOR_ID
    #define CONFIG_USBDEV_RNDIS_VENDOR_ID 0x0000ffff
    #endif
    
    #ifndef CONFIG_USBDEV_RNDIS_VENDOR_DESC
    #define CONFIG_USBDEV_RNDIS_VENDOR_DESC "CherryUSB"
    #endif
    
    • Wrote initialization functions:
    static struct usbd_interface intf0;
    static struct usbd_interface intf1;
    static uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
    
    void cdc_rndis_init(void)
    {
        usbd_desc_register(cdc_descriptor);
        usbd_add_interface(usbd_rndis_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, mac));
        usbd_add_interface(usbd_rndis_init_intf(&intf1, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, mac));
        usbd_initialize();
    }
    
    void usb_dc_low_level_init(void)
    {
    	 __HAL_RCC_GPIOA_CLK_ENABLE();
    	  GPIO_InitTypeDef GPIO_InitStruct = {0};
    		
        GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    	
       __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
    	 
       HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
       HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
    }
    
    void usb_dc_low_level_deinit(void)
    {
    	 __HAL_RCC_USB_OTG_FS_CLK_DISABLE();
       HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
    }
    
    • And lwip initialization:
    static struct netif rndis_netif; //network interface
    
    static const ip_addr_t ipaddr  = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
    static const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
    static const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
    
    err_t output_fn(struct netif *netif, struct pbuf *p, const ip_addr_t *ipaddr)
    {
        return etharp_output(netif, p, ipaddr);
    }
    
    err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
    {
    	static int ret;
    	ret = usbd_rndis_eth_tx(p);
    	
    	if(ret == 0)
    		return ERR_OK;
    	else
    		return ERR_BUF;
    }
    
    err_t rndisif_init(struct netif *netif)
    {
        LWIP_ASSERT("netif != NULL", (netif != NULL));
        netif->mtu = 1500;
        netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
        netif->state = NULL;
        netif->name[0] = IFNAME0;
        netif->name[1] = IFNAME1;
        netif->output = output_fn;
        netif->linkoutput = linkoutput_fn;
        return ERR_OK;
    }
    
    err_t rndisif_input(struct netif *netif)
    {
    	static err_t err;
    	static struct pbuf *p;
    	p = usbd_rndis_eth_rx();
    	if(p != NULL)
    	{
    		err = netif->input(p, netif);
    		if(err != ERR_OK)
    		{
    			pbuf_free(p);
    		}
    	}
    	else
    	{
    		return ERR_BUF;
    	}
      return err;
    }
    
    #define NUM_DHCP_ENTRY 3
    
    static dhcp_entry_t entries[NUM_DHCP_ENTRY] =
    {
        /* mac    ip address        subnet mask        lease time */
        { {0}, {192, 168, 17, 2}, {255, 255, 255, 0}, 24 * 60 * 60 },
        { {0}, {192, 168, 17, 3}, {255, 255, 255, 0}, 24 * 60 * 60 },
        { {0}, {192, 168, 17, 4}, {255, 255, 255, 0}, 24 * 60 * 60 }
    };
    
    static dhcp_config_t dhcp_config =
    {
        {192, 168, 17, 1}, 67, /* server address, port */
        {192, 168, 17, 1},     /* dns server */
        "stm",                /* dns suffix */
        NUM_DHCP_ENTRY,       /* num entry */
        entries               /* entries */
    };
    
    uint32_t dns_query_proc(const char *name, ip_addr_t *addr)
    {
        if (strcmp(name, "run.stm") == 0 || strcmp(name, "www.run.stm") == 0)
        {
            addr->addr = ipaddr.addr;
            return 1;
        }
        return 0;
    }
    
    void LwIP_Init(void)
    {
        struct netif  *netif = &rndis_netif;
    
        lwip_init();
    
        netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, rndisif_init, ethernet_input);
        netif_set_default(netif);
    	dhserv_init(&dhcp_config);
    	dnserv_init(&ipaddr, PORT_DNS, dns_query_proc);
    }
    
    /**
    * @brief  Called when a frame is received
    * @param  None
    * @retval None
    */
    void LwIP_Pkt_Handle(void)
    {
      /* Read a received packet from the buffers and send it to the lwIP for handling */
      rndisif_input(&rndis_netif);
    	//netif_is_up(&rndis_netif);
    }
    

    I call the LwIP_Pkt_Handle function in the while loop.

    opened by alexpa940 8
  • GD32 dwc2驱动的GCCFG_NOVBUSSENS寄存器兼容性和stm32存在区别

    GD32 dwc2驱动的GCCFG_NOVBUSSENS寄存器兼容性和stm32存在区别

    虽然dwc2的文档里面写的支持GD32F30X_CL, 但在GD32305R-START(GD32F30X_CL)的开发板上测试的时候连电脑识别失败

    和gd32的官方的代码对比了下, 发现不开CONFIG_DWC2_VBUS_SENSING_ENABLE的时候, gd32即使设置了USB_OTG_GCCFG_NOVBUSSENS, 也必须设置USB_OTG_GCCFG_VBUSASEN/_VBUSBSEN才能正常工作(gd32官方驱动这里就和stm32的有区别, gd32官方设置的是两个都设置了, 实测设置至少其一就能工作, 不知道a和b啥区别, stm32是两个都取消了) (另参考http://www.elmagnifico.tech/2021/06/21/CUBEMX-GD32F450-USB/#usbfs_gccfg)

    https://github.com/sakumisu/CherryUSB/blob/f1a5345f26c645e2e1d30dbccb0eee19490e0619/port/dwc2/usb_dc_dwc2.c#L574-L580 虽然没用但怀疑usb_hc_dwc2也是一样的 这里希望能加个宏定义或者在USB IP 勘误文档中给个说明

        #ifdef CONFIG_DWC2_GD32
        USB_OTG_GLB->GCCFG |= USB_OTG_GCCFG_VBUSASEN | USB_OTG_GCCFG_VBUSBSEN;
        #else
        USB_OTG_GLB->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
        USB_OTG_GLB->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
        #endif
    
    bug 
    opened by yyjdelete 1
  • Later roadmap

    Later roadmap

    • class driver

    • [x] msc device driver with os,support filesystem interface

    • [x] printer class support, device and host

    • [ ] mtp class support, device and host

    • [ ] cdc rndis host

    • [x] cdc rndis device

    • [x] hub process optimise in host mode

    • [ ] ftdi simulation,Copyright?

    • [ ] adb server driver

    • port

    • [x] synopsys dwc2 device port optimise

    • [x] synopsys dwc2 host port optimise

    • [ ] renesas usb ip port

    • [ ] rp2040 port ?

    • [ ] ohci port

    • [x] ehci with iso transfer

    • [x] xhci port

    • application

    • [x] daplink template for all chips

    • [x] audio with i2s or audio codec

    • [x] video with dvp

    • [ ] network with eth

    • [ ] network with wifi

    • [ ] network with printer,downloading file from net and print through printer

    • [ ] uf2

    • [ ] usb2 all bus:uart、i2c、spi、can

    • [x] dfu download with st tool

    • tool

    • [ ] ci test

    • [ ] config tool update:winusb desc、hid report desc、audio desc、video desc

    • [ ] class Stress tests scripts

    • [ ] winusb windows and linux driver

    opened by sakumisu 9
  • USB-MTP(Media Transfer Protocol)

    USB-MTP(Media Transfer Protocol)

    是否考虑增加 MTP?

    设备需要通过USB与PC进行文件传输,目前有两种方案 1、使用大容量存储(MSC) 使用MSC需要配合FAT文件系统, 由于FAT没有擦写均衡以及掉电保护,不适合用于设备中。

    2、使用MTP 可以兼顾文件系统的性能以及与PC交互的便捷性,目前看一些USB协议栈好像都没支持这种class,不知道是难度太大还是需求太少。

    feature 
    opened by HubertXie 3
Releases(v0.7.0)
  • v0.7.0(Nov 19, 2022)

    Common

    • Move malloc, free, printf into xxx_config.h
    • Fix usb_osal_sem_take api when using rt-thread
    • Add daplink support
    • Add CHERRYUSB_VERSION
    • Update sconscript for hpm and rndis

    Device

    • Update rndis driver and demo with rt-thread
    • Remove intf with malloc, use static instead

    Host

    • Remove bulk_transfer and intr_transfer
    • Fix hid set_idle cmd, ignore this cmd when failed
    • Update video driver and template

    Port

    • Add rp2040 device port
    • Update ch58x port
    • Fix dwc2 ep config when reset, fix USB_OTG_HPRT_PRES with USB_OTG_HPRT_PRST, fix tx empty irq process
    • Add dwc2 host iso support
    • Fix musb bugs in irq handler
    • Remove mm32, imxrt, nuvoton port
    Source code(tar.gz)
    Source code(zip)
  • v0.6.0(Sep 28, 2022)

    Common

    • Move all usb transfer memories into no cache ram
    • Update fatfs port
    • Update readme and doc
    • Update hpm demo

    Device

    • Fix hid duration
    • Add msc readonly support
    • Fix video head toggle process
    • Update dfu and rndis class driver
    • Add usb_deinit function

    Host

    • Refactor usb host stack
    • Complete and optimise hub processs
    • Add uvc class support
    • Add mult support
    • Add interface altsettings support

    Port

    • Update ehci driver, add iso support for high speed
    • Add xhci driver support
    • Update nrf5x dcd driver
    • Fix dwc2 dcd driver ep_mps overflow in high speed,move out cmsis dependence
    • Add bcdr register for fsdev
    Source code(tar.gz)
    Source code(zip)
  • v0.5.2(Sep 9, 2022)

    • refactor usb device intf and endpoint register api
    • add ep_enable for dcd write read api to check if ep is enable.
    • update dwc2 iso transfer
    • remove custom handler, split class handler into interface and endpoint.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.1(Aug 20, 2022)

Owner
sakumisu
sakumisu
ESP32 software USB host through general IO pins. We can connect up to 4 USB-LS HID (keyboard mouse joystick) devices simultaneously.

esp32_usb_soft_host esp32 USB-LS pure software host thru general IO pins. Up to 4 HID devices simultaneously. board ~$3 :https://www.aliexpress.com/pr

Samsonov Dima 313 Jan 1, 2023
ESP8266 software USB host through general IO pins. We can connect up to 2 USB-LS HID (keyboard mouse joystick) devices simultaneously.

esp8266_usb_soft_host Test for esp8266 usb host . Works with ESP-IDF v3.4 80MHz and 160MHz. WorkInProgress Test run with mouse & combined mouse & keyb

Samsonov Dima 28 Sep 1, 2022
ESP32S2 Arduino USB host printer, MIDI, and keyboard demos

ESP32 S2 USB Host Jumpstart A collection of experiments and demos using Espressif ESP32 S2 in USB host mode. Most of the sketches have little or no C+

null 31 Jan 4, 2023
esp32S2 USB HID host example.

Simple usb host HID example Since usb host support is still beta stage and include file is in private_include i copied component to this example. Code

null 16 Oct 10, 2022
ESP32 S2 USB host with examples.

This new USB host library based is based on changed and hopefully somehow final API from espressif. The idea is to make this library usable with esp-i

null 22 Nov 10, 2022
USB host implementation using PIO of raspberry pi pico (RP2040).

Pico-PIO-USB USB host implementation using PIO of raspberry pi pico (RP2040). You can add additional USB port to RP2040. ?? This library is WIP. API m

null 730 Dec 30, 2022
LoRa Driver for Semtech SX1262 on Linux (PineDio USB Adapter) and BL602 (PineDio Stack BL604)

LoRa Driver for Semtech SX1262 on Linux (PineDio USB Adapter) and BL602 (PineDio Stack BL604) Read the articles... "Build a Linux Driver for PineDio L

Lee Lup Yuen 8 Sep 17, 2022
Firmware for DMR transceivers using the NXP MK22 MCU, AT1846S RF chip and HR-C6000 DMR chipset. Including the Radioddiy GD-77, Baofeng DM-1801 and Baofeng RD-5R.

OpenGD77 Firmware for DMR transceivers using the NXP MK22 MCU, AT1846S RF chip and HR-C6000 DMR chipset. This includes the Radioddiy GD-77, Radioddity

Open Ham 109 Dec 31, 2022
Tiny and portable Game Boy emulator written in C.

TinyGB TinyGB is a tiny and portable Game Boy emulator written entirely in C as a hobbyist one-man project. I only wrote this to deepen my understandi

Jewel 23 Dec 24, 2022
A simple tool using PC mouse via USART to control MCU and LCD/OLED (with LVGL library), if your screen cannot be controlled by touch.

LVGL_USB_Mouse A simple tool using PC mouse via USART to control MCU and LCD/OLED (with LVGL library), if your screen cannot be controlled by touch. 如

k_ying 5 May 5, 2022
Create a firework effect with WS2812b LED and a MCU

LED-Fireworks Firework effect with WS2812b LED and a MCU This project uses FastLED library to control the LED strip. WS2812b IC is embedded into each

null 75 Dec 5, 2022
bl_mcu_sdk is MCU software development kit provided by Bouffalo Lab Team for BL602/BL604, BL702/BL704/BL706 and other series of RISC-V based chips in the future.

bl mcu sdk is an MCU software development kit provided by the Bouffalo Lab Team for BL602/BL604, BL702/BL704/BL706 and other series of chips in the future

Bouffalo Lab 165 Dec 23, 2022
Arduino code for a high speed 8000hz wired mouse using a teensy 4 MCU

teensy4_mouse Arduino code for a high speed 8000Hz wired mouse using a teensy 4 MCU. This code is inspired by https://github.com/mrjohnk/PMW3360DM-T2Q

Herbert Trip 9 Nov 19, 2022
Single-chip solution for Hi-speed USB2.0(480Mbps) JTAG/SPI Debugger based on RISC-V MCU CH32V30x/CH32V20x

480Mbps High Speed USB2.0 JTAG Debugger Open source information |-- bin |--------MCU: MCU target program |--------WIN APP |------------------USB20Jtag

RISC-V 58 Jan 5, 2023
Cross-OS library for implementing USB device-mode interfaces

libusbd Cross-OS library for implementing USB device-mode interfaces. WIP. I'm currently working out most of the API details with the programs in exam

Max Thomas 46 Dec 13, 2022
A tiny external monitor for PC using STM32 and ST7789. Connects to PC over USB and displays the captured screen on ST7789 (240x240) display.

STM32 Tiny Monitor A super tiny monitor for your PC, suitable for your pet ant. A python script sends the captured screen over USB to the STM32 microc

Avra Mitra 69 Dec 27, 2022
USB-C_PCB_experiments - USB-C ports made from a flex PCB and an ATtiny84A

USB-C PCB Experiments This is part of an ongoing series of projects involving creative interpretations of the USB mechanical standards. You've probabl

Sam Ettinger 23 Sep 5, 2022
Anotter USB temperature logger that can record up to four channels with thermocouple or NTCs connected via CDC directly or SCPI to USB.

temperature-logger Anotter USB temperature logger that can record up to four channels with thermocouple or NTCs connected via CDC directly or SCPI to

Jana Marie Hemsing 50 Nov 24, 2022
Command line C++ and Python VSTi Host library with MFCC, FFT, RMS and audio extraction and .wav writing.

______ _ ___ ___ | ___ \ | | | \/ | | |_/ /___ _ __ __| | ___ _ __| . . | __ _ _ __

Leon Fedden 297 Dec 30, 2022