Skip to content

Commit 5ea1d26

Browse files
committed
more than one LUN are available
Former-commit-id: 7173c01
1 parent 6a39b6e commit 5ea1d26

File tree

2 files changed

+105
-62
lines changed

2 files changed

+105
-62
lines changed

libraries/UsbMsd/UsbMsd.cpp

Lines changed: 91 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,145 @@
11
#include "UsbMsd.h"
22

3+
/* -------------------------------------------------------------------------- */
4+
/* extern "C" functions --> defined in tu_msd.c */
5+
/* -------------------------------------------------------------------------- */
6+
#ifdef DEBUG_MSD
7+
extern "C" int mylogadd(const char *fmt, ...) ;
8+
#endif
39

4-
#define BLOCK_SIZE 512
5-
10+
void usb_msd_set_dev_ptr(USBMSD *prt);
611

712
/* -------------------------------------------------------------------------- */
813
/* Add Mass Storage USB capability when present */
914
/* -------------------------------------------------------------------------- */
1015
void __USBInstallMSD() {}
1116

1217

18+
USBMSD::USBMSD(std::initializer_list<BlockDevice *> args) {
19+
_bd = new BlockDevice *[args.size()];
20+
num = args.size();
1321

14-
15-
/* -------------------------------------------------------------------------- */
16-
/* extern "C" functions --> defined in tu_msd.c */
17-
/* -------------------------------------------------------------------------- */
18-
void usb_msd_set_dev_ptr(USBMSD *prt);
22+
for(int i = 0; i < num; i++) {
23+
_bd[i] = args.begin()[i];
24+
}
25+
usb_msd_set_dev_ptr(this);
26+
}
1927

2028
/* -------------------------------------------------------------------------- */
2129
/* CONSTRUCTOR -> initalize block device and pass a "hook" to the tu_msd module */
2230
/* -------------------------------------------------------------------------- */
2331
USBMSD::USBMSD(BlockDevice *bd) {
24-
_bd = bd;
25-
32+
_bd = new BlockDevice *[0];
33+
_bd[0] = bd;
34+
num = 1;
2635
usb_msd_set_dev_ptr(this);
36+
2737
}
2838

