Skip to content

Commit

Permalink
General improvements in STM32 USB code (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored May 16, 2018
1 parent a3090b8 commit 907bcc7
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 96 deletions.
55 changes: 37 additions & 18 deletions targets/CMSIS-OS/ChibiOS/MBN_QUAIL/common/usbcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
SerialUSBDriver SDU1;

/*
* Endpoints to be used for USBD1.
* Endpoints to be used for USBD2.
*/
#define USBD1_DATA_REQUEST_EP 1
#define USBD1_DATA_AVAILABLE_EP 1
#define USBD1_INTERRUPT_REQUEST_EP 2
#define USBD2_DATA_REQUEST_EP 1
#define USBD2_DATA_AVAILABLE_EP 1
#define USBD2_INTERRUPT_REQUEST_EP 2

// address for device unique ID
// valid for STM32F4 series
Expand Down Expand Up @@ -149,7 +149,7 @@ static const uint8_t vcom_configuration_descriptor_data[] = {
USB_DESC_BYTE (0x01), /* bSlaveInterface0 (Data Class
Interface). */
/* Endpoint 2 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
USB_DESC_ENDPOINT (USBD2_INTERRUPT_REQUEST_EP|0x80,
0x03, /* bmAttributes (Interrupt). */
0x0008, /* wMaxPacketSize. */
0xFF), /* bInterval. */
Expand All @@ -165,12 +165,12 @@ static const uint8_t vcom_configuration_descriptor_data[] = {
4.7). */
0x00), /* iInterface. */
/* Endpoint 3 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
USB_DESC_ENDPOINT (USBD2_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
0x02, /* bmAttributes (Bulk). */
0x0040, /* wMaxPacketSize. */
0x00), /* bInterval. */
/* Endpoint 1 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
USB_DESC_ENDPOINT (USBD2_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
0x02, /* bmAttributes (Bulk). */
0x0040, /* wMaxPacketSize. */
0x00) /* bInterval. */
Expand Down Expand Up @@ -365,14 +365,19 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
case USB_EVENT_CONFIGURED:
chSysLockFromISR();

/* Enables the endpoints specified into the configuration.
Note, this callback is invoked from an ISR so I-Class functions
must be used.*/
usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config);
usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config);
if (usbp->state == USB_ACTIVE) {
/* Enables the endpoints specified into the configuration.
Note, this callback is invoked from an ISR so I-Class functions
must be used.*/
usbInitEndpointI(usbp, USBD2_DATA_REQUEST_EP, &ep1config);
usbInitEndpointI(usbp, USBD2_INTERRUPT_REQUEST_EP, &ep2config);

/* Resetting the state of the CDC subsystem.*/
sduConfigureHookI(&SDU1);
/* Resetting the state of the CDC subsystem.*/
sduConfigureHookI(&SDU1);
}
else if (usbp->state == USB_SELECTED) {
usbDisableEndpointsI(usbp);
}

chSysUnlockFromISR();
return;
Expand Down Expand Up @@ -402,6 +407,20 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
return;
}

