Skip to content

Commit f310ae7

Browse files
committed
Usb Host Mass Storage Device working fine but not all USB sticks work
1 parent abadef9 commit f310ae7

File tree

3 files changed

+264
-150
lines changed

3 files changed

+264
-150
lines changed

libraries/UsbHostMsd/UsbHostMsd.cpp

+165-122
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#include "UsbHostMsd.h"
2121

2222

23+
extern "C" int usb_host_msd_get_device_address();
24+
extern "C" uint8_t usb_host_msd_get_default_lun();
25+
extern "C" uint8_t usb_host_msd_get_lun_num();
26+
extern "C" uint32_t usb_host_msd_get_num_of_blocks(uint8_t lun);
27+
extern "C" uint32_t usb_host_msd_get_block_size(uint8_t lun);
2328

2429

2530
#define CBW_SIGNATURE 0x43425355
@@ -31,172 +36,210 @@
3136
#define GET_MAX_LUN (0xFE)
3237
#define BO_MASS_STORAGE_RESET (0xFF)
3338

34-
USBHostMSD::USBHostMSD()
35-
{
36-
37-
}
38-
3939

40+
/* -------------------------------------------------------------------------- */
41+
/* CONSTRUCTOR */
42+
/* -------------------------------------------------------------------------- */
43+
USBHostMSD::USBHostMSD() : selected_lun(-1), block_size(0) { }
4044

41-
bool USBHostMSD::connected()
42-
{
43-
return false;
45+
/* -------------------------------------------------------------------------- */
46+
/* INIT */
47+
/* -------------------------------------------------------------------------- */
48+
int USBHostMSD::init() {
49+
return BLOCK_DEVICE_OK;
4450
}
4551

46-
bool USBHostMSD::connect()
47-
{
48-
49-
50-
return false;
52+
/* -------------------------------------------------------------------------- */
53+
/* OPEN */
54+
/* -------------------------------------------------------------------------- */
55+
int USBHostMSD::open() {
56+
return BLOCK_DEVICE_OK;
5157
}
5258

53-
/*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid)
54-
{
55-
// we don't check VID/PID for MSD driver
59+
/* -------------------------------------------------------------------------- */
60+
/* DEINIT */
61+
/* -------------------------------------------------------------------------- */
62+
int USBHostMSD::deinit() {
63+
return BLOCK_DEVICE_OK;
5664
}
5765

58-
/*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
59-
{
60-
61-
return false;
66+
/* -------------------------------------------------------------------------- */
67+
/* CLOSE */
68+
/* -------------------------------------------------------------------------- */
69+
int USBHostMSD::close() {
70+
return BLOCK_DEVICE_OK;
6271
}
6372

64-
/*virtual*/ //bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
65-
//{
66-
67-
//return false;
68-
//}
69-
70-
/*
71-
int USBHostMSD::testUnitReady()
72-
{
73-
return 0;
73+
/* -------------------------------------------------------------------------- */
74+
/* PROGRAM - 'remapped' on write */
75+
/* -------------------------------------------------------------------------- */
76+
int USBHostMSD::program(const void *buffer, bd_addr_t addr, bd_size_t _size) {
77+
return write(buffer, addr, _size);
7478
}
7579

76-
77-
int USBHostMSD::readCapacity()
78-
{
79-
return 0;
80-
}
81-
82-
83-
int USBHostMSD::SCSIRequestSense()
84-
{
85-
return 0;
86-
}
87-
88-
89-
int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code)
90-
{
91-
92-
return 0;
93-
}
94-
95-
int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep)
96-
{
97-
98-
99-
return 0;
80+
/* -------------------------------------------------------------------------- */
81+
/* CONNECTED (if mount callback has been called and address is assigned) */
82+
/* -------------------------------------------------------------------------- */
83+
bool USBHostMSD::connected() {
84+
if(usb_host_msd_get_device_address() >= 0) {
85+
if(tuh_msc_mounted(usb_host_msd_get_device_address())){
86+
block_size = (bd_size_t)usb_host_msd_get_block_size(get_lun());
87+
num_of_blocks = usb_host_msd_get_num_of_blocks(get_lun());
88+
total_size = block_size * num_of_blocks;
89+
return true;
90+
}
91+
}
92+
return false;
10093
}
10194

102-
103-
int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len)
104-
{
105-
106-
107-
108-
109-
return 0;
95+
/* -------------------------------------------------------------------------- */
96+
/* CONNECT - just remapped on CONNECTED */
97+
/* -------------------------------------------------------------------------- */
98+
bool USBHostMSD::connect() {
99+
return connected();
110100
}
111101

112-
113-
int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction)
114-
{
115-
return 0;
102+
/* -------------------------------------------------------------------------- */
103+
/* */
104+
/* -------------------------------------------------------------------------- */
105+
static bool in_progress = false;
106+
bool complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw) {
107+
in_progress = false;
116108
}
117109