29-
bool USBMSD::begin() {
30-
_bd->init();
39+
bool USBMSD::begin(uint8_t lun) {
40+
if(lun < num){
41+
if(_bd[lun]->init() == 0) {
42+
return true;
43+
}
44+
}
45+
return false;
46+
3147
}
3248

3349
/* -------------------------------------------------------------------------- */
3450
/* DISTRUCTOR */
3551
/* -------------------------------------------------------------------------- */
3652
USBMSD::~USBMSD() {
37-
53+
delete []_bd;
3854
}
3955

4056
/* -------------------------------------------------------------------------- */
4157
/* AVAILABLE */
4258
/* -------------------------------------------------------------------------- */
43-
bool USBMSD::available() {
44-
begin();
45-
return _bd->available();
59+
bool USBMSD::available(uint8_t lun) {
60+
if(!begin(lun))
61+
return false;
62+
if(lun < num) {
63+
return _bd[lun]->available();
64+
}
65+
return false;
4666
}
4767

4868
/* -------------------------------------------------------------------------- */
4969
/* GET BLOCK COUNT */
5070
/* -------------------------------------------------------------------------- */
51-
uint32_t USBMSD::get_block_count() {
52-
begin();
53-
return (_bd->size() / BLOCK_SIZE);
71+
uint32_t USBMSD::get_block_count(uint8_t lun) {
72+
if(!begin(lun))
73+
return 0;
74+
if(lun < num) {
75+
return (_bd[lun]->size() / _bd[lun]->get_erase_size());
76+
}
77+
return 0;
5478
}
5579

5680
/* -------------------------------------------------------------------------- */
5781
/* GET BLOCK SIZE */
5882
/* -------------------------------------------------------------------------- */
59-
uint16_t USBMSD::get_block_size() {
60-
begin();
61-
return BLOCK_SIZE;
83+
uint16_t USBMSD::get_block_size(uint8_t lun) {
84+
if(!begin(lun))
85+
return 0;
86+
return _bd[lun]->get_erase_size();
6287
}
6388

64-
extern "C" int mylogadd(const char *fmt, ...) ;
89+
6590

6691
/* -------------------------------------------------------------------------- */
6792
/* READ */
6893
/* -------------------------------------------------------------------------- */
69-
int USBMSD::read(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
70-
//mylogadd("READ %i %i %i", lba, offset, bufsize) ;
71-
begin();
72-
bd_addr_t add = (bd_addr_t)((lba * BLOCK_SIZE) + offset);
73-
int retval = _bd->read(buffer, add, (bd_size_t)bufsize);
94+
int USBMSD::read(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
95+
96+
if(!begin(lun)) {
97+
#ifdef DEBUG_MSD
98+
mylogadd("READ FAILED 1 %i %i %i", lun, lba, offset) ;
99+
#endif
100+
return MSD_ERROR;
101+
}
102+
103+
bd_addr_t add = (bd_addr_t)((lba * _bd[lun]->get_erase_size()) + offset);
104+
int retval = 1;
105+
if(lun < num) {
106+
retval = _bd[lun]->read(buffer, add, (bd_size_t)bufsize);
107+
}
74108
if(retval == 0) {
75109
return bufsize;
76110
}
77111
else {
112+
#ifdef DEBUG_MSD
113+
mylogadd("READ FAILED %i %i %i", lun, lba, offset) ;
114+
#endif
78115
return MSD_ERROR;
79116
}
80117
}
81-
#define PRINT_SIZE 32
82-
83-
/* -------------------------------------------------------------------------- */
84-
extern void print_uint8(uint8_t n);
85118

86-
/* -------------------------------------------------------------------------- */
87-
extern void print_buffer(uint8_t *buff, uint32_t _size);
88119

89120

90121
/* -------------------------------------------------------------------------- */
91122
/* WRITE */
92123
/* -------------------------------------------------------------------------- */
93-
int USBMSD::write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
94-
//mylogadd("WRITE %i %i %i", lba, offset, bufsize) ;
95-
begin();
96-
bd_addr_t add = (bd_addr_t)((lba * BLOCK_SIZE) + offset);
124+
int USBMSD::write(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
125+
126+
if(lun >= num) {
127+
#ifdef DEBUG_MSD
128+
mylogadd("WRITE FAILED 1 %i %i %i", lun, lba, offset) ;
129+
#endif
130+
return MSD_ERROR;
131+
}
97132

98-
//mylogadd(" add %i" , add);
133+
if(!begin(lun)) {
134+
#ifdef DEBUG_MSD
135+
mylogadd("WRITE FAILED 2 %i %i %i", lun, lba, offset) ;
136+
#endif
137+
return MSD_ERROR;
138+
}
99139

100-
bd_size_t block_size = _bd->get_erase_size();
140+
bd_addr_t add = (bd_addr_t)((lba * _bd[lun]->get_erase_size()) + offset);
141+
142+
bd_size_t block_size = _bd[lun]->get_erase_size();
101143
uint8_t *bk = new uint8_t[block_size];
102144
uint8_t *buff = buffer;
103145

@@ -106,42 +148,35 @@ int USBMSD::write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsi
106148
if(bk != nullptr) {
107149
int64_t internal_size = (int64_t)bufsize;
108150
bd_addr_t block_address = block_size * (add / block_size);
109-
//mylogadd(" block_address %i" , block_address);
151+
110152
uint32_t byte_left_in_block = block_size - (add % block_size);
111-
//Serial.print("block_size ");
112-
//Serial.println(block_size);
113-
114-
//Serial.print("byte_left_in_block ");
115-
//Serial.println(byte_left_in_block);
153+
116154
err = 0;
117155
while(internal_size > 0 && err == 0) {
118156
/*
119157
* READ the block as erase dimension (4096 bytes)
120158
*/
121159
bool erase_needed = false;
122-
err = _bd->read(bk, block_address, block_size);
123-
//Serial.println("-----prima----");
124-
//print_buffer(bk, block_size);
160+
err = _bd[lun]->read(bk, block_address, block_size);
161+
125162
/*
126163
* Write in RAM
127164
*/
128165
if(err == 0) {
129166
uint32_t bytes_to_copy = (uint32_t)((internal_size > byte_left_in_block) ? byte_left_in_block : internal_size);
130167
uint8_t *ptr = bk + block_size - byte_left_in_block;
131168
for(int i = 0; i < bytes_to_copy; i++) {
132-
if(*(ptr+i) != _bd->get_erase_value()) {
169+
if(*(ptr+i) != _bd[lun]->get_erase_value()) {
133170
erase_needed = true;
134171
}
135172
*(ptr + i) = *(buffer + i);
136173
}
137174
if(erase_needed) {
138-
err = _bd->erase(block_address,block_size);
175+
err = _bd[lun]->erase(block_address,block_size);
139176
}
140177
}
141178
if(err == 0) {
142-
//Serial.println("-----dopo----");
143-
//print_buffer(bk, block_size);
144-
err = _bd->program(bk, block_address, block_size);
179+
err = _bd[lun]->program(bk, block_address, block_size);
145180
}
146181
block_address += block_size;
147182
buff += byte_left_in_block;
@@ -154,5 +189,8 @@ int USBMSD::write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsi
154189
if(err == 0) {
155190
return bufsize;
156191
}
192+
#ifdef DEBUG_MSD
193+
mylogadd("WRITE FAILED %i %i %i", lun, lba, offset) ;
194+
#endif
157195
return err;
158196
}

libraries/UsbMsd/UsbMsd.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@
66

77
#define MSD_ERROR -1
88

9+
//#define DEBUG_MSD
10+
911
class USBMSD {
1012

1113
private:
12-
BlockDevice *_bd;
14+
BlockDevice **_bd;
15+
int num;
1316

1417
public:
1518
USBMSD() = delete;
1619
USBMSD(BlockDevice *bd);
20+
USBMSD(std::initializer_list<BlockDevice *> args);
1721
virtual ~USBMSD();
18-
int read(void *buffer, bd_addr_t addr, bd_size_t size);
19-
int write(const void *buffer, bd_addr_t addr, bd_size_t size);
20-
bool available();
21-
uint32_t get_block_count();
22-
uint16_t get_block_size();
23-
int read(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
24-
int write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
25-
bool begin();
22+
int read(uint8_t lun, void *buffer, bd_addr_t addr, bd_size_t size);
23+
int write(uint8_t lun,const void *buffer, bd_addr_t addr, bd_size_t size);
24+
bool available(uint8_t lun);
25+
uint32_t get_block_count(uint8_t lun);
26+
uint16_t get_block_size(uint8_t lun);
27+
int read(uint8_t lun,uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
28+
int write(uint8_t lun,uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
29+
bool begin(uint8_t lun);
30+
int get_lun() {return num;}
2631

2732

2833
};

0 commit comments

Comments
 (0)