Skip to content

Commit

Permalink
Support all USB2 speeds for stress test
Browse files Browse the repository at this point in the history
  • Loading branch information
kauwua committed Jul 16, 2024
1 parent 511ba31 commit d6d5d0d
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 34 deletions.
76 changes: 61 additions & 15 deletions tests/test_firmware_usb_stress_test/User/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ limitations under the License.
#pragma GCC diagnostic pop
#pragma GCC diagnostic pop

#include "usb2_device_descriptors.h"
#include "usb2_fs_device_descriptors.h"
#include "usb2_hs_device_descriptors.h"
#include "usb2_ls_device_descriptors.h"
#include "usb_device.h"
#include "wch-ch56x-lib/logging/logging.h"
#include "wch-ch56x-lib/USBDevice/usb20.h"
Expand Down Expand Up @@ -72,7 +74,6 @@ void endp2_tx_complete(TRANSACTION_STATUS status)
uint8_t endp1_rx_callback(uint8_t* const ptr, uint16_t size);
uint8_t endp1_rx_callback(uint8_t* const ptr, uint16_t size)
{
LOG("writing %d bytes %x %x %x %x \r\n", size, endp0_buffer[0], endp0_buffer[1], endp0_buffer[2], endp0_buffer[3]);
memcpy(last_out_buffer + out_total_sent, ptr, size);
out_total_sent += size;
last_out_total_sent = out_total_sent; //because no ZLP is sent for bulk OUT ... meaning we can't detect the end of the transfer
Expand Down Expand Up @@ -171,7 +172,7 @@ int main()
bsp_init(FREQ_SYS);

LOG_INIT(FREQ_SYS);
LOG("USB stress test azeaez\r\n");
LOG("USB stress test\r\n");

usb_device_0.endpoints.endp0_user_handled_control_request = endp0_user_handled_control_request;
usb_device_0.endpoints.tx_complete[2] = endp2_tx_complete;
Expand All @@ -181,21 +182,66 @@ int main()
usb2_user_handled.usb2_device_handle_bus_reset =
&usb2_device_handle_bus_reset;

// Finish initializing the descriptor parameters
init_usb2_descriptors();
init_string_descriptors();
if (bsp_ubtn())
{
usb_device_0.speed = USB2_FULLSPEED;
}
else
{
usb_device_0.speed = USB2_HIGHSPEED;
}

// Set the USB device parameters
usb_device_set_usb2_device_descriptor(&usb_device_0, &usb2_descriptors.usb_device_descr);
usb_device_set_usb2_config_descriptors(&usb_device_0, usb2_device_configs);
usb_device_set_string_descriptors(&usb_device_0, device_string_descriptors);
usb_device_set_endpoint_mask(&usb_device_0, ENDPOINT_1_RX | ENDPOINT_2_TX);
if (usb_device_0.speed == USB2_LOWSPEED)
{
// Finish initializing the descriptor parameters
init_usb2_ls_descriptors();
init_string_descriptors();

init_endpoints();
// Set the USB device parameters
usb_device_set_usb2_device_descriptor(&usb_device_0, &usb2_ls_descriptors.usb_device_descr);
usb_device_set_usb2_config_descriptors(&usb_device_0, usb2_ls_device_configs);
usb_device_set_string_descriptors(&usb_device_0, device_string_descriptors);
usb_device_set_endpoint_mask(&usb_device_0, ENDPOINT_1_RX | ENDPOINT_2_TX);

init_endpoints_ls();

usb2_device_init();
usb2_enable_nak(true);
}
else if (usb_device_0.speed == USB2_FULLSPEED)
{
// Finish initializing the descriptor parameters
init_usb2_fs_descriptors();
init_string_descriptors();

usb_device_0.speed = USB2_HIGHSPEED;
usb2_device_init();
usb2_enable_nak(true);
// Set the USB device parameters
usb_device_set_usb2_device_descriptor(&usb_device_0, &usb2_fs_descriptors.usb_device_descr);
usb_device_set_usb2_config_descriptors(&usb_device_0, usb2_fs_device_configs);
usb_device_set_string_descriptors(&usb_device_0, device_string_descriptors);
usb_device_set_endpoint_mask(&usb_device_0, ENDPOINT_1_RX | ENDPOINT_2_TX);

init_endpoints_fs();

usb2_device_init();
usb2_enable_nak(true);
}
else if (usb_device_0.speed == USB2_HIGHSPEED)
{
// Finish initializing the descriptor parameters
init_usb2_hs_descriptors();
init_string_descriptors();

// Set the USB device parameters
usb_device_set_usb2_device_descriptor(&usb_device_0, &usb2_hs_descriptors.usb_device_descr);
usb_device_set_usb2_config_descriptors(&usb_device_0, usb2_hs_device_configs);
usb_device_set_string_descriptors(&usb_device_0, device_string_descriptors);
usb_device_set_endpoint_mask(&usb_device_0, ENDPOINT_1_RX | ENDPOINT_2_TX);

init_endpoints_hs();

usb2_device_init();
usb2_enable_nak(true);
}

// Infinite loop USB2/USB3 managed with Interrupt
while (1)
Expand Down
106 changes: 106 additions & 0 deletions tests/test_firmware_usb_stress_test/User/usb2_fs_device_descriptors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/********************************** (C) COPYRIGHT *******************************
Copyright (c) 2023 Quarkslab
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************/

#ifndef USB2_FS_DEVICE_DESCRIPTOR_H
#define USB2_FS_DEVICE_DESCRIPTOR_H

#include "definitions.h"
#include "wch-ch56x-lib/USBDevice/usb_descriptors.h"

const uint8_t* usb2_fs_device_configs[1];

struct usb2_fs_descriptors
{
USB_DEV_DESCR usb_device_descr;
struct __PACKED
{
USB_CFG_DESCR usb_cfg_descr;
USB_ITF_DESCR usb_itf_descr;
USB_ENDP_DESCR usb_endp_descr_1;
USB_ENDP_DESCR usb_endp_descr_2_tx;
} other_descr;
} usb2_fs_descriptors;

void init_usb2_fs_descriptors(void);
void init_usb2_fs_descriptors(void)
{
usb2_fs_descriptors.usb_device_descr = (USB_DEV_DESCR){
.bLength = 0x12,
.bDescriptorType = 0x01, // device descriptor type
.bcdUSB = 0x0200, // usb2.0
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 64,
.bcdDevice = 0x0001,
.idVendor =
0x1209, // https://github.com/obdev/v-usb/blob/master/usbdrv/usb-ids-for-free.txt
.idProduct = 0x0001,
.iProduct = 0x01,
.iManufacturer = 0x00,
.iSerialNumber = 0x00,
.bNumConfigurations = 0x01
};

usb2_fs_descriptors.other_descr.usb_cfg_descr = (USB_CFG_DESCR){
.bLength = 0x09,
.bDescriptorType = 0x02,
.wTotalLength = sizeof(usb2_fs_descriptors.other_descr),
.bNumInterfaces = 0x01,
.bConfigurationValue = 0x01,
.iConfiguration = 0x00,
.bmAttributes = 0xa0, // supports remote wake-up
.MaxPower = 0x64 // 200ma
};

usb2_fs_descriptors.other_descr.usb_itf_descr =
(USB_ITF_DESCR){ .bLength = 0x09,
.bDescriptorType = 0x04,
.bInterfaceNumber = 0x00,
.bAlternateSetting = 0x00,
.bNumEndpoints = 0x02,
.bInterfaceClass = 0xff, // vendor-specific
.bInterfaceSubClass = 0xff,
.bInterfaceProtocol = 0xff,
.iInterface = 0x00 };

usb2_fs_descriptors.other_descr.usb_endp_descr_1 = (USB_ENDP_DESCR){
.bLength = 0x07,
.bDescriptorType = 0x05,
.bEndpointAddress = (ENDPOINT_DESCRIPTOR_ADDRESS_OUT | 0x01) &
ENDPOINT_DESCRIPTOR_ADDRESS_MASK,
.bmAttributes = ENDPOINT_DESCRIPTOR_BULK_TRANSFER,
.wMaxPacketSizeL = 0x40,
.wMaxPacketSizeH = 0x00, // 64 bytes
.bInterval = 255 // max NAK rate
};

usb2_fs_descriptors.other_descr.usb_endp_descr_2_tx = (USB_ENDP_DESCR){
.bLength = 0x07,
.bDescriptorType = 0x05,
.bEndpointAddress = (ENDPOINT_DESCRIPTOR_ADDRESS_IN | 0x02) &
ENDPOINT_DESCRIPTOR_ADDRESS_MASK,
.bmAttributes = ENDPOINT_DESCRIPTOR_BULK_TRANSFER,
.wMaxPacketSizeL = 0x40,
.wMaxPacketSizeH = 0x00, // 64 bytes
.bInterval = 0
};

usb2_fs_device_configs[0] = (uint8_t*)&usb2_fs_descriptors.other_descr;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ limitations under the License.
*******************************************************************************/

#ifndef USB2_DEVICE_DESCRIPTOR_H
#define USB2_DEVICE_DESCRIPTOR_H
#ifndef USB2_HS_DEVICE_DESCRIPTOR_H
#define USB2_HS_DEVICE_DESCRIPTOR_H

#include "definitions.h"
#include "wch-ch56x-lib/USBDevice/usb_descriptors.h"

const uint8_t* usb2_device_configs[1];
const uint8_t* usb2_hs_device_configs[1];

struct usb2_descriptors
struct usb2_hs_descriptors
{
USB_DEV_DESCR usb_device_descr;
struct __PACKED
Expand All @@ -33,13 +33,12 @@ struct usb2_descriptors
USB_ENDP_DESCR usb_endp_descr_1;
USB_ENDP_DESCR usb_endp_descr_2_tx;
} other_descr;
} usb2_descriptors;
} usb2_hs_descriptors;

