1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
23
24
28
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
65
71
72
77
92
100
101
102
103
104
105
106
122
126
127
131
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
182
186
187
191
192
193
194
195
196
197
202
203
204
209
212
213
214
215
216
217
218
219
226
227
228
229
230
231
232
233
234
235
240
253
266
279
281
282
283
284
285
/* ... */
#include "ff_gen_drv.h"
#include "usbh_diskio_dma.h"
Includes
#define USB_DEFAULT_BLOCK_SIZE 512
Private define
static DWORD scratch[_MAX_SS / 4];
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;
USBH_StatusTypeDef status = USBH_OK;
if (((DWORD)buff & 3) && (((HCD_HandleTypeDef *)hUSB_Host.pData)->Init.dma_enable))
{
while ((count--)&&(status == USBH_OK))
{
status = USBH_MSC_Read(&hUSB_Host, lun, sector + count, (uint8_t *)scratch, 1);
if(status == USBH_OK)
{
memcpy (&buff[count * _MAX_SS] ,scratch, _MAX_SS);
}if (status == USBH_OK) { ... }
else
{
break;
}else { ... }
}while ((count--)&&(status == USBH_OK)) { ... }
}if (((DWORD)buff & 3) && (((HCD_HandleTypeDef *)hUSB_Host.pData)->Init.dma_enable)) { ... }
else
{
status = USBH_MSC_Read(&hUSB_Host, lun, sector, buff, count);
}else { ... }
if(status == USBH_OK)
{
res = RES_OK;
}if (status == 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;
USBH_StatusTypeDef status = USBH_OK;
if (((DWORD)buff & 3) && (((HCD_HandleTypeDef *)hUSB_Host.pData)->Init.dma_enable))
{
while (count--)
{
memcpy (scratch, &buff[count * _MAX_SS], _MAX_SS);
status = USBH_MSC_Write(&hUSB_Host, lun, sector + count, (BYTE *)scratch, 1) ;
if(status == USBH_FAIL)
{
break;
}if (status == USBH_FAIL) { ... }
}while (count--) { ... }
}if (((DWORD)buff & 3) && (((HCD_HandleTypeDef *)hUSB_Host.pData)->Init.dma_enable)) { ... }
else
{
status = USBH_MSC_Write(&hUSB_Host, lun, sector, (BYTE *)buff, count);
}else { ... }
if(status == USBH_OK)
{
res = RES_OK;
}if (status == 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