LED clock
 All Files Functions Variables Macros Groups Pages
Functions | Variables
usb_cdcacm.c File Reference

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

Go to the source code of this file.

Functions

struct {
   struct usb_cdc_header_descriptor   header
 
   struct
usb_cdc_call_management_descriptor   call_mgmt
 
   struct usb_cdc_acm_descriptor   acm
 
   struct usb_cdc_union_descriptor   cdc_union
 
__attribute__ ((packed))
 USB CDC ACM functional descriptor. More...
 
static void usb_disconnect (void)
 disconnect USB by pulling down D+ to for re-enumerate
 
static int 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 cdcacm_data_rx_cb (usbd_device *usbd_dev, uint8_t ep)
 USB CDC ACM data received callback. More...
 
static void cdcacm_data_tx_cb (usbd_device *usbd_dev, uint8_t ep)
 USB CDC ACM data transmitted callback. More...
 
static void cdcacm_set_config (usbd_device *usbd_dev, uint16_t wValue)
 set USB CDC ACM configuration More...
 
void cdcacm_setup (void)
 setup USB CDC ACM peripheral
 
char cdcacm_getchar (void)
 get character received over USB (blocking) More...
 
void 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

static const struct
usb_device_descriptor 
device_descriptor
 USB CDC ACM device descriptor. More...
 
static const struct
usb_endpoint_descriptor 
data_endpoints []
 USB CDC ACM data endpoints. More...
 
static const struct
usb_endpoint_descriptor 
communication_endpoints []
 USB CDC ACM communication endpoints. More...
 
static const struct
usb_interface_descriptor 
communication_interface []
 USB CDC interface descriptor. More...
 
static const struct
usb_interface_descriptor 
data_interface []
 USB CDC ACM data class interface descriptor. More...
 
static const struct usb_interface interfaces []
 USB CDC ACM interface descriptor. More...
 
static const struct
usb_config_descriptor 
config
 USB CDC ACM configuration descriptor. More...
 
static const char * usb_strings []
 USB string table. More...
 
static uint8_t usbd_control_buffer [128] = {0}
 
static usbd_device * usb_device = NULL
 
static bool connected = false
 
static uint8_t rx_buffer [CDCACM_BUFFER] = {0}
 
static volatile uint8_t rx_i = 0
 
static volatile uint8_t rx_used = 0
 
mutex_t rx_lock = MUTEX_UNLOCKED
 
static uint8_t tx_buffer [CDCACM_BUFFER] = {0}
 
static volatile uint8_t tx_i = 0
 
static volatile uint8_t tx_used = 0
 
mutex_t tx_lock = MUTEX_UNLOCKED
 
volatile uint8_t cdcacm_received = 0
 

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
Bug:
sometimes usbd_ep_write_packet packets get lost, even if the return value is right. I have no idea why

Definition in file usb_cdcacm.c.

Function Documentation

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

USB CDC ACM functional descriptor.

Returns
Note
as defined in USB CDC specification section 5.2.3

Definition at line 100 of file usb_cdcacm.c.

static int 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]completenot used
Returns
0 if succeeded, error else
Note
resets device when configured with 5 bits

Definition at line 246 of file usb_cdcacm.c.

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

USB CDC ACM data received callback.

Parameters
[in]usbd_devUSB device descriptor
[in]ependpoint where data came in

Definition at line 300 of file usb_cdcacm.c.

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

USB CDC ACM data transmitted callback.

Parameters
[in]usbd_devUSB device descriptor
[in]ependpoint where data came in

Definition at line 322 of file usb_cdcacm.c.

char cdcacm_getchar ( void  )

get character received over USB (blocking)

Returns
character received over USB
Note
blocks until character is received over USB when received buffer is empty

Definition at line 380 of file usb_cdcacm.c.

void 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 392 of file usb_cdcacm.c.

static void 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 347 of file usb_cdcacm.c.

Variable Documentation

volatile uint8_t cdcacm_received = 0

how many bytes available in the received buffer since last read

Definition at line 211 of file usb_cdcacm.c.

