CuVoodoo STM32F1 firmware template
Macros | Functions | Variables
usb_cdcacm.c File Reference

library for USB CDC ACM communication (code) More...

Go to the source code of this file.

Macros

#define USB_DATA_TRANSFER_SIZE   64
 maximum packet size for USB data transfer
 

Functions

struct {
   struct usb_cdc_header_descriptor   header
 header
 
   struct usb_cdc_call_management_descriptor   call_mgmt
 call management descriptor
 
   struct usb_cdc_acm_descriptor   acm
 descriptor
 
   struct usb_cdc_union_descriptor   cdc_union
 descriptor
 
__attribute__ ((packed))
 USB CDC ACM functional descriptor. More...
 
static void usb_disconnect (void)
 disconnect USB by pulling down D+ to for re-enumerate
 
static void usb_dfu_detach (usbd_device *usbd_dev, struct usb_setup_data *req)
 DFU detach (disconnect USB and perform core reset) More...
 
static enum usbd_request_return_codes usb_cdcacm_control_request (usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void(**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
 incoming USB CDC ACM control request More...
 
static void usb_cdcacm_data_rx_cb (usbd_device *usbd_dev, uint8_t ep)
 USB CDC ACM data received callback. More...
 
static void usb_cdcacm_data_tx_cb (usbd_device *usbd_dev, uint8_t ep)
 USB CDC ACM data transmitted callback. More...
 
static void usb_cdcacm_communication_cb (usbd_device *usbd_dev, uint8_t ep)
 USB CDC ACM communication callback. More...
 
static void usb_cdcacm_set_config (usbd_device *usbd_dev, uint16_t wValue)
 set USB CDC ACM configuration More...
 
void usb_cdcacm_setup (void)
 setup USB CDC ACM peripheral
 
void usb_cdcacm_putchar (char c)
 send character over USB (non-blocking) More...
 
void usb_lp_can_rx0_isr (void)
 USB interrupt service routine called when data is received.
 

Variables

volatile bool usb_cdcacm_connecting = false
 flag set to true when user is connected to USB CDC ACM port (e.g. More...
 
static uint8_t usbd_control_buffer [128] = {0}
 buffer to be used for control requests
 
static usbd_device * usb_device = NULL
 structure holding all the info related to the USB device
 
static volatile bool first_connection = false
 used to detect when the first connection occurred
 
static uint8_t tx_buffer [256] = {0}
 ring buffer for data to transmit
 
static volatile uint16_t tx_i = 0
 current position if transmitted data
 
static volatile uint16_t tx_used = 0
 how much data needs to be transmitted
 
static volatile bool tx_lock = false
 if the transmit buffer is currently being written
 
static const struct usb_device_descriptor usb_cdcacm_device_descriptor
 USB CDC ACM device descriptor. More...
 
static const struct usb_endpoint_descriptor usb_cdcacm_data_endpoints []
 USB CDC ACM data endpoints. More...
 
static const struct usb_endpoint_descriptor usb_cdcacm_communication_endpoints []
 USB CDC ACM communication endpoints. More...
 
static const struct usb_interface_descriptor usb_cdcacm_communication_interface
 USB CDC interface descriptor. More...
 
static const struct usb_interface_descriptor usb_cdcacm_data_interface
 USB CDC ACM data class interface descriptor. More...
 
static const struct usb_dfu_descriptor usb_dfu_functional
 USB DFU functional descriptor. More...
 
static const struct usb_interface_descriptor usb_dfu_interface
 USB DFU interface descriptor. More...
 
static const struct usb_interface usb_cdcacm_interfaces []
 USB CDC ACM interface descriptor. More...
 
static const struct usb_config_descriptor usb_cdcacm_configuration_descriptor
 USB CDC ACM configuration descriptor. More...
 
static const char * usb_strings []
 USB string table. More...
 

Detailed Description

library for USB CDC ACM communication (code)

Author
King Kévin kingk.nosp@m.evin.nosp@m.@cuvo.nosp@m.odoo.nosp@m..info
Date
2016-2018

Definition in file usb_cdcacm.c.

Function Documentation

◆ __attribute__()

static const struct @1 __attribute__ ( (packed)  )
static

USB CDC ACM functional descriptor.

Note
as defined in USB CDC specification section 5.2.3
Returns
packed structure

< descriptor length
















< descriptor type















< descriptor subtype














< CDC value













< descriptor length












< descriptor type











< descriptor subtype










< capabilities









< data interface (==usb_cdcacm_data_interface.bInterfaceNumber)








< descriptor length







< descriptor type






< descriptor subtype





< capabilities




< descriptor length



< descriptor type


< descriptor subtype

< control interface (==usb_cdcacm_com_interface.bInterfaceNumber)
< subordinate interface (==usb_cdcacm_data_interface.bInterfaceNumber)

Definition at line 116 of file usb_cdcacm.c.

◆ usb_cdcacm_communication_cb()

static void usb_cdcacm_communication_cb ( usbd_device *  usbd_dev,
uint8_t  ep 
)
static

USB CDC ACM communication callback.

Note
if transmission happens before the control setting is complete with a response form the communication endpoint, Linux echoes back the data
Parameters
[in]usbd_devUSB device descriptor
[in]ependpoint where data came in

Definition at line 412 of file usb_cdcacm.c.

◆ usb_cdcacm_control_request()

static enum usbd_request_return_codes usb_cdcacm_control_request ( usbd_device *  usbd_dev,
struct usb_setup_data *  req,
uint8_t **  buf,
uint16_t *  len,
void(**)(usbd_device *usbd_dev, struct usb_setup_data *req)  complete 
)
static

incoming USB CDC ACM control request

Parameters
[in]usbd_devUSB device descriptor
[in]reqcontrol request information
[in]bufcontrol request data
[in]lencontrol request data length
[in]completefunction to run after request completed
Returns
USBD_REQ_HANDLED if handled correctly, USBD_REQ_NOTSUPP else
Note
resets device when configured with 5 bits

Definition at line 297 of file usb_cdcacm.c.

◆ usb_cdcacm_data_rx_cb()

static void usb_cdcacm_data_rx_cb ( usbd_device *  usbd_dev,
uint8_t  ep 
)
static

USB CDC ACM data received callback.

Note
called when data has been received
Parameters
[in]usbd_devUSB device descriptor
[in]ependpoint where data came in

Definition at line 359 of file usb_cdcacm.c.

◆ usb_cdcacm_data_tx_cb()

static void usb_cdcacm_data_tx_cb ( usbd_device *  usbd_dev,
uint8_t  ep 
)
static

USB CDC ACM data transmitted callback.

Note
called once transmission is completed
Parameters
[in]usbd_devUSB device descriptor
[in]ependpoint where data came in

Definition at line 380 of file usb_cdcacm.c.

◆ usb_cdcacm_putchar()

void usb_cdcacm_putchar ( char  c)

send character over USB (non-blocking)

Parameters
[in]ccharacter to send
Note
blocks if transmit buffer is full, else puts in buffer and returns

Definition at line 462 of file usb_cdcacm.c.

◆ usb_cdcacm_set_config()

static void usb_cdcacm_set_config ( usbd_device *  usbd_dev,
uint16_t  wValue 
)
static

set USB CDC ACM configuration

Parameters
[in]usbd_devUSB device descriptor
[in]wValuenot used

Definition at line 427 of file usb_cdcacm.c.

◆ usb_dfu_detach()

static void usb_dfu_detach ( usbd_device *  usbd_dev,
struct usb_setup_data *  req 
)
static

DFU detach (disconnect USB and perform core reset)

Parameters
[in]usbd_devUSB device (unused)
[in]reqUSB request (unused)

Definition at line 278 of file usb_cdcacm.c.

Variable Documentation

◆ usb_cdcacm_communication_endpoints

const struct usb_endpoint_descriptor usb_cdcacm_communication_endpoints[]
static
Initial value:
= {{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x81,
.bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
.wMaxPacketSize = 16,
.bInterval = 255,
}}

USB CDC ACM communication endpoints.

Note
This notification endpoint isn't implemented. According to CDC spec its optional, but its absence causes a NULL pointer dereference in Linux cdc_acm driver

Definition at line 98 of file usb_cdcacm.c.

◆ usb_cdcacm_communication_interface

const struct usb_interface_descriptor usb_cdcacm_communication_interface
static
Initial value:
= {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 1,
.bInterfaceClass = USB_CLASS_CDC,
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
.bInterfaceProtocol = USB_CDC_PROTOCOL_NONE,
.iInterface = 0,
.extra = &usb_cdcacm_functional_descriptors,
.extralen = sizeof(usb_cdcacm_functional_descriptors),
}
static const struct usb_endpoint_descriptor usb_cdcacm_communication_endpoints[]
USB CDC ACM communication endpoints.
Definition: usb_cdcacm.c:98

USB CDC interface descriptor.

Note
as defined in USB CDC specification section 5.1.3

Definition at line 148 of file usb_cdcacm.c.

◆ usb_cdcacm_configuration_descriptor

const struct usb_config_descriptor usb_cdcacm_configuration_descriptor
static
Initial value:
= {
.bLength = USB_DT_CONFIGURATION_SIZE,
.bDescriptorType = USB_DT_CONFIGURATION,
.wTotalLength = 0,
.bNumInterfaces = LENGTH(usb_cdcacm_interfaces),
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0x80,
.bMaxPower = 0xfa,
.interface = usb_cdcacm_interfaces,
}
#define LENGTH(x)
get the length of an array
Definition: global.h:26
static const struct usb_interface usb_cdcacm_interfaces[]
USB CDC ACM interface descriptor.
Definition: usb_cdcacm.c:213

USB CDC ACM configuration descriptor.

Definition at line 225 of file usb_cdcacm.c.

◆ usb_cdcacm_connecting

volatile bool usb_cdcacm_connecting = false

flag set to true when user is connected to USB CDC ACM port (e.g.

when a terminal is opened)

Definition at line 44 of file usb_cdcacm.c.

◆ usb_cdcacm_data_endpoints

const struct usb_endpoint_descriptor usb_cdcacm_data_endpoints[]
static
Initial value:
= {{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x02,
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
.wMaxPacketSize = USB_DATA_TRANSFER_SIZE,
.bInterval = 1,
},{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x82,
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
.wMaxPacketSize = USB_DATA_TRANSFER_SIZE,
.bInterval = 1,
}}
#define USB_DATA_TRANSFER_SIZE
maximum packet size for USB data transfer
Definition: usb_cdcacm.c:42

USB CDC ACM data endpoints.

Note
as defined in USB CDC specification section 5

Definition at line 79 of file usb_cdcacm.c.

◆ usb_cdcacm_data_interface

const struct usb_interface_descriptor usb_cdcacm_data_interface
static
Initial value:
= {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 1,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_DATA,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 0,
}
static const struct usb_endpoint_descriptor usb_cdcacm_data_endpoints[]
USB CDC ACM data endpoints.
Definition: usb_cdcacm.c:79

USB CDC ACM data class interface descriptor.

Note
as defined in USB CDC specification section 5.1.3

Definition at line 168 of file usb_cdcacm.c.

◆ usb_cdcacm_device_descriptor

const struct usb_device_descriptor usb_cdcacm_device_descriptor
static
Initial value:
= {
.bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = USB_CLASS_CDC,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = 64,
.idVendor = 0x1209,
.idProduct = 0x4256,
.bcdDevice = 0x0000,
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
.bNumConfigurations = 1,
}

USB CDC ACM device descriptor.

Note
as defined in USB CDC specification section 5

Definition at line 59 of file usb_cdcacm.c.

◆ usb_cdcacm_interfaces

const struct usb_interface usb_cdcacm_interfaces[]
static
Initial value:
= {{
.num_altsetting = 1,
}, {
.num_altsetting = 1,
.altsetting = &usb_cdcacm_data_interface,
}, {
.num_altsetting = 1,
.altsetting = &usb_dfu_interface,
}}
static const struct usb_interface_descriptor usb_dfu_interface
USB DFU interface descriptor.
Definition: usb_cdcacm.c:197
static const struct usb_interface_descriptor usb_cdcacm_communication_interface
USB CDC interface descriptor.
Definition: usb_cdcacm.c:148
static const struct usb_interface_descriptor usb_cdcacm_data_interface
USB CDC ACM data class interface descriptor.
Definition: usb_cdcacm.c:168

USB CDC ACM interface descriptor.

Definition at line 213 of file usb_cdcacm.c.

◆ usb_dfu_functional

const struct usb_dfu_descriptor usb_dfu_functional
static
Initial value:
= {
.bLength = sizeof(struct usb_dfu_descriptor),
.bDescriptorType = DFU_FUNCTIONAL,
.bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
.wDetachTimeout = 200,
.wTransferSize = sizeof(usbd_control_buffer),
.bcdDFUVersion = 0x0110,
}
static uint8_t usbd_control_buffer[128]
buffer to be used for control requests
Definition: usb_cdcacm.c:46

USB DFU functional descriptor.

Note
as defined in USB Device Firmware Upgrade specification section 4.2.4

Definition at line 185 of file usb_cdcacm.c.

◆ usb_dfu_interface

const struct usb_interface_descriptor usb_dfu_interface
static
Initial value:
= {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 2,
.bAlternateSetting = 0,
.bNumEndpoints = 0,
.bInterfaceClass = 0xFE,
.bInterfaceSubClass = 1,
.bInterfaceProtocol = 1,
.iInterface = 4,
.extra = &usb_dfu_functional,
.extralen = sizeof(usb_dfu_functional),
}
static const struct usb_dfu_descriptor usb_dfu_functional
USB DFU functional descriptor.
Definition: usb_cdcacm.c:185

USB DFU interface descriptor.

Note
as defined in USB Device Firmware Upgrade specification section 4.2.3

Definition at line 197 of file usb_cdcacm.c.

◆ usb_strings

const char* usb_strings[]
static
Initial value:
= {
"CuVoodoo",
"BusVoodoo multi-protocol debugging adapter",
"0",
"DFU bootloader (runtime mode)",
}

USB string table.

Note
starting with index 1

Definition at line 241 of file usb_cdcacm.c.