Skip to content

Commit 3d195fc

Browse files
All selection of USB Type (None,CDC,CDC+MSC)
1 parent 2cc7c22 commit 3d195fc

File tree

15 files changed

+637
-378
lines changed

15 files changed

+637
-378
lines changed

boards.txt

Lines changed: 213 additions & 84 deletions
Large diffs are not rendered by default.

cores/stm32l4/USBCore.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,82 @@
3232

3333
#if defined(USBCON)
3434

35+
#define USB_TYPE_NONE 0
36+
#define USB_TYPE_CDC 1
37+
#define USB_TYPE_CDC_MSC 2
38+
39+
#if (USB_TYPE == USB_TYPE_CDC_MSC)
40+
#define USB_CLASS USBD_CDC_MSC_Initialize
41+
#elif (USB_TYPE == USB_TYPE_CDC)
42+
#define USB_CLASS USBD_CDC_Initialize
43+
#endif
44+
45+
static const uint8_t USBDDeviceDescriptor[] = {
46+
0x12, /* bLength */
47+
0x01, /* bDescriptorType */
48+
0x00, 0x02, /* bcdUSB */
49+
0xef, /* bDeviceClass */
50+
0x02, /* bDeviceSubClass */
51+
0x01, /* bDeviceProtocol */
52+
64, /* bMaxPacketSize */
53+
LOBYTE(USB_VID), /* idVendor */
54+
HIBYTE(USB_VID), /* idVendor */
55+
LOBYTE(USB_PID), /* idVendor */
56+
HIBYTE(USB_PID), /* idVendor */
57+
0x00, 0x02, /* bcdDevice rel. 2.00 */
58+
1, /* Index of manufacturer string */
59+
2, /* Index of product string */
60+
3, /* Index of serial number string */
61+
1, /* bNumConfigurations */
62+
};
63+
64+
static const char16_t * USBManufacturer = USB_MANUFACTURER;
65+
static const char16_t * USBProduct = USB_PRODUCT;
66+
3567
void USBDeviceClass::init()
3668
{
37-
USBD_Initialize(STM32L4_CONFIG_USB_VBUS, STM32L4_USB_IRQ_PRIORITY);
69+
#if defined(USB_CLASS)
70+
USBD_Initialize(USBDDeviceDescriptor, (const uint8_t*)USBManufacturer, (const uint8_t*)USBProduct, USB_CLASS, STM32L4_CONFIG_USB_VBUS, STM32L4_USB_IRQ_PRIORITY);
71+
#endif
3872

3973
initialized = true;
4074
}
4175

4276
bool USBDeviceClass::attach()
4377
{
78+
#if defined(USB_CLASS)
4479
if (!initialized)
4580
return false;
4681

4782
USBD_Attach();
4883

4984
return true;
85+
#else
86+
return false;
87+
#endif
5088
}
5189

5290
bool USBDeviceClass::detach()
5391
{
92+
#if defined(USB_CLASS)
5493
if (!initialized)
5594
return false;
5695

5796
USBD_Detach();
5897

5998
return true;
99+
#else
100+
return false;
101+
#endif
60102
}
61103

62104
void USBDeviceClass::poll()
63105
{
106+
#if defined(USB_CLASS)
64107
if (initialized) {
65108
USBD_Poll();
66109
}
110+
#endif
67111
}
68112

69113
bool USBDeviceClass::connected()

