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
41
42
43
44
45
46
47
48
51
52
55
56
59
60
64
65
66
69
72
73
76
79
80
83
86
87
88
91
94
95
96
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
128
129
130
133
134
137
138
139
142
143
144
150
151
152
153
154
155
156
157
158
159
163
164
165
166
170
171
172
173
174
179
180
181
182
183
188
193
194
199
204
205
206
207
208
209
210
211
212
213
214
220
224
225
231
235
236
237
238
239
240
241
242
248
249
250
251
252
258
259
265
266
271
272
273
274
275
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
298
303
304
315
316
317
325
327
328
329
330
331
332
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
362
363
364
365
372
376
384
385
386
387
388
389
390
391
396
400
401
402
403
404
405
406
407
412
416
417
418
419
420
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
451
459
460
461
462
463
464
465
466
476
477
478
479
480
481
482
483
488
496
497
498
502
504
505
506
507
513
514
515
516
517
518
519
520
521
527
528
529
530
531
532
536
538
539
540
541
542
543
549
556
563
564
565
566
567
568
569
570
571
572
573
574
575
576
581
585
593
594
595
601
602
603
604
605
606
607
612
616
624
625
626
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
652
660
661
662
668
669
670
671
674
675
676
677
678
684
685
686
687
688
689
693
697
698
699
700
701
707
708
709
710
711
715
716
717
718
719
726
727
728
729
730
731
735
739
740
741
742
743
750
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
789
790
791
792
793
794
795
796
797
798
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
836
837
838
839
840
841
842
843
844
852
853
854
855
856
859
860
863
864
867
868
871
872
875
/* ... */
/* ... */
#include "usbh_msc.h"
#include "usbh_msc_bot.h"
#include "usbh_msc_scsi.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
static USBH_StatusTypeDef USBH_MSC_InterfaceInit(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_MSC_InterfaceDeInit(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_MSC_Process(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_MSC_ClassRequest(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_MSC_SOFProcess(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun);
USBH_ClassTypeDef USBH_msc =
{
"MSC",
USB_MSC_CLASS,
USBH_MSC_InterfaceInit,
USBH_MSC_InterfaceDeInit,
USBH_MSC_ClassRequest,
USBH_MSC_Process,
USBH_MSC_SOFProcess,
NULL,
...};
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
static USBH_StatusTypeDef USBH_MSC_InterfaceInit(USBH_HandleTypeDef *phost)
{
USBH_StatusTypeDef status;
uint8_t interface;
MSC_HandleTypeDef *MSC_Handle;
interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, MSC_TRANSPARENT, MSC_BOT);
if ((interface == 0xFFU) || (interface >= USBH_MAX_NUM_INTERFACES))
{
USBH_DbgLog("Cannot Find the interface for %s class.", phost->pActiveClass->Name);
return USBH_FAIL;
...}
status = USBH_SelectInterface(phost, interface);
if (status != USBH_OK)
{
return USBH_FAIL;
}if (status != USBH_OK) { ... }
phost->pActiveClass->pData = (MSC_HandleTypeDef *)USBH_malloc(sizeof(MSC_HandleTypeDef));
MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
if (MSC_Handle == NULL)
{
USBH_DbgLog("Cannot allocate memory for MSC Handle");
return USBH_FAIL;
}if (MSC_Handle == NULL) { ... }
(void)USBH_memset(MSC_Handle, 0, sizeof(MSC_HandleTypeDef));
if ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) != 0U)
{
MSC_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress);
MSC_Handle->InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize;
}if ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) != 0U) { ... }
else
{
MSC_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress);
MSC_Handle->OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize;
}else { ... }
if ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress & 0x80U) != 0U)
{
MSC_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress);
MSC_Handle->InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].wMaxPacketSize;
}if ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress & 0x80U) != 0U) { ... }
else
{
MSC_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress);
MSC_Handle->OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].wMaxPacketSize;
}else { ... }
MSC_Handle->state = MSC_INIT;
MSC_Handle->error = MSC_OK;
MSC_Handle->req_state = MSC_REQ_IDLE;
MSC_Handle->OutPipe = USBH_AllocPipe(phost, MSC_Handle->OutEp);
MSC_Handle->InPipe = USBH_AllocPipe(phost, MSC_Handle->InEp);
(void)USBH_MSC_BOT_Init(phost);
if ((MSC_Handle->OutEp != 0U) && (MSC_Handle->OutEpSize != 0U))
{
(void)USBH_OpenPipe(phost, MSC_Handle->OutPipe, MSC_Handle->OutEp,
phost->device.address, phost->device.speed,
USB_EP_TYPE_BULK, MSC_Handle->OutEpSize);
}if ((MSC_Handle->OutEp != 0U) && (MSC_Handle->OutEpSize != 0U)) { ... }
else
{
return USBH_NOT_SUPPORTED;
}else { ... }
if ((MSC_Handle->InEp != 0U) && (MSC_Handle->InEpSize != 0U))
{
(void)USBH_OpenPipe(phost, MSC_Handle->InPipe, MSC_Handle->InEp,
phost->device.address, phost->device.speed, USB_EP_TYPE_BULK,
MSC_Handle->InEpSize);
}if ((MSC_Handle->InEp != 0U) && (MSC_Handle->InEpSize != 0U)) { ... }
else
{
return USBH_NOT_SUPPORTED;
}else { ... }
(void)USBH_LL_SetToggle(phost, MSC_Handle->InPipe, 0U);
(void)USBH_LL_SetToggle(phost, MSC_Handle->OutPipe, 0U);
return USBH_OK;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_MSC_InterfaceDeInit(USBH_HandleTypeDef *phost)
{
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
if ((MSC_Handle->OutPipe) != 0U)
{
(void)USBH_ClosePipe(phost, MSC_Handle->OutPipe);
(void)USBH_FreePipe(phost, MSC_Handle->OutPipe);
MSC_Handle->OutPipe = 0U;
}if ((MSC_Handle->OutPipe) != 0U) { ... }
if ((MSC_Handle->InPipe != 0U))
{
(void)USBH_ClosePipe(phost, MSC_Handle->InPipe);
(void)USBH_FreePipe(phost, MSC_Handle->InPipe);
MSC_Handle->InPipe = 0U;
}if ((MSC_Handle->InPipe != 0U)) { ... }
if ((phost->pActiveClass->pData) != NULL)
{
USBH_free(phost->pActiveClass->pData);
phost->pActiveClass->pData = 0U;
}if ((phost->pActiveClass->pData) != NULL) { ... }
return USBH_OK;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_MSC_ClassRequest(USBH_HandleTypeDef *phost)
{
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
USBH_StatusTypeDef status = USBH_BUSY;
uint8_t i;
switch (MSC_Handle->req_state)
{
case MSC_REQ_IDLE:
case MSC_REQ_GET_MAX_LUN:
status = USBH_MSC_BOT_REQ_GetMaxLUN(phost, &MSC_Handle->max_lun);
/* ... */
if (status == USBH_NOT_SUPPORTED)
{
MSC_Handle->max_lun = 0U;
status = USBH_OK;
}if (status == USBH_NOT_SUPPORTED) { ... }
if (status == USBH_OK)
{
MSC_Handle->max_lun = (MSC_Handle->max_lun > MAX_SUPPORTED_LUN) ? MAX_SUPPORTED_LUN : (MSC_Handle->max_lun + 1U);
USBH_UsrLog("Number of supported LUN: %d", MSC_Handle->max_lun);
for (i = 0U; i < MSC_Handle->max_lun; i++)
{
MSC_Handle->unit[i].prev_ready_state = USBH_FAIL;
MSC_Handle->unit[i].state_changed = 0U;
}for (i = 0U; i < MSC_Handle->max_lun; i++) { ... }
}if (status == USBH_OK) { ... }
break;
case MSC_REQ_GET_MAX_LUN:
case MSC_REQ_ERROR:
if (USBH_ClrFeature(phost, 0x00U) == USBH_OK)
{
MSC_Handle->req_state = MSC_Handle->prev_req_state;
}if (USBH_ClrFeature(phost, 0x00U) == USBH_OK) { ... }
break;
case MSC_REQ_ERROR:
default:
break;default
}switch (MSC_Handle->req_state) { ... }
return status;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_MSC_Process(USBH_HandleTypeDef *phost)
{
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
USBH_StatusTypeDef error = USBH_BUSY;
USBH_StatusTypeDef scsi_status = USBH_BUSY;
USBH_StatusTypeDef ready_status = USBH_BUSY;
switch (MSC_Handle->state)
{
case MSC_INIT:
if (MSC_Handle->current_lun < MSC_Handle->max_lun)
{
MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_NOT_READY;
switch (MSC_Handle->unit[MSC_Handle->current_lun].state)
{
case MSC_INIT:
USBH_UsrLog("LUN #%d: ", MSC_Handle->current_lun);
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_READ_INQUIRY;
MSC_Handle->timer = phost->Timer;
break;
case MSC_INIT:
case MSC_READ_INQUIRY:
scsi_status = USBH_MSC_SCSI_Inquiry(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].inquiry);
if (scsi_status == USBH_OK)
{
USBH_UsrLog("Inquiry Vendor : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.vendor_id);
USBH_UsrLog("Inquiry Product : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.product_id);
USBH_UsrLog("Inquiry Version : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.revision_id);
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_TEST_UNIT_READY;
}if (scsi_status == USBH_OK) { ... }
if (scsi_status == USBH_FAIL)
{
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE;
}if (scsi_status == USBH_FAIL) { ... }
else
{
if (scsi_status == USBH_UNRECOVERED_ERROR)
{
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR;
}if (scsi_status == USBH_UNRECOVERED_ERROR) { ... }
}else { ... }
break;
case MSC_READ_INQUIRY:
case MSC_TEST_UNIT_READY:
ready_status = USBH_MSC_SCSI_TestUnitReady(phost, (uint8_t)MSC_Handle->current_lun);
if (ready_status == USBH_OK)
{
if (MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_OK)
{
MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 1U;
USBH_UsrLog("MSC Device ready");
}if (MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_OK) { ... }
else
{
MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 0U;
}else { ... }
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_READ_CAPACITY10;
MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_OK;
MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state = USBH_OK;
}if (ready_status == USBH_OK) { ... }
if (ready_status == USBH_FAIL)
{
if (MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_FAIL)
{
MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 1U;
USBH_UsrLog("MSC Device NOT ready");
}if (MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_FAIL) { ... }
else
{
MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 0U;
}else { ... }
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE;
MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_NOT_READY;
MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state = USBH_FAIL;
}if (ready_status == USBH_FAIL) { ... }
else
{
if (ready_status == USBH_UNRECOVERED_ERROR)
{
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR;
}if (ready_status == USBH_UNRECOVERED_ERROR) { ... }
}else { ... }
break;
case MSC_TEST_UNIT_READY:
case MSC_READ_CAPACITY10:
scsi_status = USBH_MSC_SCSI_ReadCapacity(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].capacity);
if (scsi_status == USBH_OK)
{
if (MSC_Handle->unit[MSC_Handle->current_lun].state_changed == 1U)
{
USBH_UsrLog("MSC Device capacity : %u Bytes", \
(unsigned int)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_nbr *
MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_size));
USBH_UsrLog("Block number : %u", (unsigned int)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_nbr));
USBH_UsrLog("Block Size : %u", (unsigned int)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_size));
}if (MSC_Handle->unit[MSC_Handle->current_lun].state_changed == 1U) { ... }
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_OK;
MSC_Handle->current_lun++;
}if (scsi_status == USBH_OK) { ... }
else if (scsi_status == USBH_FAIL)
{
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE;
}else if (scsi_status == USBH_FAIL) { ... }
else
{
if (scsi_status == USBH_UNRECOVERED_ERROR)
{
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR;
}if (scsi_status == USBH_UNRECOVERED_ERROR) { ... }
}else { ... }
break;
case MSC_READ_CAPACITY10:
case MSC_REQUEST_SENSE:
scsi_status = USBH_MSC_SCSI_RequestSense(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].sense);
if (scsi_status == USBH_OK)
{
if ((MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_UNIT_ATTENTION) ||
(MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_NOT_READY))
{
if ((phost->Timer - MSC_Handle->timer) < 10000U)
{
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_TEST_UNIT_READY;
break;
}if ((phost->Timer - MSC_Handle->timer) < 10000U) { ... }
}if ((MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_UNIT_ATTENTION) || (MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_NOT_READY)) { ... }
USBH_UsrLog("Sense Key : %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.key);
USBH_UsrLog("Additional Sense Code : %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.asc);
USBH_UsrLog("Additional Sense Code Qualifier: %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.ascq);
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
MSC_Handle->current_lun++;
}if (scsi_status == USBH_OK) { ... }
if (scsi_status == USBH_FAIL)
{
USBH_UsrLog("MSC Device NOT ready");
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_UNRECOVERED_ERROR;
}if (scsi_status == USBH_FAIL) { ... }
else
{
if (scsi_status == USBH_UNRECOVERED_ERROR)
{
MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE;
MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR;
}if (scsi_status == USBH_UNRECOVERED_ERROR) { ... }
}else { ... }
break;
case MSC_REQUEST_SENSE:
case MSC_UNRECOVERED_ERROR:
MSC_Handle->current_lun++;
break;
case MSC_UNRECOVERED_ERROR:
default:
break;default
}switch (MSC_Handle->unit[MSC_Handle->current_lun].state) { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
}if (MSC_Handle->current_lun < MSC_Handle->max_lun) { ... }
else
{
MSC_Handle->current_lun = 0U;
MSC_Handle->state = MSC_IDLE;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
phost->pUser(phost, HOST_USER_CLASS_ACTIVE);
}else { ... }
break;
case MSC_INIT:
case MSC_IDLE:
error = USBH_OK;
break;
case MSC_IDLE:
default:
break;default
}switch (MSC_Handle->state) { ... }
return error;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_MSC_SOFProcess(USBH_HandleTypeDef *phost)
{
UNUSED(phost);
return USBH_OK;
}{ ... }
/* ... */
static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun)
{
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
USBH_StatusTypeDef error = USBH_BUSY;
USBH_StatusTypeDef scsi_status = USBH_BUSY;
switch (MSC_Handle->unit[lun].state)
{
case MSC_READ:
scsi_status = USBH_MSC_SCSI_Read(phost, lun, 0U, NULL, 0U);
if (scsi_status == USBH_OK)
{
MSC_Handle->unit[lun].state = MSC_IDLE;
error = USBH_OK;
}if (scsi_status == USBH_OK) { ... }
else if (scsi_status == USBH_FAIL)
{
MSC_Handle->unit[lun].state = MSC_REQUEST_SENSE;
}else if (scsi_status == USBH_FAIL) { ... }
else
{
if (scsi_status == USBH_UNRECOVERED_ERROR)
{
MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR;
error = USBH_FAIL;
}if (scsi_status == USBH_UNRECOVERED_ERROR) { ... }
}else { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
break;
case MSC_READ:
case MSC_WRITE:
scsi_status = USBH_MSC_SCSI_Write(phost, lun, 0U, NULL, 0U);
if (scsi_status == USBH_OK)
{
MSC_Handle->unit[lun].state = MSC_IDLE;
error = USBH_OK;
}if (scsi_status == USBH_OK) { ... }
else if (scsi_status == USBH_FAIL)
{
MSC_Handle->unit[lun].state = MSC_REQUEST_SENSE;
}else if (scsi_status == USBH_FAIL) { ... }
else
{
if (scsi_status == USBH_UNRECOVERED_ERROR)
{
MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR;
error = USBH_FAIL;
}if (scsi_status == USBH_UNRECOVERED_ERROR) { ... }
}else { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
break;
case MSC_WRITE:
case MSC_REQUEST_SENSE:
scsi_status = USBH_MSC_SCSI_RequestSense(phost, lun, &MSC_Handle->unit[lun].sense);
if (scsi_status == USBH_OK)
{
USBH_UsrLog("Sense Key : %x", MSC_Handle->unit[lun].sense.key);
USBH_UsrLog("Additional Sense Code : %x", MSC_Handle->unit[lun].sense.asc);
USBH_UsrLog("Additional Sense Code Qualifier: %x", MSC_Handle->unit[lun].sense.ascq);
MSC_Handle->unit[lun].state = MSC_IDLE;
MSC_Handle->unit[lun].error = MSC_ERROR;
error = USBH_FAIL;
}if (scsi_status == USBH_OK) { ... }
if (scsi_status == USBH_FAIL)
{
USBH_UsrLog("MSC Device NOT ready");
}if (scsi_status == USBH_FAIL) { ... }
else
{
if (scsi_status == USBH_UNRECOVERED_ERROR)
{
MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR;
error = USBH_FAIL;
}if (scsi_status == USBH_UNRECOVERED_ERROR) { ... }
}else { ... }
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
#if (osCMSIS < 0x20000U)
(void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, 0U);
#endif/* ... */
#endif
break;
case MSC_REQUEST_SENSE:
default:
break;
default
}switch (MSC_Handle->unit[lun].state) { ... }
return error;
}{ ... }
/* ... */
uint8_t USBH_MSC_IsReady(USBH_HandleTypeDef *phost)
{
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
uint8_t res;
if ((phost->gState == HOST_CLASS) && (MSC_Handle->state == MSC_IDLE))
{
res = 1U;
}if ((phost->gState == HOST_CLASS) && (MSC_Handle->state == MSC_IDLE)) { ... }
else
{
res = 0U;
}else { ... }
return res;
}{ ... }
/* ... */
uint8_t USBH_MSC_GetMaxLUN(USBH_HandleTypeDef *phost)
{
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
if ((phost->gState == HOST_CLASS) && (MSC_Handle->state == MSC_IDLE))
{
return (uint8_t)MSC_Handle->max_lun;
}if ((phost->gState == HOST_CLASS) && (MSC_Handle->state == MSC_IDLE)) { ... }
return 0xFFU;
}{ ... }
/* ... */
uint8_t USBH_MSC_UnitIsReady(USBH_HandleTypeDef *phost, uint8_t lun)
{
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
uint8_t res;
if ((phost->gState == HOST_CLASS) && (MSC_Handle->unit[lun].error == MSC_OK))
{
res = 1U;
}if ((phost->gState == HOST_CLASS) && (MSC_Handle->unit[lun].error == MSC_OK)) { ... }
else
{
res = 0U;
}else { ... }
return res;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_MSC_GetLUNInfo(USBH_HandleTypeDef *phost, uint8_t lun, MSC_LUNTypeDef *info)
{
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
if (phost->gState == HOST_CLASS)
{
(void)USBH_memcpy(info, &MSC_Handle->unit[lun], sizeof(MSC_LUNTypeDef));
return USBH_OK;
}if (phost->gState == HOST_CLASS) { ... }
else
{
return USBH_FAIL;
}else { ... }
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_MSC_Read(USBH_HandleTypeDef *phost,
uint8_t lun,
uint32_t address,
uint8_t *pbuf,
uint32_t length)
{
uint32_t timeout;
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
if ((phost->device.is_connected == 0U) ||
(phost->gState != HOST_CLASS) ||
(MSC_Handle->unit[lun].state != MSC_IDLE))
{
return USBH_FAIL;
}if ((phost->device.is_connected == 0U) || (phost->gState != HOST_CLASS) || (MSC_Handle->unit[lun].state != MSC_IDLE)) { ... }
MSC_Handle->state = MSC_READ;
MSC_Handle->unit[lun].state = MSC_READ;
MSC_Handle->rw_lun = lun;
(void)USBH_MSC_SCSI_Read(phost, lun, address, pbuf, length);
timeout = phost->Timer;
while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY)
{
if (((phost->Timer - timeout) > (10000U * length)) || (phost->device.is_connected == 0U))
{
MSC_Handle->state = MSC_IDLE;
return USBH_FAIL;
}if (((phost->Timer - timeout) > (10000U * length)) || (phost->device.is_connected == 0U)) { ... }
}while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY) { ... }
MSC_Handle->state = MSC_IDLE;
return USBH_OK;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_MSC_Write(USBH_HandleTypeDef *phost,
uint8_t lun,
uint32_t address,
uint8_t *pbuf,
uint32_t length)
{
uint32_t timeout;
MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
if ((phost->device.is_connected == 0U) ||
(phost->gState != HOST_CLASS) ||
(MSC_Handle->unit[lun].state != MSC_IDLE))
{
return USBH_FAIL;
}if ((phost->device.is_connected == 0U) || (phost->gState != HOST_CLASS) || (MSC_Handle->unit[lun].state != MSC_IDLE)) { ... }
MSC_Handle->state = MSC_WRITE;
MSC_Handle->unit[lun].state = MSC_WRITE;
MSC_Handle->rw_lun = lun;
(void)USBH_MSC_SCSI_Write(phost, lun, address, pbuf, length);
timeout = phost->Timer;
while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY)
{
if (((phost->Timer - timeout) > (10000U * length)) || (phost->device.is_connected == 0U))
{
MSC_Handle->state = MSC_IDLE;
return USBH_FAIL;
}if (((phost->Timer - timeout) > (10000U * length)) || (phost->device.is_connected == 0U)) { ... }
}while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY) { ... }
MSC_Handle->state = MSC_IDLE;
return USBH_OK;
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
Includes