1
1
#include " UsbMsd.h"
2
2
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
3
9
4
- #define BLOCK_SIZE 512
5
-
10
+ void usb_msd_set_dev_ptr (USBMSD *prt);
6
11
7
12
/* -------------------------------------------------------------------------- */
8
13
/* Add Mass Storage USB capability when present */
9
14
/* -------------------------------------------------------------------------- */
10
15
void __USBInstallMSD () {}
11
16
12
17
18
+ USBMSD::USBMSD (std::initializer_list<BlockDevice *> args) {
19
+ _bd = new BlockDevice *[args.size ()];
20
+ num = args.size ();
13
21
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
+ }
19
27
20
28
/* -------------------------------------------------------------------------- */
21
29
/* CONSTRUCTOR -> initalize block device and pass a "hook" to the tu_msd module */
22
30
/* -------------------------------------------------------------------------- */
23
31
USBMSD::USBMSD (BlockDevice *bd) {
24
- _bd = bd;
25
-
32
+ _bd = new BlockDevice *[0 ];
33
+ _bd[0 ] = bd;
34
+ num = 1 ;
26
35
usb_msd_set_dev_ptr (this );
36
+
27
37
}
28
38
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
+
31
47
}
32
48
33
49
/* -------------------------------------------------------------------------- */
34
50
/* DISTRUCTOR */
35
51
/* -------------------------------------------------------------------------- */
36
52
USBMSD::~USBMSD () {
37
-
53
+ delete [] _bd;
38
54
}
39
55
40
56
/* -------------------------------------------------------------------------- */
41
57
/* AVAILABLE */
42
58
/* -------------------------------------------------------------------------- */
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 ;
46
66
}
47
67
48
68
/* -------------------------------------------------------------------------- */
49
69
/* GET BLOCK COUNT */
50
70
/* -------------------------------------------------------------------------- */
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 ;
54
78
}
55
79
56
80
/* -------------------------------------------------------------------------- */
57
81
/* GET BLOCK SIZE */
58
82
/* -------------------------------------------------------------------------- */
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 ();
62
87
}
63
88
64
- extern " C " int mylogadd ( const char *fmt, ...) ;
89
+
65
90
66
91
/* -------------------------------------------------------------------------- */
67
92
/* READ */
68
93
/* -------------------------------------------------------------------------- */
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
+ }
74
108
if (retval == 0 ) {
75
109
return bufsize;
76
110
}
77
111
else {
112
+ #ifdef DEBUG_MSD
113
+ mylogadd (" READ FAILED %i %i %i" , lun, lba, offset) ;
114
+ #endif
78
115
return MSD_ERROR;
79
116
}
80
117
}
81
- #define PRINT_SIZE 32
82
-
83
- /* -------------------------------------------------------------------------- */
84
- extern void print_uint8 (uint8_t n);
85
118
86
- /* -------------------------------------------------------------------------- */
87
- extern void print_buffer (uint8_t *buff, uint32_t _size);
88
119
89
120
90
121
/* -------------------------------------------------------------------------- */
91
122
/* WRITE */
92
123
/* -------------------------------------------------------------------------- */
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
+ }
97
132
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
+ }
99
139
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 ();
101
143
uint8_t *bk = new uint8_t [block_size];
102
144
uint8_t *buff = buffer;
103
145
@@ -106,42 +148,35 @@ int USBMSD::write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsi
106
148
if (bk != nullptr ) {
107
149
int64_t internal_size = (int64_t )bufsize;
108
150
bd_addr_t block_address = block_size * (add / block_size);
109
- // mylogadd(" block_address %i" , block_address);
151
+
110
152
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
+
116
154
err = 0 ;
117
155
while (internal_size > 0 && err == 0 ) {
118
156
/*
119
157
* READ the block as erase dimension (4096 bytes)
120
158
*/
121
159
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
+
125
162
/*
126
163
* Write in RAM
127
164
*/
128
165
if (err == 0 ) {
129
166
uint32_t bytes_to_copy = (uint32_t )((internal_size > byte_left_in_block) ? byte_left_in_block : internal_size);
130
167
uint8_t *ptr = bk + block_size - byte_left_in_block;
131
168
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 ()) {
133
170
erase_needed = true ;
134
171
}
135
172
*(ptr + i) = *(buffer + i);
136
173
}
137
174
if (erase_needed) {
138
- err = _bd->erase (block_address,block_size);
175
+ err = _bd[lun] ->erase (block_address,block_size);
139
176
}
140
177
}
141
178
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);
145
180
}
146
181
block_address += block_size;
147
182
buff += byte_left_in_block;
@@ -154,5 +189,8 @@ int USBMSD::write(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsi
154
189
if (err == 0 ) {
155
190
return bufsize;
156
191
}
192
+ #ifdef DEBUG_MSD
193
+ mylogadd (" WRITE FAILED %i %i %i" , lun, lba, offset) ;
194
+ #endif
157
195
return err;
158
196
}
0 commit comments