cores/stm32l4/stm32l4_wiring_private.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ extern "C" {
6969
#define STM32L4_TONE_IRQ_PRIORITY 2
7070
#define STM32L4_SERVO_IRQ_PRIORITY 1
7171

72-
extern void USBD_Initialize(unsigned int pin_vbus, unsigned int priority);
72+
#define LOBYTE(x) ((uint8_t)(x & 0x00FF))
73+
#define HIBYTE(x) ((uint8_t)((x & 0xFF00) >>8))
74+
75+
extern void USBD_CDC_Initialize(void *);
76+
extern void USBD_CDC_MSC_Initialize(void *);
77+
78+
extern void USBD_Initialize(const uint8_t *device, const uint8_t *manufacturer, const uint8_t *product, void(*initialize)(void *), unsigned int pin_vbus, unsigned int priority);
7379
extern void USBD_Attach(void);
7480
extern void USBD_Detach(void);
7581
extern void USBD_Poll(void);

platform.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ compiler.warning_flags.all=-Wall -Wextra
3131

3232
compiler.path={runtime.tools.arm-none-eabi-gcc.path}/bin/
3333
compiler.c.cmd=arm-none-eabi-gcc
34-
compiler.c.flags=-mcpu={build.mcu} -mthumb -c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -MMD
34+
compiler.c.flags=-mcpu={build.mcu} -mthumb -c -g {build.flags.optimize} {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -nostdlib -MMD
3535
compiler.c.elf.cmd=arm-none-eabi-gcc
36-
compiler.c.elf.flags=-Os -Wl,--gc-sections -save-temps
36+
compiler.c.elf.flags=-g {build.flags.optimize} -Wl,--gc-sections -save-temps
3737
compiler.S.cmd=arm-none-eabi-gcc
3838
compiler.S.flags=-c -g -x assembler-with-cpp
3939
compiler.cpp.cmd=arm-none-eabi-g++
40-
compiler.cpp.flags=-mcpu={build.mcu} -mthumb -c -g -Os {compiler.warning_flags} -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD
40+
compiler.cpp.flags=-mcpu={build.mcu} -mthumb -c -g {build.flags.optimize} {compiler.warning_flags} -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib -fno-rtti -fno-exceptions -MMD
4141
compiler.ar.cmd=arm-none-eabi-ar
4242
compiler.ar.flags=rcs
4343
compiler.objcopy.cmd=arm-none-eabi-objcopy
@@ -64,6 +64,10 @@ compiler.S.extra_flags=
6464
compiler.ar.extra_flags=
6565
compiler.elf2hex.extra_flags=
6666

67+
# USB Flags
68+
# ---------
69+
build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} -DUSBCON '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' '-DUSB_TYPE={build.usb_type}'
70+
6771
# Compile patterns
6872
# ----------------
6973

@@ -82,7 +86,7 @@ archive_file_path={build.path}/{archive_file}
8286
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}"
8387

8488
## Combine gc-sections, archives, and objects
85-
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} {build.extra_flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" {object_files} -Wl,--start-group "{build.path}/{archive_file}" -Wl,--end-group {build.variant_system_libs} -lc -lm
89+
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} {build.extra_flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {build.flags.ldspecs} {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" {object_files} -Wl,--start-group "{build.path}/{archive_file}" -Wl,--end-group {build.variant_system_libs} -lc -lm
8690

8791
## Create output (bin file)
8892
recipe.objcopy.bin.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin"
-1.42 KB
Binary file not shown.
-1.41 KB
Binary file not shown.
-1.4 KB
Binary file not shown.

system/STM32L4xx/Source/USB/Class/CDC/Inc/usbd_cdc.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@
4949
/** @defgroup usbd_cdc_Exported_Defines
5050
* @{
5151
*/
52-
#define CDC_IN_EP 0x83 /* EP1 for data IN */
53-
#define CDC_OUT_EP 0x03 /* EP1 for data OUT */
54-
#define CDC_CMD_EP 0x82 /* EP2 for CDC commands */
52+
#define CDC_CMD_EP 0x81 /* EP1 for CDC commands */
53+
#define CDC_IN_EP 0x82 /* EP2 for data IN */
54+
#define CDC_OUT_EP 0x02 /* EP2 for data OUT */
5555

56-
#define CDC_CONTROL_INTERFACE 1
57-
#define CDC_DATA_INTERFACE 2
56+
#define CDC_CONTROL_INTERFACE 0
57+
#define CDC_DATA_INTERFACE 1
5858

5959
/* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */
6060
#define CDC_DATA_HS_MAX_PACKET_SIZE 512 /* Endpoint IN & OUT Packet size */

system/STM32L4xx/Source/USB/Class/MSC/Inc/usbd_msc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959
#define USB_MSC_CONFIG_DESC_SIZ 32
6060

6161

62-
#define MSC_EPIN_ADDR 0x81
63-
#define MSC_EPOUT_ADDR 0x01
62+
#define MSC_EPIN_ADDR 0x83
63+
#define MSC_EPOUT_ADDR 0x03
6464

65-
#define MSC_INTERFACE 0
65+
#define MSC_INTERFACE 2
6666

6767
/**
6868
* @}

0 commit comments

Comments
 (0)