Skip to content

Commit 2707347

Browse files
committed
Update example.
Signed-off-by: HiFiPhile <[email protected]>
1 parent 84f8876 commit 2707347

File tree

2 files changed

+79
-53
lines changed

2 files changed

+79
-53
lines changed

examples/device/cdc_msc_freertos/src/msc_disk.c

Lines changed: 75 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,28 @@
2828

2929
#if CFG_TUD_MSC
3030

31-
// Simulate read/write operation time
32-
#define SIM_IO_TIME_MS 0
31+
#if CFG_EXAMPLE_MSC_ASYNC_IO
3332

34-
#if CFG_TUD_MSC_ASYNC_IO
35-
TimerHandle_t sim_io_ops_timer;
36-
static int32_t bytes_processed;
33+
#define IO_STACK_SIZE configMINIMAL_STACK_SIZE
34+
35+
typedef struct {
36+
uint8_t lun;
37+
bool is_read;
38+
uint32_t lba;
39+
uint32_t offset;
40+
void* buffer;
41+
uint32_t bufsize;
42+
} io_ops_t;
43+
44+
QueueHandle_t io_queue;
3745
#if configSUPPORT_STATIC_ALLOCATION
38-
StaticTimer_t sim_io_ops_timer_buf;
46+
uint8_t io_queue_buf[sizeof(io_ops_t)];
47+
StaticQueue_t io_queue_static;
48+
StackType_t io_stack[IO_STACK_SIZE];
49+
StaticTask_t io_taskdef;
3950
#endif
40-
static void sim_io_ops_done_cb(TimerHandle_t xTimer);
51+
52+
static void io_task(void *params);
4153
#endif
4254

4355
void msc_disk_init(void);
@@ -133,20 +145,37 @@ uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
133145
README_CONTENTS
134146
};
135147

136-
#if CFG_TUD_MSC_ASYNC_IO
148+
#if CFG_EXAMPLE_MSC_ASYNC_IO
137149
void msc_disk_init() {
138150

139-
#if configSUPPORT_DYNAMIC_ALLOCATION
140-
sim_io_ops_timer = xTimerCreate("sim_io_ops", pdMS_TO_TICKS(SIM_IO_TIME_MS), pdFALSE, NULL, sim_io_ops_done_cb);
151+
#if configSUPPORT_STATIC_ALLOCATION
152+
io_queue = xQueueCreateStatic(1, sizeof(io_ops_t), io_queue_buf, &io_queue_static);
153+
xTaskCreateStatic(io_task, "io", IO_STACK_SIZE, NULL, 2, io_stack, &io_taskdef);
141154
#else
142-
sim_io_ops_timer = xTimerCreateStatic("sim_io_ops", pdMS_TO_TICKS(SIM_IO_TIME_MS), pdFALSE, NULL, sim_io_ops_done_cb, &sim_io_ops_timer_buf);
155+
io_queue = xQueueCreate(1, sizeof(io_ops_t));
156+
xTaskCreate(io_task, "io", IO_STACK_SIZE, NULL, 2, NULL);
143157
#endif
144158
}
145159

