1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
23
24
28
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
64
70
71
76
91
92
100
101
102
103
104
105
109
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
149
150
151
152
153
154
155
160
161
162
167
170
171
172
173
174
175
176
177
184
185
186
187
188
189
190
191
192
193
198
211
224
237
239
240
241
242
243
/* ... */
#include "ff_gen_drv.h"
#include "usbh_diskio.h"
Includes
#define USB_DEFAULT_BLOCK_SIZE 512
Private define
extern USBH_HandleTypeDef hUSB_Host;
Private variables
DSTATUS USBH_initialize (BYTE);
DSTATUS USBH_status (BYTE);
DRESULT USBH_read (BYTE, BYTE*, DWORD, UINT);
#if _USE_WRITE == 1
DRESULT USBH_write (BYTE, const BYTE*, DWORD, UINT);
#endif
#if _USE_IOCTL == 1
DRESULT USBH_ioctl (BYTE, BYTE, void*);
#endif
const Diskio_drvTypeDef USBH_Driver =
{
USBH_initialize,
USBH_status,
USBH_read,
#if _USE_WRITE == 1
USBH_write,
#endif
#if _USE_IOCTL == 1
USBH_ioctl,
#endif
...};
Private function prototypes
/* ... */
DSTATUS USBH_initialize(BYTE lun)
{
return RES_OK;
}{ ... }
/* ... */
DSTATUS USBH_status(BYTE lun)
{
DRESULT res = RES_ERROR;
if(USBH_MSC_UnitIsReady(&hUSB_Host, lun))
{
res = RES_OK;
}if (USBH_MSC_UnitIsReady(&hUSB_Host, lun)) { ... }
else
{
res = RES_ERROR;
}else { ... }
return res;
}{ ... }
/* ... */
DRESULT USBH_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
{
DRESULT res = RES_ERROR;
MSC_LUNTypeDef info;
if(USBH_MSC_Read(&hUSB_Host, lun, sector, buff, count) == USBH_OK)
{
res = RES_OK;
}if (USBH_MSC_Read(&hUSB_Host, lun, sector, buff, count) == USBH_OK) { ... }
else
{
USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info);
switch (info.sense.asc)
{
case SCSI_ASC_LOGICAL_UNIT_NOT_READY:
case SCSI_ASC_MEDIUM_NOT_PRESENT:
case SCSI_ASC_NOT_READY_TO_READY_CHANGE:
USBH_ErrLog ("USB Disk is not ready!");
res = RES_NOTRDY;
break;
case SCSI_ASC_NOT_READY_TO_READY_CHANGE:
default:
res = RES_ERROR;
break;default
}switch (info.sense.asc) { ... }
}else { ... }
return res;
}{ ... }
/* ... */
#if _USE_WRITE == 1
DRESULT USBH_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
{
DRESULT res = RES_ERROR;
MSC_LUNTypeDef info;
if(USBH_MSC_Write(&hUSB_Host, lun, sector, (BYTE *)buff, count) == USBH_OK)
{
res = RES_OK;
}if (USBH_MSC_Write(&hUSB_Host, lun, sector, (BYTE *)buff, count) == USBH_OK) { ... }
else
{
USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info);
switch (info.sense.asc)
{
case SCSI_ASC_WRITE_PROTECTED:
USBH_ErrLog("USB Disk is Write protected!");
res = RES_WRPRT;
break;
case SCSI_ASC_WRITE_PROTECTED:
case SCSI_ASC_LOGICAL_UNIT_NOT_READY:
case SCSI_ASC_MEDIUM_NOT_PRESENT:
case SCSI_ASC_NOT_READY_TO_READY_CHANGE:
USBH_ErrLog("USB Disk is not ready!");
res = RES_NOTRDY;
break;
case SCSI_ASC_NOT_READY_TO_READY_CHANGE:
default:
res = RES_ERROR;
break;default
}switch (info.sense.asc) { ... }
}else { ... }
return res;
}{ ... }
/* ... */#endif
/* ... */
#if _USE_IOCTL == 1
DRESULT USBH_ioctl(BYTE lun, BYTE cmd, void *buff)
{
DRESULT res = RES_ERROR;
MSC_LUNTypeDef info;
switch (cmd)
{
case CTRL_SYNC:
res = RES_OK;
break;
case CTRL_SYNC:
case GET_SECTOR_COUNT :
if(USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info) == USBH_OK)
{
*(DWORD*)buff = info.capacity.block_nbr;
res = RES_OK;
}if (USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info) == USBH_OK) { ... }
else
{
res = RES_ERROR;
}else { ... }
break;
case GET_SECTOR_COUNT :
case GET_SECTOR_SIZE :
if(USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info) == USBH_OK)
{
*(DWORD*)buff = info.capacity.block_size;
res = RES_OK;
}if (USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info) == USBH_OK) { ... }
else
{
res = RES_ERROR;
}else { ... }
break;
case GET_SECTOR_SIZE :
case GET_BLOCK_SIZE :
if(USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info) == USBH_OK)
{
*(DWORD*)buff = info.capacity.block_size / USB_DEFAULT_BLOCK_SIZE;
res = RES_OK;
}if (USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info) == USBH_OK) { ... }
else
{
res = RES_ERROR;
}else { ... }
break;
case GET_BLOCK_SIZE :
default:
res = RES_PARERR;default
}switch (cmd) { ... }
return res;
}{ ... }
/* ... */#endifPrivate functions