void init_usb2_descriptors(void);

void init_usb2_descriptors(void)
void init_usb2_hs_descriptors(void);
void init_usb2_hs_descriptors(void)
{
usb2_descriptors.usb_device_descr = (USB_DEV_DESCR){
usb2_hs_descriptors.usb_device_descr = (USB_DEV_DESCR){
.bLength = 0x12,
.bDescriptorType = 0x01, // device descriptor type
.bcdUSB = 0x0200, // usb2.0
Expand All @@ -57,18 +56,18 @@ void init_usb2_descriptors(void)
.bNumConfigurations = 0x01
};

usb2_descriptors.other_descr.usb_cfg_descr = (USB_CFG_DESCR){
usb2_hs_descriptors.other_descr.usb_cfg_descr = (USB_CFG_DESCR){
.bLength = 0x09,
.bDescriptorType = 0x02,
.wTotalLength = sizeof(usb2_descriptors.other_descr),
.wTotalLength = sizeof(usb2_hs_descriptors.other_descr),
.bNumInterfaces = 0x01,
.bConfigurationValue = 0x01,
.iConfiguration = 0x00,
.bmAttributes = 0xa0, // supports remote wake-up
.MaxPower = 0x64 // 200ma
};

usb2_descriptors.other_descr.usb_itf_descr =
usb2_hs_descriptors.other_descr.usb_itf_descr =
(USB_ITF_DESCR){ .bLength = 0x09,
.bDescriptorType = 0x04,
.bInterfaceNumber = 0x00,
Expand All @@ -79,7 +78,7 @@ void init_usb2_descriptors(void)
.bInterfaceProtocol = 0xff,
.iInterface = 0x00 };

usb2_descriptors.other_descr.usb_endp_descr_1 = (USB_ENDP_DESCR){
usb2_hs_descriptors.other_descr.usb_endp_descr_1 = (USB_ENDP_DESCR){
.bLength = 0x07,
.bDescriptorType = 0x05,
.bEndpointAddress = (ENDPOINT_DESCRIPTOR_ADDRESS_OUT | 0x01) &
Expand All @@ -90,7 +89,7 @@ void init_usb2_descriptors(void)
.bInterval = 255 // max NAK rate
};

usb2_descriptors.other_descr.usb_endp_descr_2_tx = (USB_ENDP_DESCR){
usb2_hs_descriptors.other_descr.usb_endp_descr_2_tx = (USB_ENDP_DESCR){
.bLength = 0x07,
.bDescriptorType = 0x05,
.bEndpointAddress = (ENDPOINT_DESCRIPTOR_ADDRESS_IN | 0x02) &
Expand All @@ -101,7 +100,7 @@ void init_usb2_descriptors(void)
.bInterval = 0
};

usb2_device_configs[0] = (uint8_t*)&usb2_descriptors.other_descr;
usb2_hs_device_configs[0] = (uint8_t*)&usb2_hs_descriptors.other_descr;
}

#endif
Loading

0 comments on commit d6d5d0d

Please sign in to comment.