146-
static void sim_io_ops_done_cb(TimerHandle_t xTimer) {
147-
(void) xTimer;
148-
tud_msc_async_io_done(bytes_processed);
160+
static void io_task(void *params) {
161+
(void) params;
162+
io_ops_t io_ops;
163+
while (1) {
164+
if (xQueueReceive(io_queue, &io_ops, portMAX_DELAY)) {
165+
if (io_ops.is_read) {
166+
uint8_t const* addr = msc_disk[io_ops.lba] + io_ops.offset;
167+
memcpy(io_ops.buffer, addr, io_ops.bufsize);
168+
} else {
169+
uint8_t* addr = msc_disk[io_ops.lba] + io_ops.offset;
170+
memcpy(addr, io_ops.buffer, io_ops.bufsize);
171+
}
172+
173+
tusb_time_delay_ms_api(CFG_EXAMPLE_MSC_IO_DELAY_MS);
174+
tud_msc_async_io_done(io_ops.bufsize);
175+
}
176+
}
149177
}
178+
150179
#else
151180
void msc_disk_init() {}
152181
#endif
@@ -220,33 +249,31 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo
220249
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
221250
{
222251
(void) lun;
223-
int32_t ret = bufsize;
224252

225253
// out of ramdisk
226254
if ( lba >= DISK_BLOCK_NUM ) {
227-
ret = -1;
255+
return TUD_MSC_RET_ERROR;
228256
}
229257

230258
// Check for overflow of offset + bufsize
231259
if ( lba * DISK_BLOCK_SIZE + offset + bufsize > DISK_BLOCK_NUM * DISK_BLOCK_SIZE ) {
232-
ret = -1;
260+
return TUD_MSC_RET_ERROR;
233261
}
234262

235-
if (ret != -1) {
236-
uint8_t const* addr = msc_disk[lba] + offset;
237-
memcpy(buffer, addr, bufsize);
238-
}
263+
#if CFG_EXAMPLE_MSC_ASYNC_IO
264+
io_ops_t io_ops = { .is_read = true, .lun = lun, .lba = lba, .offset = offset, .buffer = buffer, .bufsize = bufsize };
239265

240-
#if CFG_TUD_MSC_ASYNC_IO
241-
// Simulate background read operation
242-
bytes_processed = ret;
243-
xTimerStart(sim_io_ops_timer, 0);
244-
#elif SIM_IO_TIME_MS > 0
245-
// Simulate read operation
246-
tusb_time_delay_ms_api(SIM_IO_TIME_MS);
247-
#endif
266+
// Send IO operation to IO task
267+
TU_ASSERT(xQueueSend(io_queue, &io_ops, 0) == pdPASS);
268+
269+
return TUD_MSC_RET_ASYNC;
270+
#else
271+
uint8_t const* addr = msc_disk[lba] + offset;
272+
memcpy(buffer, addr, bufsize);
273+
tusb_time_delay_ms_api(CFG_EXAMPLE_MSC_IO_DELAY_MS);
248274

249-
return ret;
275+
return bufsize;
276+
#endif
250277
}
251278

252279
bool tud_msc_is_writable_cb (uint8_t lun)
@@ -264,38 +291,35 @@ bool tud_msc_is_writable_cb (uint8_t lun)
264291
// Process data in buffer to disk's storage and return number of written bytes
265292
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
266293
{
267-
(void) lun;
268-
int32_t ret = bufsize;
269-
270294
// out of ramdisk
271295
if ( lba >= DISK_BLOCK_NUM ) {
272-
ret = -1;
296+
return TUD_MSC_RET_ERROR;
273297
}
274298

275299
// Check for overflow of offset + bufsize
276300
if ( lba * DISK_BLOCK_SIZE + offset + bufsize > DISK_BLOCK_NUM * DISK_BLOCK_SIZE ) {
277-
ret = -1;
301+
return TUD_MSC_RET_ERROR;
278302
}
279303

280-
#ifndef CFG_EXAMPLE_MSC_READONLY
281-
if (ret != -1) {
282-
uint8_t* addr = msc_disk[lba] + offset;
283-
memcpy(addr, buffer, bufsize);
284-
}
285-
#else
286-
(void) lba; (void) offset; (void) buffer;
304+
#ifdef CFG_EXAMPLE_MSC_READONLY
305+
(void) lun; (void) buffer;
306+
return bufsize;
287307
#endif
288308

289-
#if CFG_TUD_MSC_ASYNC_IO
290-
// Simulate background write operation
291-
bytes_processed = ret;
292-
xTimerStart(sim_io_ops_timer, 0);
293-
#elif SIM_IO_TIME_MS > 0
294-
// Simulate write operation
295-
tusb_time_delay_ms_api(SIM_IO_TIME_MS);
296-
#endif
309+
#if CFG_EXAMPLE_MSC_ASYNC_IO
310+
io_ops_t io_ops = { .is_read = false, .lun = lun, .lba = lba, .offset = offset, .buffer = buffer, .bufsize = bufsize };
311+
312+
// Send IO operation to IO task
313+
TU_ASSERT(xQueueSend(io_queue, &io_ops, 0) == pdPASS);
297314

298-
return ret;
315+
return TUD_MSC_RET_ASYNC;
316+
#else
317+
uint8_t* addr = msc_disk[lba] + offset;
318+
memcpy(addr, buffer, bufsize);
319+
tusb_time_delay_ms_api(CFG_EXAMPLE_MSC_IO_DELAY_MS);
320+
321+
return bufsize;
322+
#endif
299323
}
300324

301325
// Callback invoked when received an SCSI command not in built-in list below

examples/device/cdc_msc_freertos/src/tusb_config.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@
114114
// MSC Buffer size of Device Mass storage
115115
#define CFG_TUD_MSC_EP_BUFSIZE 512
116116

117-
// Enable Async IO on MSC
118-
#define CFG_TUD_MSC_ASYNC_IO 0
117+
// Use async IO in example or not
118+
#define CFG_EXAMPLE_MSC_ASYNC_IO 1
119119

120+
// Simulate read/write operation delay
121+
#define CFG_EXAMPLE_MSC_IO_DELAY_MS 0
120122

121123
#ifdef __cplusplus
122124
}

0 commit comments

Comments
 (0)