const struct usb_endpoint_descriptor communication_endpoints[]
static
Initial value:
= {{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x83,
.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 82 of file usb_cdcacm.c.

const struct usb_interface_descriptor 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 = &cdcacm_functional_descriptors,
.extralen = sizeof(cdcacm_functional_descriptors),
}}
static const struct usb_endpoint_descriptor communication_endpoints[]
USB CDC ACM communication endpoints.
Definition: usb_cdcacm.c:82

USB CDC interface descriptor.

Note
as defined in USB CDC specification section 5.1.3

Definition at line 132 of file usb_cdcacm.c.

const struct usb_config_descriptor config
static
Initial value:
= {
.bLength = USB_DT_CONFIGURATION_SIZE,
.bDescriptorType = USB_DT_CONFIGURATION,
.wTotalLength = 0,
.bNumInterfaces = 2,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0x80,
.bMaxPower = 0x32,
.interface = interfaces,
}
static const struct usb_interface interfaces[]
USB CDC ACM interface descriptor.
Definition: usb_cdcacm.c:167

USB CDC ACM configuration descriptor.

Definition at line 176 of file usb_cdcacm.c.

bool connected = false
static

is the USB device is connected to a host

Definition at line 200 of file usb_cdcacm.c.

const struct usb_endpoint_descriptor data_endpoints[]
static
Initial value:
= {{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x01,
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
.wMaxPacketSize = 64,
.bInterval = 1,
},{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x82,
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
.wMaxPacketSize = 64,
.bInterval = 1,
}}

USB CDC ACM data endpoints.

Note
as defined in USB CDC specification section 5

Definition at line 63 of file usb_cdcacm.c.

const struct usb_interface_descriptor 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,
.endpoint = data_endpoints,
}}
static const struct usb_endpoint_descriptor data_endpoints[]
USB CDC ACM data endpoints.
Definition: usb_cdcacm.c:63

USB CDC ACM data class interface descriptor.

Note
as defined in USB CDC specification section 5.1.3

Definition at line 152 of file usb_cdcacm.c.

const struct usb_device_descriptor 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 = 0xc440,
.idProduct = 0x0d00,
.bcdDevice = 0x0100,
.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 43 of file usb_cdcacm.c.

const struct usb_interface interfaces[]
static
Initial value:
= {{
.num_altsetting = 1,
.altsetting = communication_interface,
}, {
.num_altsetting = 1,
.altsetting = data_interface,
}}
static const struct usb_interface_descriptor communication_interface[]
USB CDC interface descriptor.
Definition: usb_cdcacm.c:132
static const struct usb_interface_descriptor data_interface[]
USB CDC ACM data class interface descriptor.
Definition: usb_cdcacm.c:152

USB CDC ACM interface descriptor.

Definition at line 167 of file usb_cdcacm.c.

uint8_t rx_buffer[CDCACM_BUFFER] = {0}
static

ring buffer for received data

Definition at line 203 of file usb_cdcacm.c.

volatile uint8_t rx_i = 0
static

current position of read received data

Definition at line 204 of file usb_cdcacm.c.

mutex_t rx_lock = MUTEX_UNLOCKED

lock to update rx_i or rx_used

Definition at line 206 of file usb_cdcacm.c.

volatile uint8_t rx_used = 0
static

how much data has been received and not red

Definition at line 205 of file usb_cdcacm.c.

uint8_t tx_buffer[CDCACM_BUFFER] = {0}
static

ring buffer for data to transmit

Definition at line 207 of file usb_cdcacm.c.

volatile uint8_t tx_i = 0
static

current position if transmitted data

Definition at line 208 of file usb_cdcacm.c.

mutex_t tx_lock = MUTEX_UNLOCKED

lock to update tx_i or tx_used

Definition at line 210 of file usb_cdcacm.c.

volatile uint8_t tx_used = 0
static

how much data needs to be transmitted

Definition at line 209 of file usb_cdcacm.c.

usbd_device* usb_device = NULL
static

structure holding all the info related to the USB device

Definition at line 199 of file usb_cdcacm.c.

const char* usb_strings[]
static
Initial value:
= {
"CuVoodoo",
"CDC-ACM",
"STM32F1",
}

USB string table.

Note
starting with index 1

Definition at line 192 of file usb_cdcacm.c.

uint8_t usbd_control_buffer[128] = {0}
static

buffer to be used for control requests

Definition at line 198 of file usb_cdcacm.c.