STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
usbh_diskio.c
Go to the documentation of this file.
1 
28 /* Includes ------------------------------------------------------------------*/
29 #include <string.h>
30 #include "ff_gen_drv.h"
31 
32 /* Private typedef -----------------------------------------------------------*/
33 /* Private define ------------------------------------------------------------*/
34 /* Private variables ---------------------------------------------------------*/
36 #if _USE_BUFF_WO_ALIGNMENT == 0
37 /* Local buffer use to handle buffer not aligned 32bits*/
38 static DWORD scratch[_MAX_SS / 4];
39 #endif
40 
41 /* Private function prototypes -----------------------------------------------*/
45 
46 #if _USE_WRITE == 1
47  DRESULT USBH_write (BYTE, const BYTE*, DWORD, UINT);
48 #endif /* _USE_WRITE == 1 */
49 
50 #if _USE_IOCTL == 1
51  DRESULT USBH_ioctl (BYTE, BYTE, void*);
52 #endif /* _USE_IOCTL == 1 */
53 
55 {
58  USBH_read,
59 #if _USE_WRITE == 1
60  USBH_write,
61 #endif /* _USE_WRITE == 1 */
62 #if _USE_IOCTL == 1
63  USBH_ioctl,
64 #endif /* _USE_IOCTL == 1 */
65 };
66 
67 /* Private functions ---------------------------------------------------------*/
68 
75 {
76  return RES_OK;
77 }
78 
85 {
86  DRESULT res = RES_ERROR;
87 
88  if(USBH_MSC_UnitIsReady(&HOST_HANDLE, lun))
89  {
90  res = RES_OK;
91  }
92  else
93  {
94  res = RES_ERROR;
95  }
96 
97  return res;
98 }
99 
108 DRESULT USBH_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
109 {
110  DRESULT res = RES_ERROR;
111  MSC_LUNTypeDef info;
112  USBH_StatusTypeDef status = USBH_OK;
113 
114  if ((DWORD)buff & 3) /* DMA Alignment issue, do single up to aligned buffer */
115  {
116 #if _USE_BUFF_WO_ALIGNMENT == 0
117  while ((count--)&&(status == USBH_OK))
118  {
119  status = USBH_MSC_Read(&HOST_HANDLE, lun, sector + count, (uint8_t *)scratch, 1);
120  if(status == USBH_OK)
121  {
122  memcpy (&buff[count * _MAX_SS] ,scratch, _MAX_SS);
123  }
124  else
125  {
126  break;
127  }
128  }
129 #else
130  return res;
131 #endif
132  }
133  else
134  {
135  status = USBH_MSC_Read(&HOST_HANDLE, lun, sector, buff, count);
136  }
137 
138  if(status == USBH_OK)
139  {
140  res = RES_OK;
141  }
142  else
143  {
144  USBH_MSC_GetLUNInfo(&HOST_HANDLE, lun, &info);
145 
146  switch (info.sense.asc)
147  {
151  USBH_ErrLog ("USB Disk is not ready!");
152  res = RES_NOTRDY;
153  break;
154 
155  default:
156  res = RES_ERROR;
157  break;
158  }
159  }
160 
161  return res;
162 }
163 
172 #if _USE_WRITE == 1
173 DRESULT USBH_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
174 {
175  DRESULT res = RES_ERROR;
176  MSC_LUNTypeDef info;
177  USBH_StatusTypeDef status = USBH_OK;
178 
179  if ((DWORD)buff & 3) /* DMA Alignment issue, do single up to aligned buffer */
180  {
181 #if _USE_BUFF_WO_ALIGNMENT == 0
182  while (count--)
183  {
184  memcpy (scratch, &buff[count * _MAX_SS], _MAX_SS);
185 
186  status = USBH_MSC_Write(&HOST_HANDLE, lun, sector + count, (BYTE *)scratch, 1) ;
187  if(status == USBH_FAIL)
188  {
189  break;
190  }
191  }
192 #else
193  return res;
194 #endif
195  }
196  else
197  {
198  status = USBH_MSC_Write(&HOST_HANDLE, lun, sector, (BYTE *)buff, count);
199  }
200 
201  if(status == USBH_OK)
202  {
203  res = RES_OK;
204  }
205  else
206  {
207  USBH_MSC_GetLUNInfo(&HOST_HANDLE, lun, &info);
208 
209  switch (info.sense.asc)
210  {
212  USBH_ErrLog("USB Disk is Write protected!");
213  res = RES_WRPRT;
214  break;
215 
219  USBH_ErrLog("USB Disk is not ready!");
220  res = RES_NOTRDY;
221  break;
222 
223  default:
224  res = RES_ERROR;
225  break;
226  }
227  }
228 
229  return res;
230 }
231 #endif /* _USE_WRITE == 1 */
232 
240 #if _USE_IOCTL == 1
241 DRESULT USBH_ioctl(BYTE lun, BYTE cmd, void *buff)
242 {
243  DRESULT res = RES_ERROR;
244  MSC_LUNTypeDef info;
245 
246  switch (cmd)
247  {
248  /* Make sure that no pending write process */
249  case CTRL_SYNC:
250  res = RES_OK;
251  break;
252 
253  /* Get number of sectors on the disk (DWORD) */
254  case GET_SECTOR_COUNT :
255  if(USBH_MSC_GetLUNInfo(&HOST_HANDLE, lun, &info) == USBH_OK)
256  {
257  *(DWORD*)buff = info.capacity.block_nbr;
258  res = RES_OK;
259  }
260  else
261  {
262  res = RES_ERROR;
263  }
264  break;
265 
266  /* Get R/W sector size (WORD) */
267  case GET_SECTOR_SIZE :
268  if(USBH_MSC_GetLUNInfo(&HOST_HANDLE, lun, &info) == USBH_OK)
269  {
270  *(DWORD*)buff = info.capacity.block_size;
271  res = RES_OK;
272  }
273  else
274  {
275  res = RES_ERROR;
276  }
277  break;
278 
279  /* Get erase block size in unit of sector (DWORD) */
280  case GET_BLOCK_SIZE :
281 
282  if(USBH_MSC_GetLUNInfo(&HOST_HANDLE, lun, &info) == USBH_OK)
283  {
284  *(DWORD*)buff = info.capacity.block_size;
285  res = RES_OK;
286  }
287  else
288  {
289  res = RES_ERROR;
290  }
291  break;
292 
293  default:
294  res = RES_PARERR;
295  }
296 
297  return res;
298 }
299 #endif /* _USE_IOCTL == 1 */
300 
301 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
302 
#define USBH_ErrLog(...)
#define GET_BLOCK_SIZE
Definition: diskio.h:55
USBH_StatusTypeDef USBH_MSC_Write(USBH_HandleTypeDef *phost, uint8_t lun, uint32_t address, uint8_t *pbuf, uint32_t length)
USBH_MSC_Write The function performs a Write operation.
Definition: usbh_msc.c:748
const Diskio_drvTypeDef USBH_Driver
Definition: usbh_diskio.c:54
#define _MAX_SS
Definition: ffconf.h:226
USBH_StatusTypeDef USBH_MSC_Read(USBH_HandleTypeDef *phost, uint8_t lun, uint32_t address, uint8_t *pbuf, uint32_t length)
USBH_MSC_Read The function performs a Read operation.
Definition: usbh_msc.c:700
Disk IO Driver structure definition.
Definition: ff_gen_drv.h:45
DSTATUS USBH_initialize(BYTE)
Initializes a Drive.
Definition: usbh_diskio.c:74
#define GET_SECTOR_COUNT
Definition: diskio.h:53
#define SCSI_ASC_MEDIUM_NOT_PRESENT
DRESULT USBH_read(BYTE, BYTE *, DWORD, UINT)
Reads Sector(s)
Definition: usbh_diskio.c:108
unsigned long DWORD
Definition: integer.h:29
BYTE DSTATUS
Definition: diskio.h:19
DRESULT
Definition: diskio.h:22
unsigned char BYTE
Definition: integer.h:16
#define SCSI_ASC_NOT_READY_TO_READY_CHANGE
Definition: diskio.h:23
SCSI_CapacityTypeDef capacity
Definition: usbh_msc.h:107
#define SCSI_ASC_LOGICAL_UNIT_NOT_READY
DSTATUS USBH_status(BYTE)
Gets Disk Status.
Definition: usbh_diskio.c:84
USBH_StatusTypeDef USBH_MSC_GetLUNInfo(USBH_HandleTypeDef *phost, uint8_t lun, MSC_LUNTypeDef *info)
USBH_MSC_GetLUNInfo The function return a LUN information.
Definition: usbh_msc.c:676
unsigned int UINT
Definition: integer.h:25
#define GET_SECTOR_SIZE
Definition: diskio.h:54
USBH_StatusTypeDef
Definition: usbh_def.h:302
uint8_t USBH_MSC_UnitIsReady(USBH_HandleTypeDef *phost, uint8_t lun)
USBH_MSC_UnitIsReady The function check whether a LUN is ready.
Definition: usbh_msc.c:655
USBH_HandleTypeDef HOST_HANDLE
Header for ff_gen_drv.c module.
#define CTRL_SYNC
Definition: diskio.h:52
#define SCSI_ASC_WRITE_PROTECTED
SCSI_SenseTypeDef sense
Definition: usbh_msc.h:108