Skip to content

Commit d7ca63e

Browse files
committed
增加usb U盘
1 parent 88794b7 commit d7ca63e

File tree

6 files changed

+66
-39
lines changed

6 files changed

+66
-39
lines changed

applications/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ int main(void)
7171
// extern void cdc_acm_init(uint8_t busid, uint32_t reg_base);
7272
// cdc_acm_init(0, CM_USBFS_BASE);
7373
//
74-
extern void msc_ram_init(uint8_t busid, uint32_t reg_base);
75-
msc_ram_init(0, CM_USBFS_BASE);
74+
extern void msc_storage_init(uint8_t busid, uint32_t reg_base);
75+
msc_storage_init(0, CM_USBFS_BASE);
7676

7777
while (1)
7878
{

applications/usb_app.c

+29-33
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
#include "usbd_core.h"
2-
3-
/*
4-
* Copyright (c) 2022, sakumisu
5-
*
6-
* SPDX-License-Identifier: Apache-2.0
7-
*/
8-
9-
#include "usbd_cdc.h"
10-
11-
121
/*
132
* Copyright (c) 2024, sakumisu
143
*
154
* SPDX-License-Identifier: Apache-2.0
165
*/
6+
#include "usbd_core.h"
177
#include "usbd_msc.h"
188

9+
#ifdef __RT_THREAD_H__
10+
1911
#define MSC_IN_EP 0x81
2012
#define MSC_OUT_EP 0x02
2113

@@ -32,8 +24,9 @@
3224
#define MSC_MAX_MPS 64
3325
#endif
3426

35-
const uint8_t msc_ram_descriptor[] = {
36-
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
27+
28+
const uint8_t msc_storage_descriptor[] = {
29+
USB_DEVICE_DESCRIPTOR_INIT(USB_1_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
3730
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
3831
MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02),
3932
///////////////////////////////////////
@@ -110,6 +103,14 @@ const uint8_t msc_ram_descriptor[] = {
110103
0x00
111104
};
112105

106+
struct usbd_interface intf0;
107+
108+
/* assume the block device is 512M */
109+
#define BLOCK_DEV_NAME "sd"
110+
#define BLOCK_SIZE 512U
111+
#define BLOCK_COUNT 0x1024U * 0x1024U
112+
static rt_device_t blk_dev = RT_NULL;
113+
113114
static void usbd_event_handler(uint8_t busid, uint8_t event)
114115
{
115116
switch (event) {
@@ -135,42 +136,37 @@ static void usbd_event_handler(uint8_t busid, uint8_t event)
135136
}
136137
}
137138

138-
#define BLOCK_SIZE 512
139-
#define BLOCK_COUNT 10
140-
141-
typedef struct
142-
{
143-
uint8_t BlockSpace[BLOCK_SIZE];
144-
} BLOCK_TYPE;
145-
146-
BLOCK_TYPE mass_block[BLOCK_COUNT];
147-
148139
void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
149140
{
150-
*block_num = 1000; //Pretend having so many buffer,not has actually.
141+
*block_num = BLOCK_COUNT;
151142
*block_size = BLOCK_SIZE;
152143
}
144+
153145
int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
154146
{
155-
if (sector < BLOCK_COUNT)
156-
memcpy(buffer, mass_block[sector].BlockSpace, length);
147+
rt_device_read(blk_dev, sector, buffer, length / BLOCK_SIZE);
157148
return 0;
158149
}
159150

160151
int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
161152
{
162-
if (sector < BLOCK_COUNT)
163-
memcpy(mass_block[sector].BlockSpace, buffer, length);
153+
rt_device_write(blk_dev, sector, buffer, length / BLOCK_SIZE);
164154
return 0;
165155
}
166156

167-
struct usbd_interface intf0;
168-
169-
void msc_ram_init(uint8_t busid, uint32_t reg_base)
157+
void msc_storage_init(uint8_t busid, uint32_t reg_base)
170158
{
171-
usbd_desc_register(busid, msc_ram_descriptor);
159+
rt_err_t res;
160+
161+
blk_dev = rt_device_find(BLOCK_DEV_NAME);
162+
RT_ASSERT(blk_dev);
163+
164+
res = rt_device_open(blk_dev, RT_DEVICE_OFLAG_RDWR);
165+
RT_ASSERT(res == RT_EOK);
166+
167+
usbd_desc_register(busid, msc_storage_descriptor);
172168
usbd_add_interface(busid, usbd_msc_init_intf(busid, &intf0, MSC_OUT_EP, MSC_IN_EP));
173169

174170
usbd_initialize(busid, reg_base, usbd_event_handler);
175171
}
176-
172+
#endif

board/usb_config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
#define CONFIG_USBDEV_MSC_VERSION_STRING "0.01"
7474
#endif
7575

76-
// #define CONFIG_USBDEV_MSC_THREAD
76+
#define CONFIG_USBDEV_MSC_THREAD
7777

7878
#ifndef CONFIG_USBDEV_MSC_PRIO
7979
#define CONFIG_USBDEV_MSC_PRIO 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2024, sakumisu
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "usb_config.h"
7+
#include "usb_dwc2_reg.h"
8+
9+
/* When using [GPIO_SetFunc(USBF_VBUS_PORT, USBF_VBUS_PIN, USBF_VBUS_FUNC);], there is no need to configure GOTGCTL */
10+
11+
#define USB_OTG_GLB ((DWC2_GlobalTypeDef *)(reg_base))
12+
13+
uint32_t usbd_get_dwc2_gccfg_conf(uint32_t reg_base)
14+
{
15+
16+
USB_OTG_GLB->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
17+
USB_OTG_GLB->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
18+
return 0;
19+
}
20+
21+
uint32_t usbh_get_dwc2_gccfg_conf(uint32_t reg_base)
22+
{
23+
USB_OTG_GLB->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOEN;
24+
USB_OTG_GLB->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOVAL;
25+
return 0;
26+
}

project.uvoptx

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
<GroupNumber>2</GroupNumber>
277277
<FileNumber>7</FileNumber>
278278
<FileType>1</FileType>
279-
<tvExp>0</tvExp>
279+
<tvExp>1</tvExp>
280280
<tvExpOptDlg>0</tvExpOptDlg>
281281
<bDave2>0</bDave2>
282282
<PathWithFileName>packages\CherryUSB-1.3.0\class\cdc\usbd_cdc.c</PathWithFileName>
@@ -900,7 +900,7 @@
900900

901901
<Group>
902902
<GroupName>Fal</GroupName>
903-
<tvExp>0</tvExp>
903+
<tvExp>1</tvExp>
904904
<tvExpOptDlg>0</tvExpOptDlg>
905905
<cbSel>0</cbSel>
906906
<RteFlg>0</RteFlg>

project.uvprojx

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<TargetName>rt-thread</TargetName>
1111
<ToolsetNumber>0x4</ToolsetNumber>
1212
<ToolsetName>ARM-ADS</ToolsetName>
13-
<pCCUsed>5060960::V5.06 update 7 (build 960)::.\ARMCC</pCCUsed>
13+
<pCCUsed>5060750::V5.06 update 6 (build 750)::.\ARMCC</pCCUsed>
1414
<uAC6>0</uAC6>
1515
<TargetOption>
1616
<TargetCommonOption>
@@ -3099,6 +3099,11 @@
30993099
<Layers>
31003100
<Layer>
31013101
<LayName>&lt;Project Info&gt;</LayName>
3102+
<LayDesc></LayDesc>
3103+
<LayUrl></LayUrl>
3104+
<LayKeys></LayKeys>
3105+
<LayCat></LayCat>
3106+
<LayLic></LayLic>
31023107
<LayTarg>0</LayTarg>
31033108
<LayPrjMark>1</LayPrjMark>
31043109
</Layer>

0 commit comments

Comments
 (0)