@@ -81,8 +81,36 @@ USBMSD::USBMSD(USBPhy *phy, mbed::BlockDevice *bd, uint16_t vendor_id, uint16_t
81
81
PluggableUSBD ().plug (this );
82
82
}
83
83
84
- static rtos::Thread _t (osPriorityRealtime, 64 * 1024 );
85
- static events::EventQueue _queue (64 *sizeof (int ));
84
+ static rtos::Thread _t (osPriorityHigh, 32 * 1024 , NULL , " msd" );
85
+
86
+ struct disk_info {
87
+ uint64_t block;
88
+ uint8_t count;
89
+ mbed::BlockDevice *_bd;
90
+ uint8_t data[4096 ];
91
+ };
92
+
93
+ static rtos::Mail<struct disk_info , 16 > mail_box;
94
+
95
+ static int _writes_i = 0 ;
96
+
97
+ static void write_chunk () {
98
+ while (true ) {
99
+ osEvent evt = mail_box.get ();
100
+ if (evt.status == osEventMail) {
101
+ struct disk_info *info = (struct disk_info *)evt.value .p ;
102
+ mbed::bd_addr_t addr = info->block * info->_bd ->get_erase_size ();
103
+ mbed::bd_size_t size = info->count * info->_bd ->get_erase_size ();
104
+
105
+ int ret = info->_bd ->erase (addr, size);
106
+ if (ret != 0 ) {
107
+ return ;
108
+ }
109
+ ret = info->_bd ->program (info->data , addr, size);
110
+ mail_box.free (info);
111
+ }
112
+ }
113
+ }
86
114
87
115
void USBMSD::init (EndpointResolver& resolver)
88
116
{
@@ -103,7 +131,7 @@ void USBMSD::init(EndpointResolver& resolver)
103
131
_page = NULL ;
104
132
connect ();
105
133
106
- _t.start (callback (&_queue, &events::EventQueue::dispatch_forever) );
134
+ _t.start (write_chunk );
107
135
}
108
136
109
137
USBMSD::~USBMSD ()
@@ -115,21 +143,21 @@ USBMSD::~USBMSD()
115
143
116
144
bool USBMSD::connect ()
117
145
{
118
- _mutex_init.lock ();
119
- _mutex.lock ();
146
+ // _mutex_init.lock();
147
+ // _mutex.lock();
120
148
121
149
// already initialized
122
150
if (_initialized) {
123
- _mutex.unlock ();
124
- _mutex_init.unlock ();
151
+ // _mutex.unlock();
152
+ // _mutex_init.unlock();
125
153
return false ;
126
154
}
127
155
128
156
// disk initialization
129
157
if (disk_status () & NO_INIT) {
130
158
if (disk_initialize ()) {
131
- _mutex.unlock ();
132
- _mutex_init.unlock ();
159
+ // _mutex.unlock();
160
+ // _mutex_init.unlock();
133
161
return false ;
134
162
}
135
163
}
@@ -146,30 +174,30 @@ bool USBMSD::connect()
146
174
free (_page);
147
175
_page = (uint8_t *)malloc (_block_size * sizeof (uint8_t ));
148
176
if (_page == NULL ) {
149
- _mutex.unlock ();
150
- _mutex_init.unlock ();
177
+ // _mutex.unlock();
178
+ // _mutex_init.unlock();
151
179
return false ;
152
180
}
153
181
}
154
182
} else {
155
- _mutex.unlock ();
156
- _mutex_init.unlock ();
183
+ // _mutex.unlock();
184
+ // _mutex_init.unlock();
157
185
return false ;
158
186
}
159
187
160
188
// connect the device
161
189
// USBDevice::connect();
162
190
_initialized = true ;
163
191
_media_removed = false ;
164
- _mutex.unlock ();
165
- _mutex_init.unlock ();
192
+ // _mutex.unlock();
193
+ // _mutex_init.unlock();
166
194
return true ;
167
195
}
168
196
169
197
void USBMSD::disconnect ()
170
198
{
171
- _mutex_init.lock ();
172
- _mutex.lock ();
199
+ // _mutex_init.lock();
200
+ // _mutex.lock();
173
201
174
202
// USBDevice::disconnect();
175
203
_initialized = false ;
@@ -178,13 +206,12 @@ void USBMSD::disconnect()
178
206
free (_page);
179
207
_page = NULL ;
180
208
181
- _mutex.unlock ();
182
- _mutex_init.unlock ();
209
+ // _mutex.unlock();
210
+ // _mutex_init.unlock();
183
211
}
184
212
185
213
void USBMSD::process ()
186
214
{
187
- _queue.dispatch ();
188
215
}
189
216
190
217
void USBMSD::attach (mbed::Callback<void ()> cb)
@@ -198,13 +225,30 @@ bool USBMSD::media_removed()
198
225
199
226
int USBMSD::disk_read (uint8_t *data, uint64_t block, uint8_t count)
200
227
{
201
- mbed::bd_addr_t addr = block * _bd->get_erase_size ();
228
+ // this operation must be executed in another thread
229
+ mbed::bd_addr_t addr = block * _bd->get_erase_size ();
202
230
mbed::bd_size_t size = count * _bd->get_erase_size ();
231
+ /*
203
232
return _bd->read(data, addr, size);
233
+ */
234
+ memcpy (data, (void *)(addr + 0x80000 ), size);
235
+ return 0 ;
204
236
}
205
237
206
238
int USBMSD::disk_write (const uint8_t *data, uint64_t block, uint8_t count)
207
239
{
240
+ // this operation must be executed in another thread
241
+ struct disk_info * info = mail_box.alloc ();
242
+ if (info == NULL ) {
243
+ return -1 ;
244
+ }
245
+ memcpy (info->data , data, _bd->get_erase_size ());
246
+ info->block = block;
247
+ info->count = count;
248
+ info->_bd = _bd;
249
+ mail_box.put (info);
250
+ return 0 ;
251
+ /*
208
252
mbed::bd_addr_t addr = block * _bd->get_erase_size();
209
253
mbed::bd_size_t size = count * _bd->get_erase_size();
210
254
int ret = _bd->erase(addr, size);
@@ -213,6 +257,7 @@ int USBMSD::disk_write(const uint8_t *data, uint64_t block, uint8_t count)
213
257
}
214
258
215
259
return _bd->program(data, addr, size);
260
+ */
216
261
}
217
262
218
263
int USBMSD::disk_initialize ()
@@ -238,20 +283,20 @@ int USBMSD::disk_status()
238
283
239
284
void USBMSD::_isr_out ()
240
285
{
241
- _queue. call (_out_task );
286
+ _out ( );
242
287
}
243
288
244
289
void USBMSD::_isr_in ()
245
290
{
246
- _queue. call (_in_task );
291
+ _in ( );
247
292
}
248
293
249
294
void USBMSD::callback_state_change (USBDevice::DeviceState new_state)
250
295
{
251
296
// called in ISR context
252
297
253
298
if (new_state != USBDevice::Configured) {
254
- _queue. call (_reset_task );
299
+ _reset ( );
255
300
}
256
301
}
257
302
@@ -285,7 +330,7 @@ bool USBMSD::callback_set_configuration(uint8_t configuration)
285
330
{
286
331
// called in ISR context
287
332
288
- _queue. call (_configure_task );
333
+ _configure ( );
289
334
return true ;
290
335
}
291
336
@@ -370,38 +415,38 @@ const uint8_t *USBMSD::configuration_desc(uint8_t index)
370
415
371
416
void USBMSD::_out ()
372
417
{
373
- _mutex.lock ();
418
+ // _mutex.lock();
374
419
375
420
_bulk_out_size = read_finish (_bulk_out);
376
421
_out_ready = true ;
377
422
_process ();
378
423
379
- _mutex.unlock ();
424
+ // _mutex.unlock();
380
425
}
381
426
382
427
void USBMSD::_in ()
383
428
{
384
- _mutex.lock ();
429
+ // _mutex.lock();
385
430
386
431
write_finish (_bulk_in);
387
432
_in_ready = true ;
388
433
_process ();
389
434
390
- _mutex.unlock ();
435
+ // _mutex.unlock();
391
436
}
392
437
393
438
void USBMSD::_reset ()
394
439
{
395
- _mutex.lock ();
440
+ // _mutex.lock();
396
441
397
442
msd_reset ();
398
443
399
- _mutex.unlock ();
444
+ // _mutex.unlock();
400
445
}
401
446
402
447
uint32_t USBMSD::_control (const USBDevice::setup_packet_t *setup, USBDevice::RequestResult *result, uint8_t ** data)
403
448
{
404
- // _mutex.lock();
449
+ // // _mutex.lock();
405
450
406
451
static const uint8_t maxLUN[1 ] = {0 };
407
452
@@ -424,13 +469,13 @@ uint32_t USBMSD::_control(const USBDevice::setup_packet_t *setup, USBDevice::Req
424
469
}
425
470
}
426
471
427
- // _mutex.unlock();
472
+ // // _mutex.unlock();
428
473
return size;
429
474
}
430
475
431
476
void USBMSD::_configure ()
432
477
{
433
- _mutex.lock ();
478
+ // _mutex.lock();
434
479
435
480
// Configure endpoints > 0
436
481
PluggableUSBD ().endpoint_add (_bulk_in, MAX_PACKET, USB_EP_TYPE_BULK, mbed::callback (this , &USBMSD::_isr_in));
@@ -444,12 +489,12 @@ void USBMSD::_configure()
444
489
// activate readings
445
490
read_start (_bulk_out, _bulk_out_buf, sizeof (_bulk_out_buf));
446
491
447
- _mutex.unlock ();
492
+ // _mutex.unlock();
448
493
}
449
494
450
495
void USBMSD::_process ()
451
496
{
452
- // Mutex must be locked by caller
497
+ // //_mutex must be locked by caller
453
498
454
499
switch (_stage) {
455
500
// the device has to decode the CBW received
0 commit comments