Skip to content

Commit 9b362b6

Browse files
committed
Improve PluggableUSBMSD interface
1 parent 5749ade commit 9b362b6

File tree

3 files changed

+30
-47
lines changed

3 files changed

+30
-47
lines changed

libraries/USBMSD/PluggableUSBMSD.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
#include "USBDevice.h"
3131
#include "USB/PluggableUSBDevice.h"
32+
#include "FATFileSystem.h"
33+
#include "Callback.h"
34+
#include "rtos/Thread.h"
3235

3336
namespace arduino {
3437

@@ -116,6 +119,8 @@ class USBMSD: public internal::PluggableUSBModule {
116119

117120

118121
void begin();
122+
mbed::FATFileSystem& getFileSystem();
123+
119124
/**
120125
* Perform USB processing
121126
*/
@@ -255,12 +260,11 @@ class USBMSD: public internal::PluggableUSBModule {
255260
uint32_t _bulk_out_size;
256261

257262
// Interrupt to thread deferral
258-
events::PolledQueue _queue;
259-
events::Task<void()> _in_task;
260-
events::Task<void()> _out_task;
261-
events::Task<void()> _reset_task;
262-
events::Task<void(const USBDevice::setup_packet_t *)> _control_task;
263-
events::Task<void()> _configure_task;
263+
mbed::Callback<void()> _in_task;
264+
mbed::Callback<void()> _out_task;
265+
mbed::Callback<void()> _reset_task;
266+
mbed::Callback<void()> _control_task;
267+
mbed::Callback<void()> _configure_task;
264268

265269
mbed::BlockDevice *_bd;
266270
rtos::Mutex _mutex_init;

libraries/USBMSD/Singleton.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@
55

66
using namespace arduino;
77

8-
static void process_msd(USBMSD* obj) {
9-
while (1) {
10-
obj->process();
11-
rtos::ThisThread::sleep_for(1);
12-
}
13-
}
14-
15-
static mbed::FATFileSystem fs("fs");
168
static FlashIAPBlockDevice bd(0x80000, 0x80000);
17-
static rtos::Thread th;
189

1910
void USBMSD::begin()
2011
{
21-
int err = fs.mount(&bd);
12+
int err = getFileSystem().mount(&bd);
2213
if (err) {
23-
err = fs.reformat(&bd);
14+
err = getFileSystem().reformat(&bd);
2415
}
16+
}
2517

26-
th.start(mbed::callback(process_msd, this));
18+
mbed::FATFileSystem& USBMSD::getFileSystem()
19+
{
20+
static mbed::FATFileSystem fs("fs");
21+
return fs;
2722
}
2823

2924
USBMSD MassStorage(&bd);

libraries/USBMSD/USBMSD.cpp

+13-29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include "Arduino.h"
1819
#include <stdint.h>
1920
#include <stdlib.h>
2021
#include "PluggableUSBMSD.h"
@@ -68,18 +69,20 @@ enum Status {
6869

6970
USBMSD::USBMSD(mbed::BlockDevice *bd, bool connect_blocking, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
7071
: arduino::internal::PluggableUSBModule(1),
71-
_initialized(false), _media_removed(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _configure_task(&_queue), _bd(bd)
72+
_initialized(false), _media_removed(false), _bd(bd)
7273
{
7374
PluggableUSBD().plug(this);
7475
}
7576

7677
USBMSD::USBMSD(USBPhy *phy, mbed::BlockDevice *bd, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
7778
: arduino::internal::PluggableUSBModule(1),
78-
_initialized(false), _media_removed(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _configure_task(&_queue), _bd(bd)
79+
_initialized(false), _media_removed(false), _bd(bd)
7980
{
8081
PluggableUSBD().plug(this);
8182
}
8283

84+
static rtos::Thread _t(osPriorityHigh, 64 * 1024);
85+
static events::EventQueue _queue(64*sizeof(int));
8386

8487
void USBMSD::init(EndpointResolver& resolver)
8588
{
@@ -99,6 +102,8 @@ void USBMSD::init(EndpointResolver& resolver)
99102
memset((void *)&_csw, 0, sizeof(CSW));
100103
_page = NULL;
101104
connect();
105+
106+
_t.start(callback(&_queue, &events::EventQueue::dispatch_forever));
102107
}
103108

104109
USBMSD::~USBMSD()
@@ -169,21 +174,6 @@ void USBMSD::disconnect()
169174
//USBDevice::disconnect();
170175
_initialized = false;
171176

172-
_in_task.cancel();
173-
_out_task.cancel();
174-
_reset_task.cancel();
175-
_configure_task.cancel();
176-
177-
_mutex.unlock();
178-
179-
// object mutex must be unlocked for waiting
180-
_in_task.wait();
181-
_out_task.wait();
182-
_reset_task.wait();
183-
_configure_task.wait();
184-
185-
_mutex.lock();
186-
187177
//De-allocate MSD page size:
188178
free(_page);
189179
_page = NULL;
@@ -199,11 +189,6 @@ void USBMSD::process()
199189

200190
void USBMSD::attach(mbed::Callback<void()> cb)
201191
{
202-
lock();
203-
204-
_queue.attach(cb);
205-
206-
unlock();
207192
}
208193

209194
bool USBMSD::media_removed()
@@ -253,21 +238,20 @@ int USBMSD::disk_status()
253238

254239
void USBMSD::_isr_out()
255240
{
256-
_out_task.call();
241+
_queue.call(_out_task);
257242
}
258243

259244
void USBMSD::_isr_in()
260245
{
261-
_in_task.call();
246+
_queue.call(_in_task);
262247
}
263248

264249
void USBMSD::callback_state_change(USBDevice::DeviceState new_state)
265250
{
266251
// called in ISR context
267252

268253
if (new_state != USBDevice::Configured) {
269-
_reset_task.cancel();
270-
_reset_task.call();
254+
_queue.call(_reset_task);
271255
}
272256
}
273257

@@ -301,7 +285,7 @@ bool USBMSD::callback_set_configuration(uint8_t configuration)
301285
{
302286
// called in ISR context
303287

304-
_configure();
288+
_queue.call(_configure_task);
305289
return true;
306290
}
307291

@@ -417,7 +401,7 @@ void USBMSD::_reset()
417401

418402
uint32_t USBMSD::_control(const USBDevice::setup_packet_t *setup, USBDevice::RequestResult *result, uint8_t** data)
419403
{
420-
_mutex.lock();
404+
//_mutex.lock();
421405

422406
static const uint8_t maxLUN[1] = {0};
423407

@@ -440,7 +424,7 @@ uint32_t USBMSD::_control(const USBDevice::setup_packet_t *setup, USBDevice::Req
440424
}
441425
}
442426

443-
_mutex.unlock();
427+
//_mutex.unlock();
444428
return size;
445429
}
446430

0 commit comments

Comments
 (0)