-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_device_mass_multi_lun_storage_rtos.c
More file actions
673 lines (574 loc) · 31.8 KB
/
demo_device_mass_multi_lun_storage_rtos.c
File metadata and controls
673 lines (574 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/***************************************************************************
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** Overview */
/** */
/** */
/** */
/** Note */
/** */
/** This demonstration is not optimized, to optimize application user */
/** sould configuer related class flag in ux_user.h and adjust */
/** UX_DEVICE_MEMORY_STACK_SIZE */
/** */
/** */
/** AUTHOR */
/** */
/** Mohamed AYED */
/** */
/**************************************************************************/
/**************************************************************************/
#include "ux_api.h"
#include "ux_device_class_storage.h"
#ifndef UX_DEVICE_SIDE_ONLY
#error UX_DEVICE_SIDE_ONLY must be defined
#endif
#ifndef UX_DEVICE_BIDIRECTIONAL_ENDPOINT_SUPPORT
#error UX_DEVICE_BIDIRECTIONAL_ENDPOINT_SUPPORT must be defined for this sample
#endif
#if UX_MAX_SLAVE_LUN != 2
#error UX_MAX_SLAVE_LUN must be 2 for this sample
#endif
#define BUILD_FILE_SYSTEM
/**************************************************/
/** Define constants */
/**************************************************/
#define UX_DEVICE_MEMORY_STACK_SIZE (10*1024)
#define UX_DEMO_THREAD_STACK_SIZE (1*1024)
#define UX_DEMO_STORAGE_DEVICE_VID 0x070A
#define UX_DEMO_STORAGE_DEVICE_PID 0x4031
#define UX_DEMO_BCD_USB 0x0200
#define UX_DEMO_MAX_EP0_SIZE 0x40U
#define UX_DEMO_STORAGE_CONFIG_DESC_SIZE 0x20U
#define UX_DEMO_STORAGE_EPIN_ADDR 0x81U
#define UX_DEMO_STORAGE_EPOUT_ADDR 0x01U
#define UX_DEMO_STORAGE_FS_EPIN_SIZE 0x40U
#define UX_DEMO_STORAGE_FS_EPOUT_SIZE 0x40U
#define UX_DEMO_STORAGE_HS_EPIN_SIZE 0x200U
#define UX_DEMO_STORAGE_HS_EPOUT_SIZE 0x200U
/**************************************************/
/** usbx device storage demo callbacks */
/**************************************************/
VOID ux_demo_device_storage_instance_activate(VOID *storage_instance);
VOID ux_demo_device_storage_instance_deactivate(VOID *storage_instance);
UINT ux_demo_device_storage_media_read(VOID *storage_instance, ULONG lun, UCHAR * data_pointer, ULONG number_blocks,
ULONG lba, ULONG *media_status);
UINT ux_demo_device_storage_media_write(VOID *storage_instance, ULONG lun, UCHAR * data_pointer, ULONG number_blocks,
ULONG lba, ULONG *media_status);
UINT ux_demo_device_storage_media_status(VOID *storage_instance, ULONG lun, ULONG media_id, ULONG *media_status);
UINT ux_demo_device_storage_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status);
/**************************************************/
/** usbx application initialization with RTOS */
/**************************************************/
VOID tx_application_define(VOID *first_unused_memory);
/**************************************************/
/** usbx device storage demo thread */
/**************************************************/
VOID ux_demo_device_storage_thread_entry(ULONG thread_input);
/**************************************************/
/** usbx device storage demo */
/**************************************************/
#ifndef RAM_DISK_SIZE
#define RAM_DISK_SIZE (1024 * 90) /* Must > 20K for windows. */
#endif
#define RAM_DISK_N_LBA (RAM_DISK_SIZE / 512)
#define RAM_DISK_LAST_LBA (RAM_DISK_N_LBA - 1)
#define RAM_DISK_BLOCK_LENGTH 512
#define BUILD_FILE_SYSTEM
/* Write Caching support. */
#define RAM_DISK_WRITE_CACHING UX_FALSE
UCHAR ram_disk1_memory[RAM_DISK_SIZE];
UCHAR ram_disk2_memory[RAM_DISK_SIZE];
static VOID usbx_storage_disk_init(VOID);
/**************************************************/
/** usbx device storage instance */
/**************************************************/
UX_SLAVE_CLASS_STORAGE *storage;
/**************************************************/
/** thread object */
/**************************************************/
static TX_THREAD ux_storage_thread;
static ULONG ux_storage_thread_stack[UX_DEMO_THREAD_STACK_SIZE / sizeof(ULONG)];
/**************************************************/
/** usbx callback error */
/**************************************************/
static VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code);
static CHAR ux_system_memory_pool[UX_DEVICE_MEMORY_STACK_SIZE];
#ifndef EXTERNAL_MAIN
extern int board_setup(void);
#endif /* EXTERNAL_MAIN */
extern int usb_device_dcd_initialize(void *param);
/**************************************************/
/** USB Report descriptor */
/**************************************************/
#define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(ux_demo_device_framework_full_speed)
UCHAR ux_demo_device_framework_full_speed[] = {
/* Device descriptor */
0x12, /* bLength */
0x01, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */
UX_W0(UX_DEMO_STORAGE_DEVICE_VID), UX_W1(UX_DEMO_STORAGE_DEVICE_VID), /* idVendor */
UX_W0(UX_DEMO_STORAGE_DEVICE_PID), UX_W1(UX_DEMO_STORAGE_DEVICE_PID), /* idProduct */
UX_W0(0x200), UX_W1(0x200), /* bcdDevice */
0x01, /* iManufacturer */
0x02, /* iProduct */
0x03, /* iSerialNumber */
0x01, /* bNumConfigurations */
/* Configuration descriptor */
0x09, /* bLength */
0x02, /* bDescriptorType */
UX_W0(UX_DEMO_STORAGE_CONFIG_DESC_SIZE), UX_W1(UX_DEMO_STORAGE_CONFIG_DESC_SIZE), /* wTotalLength */
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x04, /* iConfiguration */
0xC0, /* bmAttributes */
/* D6 : 0x1 : Self-powered */
/* D5, Remote Wakeup : 0x0 : Not supported */
0x19, /* bMaxPower : 50mA */
/* Interface descriptor */
0x09, /* bLength */
0x04, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x02, /* bNumEndpoints */
0x08, /* bInterfaceClass : 0x08 : Mass Storage */
0x06, /* bInterfaceSubClass : SCSI transparent*/
0x50, /* nInterfaceProtocol */
0x06, /* iInterface */
/* Endpoint descriptor (Bulk In) */
0x07, /* bLength */
0x05, /* bDescriptorType */
UX_DEMO_STORAGE_EPIN_ADDR, /* bEndpointAddress */
0x02, /* bmAttributes */
/* D1..0, Transfer Type : 0x2 : Bulk */
/* D3..2, Synchronization Type : 0x0 : No Synchronization */
/* D5..4, Usage Type : 0x0 : Data endpoint */
UX_W0(UX_DEMO_STORAGE_FS_EPIN_SIZE), UX_W1(UX_DEMO_STORAGE_FS_EPIN_SIZE), /* wMaxPacketSize */
/* D10..0, Max Packet Size */
/* D12..11, Additional transactions : 0x00 */
0x00, /* bInterval */
/* Endpoint descriptor (Bulk Out) */
0x07, /* bLength */
0x05, /* bDescriptorType */
UX_DEMO_STORAGE_EPOUT_ADDR, /* bEndpointAddress */
0x02, /* bmAttributes */
/* D1..0, Transfer Type : 0x2 : Bulk */
/* D3..2, Synchronization Type : 0x0 : No Synchronization */
/* D5..4, Usage Type : 0x0 : Data endpoint */
UX_W0(UX_DEMO_STORAGE_FS_EPOUT_SIZE), UX_W1(UX_DEMO_STORAGE_FS_EPOUT_SIZE), /* wMaxPacketSize */
/* D10..0, Max Packet Size */
/* D12..11, Additional transactions : 0x00 */
0x00 /* bInterval */
};
#define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(ux_demo_device_framework_high_speed)
UCHAR ux_demo_device_framework_high_speed[] = {
/* Device descriptor */
0x12, /* bLength */
0x01, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */
UX_W0(UX_DEMO_STORAGE_DEVICE_VID), UX_W1(UX_DEMO_STORAGE_DEVICE_VID), /* idVendor */
UX_W0(UX_DEMO_STORAGE_DEVICE_PID), UX_W1(UX_DEMO_STORAGE_DEVICE_PID), /* idProduct */
UX_W0(0x200), UX_W1(0x200), /* bcdDevice */
0x01, /* iManufacturer */
0x02, /* iProduct */
0x03, /* iSerialNumber */
0x01, /* bNumConfigurations */
/* Device qualifier descriptor */
0x0A, /* bLength */
0x06, /* bDescriptorType */
UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */
0x00, /* bDeviceClass : 0x00 : Interface-defined */
0x00, /* bDeviceSubClass : 0x00 : Reset */
0x00, /* bDeviceProtocol : 0x00 : Reset */
UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */
0x01, /* bNumConfigurations */
0x00, /* bReserved */
/* Configuration descriptor */
0x09, /* bLength */
0x02, /* bDescriptorType */
UX_W0(UX_DEMO_STORAGE_CONFIG_DESC_SIZE), UX_W1(UX_DEMO_STORAGE_CONFIG_DESC_SIZE), /* wTotalLength */
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x05, /* iConfiguration */
0xC0, /* bmAttributes */
/* D6 : 0x1 : Self-powered */
/* D5, Remote Wakeup : 0x0 : Not supported */
0x19, /* bMaxPower : 50mA */
/* Interface descriptor */
0x09, /* bLength */
0x04, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x02, /* bNumEndpoints */
0x08, /* bInterfaceClass : 0x08 : Mass Storage */
0x06, /* bInterfaceSubClass : SCSI transparent*/
0x50, /* nInterfaceProtocol */
0x06, /* iInterface */
/* Endpoint descriptor (Bulk In) */
0x07, /* bLength */
0x05, /* bDescriptorType */
UX_DEMO_STORAGE_EPIN_ADDR, /* bEndpointAddress */
0x02, /* bmAttributes */
/* D1..0, Transfer Type : 0x2 : Bulk */
/* D3..2, Synchronization Type : 0x0 : No Synchronization */
/* D5..4, Usage Type : 0x0 : Data endpoint */
UX_W0(UX_DEMO_STORAGE_HS_EPIN_SIZE), UX_W1(UX_DEMO_STORAGE_HS_EPIN_SIZE), /* wMaxPacketSize */
/* D10..0, Max Packet Size */
/* D12..11, Additional transactions : 0x00 */
0x00, /* bInterval */
/* Endpoint descriptor (Bulk Out) */
0x07, /* bLength */
0x05, /* bDescriptorType */
UX_DEMO_STORAGE_EPOUT_ADDR, /* bEndpointAddress */
0x02, /* bmAttributes */
/* D1..0, Transfer Type : 0x2 : Bulk */
/* D3..2, Synchronization Type : 0x0 : No Synchronization */
/* D5..4, Usage Type : 0x0 : Data endpoint */
UX_W0(UX_DEMO_STORAGE_HS_EPOUT_SIZE), UX_W1(UX_DEMO_STORAGE_HS_EPOUT_SIZE), /* wMaxPacketSize */
/* D10..0, Max Packet Size */
/* D12..11, Additional transactions : 0x00 */
0x00 /* bInterval */
};
/* String Device Framework :
Byte 0 and 1 : Word containing the language ID : 0x0904 for US
Byte 2 : Byte containing the index of the descriptor
Byte 3 : Byte containing the length of the descriptor string
*/
#define STRING_FRAMEWORK_LENGTH sizeof(ux_demo_string_framework)
UCHAR ux_demo_string_framework[] = {
/* iManufacturer string descriptor : Index 1 */
0x09, 0x04, 0x01, 12,
'U', 'S', 'B', 'X', ' ', 'e', 'c', 'l', 'i', 'p', 's', 'e',
/* iProduct string descriptor : Index 2 */
0x09, 0x04, 0x02, 20,
'S', 'T', 'O', 'R', 'A', 'G', 'E', ' ', 'R', 'A', 'M', 'D', 'I', 'S', 'K', ' ', 'D', 'e', 'm', 'o',
/* iSerialNumber Number string descriptor : Index 3 */
0x09, 0x04, 0x03, 13,
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1',
/* iConfiguration string descriptor : Index 4 */
0x09, 0x04, 0x04, 10,
'F', 'U', 'L', 'L', ' ', 'S', 'P', 'E', 'E', 'D',
/* iConfiguration string descriptor : Index 5 */
0x09, 0x04, 0x05, 10,
'H', 'I', 'G', 'H', ' ', 'S', 'P', 'E', 'E', 'D',
/* iInterface string descriptor : Index 6 */
0x09, 0x04, 0x06, 15,
'R', 'e', 'm', 'o', 'v', 'a', 'b', 'l', 'e', ' ', 'D', 'r', 'i', 'v', 'e'
};
/* Multiple languages are supported on the device, to add a language besides english,
the unicode language code must be appended to the ux_demo_language_id_framework array and the length
adjusted accordingly.
*/
#define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(ux_demo_language_id_framework)
UCHAR ux_demo_language_id_framework[] = {
/* English. */
0x09, 0x04
};
#ifdef BUILD_FILE_SYSTEM
static UCHAR sector_0[] = {
0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00,
0x02, 0x01, 0x06, 0x00, 0x02, 0x00, 0x02,
/*0x13: small sectors*/0xF0, 0x00,
0xF8, 0x01, 0x00,
0x01, 0x00, 0x01,
/*0x20: large sectors*/0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x29,
/*0x27: serial number*/0x7E, 0x0D, 0x2E, 0xF0,
0x4E, 0x4F, 0x20, 0x4E, 0x41,
0x4D, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x32, 0x20,
0x20, 0x20, 0x33, 0xC9, 0x8E, 0xD1, 0xBC, 0xF0, 0x7B, 0x8E, 0xD9, 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x20, 0x6F,
0x72, 0x20, 0x6F, 0x74, 0x68, 0x65, 0x72, 0x20, 0x6D, 0x65, 0x64, 0x69,
0x61, 0x2E, 0xFF, 0x0D, 0x0A, 0x44, 0x69, 0x73, 0x6B, 0x20, 0x65, 0x72,
0x72, 0x6F, 0x72, 0xFF, 0x0D, 0x0A, 0x50, 0x72, 0x65, 0x73, 0x73, 0x20,
0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x74, 0x6F, 0x20, 0x72,
0x65, 0x73, 0x74, 0x61, 0x72, 0x74, 0x0D, 0x0A, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xAC, 0xCB, 0xD8, 0x55, 0xAA
};
static UCHAR sector_6_7[] = {
0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
};
static const UCHAR sector_8[] = {
0x54, 0x45, 0x53, 0x54, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x08,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x92,
0x81, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x20, 0x00, 0x49,
0x00, 0x6E, 0x00, 0x66, 0x00, 0x6F, 0x00, 0x0F, 0x00, 0x72, 0x72, 0x00,
0x6D, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x6F, 0x00, 0x00, 0x00,
0x6E, 0x00, 0x00, 0x00, 0x01, 0x53, 0x00, 0x79, 0x00, 0x73, 0x00, 0x74,
0x00, 0x65, 0x00, 0x0F, 0x00, 0x72, 0x6D, 0x00, 0x20, 0x00, 0x56, 0x00,
0x6F, 0x00, 0x6C, 0x00, 0x75, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x65, 0x00,
0x53, 0x59, 0x53, 0x54, 0x45, 0x4D, 0x7E, 0x31, 0x20, 0x20, 0x20, 0x16,
0x00, 0xB5, 0x25, 0x92, 0x81, 0x52, 0x81, 0x52, 0x00, 0x00, 0x26, 0x92,
0x81, 0x52, 0x02,
};
/* Per-LUN volume labels (boot sector offset 0x2B, 11 bytes). */
static const UCHAR volume_label_lun1[11] = { 'T','E','S','T','1',' ',' ',' ',' ',' ',' ' };
static const UCHAR volume_label_lun2[11] = { 'T','E','S','T','2',' ',' ',' ',' ',' ',' ' };
#endif /* BUILD_FILE_SYSTEM */
#ifndef EXTERNAL_MAIN
int main(void)
{
/* Initialize the board. */
board_setup();
/* Enter the ThreadX kernel. */
tx_kernel_enter();
}
#endif /* EXTERNAL_MAIN */
VOID tx_application_define(VOID *first_unused_memory)
{
CHAR *memory_pointer;
UINT status;
UX_SLAVE_CLASS_STORAGE_PARAMETER storage_parameter = {UX_NULL};
UX_PARAMETER_NOT_USED(first_unused_memory);
/* Use static memory block. */
memory_pointer = ux_system_memory_pool;
/* Initialize USBX Memory */
status = ux_system_initialize(memory_pointer, UX_DEVICE_MEMORY_STACK_SIZE, UX_NULL, 0);
if(status != UX_SUCCESS)
return;
/* Install the device portion of USBX. */
status = ux_device_stack_initialize(ux_demo_device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
ux_demo_device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
ux_demo_string_framework, STRING_FRAMEWORK_LENGTH,
ux_demo_language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,
UX_NULL);
if(status != UX_SUCCESS)
return;
/* Store the number of LUN in this device storage instance. */
storage_parameter.ux_slave_class_storage_instance_activate = ux_demo_device_storage_instance_activate;
storage_parameter.ux_slave_class_storage_instance_deactivate = ux_demo_device_storage_instance_deactivate;
storage_parameter.ux_slave_class_storage_parameter_number_lun = 2;
/* Initialize the storage class parameters for reading/writing to the Flash Disk. */
/* LUN1 configuration */
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_last_lba = RAM_DISK_LAST_LBA;
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_block_length = RAM_DISK_BLOCK_LENGTH;
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_type = 0;
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_removable_flag = 0x80;
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_read_only_flag = UX_FALSE;
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_read =
ux_demo_device_storage_media_read;
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_write =
ux_demo_device_storage_media_write;
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_status =
ux_demo_device_storage_media_status;
storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_flush =
RAM_DISK_WRITE_CACHING ? ux_demo_device_storage_media_flush : UX_NULL;
/* LUN2 configuration */
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_last_lba = RAM_DISK_LAST_LBA;
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_block_length = RAM_DISK_BLOCK_LENGTH;
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_type = 0;
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_removable_flag = 0x80;
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_read_only_flag = UX_FALSE;
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_read =
ux_demo_device_storage_media_read;
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_write =
ux_demo_device_storage_media_write;
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_status =
ux_demo_device_storage_media_status;
storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_flush =
RAM_DISK_WRITE_CACHING ? ux_demo_device_storage_media_flush : UX_NULL;
/* Initialize the device storage class. The class is connected with interface 0 on configuration 1. */
status = ux_device_stack_class_register(_ux_system_slave_class_storage_name, _ux_device_class_storage_entry,
1, 0, (VOID *)&storage_parameter);
if(status != UX_SUCCESS)
return;
/* Create the main demo thread. */
status = ux_utility_thread_create(&ux_storage_thread, "storage_usbx_app_thread_entry",
ux_demo_device_storage_thread_entry, 0, ux_storage_thread_stack,
UX_DEMO_THREAD_STACK_SIZE, 20, 20, 1, UX_AUTO_START);
if(status != UX_SUCCESS)
return;
/* Register error callback */
ux_utility_error_callback_register(ux_demo_error_callback);
}
/********************************************************************/
/** ux_demo_device_storage_instance_activate */
/********************************************************************/
VOID ux_demo_device_storage_instance_activate(VOID *storage_instance)
{
if (storage == UX_NULL)
storage = (UX_SLAVE_CLASS_STORAGE*) storage_instance;
}
/********************************************************************/
/** ux_demo_device_storage_instance_deactivate */
/********************************************************************/
VOID ux_demo_device_storage_instance_deactivate(VOID *storage_instance)
{
if (storage_instance == (VOID *)storage)
storage = UX_NULL;
}
/********************************************************************/
/** ux_demo_device_storage_media_read */
/********************************************************************/
UINT ux_demo_device_storage_media_read(VOID *storage, ULONG lun, UCHAR *data_pointer, ULONG number_blocks,
ULONG lba, ULONG *media_status)
{
UINT status = UX_SUCCESS;
UCHAR *memory_pointer = UX_NULL;
UX_PARAMETER_NOT_USED(storage);
UX_PARAMETER_NOT_USED(media_status);
if (lun == 0)
memory_pointer = ram_disk1_memory;
else if (lun == 1)
memory_pointer = ram_disk2_memory;
else
return UX_ERROR;
ux_utility_memory_copy(data_pointer,
memory_pointer + lba * RAM_DISK_BLOCK_LENGTH,
number_blocks * RAM_DISK_BLOCK_LENGTH);
return(status);
}
/********************************************************************/
/** ux_demo_device_storage_media_write */
/********************************************************************/
UINT ux_demo_device_storage_media_write(VOID *storage, ULONG lun, UCHAR *data_pointer, ULONG number_blocks,
ULONG lba, ULONG *media_status)
{
UINT status = UX_SUCCESS;
UCHAR *memory_pointer = UX_NULL;
UX_PARAMETER_NOT_USED(storage);
UX_PARAMETER_NOT_USED(media_status);
if (lun == 0)
memory_pointer = ram_disk1_memory;
else if (lun == 1)
memory_pointer = ram_disk2_memory;
else
return UX_ERROR;
ux_utility_memory_copy(memory_pointer + lba * RAM_DISK_BLOCK_LENGTH,
data_pointer,
number_blocks * RAM_DISK_BLOCK_LENGTH);
return(status);
}
/********************************************************************/
/** ux_demo_device_storage_media_status */
/********************************************************************/
UINT ux_demo_device_storage_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
{
UX_PARAMETER_NOT_USED(storage);
UX_PARAMETER_NOT_USED(lun);
UX_PARAMETER_NOT_USED(media_id);
UX_PARAMETER_NOT_USED(media_status);
return(UX_SUCCESS);
}
/********************************************************************/
/** ux_demo_device_storage_media_flush */
/********************************************************************/
UINT ux_demo_device_storage_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status)
{
UX_PARAMETER_NOT_USED(storage);
UX_PARAMETER_NOT_USED(lun);
UX_PARAMETER_NOT_USED(number_blocks);
UX_PARAMETER_NOT_USED(lba);
UX_PARAMETER_NOT_USED(media_status);
return(UX_SUCCESS);
}
/********************************************************************/
/** ux_demo_device_storage_thread_entry: storage thread */
/********************************************************************/
VOID ux_demo_device_storage_thread_entry(ULONG thread_input)
{
UX_PARAMETER_NOT_USED(thread_input);
/* Register the USB device controllers available in this system. */
usb_device_dcd_initialize(UX_NULL);
usbx_storage_disk_init();
}
/********************************************************************/
/** usbx_storage_disk_init */
/********************************************************************/
static VOID usbx_storage_disk_init(VOID)
{
ux_utility_memory_set(ram_disk1_memory, 0, RAM_DISK_SIZE);
ux_utility_memory_set(ram_disk2_memory, 0, RAM_DISK_SIZE);
#ifdef BUILD_FILE_SYSTEM
ux_utility_memory_copy(ram_disk1_memory + 0 * 512, (void*)sector_0, sizeof(sector_0));
ux_utility_memory_copy(ram_disk2_memory + 0 * 512, (void*)sector_0, sizeof(sector_0));
/* Set unique volume labels per LUN in boot sector (offset 0x2B). */
ux_utility_memory_copy(ram_disk1_memory + 0x2B, (void*)volume_label_lun1, sizeof(volume_label_lun1));
ux_utility_memory_copy(ram_disk2_memory + 0x2B, (void*)volume_label_lun2, sizeof(volume_label_lun2));
/* Update number of sectors. */
if (RAM_DISK_N_LBA >= 0x100)
{
ux_utility_short_put(ram_disk1_memory + 0x13, 0);
ux_utility_long_put(ram_disk1_memory + 0x20, RAM_DISK_N_LBA);
ux_utility_short_put(ram_disk2_memory + 0x13, 0);
ux_utility_long_put(ram_disk2_memory + 0x20, RAM_DISK_N_LBA);
}
else
{
ux_utility_short_put(ram_disk1_memory + 0x13, RAM_DISK_N_LBA);
ux_utility_long_put(ram_disk1_memory + 0x20, 0);
ux_utility_short_put(ram_disk2_memory + 0x13, RAM_DISK_N_LBA);
ux_utility_long_put(ram_disk2_memory + 0x20, 0);
}
ux_utility_memory_copy(ram_disk1_memory + 6 * 512, (void*)sector_6_7, sizeof(sector_6_7));
ux_utility_memory_copy(ram_disk1_memory + 7 * 512, (void*)sector_6_7, sizeof(sector_6_7));
ux_utility_memory_copy(ram_disk1_memory + 8 * 512, (void*)sector_8, sizeof(sector_8));
ux_utility_memory_copy(ram_disk2_memory + 6 * 512, (void*)sector_6_7, sizeof(sector_6_7));
ux_utility_memory_copy(ram_disk2_memory + 7 * 512, (void*)sector_6_7, sizeof(sector_6_7));
ux_utility_memory_copy(ram_disk2_memory + 8 * 512, (void*)sector_8, sizeof(sector_8));
/* Root directory volume label entry (first entry, 32 bytes). */
ux_utility_memory_set(ram_disk1_memory + 8 * 512, 0, 32);
ux_utility_memory_copy(ram_disk1_memory + 8 * 512, (void*)volume_label_lun1, sizeof(volume_label_lun1));
*(ram_disk1_memory + 8 * 512 + 0x0B) = 0x08; /* Attribute: Volume Label */
ux_utility_memory_set(ram_disk2_memory + 8 * 512, 0, 32);
ux_utility_memory_copy(ram_disk2_memory + 8 * 512, (void*)volume_label_lun2, sizeof(volume_label_lun2));
*(ram_disk2_memory + 8 * 512 + 0x0B) = 0x08; /* Attribute: Volume Label */
#endif /* BUILD_FILE_SYSTEM */
}
static VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code)
{
/*
* Refer to ux_api.h. For example,
* UX_SYSTEM_LEVEL_INTERRUPT, UX_SYSTEM_CONTEXT_DCD, UX_DEVICE_HANDLE_UNKNOWN
*/
printf("USBX error: system level(%d), context(%d), error code(0x%x)\r\n", system_level, system_context, error_code);
}