Browse Source

USB: improve comments and a fix of code

spark_strober
King Kévin 5 years ago
parent
commit
372655c97c
  1. 77
      lib/usb_cdcacm.c

77
lib/usb_cdcacm.c

@ -124,7 +124,7 @@ static const struct { @@ -124,7 +124,7 @@ static const struct {
.bDescriptorType = CS_INTERFACE, /**< descriptor type */
.bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, /**< descriptor subtype */
.bmCapabilities = 0, /**< capabilities */
.bDataInterface = 1, /**< data interface */
.bDataInterface = 1, /**< data interface (==usb_cdcacm_data_interface.bInterfaceNumber) */
},
.acm = {
.bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), /**< descriptor length */
@ -136,8 +136,8 @@ static const struct { @@ -136,8 +136,8 @@ static const struct {
.bFunctionLength = sizeof(struct usb_cdc_union_descriptor), /**< descriptor length */
.bDescriptorType = CS_INTERFACE, /**< descriptor type */
.bDescriptorSubtype = USB_CDC_TYPE_UNION, /**< descriptor subtype */
.bControlInterface = 0, /**< control interface */
.bSubordinateInterface0 = 1, /**< subordinate interface */
.bControlInterface = 0, /**< control interface (==usb_cdcacm_com_interface.bInterfaceNumber) */
.bSubordinateInterface0 = 1, /**< subordinate interface (==usb_cdcacm_data_interface.bInterfaceNumber) */
},
};
@ -145,20 +145,20 @@ static const struct { @@ -145,20 +145,20 @@ static const struct {
* @note as defined in USB CDC specification section 5.1.3
*/
static const struct usb_interface_descriptor usb_cdcacm_communication_interface = {
.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,
.endpoint = usb_cdcacm_communication_endpoints,
.extra = &usb_cdcacm_functional_descriptors,
.extralen = sizeof(usb_cdcacm_functional_descriptors),
.bLength = USB_DT_INTERFACE_SIZE, /**< size of descriptor in byte */
.bDescriptorType = USB_DT_INTERFACE, /**< interface descriptor type */
.bInterfaceNumber = 0, /**< interface number in the list */
.bAlternateSetting = 0, /**< no alternative settings */
.bNumEndpoints = 1, /**< number of endpoints used (communication OUT)*/
.bInterfaceClass = USB_CLASS_CDC, /**< USB CDC interface class for CDC */
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, /**< USB CDC ACM interface subclass */
.bInterfaceProtocol = USB_CDC_PROTOCOL_NONE, /**< USB CDC ACM none protocol */
.iInterface = 0, /**< the index of the string in the string table that represents interface description */
.endpoint = usb_cdcacm_communication_endpoints, /**< pointer to endpoint descriptor */
.extra = &usb_cdcacm_functional_descriptors, /**< pointer to functional description (for the data interface) */
.extralen = sizeof(usb_cdcacm_functional_descriptors), /**< number of functional descriptions */
};
/** USB CDC ACM data class interface descriptor
@ -169,13 +169,13 @@ static const struct usb_interface_descriptor usb_cdcacm_data_interface = { @@ -169,13 +169,13 @@ static const struct usb_interface_descriptor usb_cdcacm_data_interface = {
.bDescriptorType = USB_DT_INTERFACE, /**< interface descriptor type */
.bInterfaceNumber = 1, /**< interface number in the list */
.bAlternateSetting = 0, /**< no alternative settings */
.bNumEndpoints = 2, /**< only the control pipe at endpoint 0 is used */
.bInterfaceClass = USB_CLASS_DATA, /**< USB CDC interface class */
.bNumEndpoints = 2, /**< number of endpoints used (data IN and OUT) */
.bInterfaceClass = USB_CLASS_DATA, /**< USB CDC interface class for data */
.bInterfaceSubClass = 0, /**< USB CDC ACM interface subclass */
.bInterfaceProtocol = 0, /**< USB CDC ACM none protocol */
.iInterface = 0, /**< the index of the string in the string table that represents interface description */
.endpoint = usb_cdcacm_data_endpoints, /**< point to endpoint descriptor */
.endpoint = usb_cdcacm_data_endpoints, /**< pointer to endpoint descriptor */
};
/** USB DFU functional descriptor
@ -265,20 +265,6 @@ static void usb_disconnect(void) @@ -265,20 +265,6 @@ static void usb_disconnect(void)
#endif
}
/** disconnect USB and perform system reset
* @param[in] usbd_dev USB device (unused)
* @param[in] req USB request (unused)
*/
static void usb_reset(usbd_device *usbd_dev, struct usb_setup_data *req)
{
(void)usbd_dev; // variable not used
(void)req; // variable not used
usb_disconnect(); // USB detach (disconnect to force re-enumeration)
scb_reset_system(); // reset device
while (true); // wait for the reset to happen
}
/** DFU detach (disconnect USB and perform core reset)
* @param[in] usbd_dev USB device (unused)
* @param[in] req USB request (unused)
@ -341,21 +327,16 @@ static int usb_cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_da @@ -341,21 +327,16 @@ static int usb_cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_da
notif->wLength = 2;
reply[8] = req->wValue & 3;
reply[9] = 0;
usbd_ep_write_packet(usbd_dev, 0x81, reply, LENGTH(reply)); // send the reply from communication endpoint (we can't use the complete callback because we are not using the current control endpoint)
usbd_ep_write_packet(usbd_dev, usb_cdcacm_communication_endpoints[0].bEndpointAddress, reply, LENGTH(reply)); // send the reply from communication endpoint (we can't use the complete callback because we are not using the current control endpoint)
break;
case USB_CDC_REQ_SET_LINE_CODING:
// ignore if length is wrong
if (*len < sizeof(struct usb_cdc_line_coding)) {
return 0;
}
// get the line coding
struct usb_cdc_line_coding *coding = (struct usb_cdc_line_coding *)*buf;
/* reset device is the data bits is set to 5
* to reset the device from the host you can use stty --file /dev/ttyACM0 raw cs5
*/
if (coding->bDataBits==5) {
*complete = usb_reset; // perform reset after reply
}
// line coding is ignored
// to get the line coding
// struct usb_cdc_line_coding *coding = (struct usb_cdc_line_coding *)*buf;
break;
default:
return 0;
@ -375,7 +356,7 @@ static void usb_cdcacm_communication_cb(usbd_device *usbd_dev, uint8_t ep) @@ -375,7 +356,7 @@ static void usb_cdcacm_communication_cb(usbd_device *usbd_dev, uint8_t ep)
first_connection |= usb_cdcacm_connecting; // check if port has been opened
if (usbd_dev && tx_used>0 && !usb_tx_ongoing && first_connection) { // if buffer is not empty
usbd_ep_write_packet(usbd_dev, 0x82, NULL, 0); // trigger tx callback
usbd_ep_write_packet(usbd_dev, usb_cdcacm_data_endpoints[1].bEndpointAddress, NULL, 0); // trigger tx callback
}
}
@ -392,7 +373,7 @@ static void usb_cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep) @@ -392,7 +373,7 @@ static void usb_cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
uint16_t usb_length = 0; // length of incoming data
// receive data
usb_length = usbd_ep_read_packet(usbd_dev, 0x02, usb_data, sizeof(usb_data));
usb_length = usbd_ep_read_packet(usbd_dev, usb_cdcacm_data_endpoints[0].bEndpointAddress, usb_data, sizeof(usb_data));
if (usb_length) { // copy received data
for (uint16_t i=0; i<usb_length && i<LENGTH(usb_data); i++) { // only until buffer is full
user_input_store(usb_data[i]); // store user data
@ -417,11 +398,11 @@ static void usb_cdcacm_data_tx_cb(usbd_device *usbd_dev, uint8_t ep) @@ -417,11 +398,11 @@ static void usb_cdcacm_data_tx_cb(usbd_device *usbd_dev, uint8_t ep)
usb_tx_ongoing = true; // remember we started transmission
uint16_t usb_length = (tx_used > USB_DATA_TRANSFER_SIZE ? USB_DATA_TRANSFER_SIZE : tx_used); // length of data to be transmitted (respect max packet size)
usb_length = (usb_length > (LENGTH(tx_buffer)-tx_i) ? LENGTH(tx_buffer)-tx_i : usb_length); // since here we use the source array not as ring buffer, only go up to the end
while (usb_length != usbd_ep_write_packet(usb_device, 0x82, (void*)(&tx_buffer[tx_i]), usb_length)); // ensure data is written into transmit buffer
while (usb_length != usbd_ep_write_packet(usb_device, usb_cdcacm_data_endpoints[1].bEndpointAddress, (void*)(&tx_buffer[tx_i]), usb_length)); // ensure data is written into transmit buffer
tx_i = (tx_i+usb_length)%LENGTH(tx_buffer); // update location on buffer
tx_used -= usb_length; // update used size
} else {
usbd_ep_write_packet(usb_device, 0x82, NULL, 0); // trigger empty tx for a later callback
usbd_ep_write_packet(usb_device, usb_cdcacm_data_endpoints[1].bEndpointAddress, NULL, 0); // trigger empty tx for a later callback
}
usbd_poll(usb_device); // ensure the data gets sent
}
@ -476,7 +457,7 @@ void usb_cdcacm_putchar(char c) @@ -476,7 +457,7 @@ void usb_cdcacm_putchar(char c)
}
tx_lock = false; // release lock on transmit buffer
if (tx_used>0 && usb_device && !usb_tx_ongoing) { // if buffer is not empty anymore
usbd_ep_write_packet(usb_device, 0x82, NULL, 0); // trigger tx callback
usbd_ep_write_packet(usb_device, usb_cdcacm_data_endpoints[1].bEndpointAddress, NULL, 0); // trigger tx callback
}
}

Loading…
Cancel
Save