Skip to content

Commit fb25c2d

Browse files
committed
stmhal: USB CDC and MSC device work together.
1 parent b32db4e commit fb25c2d

File tree

15 files changed

+3344
-15
lines changed

15 files changed

+3344
-15
lines changed

stmhal/Makefile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ INC += -I$(PY_SRC)
2121
INC += -I$(CMSIS_DIR)/inc
2222
INC += -I$(CMSIS_DIR)/devinc
2323
INC += -I$(HAL_DIR)/inc
24-
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/cdc/inc -I$(USBDEV_DIR)/class/msc/inc
24+
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/cdc_msc/inc
2525
#INC += -I$(USBHOST_DIR)
2626
INC += -I$(FATFS_DIR)/src
2727
#INC += -I$(CC3K_DIR)
@@ -56,9 +56,8 @@ SRC_C = \
5656
stm32f4xx_it.c \
5757
stm32f4xx_hal_msp.c \
5858
usbd_conf.c \
59-
usbd_desc_vcp.c \
59+
usbd_desc_cdc_msc.c \
6060
usbd_cdc_interface.c \
61-
usbd_desc_msc.c \
6261
usbd_msc_storage.c \
6362
pendsv.c \
6463
systick.c \
@@ -124,13 +123,14 @@ SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\
124123
core/src/usbd_core.c \
125124
core/src/usbd_ctlreq.c \
126125
core/src/usbd_ioreq.c \
127-
class/cdc/src/usbd_cdc.c \
128-
class/msc/src/usbd_msc.c \
129-
class/msc/src/usbd_msc_bot.c \
130-
class/msc/src/usbd_msc_scsi.c \
131-
class/msc/src/usbd_msc_data.c \
126+
class/cdc_msc/src/usbd_cdc_msc.c \
127+
class/cdc_msc/src/usbd_msc_bot.c \
128+
class/cdc_msc/src/usbd_msc_scsi.c \
129+
class/cdc_msc/src/usbd_msc_data.c \
132130
)
133131

132+
# class/cdc/src/usbd_cdc.c \
133+
class/msc/src/usbd_msc.c \
134134
# usbd_core.c \
135135
usbd_ioreq.c \
136136
usbd_req.c \