/*
* Handling messages not implemented in the default handler nor in the
* SerialUSB handler.
*/
static bool requests_hook(USBDriver *usbp) {

if (((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE) &&
(usbp->setup[1] == USB_REQ_SET_INTERFACE)) {
usbSetupTransfer(usbp, NULL, 0, NULL);
return true;
}
return sduRequestsHook(usbp);
}

/*
* Handles the USB driver global events.
*/
Expand All @@ -420,7 +439,7 @@ static void sof_handler(USBDriver *usbp) {
const USBConfig usbcfg = {
usb_event,
get_descriptor,
sduRequestsHook,
requests_hook,
sof_handler
};

Expand All @@ -429,7 +448,7 @@ const USBConfig usbcfg = {
*/
const SerialUSBConfig serusbcfg = {
&USBD1,
USBD1_DATA_REQUEST_EP,
USBD1_DATA_AVAILABLE_EP,
USBD1_INTERRUPT_REQUEST_EP
USBD2_DATA_REQUEST_EP,
USBD2_DATA_AVAILABLE_EP,
USBD2_INTERRUPT_REQUEST_EP
};
55 changes: 37 additions & 18 deletions targets/CMSIS-OS/ChibiOS/NETDUINO3_WIFI/common/usbcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
SerialUSBDriver SDU1;

/*
* Endpoints to be used for USBD1.
* Endpoints to be used for USBD2.
*/
#define USBD1_DATA_REQUEST_EP 1
#define USBD1_DATA_AVAILABLE_EP 1
#define USBD1_INTERRUPT_REQUEST_EP 2
#define USBD2_DATA_REQUEST_EP 1
#define USBD2_DATA_AVAILABLE_EP 1
#define USBD2_INTERRUPT_REQUEST_EP 2

// address for device unique ID
// valid for STM32F4 series
Expand Down Expand Up @@ -149,7 +149,7 @@ static const uint8_t vcom_configuration_descriptor_data[] = {
USB_DESC_BYTE (0x01), /* bSlaveInterface0 (Data Class
Interface). */
/* Endpoint 2 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
USB_DESC_ENDPOINT (USBD2_INTERRUPT_REQUEST_EP|0x80,
0x03, /* bmAttributes (Interrupt). */
0x0008, /* wMaxPacketSize. */
0xFF), /* bInterval. */
Expand All @@ -165,12 +165,12 @@ static const uint8_t vcom_configuration_descriptor_data[] = {
4.7). */
0x00), /* iInterface. */
/* Endpoint 3 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
USB_DESC_ENDPOINT (USBD2_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
0x02, /* bmAttributes (Bulk). */
0x0040, /* wMaxPacketSize. */
0x00), /* bInterval. */
/* Endpoint 1 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
USB_DESC_ENDPOINT (USBD2_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
0x02, /* bmAttributes (Bulk). */
0x0040, /* wMaxPacketSize. */
0x00) /* bInterval. */
Expand Down Expand Up @@ -365,14 +365,19 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
case USB_EVENT_CONFIGURED:
chSysLockFromISR();

/* Enables the endpoints specified into the configuration.
Note, this callback is invoked from an ISR so I-Class functions
must be used.*/
usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config);
usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config);
if (usbp->state == USB_ACTIVE) {
/* Enables the endpoints specified into the configuration.
Note, this callback is invoked from an ISR so I-Class functions
must be used.*/
usbInitEndpointI(usbp, USBD2_DATA_REQUEST_EP, &ep1config);
usbInitEndpointI(usbp, USBD2_INTERRUPT_REQUEST_EP, &ep2config);

/* Resetting the state of the CDC subsystem.*/
sduConfigureHookI(&SDU1);
/* Resetting the state of the CDC subsystem.*/
sduConfigureHookI(&SDU1);
}
else if (usbp->state == USB_SELECTED) {
usbDisableEndpointsI(usbp);
}

chSysUnlockFromISR();
return;
Expand Down Expand Up @@ -402,6 +407,20 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
return;
}

