USB: improve comments and a fix of code

This commit is contained in:
King Kévin 2018-04-08 15:45:34 +02:00
parent dd0fcc1df7
commit 372655c97c
1 changed files with 27 additions and 46 deletions

View File

@ -124,7 +124,7 @@ static const struct {
.bDescriptorType = CS_INTERFACE, /**< descriptor type */ .bDescriptorType = CS_INTERFACE, /**< descriptor type */
.bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, /**< descriptor subtype */ .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, /**< descriptor subtype */
.bmCapabilities = 0, /**< capabilities */ .bmCapabilities = 0, /**< capabilities */
.bDataInterface = 1, /**< data interface */ .bDataInterface = 1, /**< data interface (==usb_cdcacm_data_interface.bInterfaceNumber) */
}, },
.acm = { .acm = {
.bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), /**< descriptor length */ .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), /**< descriptor length */
@ -136,8 +136,8 @@ static const struct {
.bFunctionLength = sizeof(struct usb_cdc_union_descriptor), /**< descriptor length */ .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), /**< descriptor length */
.bDescriptorType = CS_INTERFACE, /**< descriptor type */ .bDescriptorType = CS_INTERFACE, /**< descriptor type */
.bDescriptorSubtype = USB_CDC_TYPE_UNION, /**< descriptor subtype */ .bDescriptorSubtype = USB_CDC_TYPE_UNION, /**< descriptor subtype */
.bControlInterface = 0, /**< control interface */ .bControlInterface = 0, /**< control interface (==usb_cdcacm_com_interface.bInterfaceNumber) */
.bSubordinateInterface0 = 1, /**< subordinate interface */ .bSubordinateInterface0 = 1, /**< subordinate interface (==usb_cdcacm_data_interface.bInterfaceNumber) */
}, },
}; };
@ -145,20 +145,20 @@ static const struct {
* @note as defined in USB CDC specification section 5.1.3 * @note as defined in USB CDC specification section 5.1.3
*/ */
static const struct usb_interface_descriptor usb_cdcacm_communication_interface = { static const struct usb_interface_descriptor usb_cdcacm_communication_interface = {
.bLength = USB_DT_INTERFACE_SIZE, .bLength = USB_DT_INTERFACE_SIZE, /**< size of descriptor in byte */
.bDescriptorType = USB_DT_INTERFACE, .bDescriptorType = USB_DT_INTERFACE, /**< interface descriptor type */
.bInterfaceNumber = 0, .bInterfaceNumber = 0, /**< interface number in the list */
.bAlternateSetting = 0, .bAlternateSetting = 0, /**< no alternative settings */
.bNumEndpoints = 1, .bNumEndpoints = 1, /**< number of endpoints used (communication OUT)*/
.bInterfaceClass = USB_CLASS_CDC, .bInterfaceClass = USB_CLASS_CDC, /**< USB CDC interface class for CDC */
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, /**< USB CDC ACM interface subclass */
.bInterfaceProtocol = USB_CDC_PROTOCOL_NONE, .bInterfaceProtocol = USB_CDC_PROTOCOL_NONE, /**< USB CDC ACM none protocol */
.iInterface = 0, .iInterface = 0, /**< the index of the string in the string table that represents interface description */
.endpoint = usb_cdcacm_communication_endpoints, .endpoint = usb_cdcacm_communication_endpoints, /**< pointer to endpoint descriptor */
.extra = &usb_cdcacm_functional_descriptors, .extra = &usb_cdcacm_functional_descriptors, /**< pointer to functional description (for the data interface) */
.extralen = sizeof(usb_cdcacm_functional_descriptors), .extralen = sizeof(usb_cdcacm_functional_descriptors), /**< number of functional descriptions */
}; };
/** USB CDC ACM data class interface descriptor /** USB CDC ACM data class interface descriptor
@ -169,13 +169,13 @@ static const struct usb_interface_descriptor usb_cdcacm_data_interface = {
.bDescriptorType = USB_DT_INTERFACE, /**< interface descriptor type */ .bDescriptorType = USB_DT_INTERFACE, /**< interface descriptor type */
.bInterfaceNumber = 1, /**< interface number in the list */ .bInterfaceNumber = 1, /**< interface number in the list */
.bAlternateSetting = 0, /**< no alternative settings */ .bAlternateSetting = 0, /**< no alternative settings */
.bNumEndpoints = 2, /**< only the control pipe at endpoint 0 is used */ .bNumEndpoints = 2, /**< number of endpoints used (data IN and OUT) */
.bInterfaceClass = USB_CLASS_DATA, /**< USB CDC interface class */ .bInterfaceClass = USB_CLASS_DATA, /**< USB CDC interface class for data */
.bInterfaceSubClass = 0, /**< USB CDC ACM interface subclass */ .bInterfaceSubClass = 0, /**< USB CDC ACM interface subclass */
.bInterfaceProtocol = 0, /**< USB CDC ACM none protocol */ .bInterfaceProtocol = 0, /**< USB CDC ACM none protocol */
.iInterface = 0, /**< the index of the string in the string table that represents interface description */ .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 /** USB DFU functional descriptor
@ -265,20 +265,6 @@ static void usb_disconnect(void)
#endif #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) /** DFU detach (disconnect USB and perform core reset)
* @param[in] usbd_dev USB device (unused) * @param[in] usbd_dev USB device (unused)
* @param[in] req USB request (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
notif->wLength = 2; notif->wLength = 2;
reply[8] = req->wValue & 3; reply[8] = req->wValue & 3;
reply[9] = 0; 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; break;
case USB_CDC_REQ_SET_LINE_CODING: case USB_CDC_REQ_SET_LINE_CODING:
// ignore if length is wrong // ignore if length is wrong
if (*len < sizeof(struct usb_cdc_line_coding)) { if (*len < sizeof(struct usb_cdc_line_coding)) {
return 0; return 0;
} }
// get the line coding // line coding is ignored
struct usb_cdc_line_coding *coding = (struct usb_cdc_line_coding *)*buf; // to get the line coding
/* reset device is the data bits is set to 5 // struct usb_cdc_line_coding *coding = (struct usb_cdc_line_coding *)*buf;
* 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
}
break; break;
default: default:
return 0; return 0;
@ -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 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 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)
uint16_t usb_length = 0; // length of incoming data uint16_t usb_length = 0; // length of incoming data
// receive 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 if (usb_length) { // copy received data
for (uint16_t i=0; i<usb_length && i<LENGTH(usb_data); i++) { // only until buffer is full 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 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)
usb_tx_ongoing = true; // remember we started transmission 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) 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 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_i = (tx_i+usb_length)%LENGTH(tx_buffer); // update location on buffer
tx_used -= usb_length; // update used size tx_used -= usb_length; // update used size
} else { } 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 usbd_poll(usb_device); // ensure the data gets sent
} }
@ -476,7 +457,7 @@ void usb_cdcacm_putchar(char c)
} }
tx_lock = false; // release lock on transmit buffer tx_lock = false; // release lock on transmit buffer
if (tx_used>0 && usb_device && !usb_tx_ongoing) { // if buffer is not empty anymore 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
} }
} }