118-
int USBHostMSD::getMaxLun()
119-
{
120-
return 0;
121-
}
122-
*/
123-
int USBHostMSD::init()
124-
{
125-
return 0;
126-
}
127110

128-
int USBHostMSD::program(const void *buffer, bd_addr_t addr, bd_size_t size)
129-
{
111+
/* -------------------------------------------------------------------------- */
112+
/* WRITE */
113+
/* -------------------------------------------------------------------------- */
114+
int USBHostMSD::write(const void *buffer, bd_addr_t addr, bd_size_t size) {
130115
uint32_t block_number, count;
131116
uint8_t *buf = (uint8_t *)buffer;
132-
if (!disk_init) {
133-
init();
134-
}
135-
if (!disk_init) {
136-
return -1;
137-
}
138-
block_number = addr / block_size;
139-
count = size /block_size;
140-
141-
for (uint32_t b = block_number; b < block_number + count; b++) {
142-
/*
143-
if (dataTransfer(buf, b, 1, HOST_TO_DEVICE))
144-
return -1;
145-
*/
146-
buf += block_size;
117+
if (connected()) {
118+
while(!tuh_msc_ready(usb_host_msd_get_device_address())) {
119+
delay(10);
120+
}
121+
122+
block_number = addr / block_size;
123+
count = size / block_size;
124+
125+
for (uint32_t b = block_number; b < block_number + count; b++) {
126+
in_progress = true;
127+
if(tuh_msc_write10(usb_host_msd_get_device_address(), get_lun(), buf, b, 1, complete_cb)) {
128+
while(in_progress) {
129+
130+
}
131+
buf += block_size;
132+
}
133+
else {
134+
return -1;
135+
}
136+
}
137+
return 0;
147138
}
148-
return 0;
139+
return -1;
149140
}
150141

142+
/* -------------------------------------------------------------------------- */
143+
/* READ */
144+
/* -------------------------------------------------------------------------- */
151145
int USBHostMSD::read(void *buffer, bd_addr_t addr, bd_size_t size)
152146
{
153147
uint32_t block_number, count;
154148
uint8_t *buf = (uint8_t *)buffer;
155-
if (!disk_init) {
156-
init();
149+
if (connected()) {
150+
while(!tuh_msc_ready(usb_host_msd_get_device_address())) {
151+
delay(10);
152+
}
153+
154+
block_number = addr / block_size;
155+
count = size / block_size;
156+
157+
for (uint32_t b = block_number; b < block_number + count; b++) {
158+
in_progress = true;
159+
if(tuh_msc_read10(usb_host_msd_get_device_address(), get_lun(), buf, b, 1, complete_cb)) {
160+
while(in_progress) {
161+
162+
}
163+
buf += block_size;
164+
}
165+
else {
166+
return -1;
167+
}
168+
}
169+
return 0;
157170
}
158-
if (!disk_init) {
159-
return -1;
171+
return -1;
172+
}
173+
174+
/* -------------------------------------------------------------------------- */
175+
/* GET THE SELECTED lun or the default one if none is selected */
176+
/* -------------------------------------------------------------------------- */
177+
uint8_t USBHostMSD::get_lun() {
178+
if(selected_lun == -1) {
179+
return usb_host_msd_get_default_lun();
160180
}
161-
block_number = addr / block_size;
162-
count = size / block_size;
163-
164-
for (uint32_t b = block_number; b < block_number + count; b++) {
165-
/*
166-
if (dataTransfer(buf, b, 1, DEVICE_TO_HOST))
167-
return -1;
168-
*/
169-
buf += block_size;
181+
else {
182+
return selected_lun;
170183
}
171-
return 0;
172184
}
173185

174-
int USBHostMSD::erase(bd_addr_t addr, bd_size_t size)
175-
{
186+
/* -------------------------------------------------------------------------- */
187+
/* SELECT a LUN it return true if the selected lun is correct (lower than the max) */
188+
/* -------------------------------------------------------------------------- */
189+
bool USBHostMSD::select_lun(uint8_t lun) {
190+
if(lun < usb_host_msd_get_lun_num()) {
191+
selected_lun = lun;
192+
return true;
193+
}
194+
return false;
195+
}
196+
197+
/* -------------------------------------------------------------------------- */
198+
/* GET THE MAX NUMBER OF LUN OF THE DEVICE */
199+
/* -------------------------------------------------------------------------- */
200+
uint8_t USBHostMSD::get_lun_num() {
201+
return usb_host_msd_get_lun_num();
202+
}
203+
204+
/* -------------------------------------------------------------------------- */
205+
/* ERASE */
206+
/* -------------------------------------------------------------------------- */
207+
int USBHostMSD::erase(bd_addr_t addr, bd_size_t size) {
176208
return 0;
177209
}
178210

211+
/* -------------------------------------------------------------------------- */
212+
/* READ SIZE */
213+
/* -------------------------------------------------------------------------- */
179214
bd_size_t USBHostMSD::get_read_size() const {
180-
return (disk_init ? (bd_size_t)block_size : -1);
215+
return (bd_size_t)block_size;
181216
}
182217

183-
bd_size_t USBHostMSD::get_program_size() const
184-
{
185-
return (disk_init ? (bd_size_t)block_size : -1);
218+
/* -------------------------------------------------------------------------- */
219+
/* PROGRAM SIZE */
220+
/* -------------------------------------------------------------------------- */
221+
bd_size_t USBHostMSD::get_program_size() const {
222+
return (bd_size_t)block_size;
186223
}
187-
bd_size_t USBHostMSD::get_erase_size() const
188-
{
189-
return (disk_init ? (bd_size_t)block_size : -1);
224+
225+
/* -------------------------------------------------------------------------- */
226+
/* ERASE SIZE */
227+
/* -------------------------------------------------------------------------- */
228+
bd_size_t USBHostMSD::get_erase_size() const {
229+
return (bd_size_t)block_size;
190230
}
191231

192-
bd_size_t USBHostMSD::size() const
193-
{
194-
//USB_DBG("FILESYSTEM: size ");
195-
return (disk_init ? (bd_size_t)block_size : 0);
232+
/* -------------------------------------------------------------------------- */
233+
/* TOTAL SIZE */
234+
/* -------------------------------------------------------------------------- */
235+
bd_size_t USBHostMSD::size() const {
236+
return (bd_size_t)total_size;
196237
}
197238

198-
const char *USBHostMSD::get_type() const
199-
{
239+
/* -------------------------------------------------------------------------- */
240+
/* TYPE */
241+
/* -------------------------------------------------------------------------- */
242+
const char *USBHostMSD::get_type() const {
200243
return "USBMSD";
201244
}
202245

0 commit comments

Comments
 (0)