/*
* Handling messages not implemented in the default handler nor in the
* SerialUSB handler.
*/
static bool requests_hook(USBDriver *usbp) {

if (((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE) &&
(usbp->setup[1] == USB_REQ_SET_INTERFACE)) {
usbSetupTransfer(usbp, NULL, 0, NULL);
return true;
}
return sduRequestsHook(usbp);
}

/*
* Handles the USB driver global events.
*/
Expand All @@ -420,7 +439,7 @@ static void sof_handler(USBDriver *usbp) {
const USBConfig usbcfg = {
usb_event,
get_descriptor,
sduRequestsHook,
requests_hook,
sof_handler
};

Expand All @@ -429,7 +448,7 @@ const USBConfig usbcfg = {
*/
const SerialUSBConfig serusbcfg = {
&USBD1,
USBD1_DATA_REQUEST_EP,
USBD1_DATA_AVAILABLE_EP,
USBD1_INTERRUPT_REQUEST_EP
USBD2_DATA_REQUEST_EP,
USBD2_DATA_AVAILABLE_EP,
USBD2_INTERRUPT_REQUEST_EP
};
55 changes: 37 additions & 18 deletions targets/CMSIS-OS/ChibiOS/ST_NUCLEO144_F746ZG/common/usbcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
SerialUSBDriver SDU1;

/*
* Endpoints to be used for USBD1.
* Endpoints to be used for USBD2.
*/
#define USBD1_DATA_REQUEST_EP 1
#define USBD1_DATA_AVAILABLE_EP 1
#define USBD1_INTERRUPT_REQUEST_EP 2
#define USBD2_DATA_REQUEST_EP 1
#define USBD2_DATA_AVAILABLE_EP 1
#define USBD2_INTERRUPT_REQUEST_EP 2

// address for device unique ID
// valid for STM32F7 series
Expand Down Expand Up @@ -149,7 +149,7 @@ static const uint8_t vcom_configuration_descriptor_data[67] = {
USB_DESC_BYTE (0x01), /* bSlaveInterface0 (Data Class
Interface). */
/* Endpoint 2 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
USB_DESC_ENDPOINT (USBD2_INTERRUPT_REQUEST_EP|0x80,
0x03, /* bmAttributes (Interrupt). */
0x0008, /* wMaxPacketSize. */
0xFF), /* bInterval. */
Expand All @@ -165,12 +165,12 @@ static const uint8_t vcom_configuration_descriptor_data[67] = {
4.7). */
0x00), /* iInterface. */
/* Endpoint 3 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
USB_DESC_ENDPOINT (USBD2_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
0x02, /* bmAttributes (Bulk). */
0x0040, /* wMaxPacketSize. */
0x00), /* bInterval. */
/* Endpoint 1 Descriptor.*/
USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
USB_DESC_ENDPOINT (USBD2_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
0x02, /* bmAttributes (Bulk). */
0x0040, /* wMaxPacketSize. */
0x00) /* bInterval. */
Expand Down Expand Up @@ -365,14 +365,19 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
case USB_EVENT_CONFIGURED:
chSysLockFromISR();

/* Enables the endpoints specified into the configuration.
Note, this callback is invoked from an ISR so I-Class functions
must be used.*/
usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config);
usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config);
if (usbp->state == USB_ACTIVE) {
/* Enables the endpoints specified into the configuration.
Note, this callback is invoked from an ISR so I-Class functions
must be used.*/
usbInitEndpointI(usbp, USBD2_DATA_REQUEST_EP, &ep1config);
usbInitEndpointI(usbp, USBD2_INTERRUPT_REQUEST_EP, &ep2config);

/* Resetting the state of the CDC subsystem.*/
sduConfigureHookI(&SDU1);
/* Resetting the state of the CDC subsystem.*/
sduConfigureHookI(&SDU1);
}
else if (usbp->state == USB_SELECTED) {
usbDisableEndpointsI(usbp);
}

chSysUnlockFromISR();
return;
Expand Down Expand Up @@ -402,6 +407,20 @@ static void usb_event(USBDriver *usbp, usbevent_t event) {
return;
}

/*
* Handling messages not implemented in the default handler nor in the
* SerialUSB handler.
*/
static bool requests_hook(USBDriver *usbp) {

if (((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE) &&
(usbp->setup[1] == USB_REQ_SET_INTERFACE)) {
usbSetupTransfer(usbp, NULL, 0, NULL);
return true;
}
return sduRequestsHook(usbp);
}

/*
* Handles the USB driver global events.
*/
Expand All @@ -420,7 +439,7 @@ static void sof_handler(USBDriver *usbp) {
const USBConfig usbcfg = {
usb_event,
get_descriptor,
sduRequestsHook,
requests_hook,
sof_handler
};

Expand All @@ -429,7 +448,7 @@ const USBConfig usbcfg = {
*/
const SerialUSBConfig serusbcfg = {
&USBD1,
USBD1_DATA_REQUEST_EP,
USBD1_DATA_AVAILABLE_EP,
USBD1_INTERRUPT_REQUEST_EP
USBD2_DATA_REQUEST_EP,
USBD2_DATA_AVAILABLE_EP,
USBD2_INTERRUPT_REQUEST_EP
};
Loading

0 comments on commit 907bcc7

Please sign in to comment.