stmhal/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ int main(void) {
336336
fno.lfname = NULL;
337337
fno.lfsize = 0;
338338
#endif
339-
led_debug(0, 500);
340339
FRESULT res = f_stat("0:/boot.py", &fno);
341340
if (res == FR_OK) {
342341
if (fno.fattrib & AM_DIR) {

stmhal/usb.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
#include "usbd_core.h"
44
#include "usbd_desc.h"
5-
#include "usbd_cdc.h"
5+
#include "usbd_cdc_msc.h"
66
#include "usbd_cdc_interface.h"
7-
#include "usbd_msc.h"
87
#include "usbd_msc_storage.h"
98

109
#include "misc.h"
@@ -26,6 +25,7 @@ void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t
2625
if (!dev_is_enabled) {
2726
// only init USB once in the device's power-lifetime
2827
switch (device_kind) {
28+
#if 0
2929
case USBD_DEVICE_CDC:
3030
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
3131
// so the memory is invalid after a soft reset (which resets the GC).
@@ -48,6 +48,21 @@ void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t
4848
}
4949
USBD_Start(&hUSBDDevice);
5050
break;
51+
#endif
52+
53+
case USBD_DEVICE_CDC:
54+
case USBD_DEVICE_MSC:
55+
USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
56+
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC);
57+
USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
58+
if (medium_kind == USBD_STORAGE_MEDIUM_FLASH) {
59+
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
60+
} else {
61+
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
62+
}
63+
USBD_Start(&hUSBDDevice);
64+
break;
65+
5166

5267
case USBD_DEVICE_HID:
5368
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);

stmhal/usbd_conf.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
342342
pdev->pData = &hpcd;
343343
/*Initialize LL Driver */
344344
HAL_PCD_Init(&hpcd);
345-
345+
346346
HAL_PCD_SetRxFiFo(&hpcd, 0x80);
347-
HAL_PCD_SetTxFiFo(&hpcd, 0, 0x40);
348-
HAL_PCD_SetTxFiFo(&hpcd, 1, 0x80);
347+
HAL_PCD_SetTxFiFo(&hpcd, 0, 0x20);
348+
HAL_PCD_SetTxFiFo(&hpcd, 1, 0x40);
349+
HAL_PCD_SetTxFiFo(&hpcd, 2, 0x20);
350+
HAL_PCD_SetTxFiFo(&hpcd, 3, 0x40);
349351

350352

351353
#endif

stmhal/usbd_desc_cdc_msc.c

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/**
2+
******************************************************************************
3+
* @file USB_Device/CDC_Standalone/Src/usbd_desc.c
4+
* @author MCD Application Team
5+
* @version V1.0.1
6+
* @date 26-February-2014
7+
* @brief This file provides the USBD descriptors and string formating method.
8+
******************************************************************************
9+
* @attention
10+
*
11+
* <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
12+
*
13+
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14+
* You may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at:
16+
*
17+
* http://www.st.com/software_license_agreement_liberty_v2
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*
25+
******************************************************************************
26+
*/
27+
28+
/* Includes ------------------------------------------------------------------*/
29+
#include "usbd_core.h"
30+
#include "usbd_desc.h"
31+
#include "usbd_conf.h"
32+
33+
/* Private typedef -----------------------------------------------------------*/
34+
/* Private define ------------------------------------------------------------*/
35+
#define USBD_VID 0x0483
36+
#define USBD_PID 0x5740
37+
#define USBD_LANGID_STRING 0x409
38+
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
39+
#define USBD_PRODUCT_HS_STRING "STM32 Virtual ComPort in HS Mode"
40+
#define USBD_SERIALNUMBER_HS_STRING "00000000001A"
41+
#define USBD_PRODUCT_FS_STRING "STM32 Virtual ComPort in FS Mode"
42+
#define USBD_SERIALNUMBER_FS_STRING "00000000001B"
43+
#define USBD_CONFIGURATION_HS_STRING "VCP Config"
44+
#define USBD_INTERFACE_HS_STRING "VCP Interface"
45+
#define USBD_CONFIGURATION_FS_STRING "VCP Config"
46+
#define USBD_INTERFACE_FS_STRING "VCP Interface"
47+
48+
/* Private macro -------------------------------------------------------------*/
49+
/* Private function prototypes -----------------------------------------------*/
50+
uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
51+
uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
52+
uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
53+
uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
54+
uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
55+
uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
56+
uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
57+
#ifdef USB_SUPPORT_USER_STRING_DESC
58+
uint8_t *USBD_VCP_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
59+
#endif /* USB_SUPPORT_USER_STRING_DESC */
60+
61+
/* Private variables ---------------------------------------------------------*/
62+
USBD_DescriptorsTypeDef VCP_Desc = {
63+
USBD_VCP_DeviceDescriptor,
64+
USBD_VCP_LangIDStrDescriptor,
65+
USBD_VCP_ManufacturerStrDescriptor,
66+
USBD_VCP_ProductStrDescriptor,
67+
USBD_VCP_SerialStrDescriptor,
68+
USBD_VCP_ConfigStrDescriptor,
69+
USBD_VCP_InterfaceStrDescriptor,
70+
};
71+
72+
/* USB Standard Device Descriptor */
73+
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
74+
#pragma data_alignment=4
75+
#endif
76+
__ALIGN_BEGIN static uint8_t hUSBDDeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
77+
0x12, /* bLength */
78+
USB_DESC_TYPE_DEVICE, /* bDescriptorType */
79+
0x00, /* bcdUSB */
80+
0x02,
81+
0x00, /* bDeviceClass */
82+
0x00, /* bDeviceSubClass */
83+
0x00, /* bDeviceProtocol */
84+
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
85+
LOBYTE(USBD_VID), /* idVendor */
86+
HIBYTE(USBD_VID), /* idVendor */
87+
LOBYTE(USBD_PID), /* idVendor */
88+
HIBYTE(USBD_PID), /* idVendor */
89+
0x00, /* bcdDevice rel. 2.00 */
90+
0x02,
91+
USBD_IDX_MFC_STR, /* Index of manufacturer string */
92+
USBD_IDX_PRODUCT_STR, /* Index of product string */
93+
USBD_IDX_SERIAL_STR, /* Index of serial number string */
94+
USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */
95+
}; /* USB_DeviceDescriptor */
96+
97+
/* USB Standard Device Descriptor */
98+
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
99+
#pragma data_alignment=4
100+
#endif
101+
__ALIGN_BEGIN static uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
102+
USB_LEN_LANGID_STR_DESC,
103+
USB_DESC_TYPE_STRING,
104+
LOBYTE(USBD_LANGID_STRING),
105+
HIBYTE(USBD_LANGID_STRING),
106+
};
107+
108+
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
109+
#pragma data_alignment=4
110+
#endif
111+
__ALIGN_BEGIN static uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
112+
113+
/* Private functions ---------------------------------------------------------*/
114+
115+
/**
116+
* @brief Returns the device descriptor.
117+
* @param speed: Current device speed
118+
* @param length: Pointer to data length variable
119+
* @retval Pointer to descriptor buffer
120+
*/
121+
uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
122+
{
123+
*length = sizeof(hUSBDDeviceDesc);
124+
return hUSBDDeviceDesc;
125+
}
126+
127+
/**
128+
* @brief Returns the LangID string descriptor.
129+
* @param speed: Current device speed
130+
* @param length: Pointer to data length variable
131+
* @retval Pointer to descriptor buffer
132+
*/
133+
uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
134+
{
135+
*length = sizeof(USBD_LangIDDesc);
136+
return USBD_LangIDDesc;
137+
}
138+
139+
/**
140+
* @brief Returns the product string descriptor.
141+
* @param speed: Current device speed
142+
* @param length: Pointer to data length variable
143+
* @retval Pointer to descriptor buffer
144+
*/
145+
uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
146+
{
147+
if(speed == 0)
148+
{
149+
USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
150+
}
151+
else
152+
{
153+
USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
154+
}
155+
return USBD_StrDesc;
156+
}
157+
158+
/**
159+
* @brief Returns the manufacturer string descriptor.
160+
* @param speed: Current device speed
161+
* @param length: Pointer to data length variable
162+
* @retval Pointer to descriptor buffer
163+
*/
164+
uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
165+
{
166+
USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
167+
return USBD_StrDesc;
168+
}
169+
170+
/**
171+
* @brief Returns the serial number string descriptor.
172+
* @param speed: Current device speed
173+
* @param length: Pointer to data length variable
174+
* @retval Pointer to descriptor buffer
175+
*/
176+
uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
177+
{
178+
if(speed == USBD_SPEED_HIGH)
179+
{
180+
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length);
181+
}
182+
else
183+
{
184+
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc, length);
185+
}
186+
return USBD_StrDesc;
187+
}
188+
189+
/**
190+
* @brief Returns the configuration string descriptor.
191+
* @param speed: Current device speed
192+
* @param length: Pointer to data length variable
193+
* @retval Pointer to descriptor buffer
194+
*/
195+
uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
196+
{
197+
if(speed == USBD_SPEED_HIGH)
198+
{
199+
USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
200+
}
201+
else
202+
{
203+
USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
204+
}
205+
return USBD_StrDesc;
206+
}
207+
208+
/**
209+
* @brief Returns the interface string descriptor.
210+
* @param speed: Current device speed
211+
* @param length: Pointer to data length variable
212+
* @retval Pointer to descriptor buffer
213+
*/
214+
uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
215+
{
216+
if(speed == 0)
217+
{
218+
USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
219+
}
220+
else
221+
{
222+
USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
223+
}
224+
return USBD_StrDesc;
225+
}
226+
227+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
228+

stmhal/usbd_msc_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
******************************************************************************
2828
*/
2929

30-
#include "usbd_msc.h"
30+
#include "usbd_cdc_msc.h"
3131
#include "usbd_msc_storage.h"
3232

3333
#include "misc.h"

0 commit comments

Comments
 (0)