Skip to content

Commit 8913c04

Browse files
committed
stmhal: Add support for USB MSC device.
This gives a functioning, independent MSC device.
1 parent c070ff2 commit 8913c04

20 files changed

Lines changed: 602 additions & 65 deletions

stmhal/Makefile

Lines changed: 8 additions & 2 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
24+
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/cdc/inc -I$(USBDEV_DIR)/class/msc/inc
2525
#INC += -I$(USBHOST_DIR)
2626
INC += -I$(FATFS_DIR)/src
2727
#INC += -I$(CC3K_DIR)
@@ -56,8 +56,10 @@ SRC_C = \
5656
stm32f4xx_it.c \
5757
stm32f4xx_hal_msp.c \
5858
usbd_conf.c \
59-
usbd_desc.c \
59+
usbd_desc_vcp.c \
6060
usbd_cdc_interface.c \
61+
usbd_desc_msc.c \
62+
usbd_msc_storage.c \
6163
pendsv.c \
6264
systick.c \
6365
led.c \
@@ -123,6 +125,10 @@ SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\
123125
core/src/usbd_ctlreq.c \
124126
core/src/usbd_ioreq.c \
125127
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 \
126132
)
127133

128134
# usbd_core.c \

stmhal/main.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ int main(void) {
374374
// turn boot-up LED off
375375
led_state(PYB_LED_GREEN, 0);
376376

377+
#if defined(USE_DEVICE_MODE)
378+
usbd_storage_medium_kind_t usbd_medium_kind = USBD_STORAGE_MEDIUM_FLASH;
379+
#endif
380+
377381
#if MICROPY_HW_HAS_SDCARD
378382
// if an SD card is present then mount it on 1:/
379383
if (sdcard_is_present()) {
@@ -383,8 +387,8 @@ int main(void) {
383387
} else {
384388
if (first_soft_reset) {
385389
// use SD card as medium for the USB MSD
386-
#if 0
387-
usbd_storage_select_medium(USBD_STORAGE_MEDIUM_SDCARD);
390+
#if defined(USE_DEVICE_MODE)
391+
usbd_medium_kind = USBD_STORAGE_MEDIUM_SDCARD;
388392
#endif
389393
}
390394
}
@@ -396,7 +400,7 @@ int main(void) {
396400
pyb_usb_host_init();
397401
#elif defined(USE_DEVICE_MODE)
398402
// USB device
399-
pyb_usb_dev_init(PYB_USB_DEV_VCP_MSC);
403+
pyb_usb_dev_init(USBD_DEVICE_MSC, usbd_medium_kind);
400404
#endif
401405

402406
#if MICROPY_HW_HAS_MMA7660

stmhal/usb.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
#include <string.h>
22

3-
/*
4-
#include "usb_core.h"
5-
#include "usbd_cdc_core.h"
6-
#include "usbd_pyb_core.h"
7-
#include "usbd_usr.h"
8-
*/
93
#include "usbd_core.h"
104
#include "usbd_desc.h"
115
#include "usbd_cdc.h"
126
#include "usbd_cdc_interface.h"
7+
#include "usbd_msc.h"
8+
#include "usbd_msc_storage.h"
139

1410
#include "misc.h"
1511
#include "mpconfig.h"
1612
#include "qstr.h"
1713
#include "obj.h"
18-
//#include "pendsv.h"
1914
#include "usb.h"
2015

2116
#ifdef USE_DEVICE_MODE
@@ -26,12 +21,12 @@ static int dev_is_enabled = 0;
2621
uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */
2722
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
2823

29-
void pyb_usb_dev_init(int usb_dev_type) {
24+
void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t medium_kind) {
3025
#ifdef USE_DEVICE_MODE
3126
if (!dev_is_enabled) {
3227
// only init USB once in the device's power-lifetime
33-
switch (usb_dev_type) {
34-
case PYB_USB_DEV_VCP_MSC:
28+
switch (device_kind) {
29+
case USBD_DEVICE_CDC:
3530
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
3631
// so the memory is invalid after a soft reset (which resets the GC).
3732
USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
@@ -41,7 +36,20 @@ void pyb_usb_dev_init(int usb_dev_type) {
4136
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
4237
break;
4338

44-
case PYB_USB_DEV_HID:
39+
case USBD_DEVICE_MSC:
40+
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
41+
// so the memory is invalid after a soft reset (which resets the GC).
42+
USBD_Init(&hUSBDDevice, &MSC_Desc, 0);
43+
USBD_RegisterClass(&hUSBDDevice, &USBD_MSC);
44+
if (medium_kind == USBD_STORAGE_MEDIUM_FLASH) {
45+
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
46+
} else {
47+
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
48+
}
49+
USBD_Start(&hUSBDDevice);
50+
break;
51+
52+
case USBD_DEVICE_HID:
4553
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
4654
// TODO
4755
break;

stmhal/usb.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
#define VCP_CHAR_CTRL_C (3)
55
#define VCP_CHAR_CTRL_D (4)
66

7-
#define PYB_USB_DEV_VCP_MSC (0)
8-
#define PYB_USB_DEV_HID (1)
7+
typedef enum {
8+
USBD_DEVICE_CDC,
9+
USBD_DEVICE_MSC,
10+
USBD_DEVICE_HID,
11+
} usbd_device_kind_t;
912

10-
void pyb_usb_dev_init(int usb_dev_type);
13+
typedef enum {
14+
USBD_STORAGE_MEDIUM_FLASH,
15+
USBD_STORAGE_MEDIUM_SDCARD,
16+
} usbd_storage_medium_kind_t;
17+
18+
void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t medium_kind);
1119
bool usb_vcp_is_enabled(void);
1220
bool usb_vcp_is_connected(void);
1321
void usb_vcp_set_interrupt_char(int c);

stmhal/usbd_conf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
#define USBD_SELF_POWERED 0
4949
#define USBD_DEBUG_LEVEL 0
5050

51+
// for MSC device
52+
#define MSC_MEDIA_PACKET 8192
53+
5154
/* Exported macro ------------------------------------------------------------*/
5255
/* Memory management macros */
5356
#define USBD_malloc gc_alloc

stmhal/usbd_desc.h

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,2 @@
1-
/**
2-
******************************************************************************
3-
* @file USB_Device/CDC_Standalone/Inc/usbd_desc.h
4-
* @author MCD Application Team
5-
* @version V1.0.1
6-
* @date 26-February-2014
7-
* @brief Header for usbd_desc.c module
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-
/* Define to prevent recursive inclusion -------------------------------------*/
29-
#ifndef __USBD_DESC_H
30-
#define __USBD_DESC_H
31-
32-
/* Includes ------------------------------------------------------------------*/
33-
#include "usbd_def.h"
34-
35-
/* Exported types ------------------------------------------------------------*/
36-
/* Exported constants --------------------------------------------------------*/
37-
/* Exported macro ------------------------------------------------------------*/
38-
/* Exported functions ------------------------------------------------------- */
39-
extern USBD_DescriptorsTypeDef VCP_Desc;
40-
41-
#endif /* __USBD_DESC_H */
42-
43-
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1+
extern USBD_DescriptorsTypeDef VCP_Desc;
2+
extern USBD_DescriptorsTypeDef MSC_Desc;

0 commit comments

Comments
 (0)