add DFU runtime to USB CDC ACM profile

This commit is contained in:
King Kévin 2017-04-15 13:57:45 +02:00
parent 8664b96055
commit b26a10e085
2 changed files with 218 additions and 141 deletions

View File

@ -15,7 +15,7 @@
/** library for USB CDC ACM communication (code) /** library for USB CDC ACM communication (code)
* @file usb_cdcacm.c * @file usb_cdcacm.c
* @author King Kévin <kingkevin@cuvoodoo.info> * @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2016 * @date 2016-2017
*/ */
/* standard libraries */ /* standard libraries */
@ -32,59 +32,63 @@
#include <libopencm3/usb/usbd.h> // USB library #include <libopencm3/usb/usbd.h> // USB library
#include <libopencm3/usb/cdc.h> // USB CDC library #include <libopencm3/usb/cdc.h> // USB CDC library
#include <libopencm3/cm3/sync.h> // synchronisation utilities #include <libopencm3/cm3/sync.h> // synchronisation utilities
#include <libopencm3/usb/dfu.h> // DFU definitions
#include "global.h" // global utilities #include "global.h" // global utilities
#include "usb_cdcacm.h" // USB CDC ACM header and definitions #include "usb_cdcacm.h" // USB CDC ACM header and definitions
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 */
/** USB CDC ACM device descriptor /** USB CDC ACM device descriptor
* @note as defined in USB CDC specification section 5 * @note as defined in USB CDC specification section 5
*/ */
static const struct usb_device_descriptor device_descriptor = { static const struct usb_device_descriptor usb_cdcacm_device_descriptor = {
.bLength = USB_DT_DEVICE_SIZE, // the size of this header in bytes, 18 .bLength = USB_DT_DEVICE_SIZE, /**< the size of this header in bytes, 18 */
.bDescriptorType = USB_DT_DEVICE, // a value of 1 indicates that this is a device descriptor .bDescriptorType = USB_DT_DEVICE, /**< a value of 1 indicates that this is a device descriptor */
.bcdUSB = 0x0200, // this device supports USB 2.0 .bcdUSB = 0x0200, /**< this device supports USB 2.0 */
.bDeviceClass = USB_CLASS_CDC, // use the CDC device class .bDeviceClass = USB_CLASS_CDC, /**< use the CDC device class */
.bDeviceSubClass = 0, // unused .bDeviceSubClass = 0, /**< unused */
.bDeviceProtocol = 0, // unused .bDeviceProtocol = 0, /**< unused */
.bMaxPacketSize0 = 64, // packet size for endpoint zero in bytes .bMaxPacketSize0 = 64, /**< packet size for endpoint zero in bytes */
.idVendor = 0xc440, // Vendor ID (CuVo...) .idVendor = 0xc440, /**< Vendor ID (CuVo...) */
.idProduct = 0x0d00, // product ID within the Vendor ID space (...odoo) .idProduct = 0x0d00, /**< product ID within the Vendor ID space (...odoo) */
.bcdDevice = 0x0100, // version number for the device .bcdDevice = 0x0100, /**< version number for the device */
.iManufacturer = 1, // the index of the string in the string table that represents the name of the manufacturer of this device. .iManufacturer = 1, /**< the index of the string in the string table that represents the name of the manufacturer of this device */
.iProduct = 2, // the index of the string in the string table that represents the name of the product .iProduct = 2, /**< the index of the string in the string table that represents the name of the product */
.iSerialNumber = 3, // the index of the string in the string table that represents the serial number of this item in string form. .iSerialNumber = 3, /**< the index of the string in the string table that represents the serial number of this item in string form */
.bNumConfigurations = 1, // the number of possible configurations this device has .bNumConfigurations = 1, /**< the number of possible configurations this device has */
}; };
/** USB CDC ACM data endpoints /** USB CDC ACM data endpoints
* @note as defined in USB CDC specification section 5 * @note as defined in USB CDC specification section 5
*/ */
static const struct usb_endpoint_descriptor data_endpoints[] = {{ static const struct usb_endpoint_descriptor usb_cdcacm_data_endpoints[] = {{
.bLength = USB_DT_ENDPOINT_SIZE, // the size of the endpoint descriptor in bytes .bLength = USB_DT_ENDPOINT_SIZE, /**< the size of the endpoint descriptor in bytes */
.bDescriptorType = USB_DT_ENDPOINT, // a value of 5 indicates that this describes an endpoint .bDescriptorType = USB_DT_ENDPOINT, /**< a value of 5 indicates that this describes an endpoint */
.bEndpointAddress = 0x01, // OUT (from host) direction (0<<7), endpoint 1 .bEndpointAddress = 0x02, /**< OUT (from host) direction (0<<7), endpoint 2 */
.bmAttributes = USB_ENDPOINT_ATTR_BULK, // bulk mode .bmAttributes = USB_ENDPOINT_ATTR_BULK, /**< bulk mode */
.wMaxPacketSize = 64, // maximum packet size .wMaxPacketSize = 64, /**< maximum packet size */
.bInterval = 1, // the frequency, in number of frames, that we're going to be sending data .bInterval = 1, /**< the frequency, in number of frames, that we're going to be sending data */
},{ },{
.bLength = USB_DT_ENDPOINT_SIZE, // the size of the endpoint descriptor in bytes .bLength = USB_DT_ENDPOINT_SIZE, /**< the size of the endpoint descriptor in bytes */
.bDescriptorType = USB_DT_ENDPOINT, // a value of 5 indicates that this describes an endpoint .bDescriptorType = USB_DT_ENDPOINT, /**< a value of 5 indicates that this describes an endpoint */
.bEndpointAddress = 0x82, // IN (to host) direction (1<<7), endpoint 2 .bEndpointAddress = 0x82, /**< IN (to host) direction (1<<7), endpoint 2 */
.bmAttributes = USB_ENDPOINT_ATTR_BULK, // bulk mode .bmAttributes = USB_ENDPOINT_ATTR_BULK, /**< bulk mode */
.wMaxPacketSize = 64, // maximum packet size .wMaxPacketSize = 64, /**< maximum packet size */
.bInterval = 1, // the frequency, in number of frames, that we're going to be sending data .bInterval = 1, /**< the frequency, in number of frames, that we're going to be sending data */
}}; }};
/** USB CDC ACM communication endpoints /** 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 * @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
*/ */
static const struct usb_endpoint_descriptor communication_endpoints[] = {{ static const struct usb_endpoint_descriptor usb_cdcacm_communication_endpoints[] = {{
.bLength = USB_DT_ENDPOINT_SIZE, // the size of the endpoint descriptor in bytes .bLength = USB_DT_ENDPOINT_SIZE, /**< the size of the endpoint descriptor in bytes */
.bDescriptorType = USB_DT_ENDPOINT, // a value of 5 indicates that this describes an endpoint .bDescriptorType = USB_DT_ENDPOINT, /**< a value of 5 indicates that this describes an endpoint */
.bEndpointAddress = 0x83, // IN (to host) direction (1<<7), endpoint 3 .bEndpointAddress = 0x81, /**< IN (to host) direction (1<<7), endpoint 1 */
.bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, // interrupt mode .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, /**< interrupt mode */
.wMaxPacketSize = 16, // maximum packet size .wMaxPacketSize = 16, /**< maximum packet size */
.bInterval = 255, // the frequency, in number of frames, that we're going to be sending data .bInterval = 255, /**< the frequency, in number of frames, that we're going to be sending data */
}}; }};
/** USB CDC ACM functional descriptor /** USB CDC ACM functional descriptor
@ -96,7 +100,7 @@ static const struct {
struct usb_cdc_call_management_descriptor call_mgmt; /**< call management descriptor */ struct usb_cdc_call_management_descriptor call_mgmt; /**< call management descriptor */
struct usb_cdc_acm_descriptor acm; /**< descriptor */ struct usb_cdc_acm_descriptor acm; /**< descriptor */
struct usb_cdc_union_descriptor cdc_union; /**< descriptor */ struct usb_cdc_union_descriptor cdc_union; /**< descriptor */
} __attribute__((packed)) cdcacm_functional_descriptors = { } __attribute__((packed)) usb_cdcacm_functional_descriptors = {
.header = { .header = {
.bFunctionLength = sizeof(struct usb_cdc_header_descriptor), /**< descriptor length */ .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), /**< descriptor length */
.bDescriptorType = CS_INTERFACE, /**< descriptor type */ .bDescriptorType = CS_INTERFACE, /**< descriptor type */
@ -128,7 +132,7 @@ static const struct {
/** USB CDC interface descriptor /** USB CDC interface descriptor
* @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 communication_interface[] = {{ static const struct usb_interface_descriptor usb_cdcacm_communication_interface = {
.bLength = USB_DT_INTERFACE_SIZE, .bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE, .bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0, .bInterfaceNumber = 0,
@ -139,16 +143,16 @@ static const struct usb_interface_descriptor communication_interface[] = {{
.bInterfaceProtocol = USB_CDC_PROTOCOL_NONE, .bInterfaceProtocol = USB_CDC_PROTOCOL_NONE,
.iInterface = 0, .iInterface = 0,
.endpoint = communication_endpoints, .endpoint = usb_cdcacm_communication_endpoints,
.extra = &cdcacm_functional_descriptors, .extra = &usb_cdcacm_functional_descriptors,
.extralen = sizeof(cdcacm_functional_descriptors), .extralen = sizeof(usb_cdcacm_functional_descriptors),
}}; };
/** USB CDC ACM data class interface descriptor /** USB CDC ACM data class interface descriptor
* @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 data_interface[] = {{ static const struct usb_interface_descriptor usb_cdcacm_data_interface = {
.bLength = USB_DT_INTERFACE_SIZE, .bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE, .bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 1, .bInterfaceNumber = 1,
@ -159,30 +163,62 @@ static const struct usb_interface_descriptor data_interface[] = {{
.bInterfaceProtocol = 0, .bInterfaceProtocol = 0,
.iInterface = 0, .iInterface = 0,
.endpoint = data_endpoints, .endpoint = usb_cdcacm_data_endpoints,
}}; };
/** USB DFU functional descriptor
* @note as defined in USB Device Firmware Upgrade specification section 4.2.4
*/
static const struct usb_dfu_descriptor usb_dfu_functional = {
.bLength = sizeof(struct usb_dfu_descriptor), /**< provide own size */
.bDescriptorType = DFU_FUNCTIONAL, /**< functional descriptor type */
.bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, /**< this DFU can download and will detach after download */
.wDetachTimeout = 200, /**< maximum time (in milliseconds) needed to detach (else resume normal operation) */
.wTransferSize = sizeof(usbd_control_buffer), /**< set max transfer size */
.bcdDFUVersion = 0x0110, /**< DFU specification version 1.1 used */
};
/** USB DFU interface descriptor
* @note as defined in USB Device Firmware Upgrade specification section 4.2.3
*/
static const struct usb_interface_descriptor usb_dfu_interface = {
.bLength = USB_DT_INTERFACE_SIZE, /**< size of descriptor in byte */
.bDescriptorType = USB_DT_INTERFACE, /**< interface descriptor type */
.bInterfaceNumber = 2, /**< interface number in the list */
.bAlternateSetting = 0, /**< no alternative settings */
.bNumEndpoints = 0, /**< only the control pipe at endpoint 0 is used */
.bInterfaceClass = 0xFE, /**< DFU interface class (not defined in libopencm3 dfu lib) */
.bInterfaceSubClass = 1, /**< DFU interface subclass (not defined in libopencm3 dfu lib) */
.bInterfaceProtocol = 1, /**< runtime protocol (not defined in libopencm3 dfu lib) */
.iInterface = 4, /**< the index of the string in the string table that represents interface description */
.extra = &usb_dfu_functional, /**< point to functional descriptor */
.extralen = sizeof(usb_dfu_functional), /**< size of functional descriptor */
};
/** USB CDC ACM interface descriptor */ /** USB CDC ACM interface descriptor */
static const struct usb_interface interfaces[] = {{ static const struct usb_interface usb_cdcacm_interfaces[] = {{
.num_altsetting = 1, .num_altsetting = 1,
.altsetting = communication_interface, .altsetting = &usb_cdcacm_communication_interface,
}, { }, {
.num_altsetting = 1, .num_altsetting = 1,
.altsetting = data_interface, .altsetting = &usb_cdcacm_data_interface,
}, {
.num_altsetting = 1,
.altsetting = &usb_dfu_interface,
}}; }};
/** USB CDC ACM configuration descriptor */ /** USB CDC ACM configuration descriptor */
static const struct usb_config_descriptor config = { static const struct usb_config_descriptor usb_cdcacm_configuration_descriptor = {
.bLength = USB_DT_CONFIGURATION_SIZE, // the length of this header in bytes .bLength = USB_DT_CONFIGURATION_SIZE, /**< the length of this header in bytes */
.bDescriptorType = USB_DT_CONFIGURATION, // a value of 2 indicates that this is a configuration descriptor .bDescriptorType = USB_DT_CONFIGURATION, /**< a value of 2 indicates that this is a configuration descriptor */
.wTotalLength = 0, // this should hold the total size of the configuration descriptor including all sub interfaces. it is automatically filled in by the USB stack in libopencm3 .wTotalLength = 0, /**< this should hold the total size of the configuration descriptor including all sub interfaces. it is automatically filled in by the USB stack in libopencm3 */
.bNumInterfaces = 2, // the number of interfaces in this configuration .bNumInterfaces = LENGTH(usb_cdcacm_interfaces), /**< the number of interfaces in this configuration */
.bConfigurationValue = 1, // the index of this configuration .bConfigurationValue = 1, /**< the index of this configuration */
.iConfiguration = 0, // a string index describing this configuration (zero means not provided) .iConfiguration = 0, /**< a string index describing this configuration (zero means not provided) */
.bmAttributes = 0x80, // self powered (0<<6), supports remote wakeup (0<<5) .bmAttributes = 0x80, /**< bus powered (1<<7) */
.bMaxPower = 0x32, // the maximum amount of current that this device will draw in 2mA units .bMaxPower = 0x32, /**< the maximum amount of current that this device will draw in 2mA units */
// end of header // end of header
.interface = interfaces, // pointer to an array of interfaces .interface = usb_cdcacm_interfaces, /**< pointer to an array of interfaces */
}; };
/** USB string table /** USB string table
@ -190,14 +226,11 @@ static const struct usb_config_descriptor config = {
*/ */
static const char *usb_strings[] = { static const char *usb_strings[] = {
"CuVoodoo", "CuVoodoo",
"CDC-ACM",
"STM32F1", "STM32F1",
"CDC-ACM",
"CuVoodoo DFU bootloader (runtime mode)",
}; };
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 bool connected = false; /**< is the USB device is connected to a host */
/* input and output ring buffer, indexes, and available memory */ /* input and output ring buffer, indexes, and available memory */
static uint8_t rx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for received data */ static uint8_t rx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for received data */
static volatile uint8_t rx_i = 0; /**< current position of read received data */ static volatile uint8_t rx_i = 0; /**< current position of read received data */
@ -207,21 +240,13 @@ static uint8_t tx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for data to tran
static volatile uint8_t tx_i = 0; /**< current position if transmitted data */ static volatile uint8_t tx_i = 0; /**< current position if transmitted data */
static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */ static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */
mutex_t tx_lock = MUTEX_UNLOCKED; /**< lock to update tx_i or tx_used */ mutex_t tx_lock = MUTEX_UNLOCKED; /**< lock to update tx_i or tx_used */
volatile uint8_t cdcacm_received = 0; // same as rx_used, but since the user can write this variable we don't rely on it volatile uint8_t usb_cdcacm_received = 0; // same as rx_used, but since the user can write this variable we don't rely on it
static bool connected = false; /**< is the USB device is connected to a host */
/** disconnect USB by pulling down D+ to for re-enumerate */ /** disconnect USB by pulling down D+ to for re-enumerate */
static void usb_disconnect(void) static void usb_disconnect(void)
{ {
/* short USB disconnect to force re-enumerate */ #if defined(MAPLE_MINI)
#if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
// pull USB D+ low for a short while
rcc_periph_clock_enable(RCC_GPIOA);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
gpio_clear(GPIOA, GPIO12);
for (uint32_t i = 0; i < 0x2000; i++) {
__asm__("nop");
}
#elif defined(MAPLE_MINI)
// disconnect USB D+ using dedicated DISC line/circuit on PB9 // disconnect USB D+ using dedicated DISC line/circuit on PB9
rcc_periph_clock_enable(RCC_GPIOB); rcc_periph_clock_enable(RCC_GPIOB);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9); gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
@ -230,64 +255,115 @@ static void usb_disconnect(void)
__asm__("nop"); __asm__("nop");
} }
gpio_clear(GPIOB, GPIO9); gpio_clear(GPIOB, GPIO9);
#else
// pull USB D+ low for a short while
rcc_periph_clock_enable(RCC_GPIOA);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
gpio_clear(GPIOA, GPIO12);
for (uint32_t i = 0; i < 0x2000; i++) {
__asm__("nop");
}
#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)
* @param[in] usbd_dev USB device (unused)
* @param[in] req USB request (unused)
*/
static void usb_dfu_detach(usbd_device *usbd_dev, struct usb_setup_data *req)
{
(void)usbd_dev; // variable not used
(void)req; // variable not used
RCC_CSR |= RCC_CSR_RMVF; // clear reset flag for the bootloader to detect the core reset
usb_disconnect(); // USB detach (disconnect to force re-enumeration)
scb_reset_core(); // reset device (only the core, to the peripheral stay configured)
while (true); // wait for the reset to happen
}
/** incoming USB CDC ACM control request /** incoming USB CDC ACM control request
* @param[in] usbd_dev USB device descriptor * @param[in] usbd_dev USB device descriptor
* @param[in] req control request information * @param[in] req control request information
* @param[in] buf control request data * @param[in] buf control request data
* @param[in] len control request data length * @param[in] len control request data length
* @param[in] complete not used * @param[in] complete function to run after request completed
* @return 0 if succeeded, error else * @return 0 if succeeded, error else
* @note resets device when configured with 5 bits * @note resets device when configured with 5 bits
*/ */
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)) static int 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))
{ {
(void)complete; (void)complete;
(void)buf; (void)buf;
(void)usbd_dev; (void)usbd_dev;
switch (req->bRequest) { if (usb_dfu_interface.bInterfaceNumber==req->wIndex) { // check if request is for DFU
case USB_CDC_REQ_SET_CONTROL_LINE_STATE: switch (req->bRequest) {
connected = req->wValue ? true : false; // check if terminal is open case DFU_DETACH: // USB detach requested
//bool dtr = (req->wValue & (1 << 0)) ? true : false; *complete = usb_dfu_detach; // detach after reply
//bool rts = (req->wValue & (1 << 1)) ? true : false; break;
/* this Linux cdc_acm driver requires this to be implemented case DFU_GETSTATUS: // get status
* even though it's optional in the CDC spec, and we don't (*buf)[0] = DFU_STATUS_OK;; // set OK status
* advertise it in the ACM functional descriptor. (*buf)[1] = 0; // set null poll timeout
*/ (*buf)[2] = 0; // set null poll timeout
uint8_t reply[10] = {0}; (*buf)[3] = 0; // set null poll timeout
struct usb_cdc_notification *notif = (void *)reply; (*buf)[4] = STATE_APP_IDLE; // application is running
/* we echo signals back to host as notification. */ (*buf)[5] = 0; // string not used
notif->bmRequestType = 0xA1; *len = 6; // set length of buffer to return
notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; break;
notif->wValue = 0; default: // other requests are not supported
notif->wIndex = 0;
notif->wLength = 2;
reply[8] = req->wValue & 3;
reply[9] = 0;
usbd_ep_write_packet(usbd_dev, 0x83, reply, LENGTH(reply));
break;
case USB_CDC_REQ_SET_LINE_CODING:
// ignore if length is wrong
if (*len < sizeof(struct usb_cdc_line_coding)) {
return 0; return 0;
} }
// get the line coding } else {
struct usb_cdc_line_coding *coding = (struct usb_cdc_line_coding *)*buf; switch (req->bRequest) {
/* reset device is the data bits is set to 5 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
* this is used to allowing rebooting the device in DFU mode for reflashing connected = req->wValue ? true : false; // check if terminal is open
* to reset the device from the host you can use stty --file /dev/ttyACM0 115200 raw cs5 //bool dtr = (req->wValue & (1 << 0)) ? true : false;
*/ //bool rts = (req->wValue & (1 << 1)) ? true : false;
if (coding->bDataBits==5) { /* this Linux cdc_acm driver requires this to be implemented
usb_disconnect(); // force re-enumerate after reset * even though it's optional in the CDC spec, and we don't
scb_reset_system(); // reset device * advertise it in the ACM functional descriptor.
while (true); // wait for the reset to happen */
} uint8_t reply[10] = {0};
break; struct usb_cdc_notification *notif = (void *)reply;
default: /* we echo signals back to host as notification. */
return 0; notif->bmRequestType = 0xA1;
notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
notif->wValue = 0;
notif->wIndex = 0;
notif->wLength = 2;
reply[8] = req->wValue & 3;
reply[9] = 0;
usbd_ep_write_packet(usbd_dev, 0x81, reply, LENGTH(reply));
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
}
break;
default:
return 0;
}
} }
return 1; return 1;
} }
@ -296,7 +372,7 @@ static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *
* @param[in] usbd_dev USB device descriptor * @param[in] usbd_dev USB device descriptor
* @param[in] ep endpoint where data came in * @param[in] ep endpoint where data came in
*/ */
static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep) static void usb_cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
{ {
(void)ep; (void)ep;
@ -304,13 +380,13 @@ static void 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, 0x01, usb_data, sizeof(usb_data)); usb_length = usbd_ep_read_packet(usbd_dev, 0x02, 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 && rx_used<LENGTH(rx_buffer); i++) { // only until buffer is full for (uint16_t i=0; i<usb_length && rx_used<LENGTH(rx_buffer); i++) { // only until buffer is full
rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usb_data[i]; // put character in buffer rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usb_data[i]; // put character in buffer
rx_used++; // update used buffer rx_used++; // update used buffer
} }
cdcacm_received = rx_used; // update available data usb_cdcacm_received = rx_used; // update available data
} }
} }
@ -318,7 +394,7 @@ static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
* @param[in] usbd_dev USB device descriptor * @param[in] usbd_dev USB device descriptor
* @param[in] ep endpoint where data came in * @param[in] ep endpoint where data came in
*/ */
static void cdcacm_data_tx_cb(usbd_device *usbd_dev, uint8_t ep) static void usb_cdcacm_data_tx_cb(usbd_device *usbd_dev, uint8_t ep)
{ {
(void)ep; (void)ep;
(void)usbd_dev; (void)usbd_dev;
@ -343,40 +419,41 @@ static void cdcacm_data_tx_cb(usbd_device *usbd_dev, uint8_t ep)
* @param[in] usbd_dev USB device descriptor * @param[in] usbd_dev USB device descriptor
* @param[in] wValue not used * @param[in] wValue not used
*/ */
static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue) static void usb_cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue)
{ {
(void)wValue; (void)wValue;
usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb); usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_tx_cb); usbd_ep_setup(usbd_dev, 0x02, USB_ENDPOINT_ATTR_BULK, 64, usb_cdcacm_data_rx_cb);
usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, usb_cdcacm_data_tx_cb);
usbd_register_control_callback( usbd_dev, USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, cdcacm_control_request); usbd_register_control_callback( usbd_dev, USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, usb_cdcacm_control_request);
} }
void cdcacm_setup(void) void usb_cdcacm_setup(void)
{ {
connected = false; // start with USB not connected connected = false; // start with USB not connected
usb_disconnect(); // force re-enumerate (useful after a restart or if there is a bootloader using another USB profile)
/* initialize USB */ // initialize USB
usb_device = usbd_init(&st_usbfs_v1_usb_driver, &device_descriptor, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer)); rcc_periph_reset_pulse(RST_USB); // reset USB peripheral
usbd_register_set_config_callback(usb_device, cdcacm_set_config); usb_disconnect(); // disconnect to force re-enumeration
rcc_periph_clock_enable(RCC_GPIOA); // enable clock for GPIO used for USB
/* enable interrupts (to not have to poll all the time) */ rcc_periph_clock_enable(RCC_USB); // enable clock for USB domain
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); // without this USB isn't detected by the host usb_device = usbd_init(&st_usbfs_v1_usb_driver, &usb_cdcacm_device_descriptor, &usb_cdcacm_configuration_descriptor, usb_strings, LENGTH(usb_strings), usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usb_device, usb_cdcacm_set_config);
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); // enable interrupts (to not have to poll all the time)
/* reset buffer states */ // reset buffer states
rx_i = 0; rx_i = 0;
rx_used = 0; rx_used = 0;
mutex_unlock(&rx_lock); mutex_unlock(&rx_lock);
cdcacm_received = 0; usb_cdcacm_received = 0;
tx_i = 0; tx_i = 0;
tx_used = 0; tx_used = 0;
mutex_unlock(&tx_lock); mutex_unlock(&tx_lock);
} }
char cdcacm_getchar(void) char usb_cdcacm_getchar(void)
{ {
while (!rx_used) { // idle until data is available while (!rx_used) { // idle until data is available
__WFI(); // sleep until interrupt (not sure if it's a good idea here) __WFI(); // sleep until interrupt (not sure if it's a good idea here)
@ -384,11 +461,11 @@ char cdcacm_getchar(void)
char to_return = rx_buffer[rx_i]; // get the next available character char to_return = rx_buffer[rx_i]; // get the next available character
rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
rx_used--; // update used buffer rx_used--; // update used buffer
cdcacm_received = rx_used; // update available data usb_cdcacm_received = rx_used; // update available data
return to_return; return to_return;
} }
void cdcacm_putchar(char c) void usb_cdcacm_putchar(char c)
{ {
if (!usb_device || !connected) { if (!usb_device || !connected) {
return; return;

View File

@ -22,17 +22,17 @@
/** transmit and receive buffer sizes */ /** transmit and receive buffer sizes */
#define CDCACM_BUFFER 64 #define CDCACM_BUFFER 64
/** how many bytes available in the received buffer since last read */ /** how many bytes available in the received buffer since last read */
extern volatile uint8_t cdcacm_received; extern volatile uint8_t usb_cdcacm_received;
/** setup USB CDC ACM peripheral */ /** setup USB CDC ACM peripheral */
void cdcacm_setup(void); void usb_cdcacm_setup(void);
/** get character received over USB (blocking) /** get character received over USB (blocking)
* @return character received over USB * @return character received over USB
* @note blocks until character is received over USB when received buffer is empty * @note blocks until character is received over USB when received buffer is empty
*/ */
char cdcacm_getchar(void); char usb_cdcacm_getchar(void);
/** send character over USB (non-blocking) /** send character over USB (non-blocking)
* @param[in] c character to send * @param[in] c character to send
* @note blocks if transmit buffer is full, else puts in buffer and returns * @note blocks if transmit buffer is full, else puts in buffer and returns
*/ */
void cdcacm_putchar(char c); void usb_cdcacm_putchar(char c);