PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbHostMassStorage.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
55 /*****************************************************************************/
56 /* Include files */
57 /*****************************************************************************/
58 
59 #include "UsbHostMassStorage.h"
60 
61 #if ((FM_PERIPHERAL_USB_HOST_ENABLED == ON) && (USBHOSTMASSSTORAGE_ENABLED == ON))
62 
63 /*****************************************************************************/
64 /* Local pre-processor symbols/macros ('#define') */
65 /*****************************************************************************/
66 
67 
68 /*****************************************************************************/
69 /* function */
70 /*****************************************************************************/
71 
72 
73 /*****************************************************************************/
74 /* Global variable definitions (declared in header file with 'extern') */
75 /*****************************************************************************/
76 /*
77 UMH_HANDLE umhInHandle;
78 UMH_HANDLE umhOutHandle;
79 */
80 
81 static stc_host_endpoint_t *EndpointOUT;
82 static stc_host_endpoint_t *EndpointIN;
83 static uint8_t u8EndpointIN, u8EndpointOUT;
85 volatile static uint8_t u8Status = 0;
86 volatile static boolean_t bSetupComplete = FALSE;
90 static stc_usbn_t* pstcUsbHandle = NULL;
91 /*SETUP_DATA setup;
92 StorageInfo_t stcGlobalStorageInfo;
93 */
94 
95 /*****************************************************************************/
96 /* Local type definitions ('typedef') */
97 /*****************************************************************************/
98 
99 /*****************************************************************************/
100 /* Local variable definitions ('static') */
101 /*****************************************************************************/
102 
103 
104 
105 /*****************************************************************************/
106 /* Local function prototypes ('static') */
107 /*****************************************************************************/
108 static void WaitForSetupComplete(void);
109 static void SetupComplete(stc_usbn_t* pstcUsb,uint8_t* pu8Buffer, uint32_t u32DataSize);
110 static void convBufferToSenseResponse(uint8_t *buffer, struct RequestSenseResponse *sr) ;
111 static void convBufferToModeSenseResponse(uint8_t *buffer, struct ModeSenseResponse *msr);
112 static void convBufferToInquiryResponse(uint8_t *buffer, InquiryResponse_t *ir);
113 static void convCBWtoBuffer(cbw_t *cbw, uint8_t *Buffer);
114 static int
116  uint8_t *buffer, // buffer that contains a status wrapper block
117  int length, // length of the buffer
118  csw_t *csw
119 );
120 /*****************************************************************************/
121 /* Function implementation - global ('extern') and local ('static') */
122 /*****************************************************************************/
123 /* Storage API */
124 int UsbHostMassStorage_StorageApiRead(BYTE *buff, DWORD sector, BYTE count)
125 {
126  unsigned int status;
127  unsigned char NAKDetected = 0;
128  pstcStorageInfo = UsbHostMassStorage_GetGlobalStorageInfo();
129  ZERO_STRUCT(*pstcStorageInfo);
130  pstcStorageInfo->STORAGE_CMD = READ_MASS_STORAGE;
131  pstcStorageInfo->BUFFER = buff;
132  pstcStorageInfo->BLOCK_START = sector;
133  pstcStorageInfo->BLOCK_CNT = count;
134 
135  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
136  if (status)
137  {
138  return status;
139  }
140  if (pstcStorageInfo->CSW_STATUS == 1)
141  {
142  struct RequestSenseResponse SenseResponse;
143  uint8_t RSRBuffer[sizeof(SenseResponse)];
144  ZERO_STRUCT(*pstcStorageInfo);
145  ZERO_STRUCT(SenseResponse);
146  pstcStorageInfo->STORAGE_CMD = MASS_STORAGE_REQUEST_SENSE;
147  pstcStorageInfo->BUFFER = RSRBuffer; //(uint8_t*) &SenseResponse;
148  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
150  convBufferToSenseResponse(RSRBuffer,&SenseResponse);
151  if(status)
152  {
153  return status;
154  }
155  status = SenseResponse.SenseKey;
156  return status;
157  }
158 
159  return status;
160 }
161 
162 int UsbHostMassStorage_StorageApiWrite(BYTE *buff, DWORD sector, BYTE count)
163 {
164  unsigned int status;
165  pstcStorageInfo = UsbHostMassStorage_GetGlobalStorageInfo();
166  ZERO_STRUCT(*pstcStorageInfo);
167  pstcStorageInfo->STORAGE_CMD = WRITE_MASS_STORAGE;
168  pstcStorageInfo->BUFFER = buff;
169  pstcStorageInfo->BLOCK_START = sector;
170  pstcStorageInfo->BLOCK_CNT = count;
171 
172  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
173 
174  if (pstcStorageInfo->CSW_STATUS == 1)
175  {
176  RequestSenseResponse_t SenseResponse;
177  uint8_t NAKDetected;
178  uint8_t RSRBuffer[sizeof(SenseResponse)];
179  ZERO_STRUCT(*pstcStorageInfo);
180  ZERO_STRUCT(SenseResponse);
181  pstcStorageInfo->STORAGE_CMD = MASS_STORAGE_REQUEST_SENSE;
182  pstcStorageInfo->BUFFER = RSRBuffer; //(unsigned char *) &SenseResponse;
183  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
184 
186  convBufferToSenseResponse(RSRBuffer,&SenseResponse);
187  if (status)
188  {
189  return status;
190  }
191 
192  if(SenseResponse.SenseKey == 0x7)
193  {
194  return 0x02; // Write protect
195  }
196 
197  status = SenseResponse.SenseKey;
198  return status;
199  }
200 
201  return status;
202 }
203 
205 {
206  unsigned int status;
207  pstcStorageInfo = UsbHostMassStorage_GetGlobalStorageInfo();
208  ZERO_STRUCT(*pstcStorageInfo);
209  pstcStorageInfo->STORAGE_CMD = READ_MASS_STORAGE_CAPACITY;
210  pstcStorageInfo->BUFFER = Buff;
211 
212  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
213 
214  if (pstcStorageInfo->CSW_STATUS == 1)
215  {
216  RequestSenseResponse_t SenseResponse;
217  uint8_t NAKDetected;
218  uint8_t RSRBuffer[sizeof(SenseResponse)];
219  ZERO_STRUCT(*pstcStorageInfo);
220  ZERO_STRUCT(SenseResponse);
221  pstcStorageInfo->STORAGE_CMD = MASS_STORAGE_REQUEST_SENSE;
222  pstcStorageInfo->BUFFER = RSRBuffer; //(unsigned char *)&SenseResponse;
223  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
224 
226  convBufferToSenseResponse(RSRBuffer,&SenseResponse);
227  if (status)
228  {
229  return status;
230  }
231  status = SenseResponse.SenseKey;
232  return status;
233  }
234 
235  return status;
236 }
237 
238 
240 {
241  unsigned char cnt=0;
242  unsigned char CheckAgain = 1;
243  unsigned char NAKDetected = 0;
245  uint8_t IRbuffer[sizeof(InquiryResponse)];
246  unsigned int status=0;
247  pstcStorageInfo = UsbHostMassStorage_GetGlobalStorageInfo();
248  ZERO_STRUCT(*pstcStorageInfo);
249  ZERO_STRUCT(InquiryResponse);
250  pstcStorageInfo->STORAGE_CMD = INQUIRY_MASS_STORAGE;
251  pstcStorageInfo->BUFFER = IRbuffer;
252  //(unsigned char *)&InquiryResponse;
253 
254  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
256  convBufferToInquiryResponse(IRbuffer,&InquiryResponse);
257 
258  if (status)
259  {
260  return status;
261  }
262 
263  while(CheckAgain)
264  {
265  if(cnt >= 2)
266  {
267  return USBH_STATUS_ERROR;
268  }
269 
270  ZERO_STRUCT(*pstcStorageInfo);
271  pstcStorageInfo->STORAGE_CMD = TEST_UNIT_READY;
272 
273  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
274 
275  if (status)
276  {
277  return status;
278  }
279 
280  if (pstcStorageInfo->CSW_STATUS == 1)
281  {
282  struct RequestSenseResponse SenseResponse;
283  uint8_t RSRBuffer[sizeof(SenseResponse)];
284  ZERO_STRUCT(*pstcStorageInfo);
285  ZERO_STRUCT(SenseResponse);
286  pstcStorageInfo->STORAGE_CMD = MASS_STORAGE_REQUEST_SENSE;
287  pstcStorageInfo->BUFFER = RSRBuffer;
288  //(uint8_t *)&SenseResponse;
289  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
291  convBufferToSenseResponse(RSRBuffer,&SenseResponse);
292  if (status)
293  {
294  return status;
295  }
296  if (SenseResponse.SenseKey != 0x06)
297  {
298  CheckAgain = 0;
299  }
300  cnt++;
301  status = SenseResponse.SenseKey;
302  }
303  else
304  {
305  CheckAgain = 0;
306  }
307  } // end of while
309  return status;
310 }
311 
313 {
314  uint16_t status;
315  struct ModeSenseResponse ModeSense;
316  uint8_t MSRBuffer[sizeof(ModeSense)];
317  uint8_t NAKDetected;
318 
319  pstcStorageInfo = UsbHostMassStorage_GetGlobalStorageInfo();
320  ZERO_STRUCT(*pstcStorageInfo);
321  ZERO_STRUCT(ModeSense);
322  pstcStorageInfo->STORAGE_CMD = MODE_SENSE;
323  pstcStorageInfo->BUFFER = MSRBuffer; //(uint8_t*)&ModeSense;
324  status = UsbHostMassStorage_StorageCommandSend(pstcStorageInfo);
326  convBufferToModeSenseResponse(MSRBuffer,&ModeSense);
327  if (ModeSense.WriteProtect == 1)
328  {
329  return USBH_STATUS_ERROR;
330  }
331  return status;
332 }
333 
334 /* End of Storage API */
335 
336 boolean_t UsbHostMassStorage_InitHandler(stc_usbn_t* pstcUsb, uint8_t* pu8Configuration, uint32_t u32Length)
337 {
338  uint8_t* pu8Buffer;
339  uint8_t u8NumberOfInterfaces;
340  uint8_t u8NumberOfEndpoints;
341  uint8_t u8EndpointNumber;
342  uint8_t u8InterfaceNumber;
343 
344  if (pstcUsbHandle != NULL)
345  {
346  return FALSE;
347  }
348  pstcUsbHandle = pstcUsb;
349 
350  u8NumberOfInterfaces=pu8Configuration[4];
351  for(u8InterfaceNumber=0;u8InterfaceNumber < u8NumberOfInterfaces;u8InterfaceNumber++)
352  {
353 
354  pu8Buffer = UsbHost_GetUsbInterfaceDescriptor(pu8Configuration,u32Length,u8InterfaceNumber);
355  if (pu8Buffer == 0)
356  {
357 
358  return FALSE;
359  }
360  u8NumberOfEndpoints = pu8Buffer[4];
361 
362  if ((pu8Buffer[6] == 0x06) && (pu8Buffer[7] == 0x50)) //SubClass & Protocol
363  {
364  for(u8EndpointNumber=1;u8EndpointNumber<=u8NumberOfEndpoints;u8EndpointNumber++)
365  {
366 
367  pu8Buffer = UsbHost_GetUsbEndpointDescriptor(pu8Configuration,u32Length,u8InterfaceNumber,u8EndpointNumber);
368  if (pu8Buffer[2] & USB_IN_DIRECTION)
369  {
370  u8EndpointIN = pu8Buffer[2];
371  u16MaxPackageSizeIN = (uint16_t)(pu8Buffer[4] + (pu8Buffer[5] << 8));
372  }
373  else
374  {
375  u8EndpointOUT = pu8Buffer[2];
376  u16MaxPackageSizeOUT = (uint16_t)(pu8Buffer[4] + (pu8Buffer[5] << 8));
377  }
378  if ((u8EndpointIN != 0) && (u8EndpointOUT != 0))
379  {
380  break;
381  }
382  }
383  if ((u8EndpointIN == 0) && (u8EndpointOUT == 0))
384  {
385 
386  return FALSE;
387  }
388 
389 
390  UsbHost_AddHostEndpoint(pstcUsbHandle,&EndpointOUT,u8EndpointOUT,u16MaxPackageSizeOUT,0,0);
391  UsbHost_AddHostEndpoint(pstcUsbHandle,&EndpointIN,u8EndpointIN,u16MaxPackageSizeIN,0,0);
392 
393 
394  break;
395  }
396  }
397  if ((u8EndpointIN == 0) || (u8EndpointOUT == 0))
398  {
399  return FALSE;
400  }
401  return TRUE;
402 }
403 
405 {
406  pstcUsbHandle = NULL;
407  return TRUE;
408 }
409 
410 static void SetupComplete(stc_usbn_t* pstcUsb,uint8_t* pu8Buffer, uint32_t u32DataSize)
411 {
413 }
414 
415 static void WaitForSetupComplete()
416 {
417  volatile uint32_t u32Timeout = 1000000;
418  while((bSetupComplete == FALSE) && (u32Timeout > 5))
419  {
420  u32Timeout--;
421  }
422 }
424 {
425  //SwapBuffer(EndpointIN->pu8Buffer,EndpointIN->u32DataSize);
426  stcGlobalStorageInfo.STATUS = ((EndpointIN->u8Status) & 0xF0);
427  stcGlobalStorageInfo.BYTES_TRANSFERRED = EndpointIN->u32DataSize;
429 }
430 
432 {
433  stcGlobalStorageInfo.STATUS = ((EndpointOUT->u8Status) & 0xF0);
434  stcGlobalStorageInfo.BYTES_TRANSFERRED = EndpointOUT->u32DataSize;
436 }
437 
438 // transfer done is called from the transfer completion function
440 {
442 }
443 
445 {
447 }
448 
449 
450 
452 {
453  return &stcGlobalStorageInfo;
454 }
455 
456 
457 
461 uint8_t CswBuffer[13];
462 uint8_t CbwBuffer[31];
463 
464 static void convBufferToSenseResponse(uint8_t *buffer, struct RequestSenseResponse *sr)
465 {
466  sr->ResponseCode = ((buffer[0] & 0xFE) >> 1);
467  sr->VALID = (buffer[0] & 0x01);
468  sr->Obsolete = buffer[1];
469  sr->SenseKey = ((buffer[2] & 0xF0) >> 4);
470  sr->ILI = ((buffer[2] & 0x04) >> 2);
471  sr->EOM = ((buffer[2] & 0x02) >> 1);
472  sr->FILEMARK = (buffer[2] & 0x01);
473  sr->Information = LENDIAN_BUFFER_TO_U32(buffer+3);
474  sr->AddSenseLen = buffer[7];
475  sr->CmdSpecificInfo = LENDIAN_BUFFER_TO_U32(buffer+8);
476  sr->ASC = buffer[12];
477  sr->ASCQ = buffer[13];
478  sr->FRUC = buffer[14];
479  sr->SenseKeySpecific[0] = buffer[15];
480  sr->SenseKeySpecific[1] = buffer[16];
481  sr->SenseKeySpecific[2] = buffer[17];
482 }
483 static void convBufferToModeSenseResponse(uint8_t *buffer, struct ModeSenseResponse *msr) {
484  msr->ModeDataLength = buffer[0];
485  msr->MediumType = buffer[1];
486  msr->Reserved1 = ((buffer[2] & 0xF0) >> 4);
487  msr->DPOFUA = ((buffer[2] & 0x08) >> 3);
488  msr->Reserved2 = ((buffer[2] & 0x06) >> 1);
489  msr->WriteProtect = (buffer[2] & 0x01);
490  msr->BlockDescriptorLength = buffer[3];
491 }
492 static void convBufferToInquiryResponse(uint8_t *buffer, InquiryResponse_t *ir) {
493  unsigned char i;
494  ir->Peripheral = buffer[0];
495  ir->Removable = buffer[1];
496  ir->Version = buffer[2];
497  ir->ResponseDataFormat = buffer[3];
498  ir->AdditionalLength = buffer[4];
499  ir->Sccstp = buffer[5];
500  ir->Bqueetc = buffer[6];
501  ir->CmdQue = buffer[7];
502  for(i=0;i<8;i++)
503  {
504  ir->VendorID[i] = buffer[8+i];
505  }
506  for(i=0;i<16;i++)
507  {
508  ir->ProductID[i] = buffer[16+i];
509  }
510 }
511 static void convCBWtoBuffer(cbw_t *cbw, uint8_t *Buffer)
512 {
513  unsigned char i;
514  U32_TO_LENDIAN_BUFFER(Buffer,cbw->dCBWSignature); // index 0:Signature
515  U32_TO_LENDIAN_BUFFER(Buffer+4,cbw->dCBWTag); // index:4 Tag
516  U32_TO_LENDIAN_BUFFER(Buffer+8,cbw->dCBWDataTransferLength); // index:8 DataTransferLength
517  Buffer[12]=cbw->bCBWFlags;
518  Buffer[13]=cbw->bCBWLUN;
519  Buffer[14]=cbw->bCBWCBLength;
520  for(i=0;i<16;i++)
521  {
522  Buffer[15 + i] = cbw->CBWCB[i];
523  }
524 }
525 
526 static int
528  uint8_t *buffer, // buffer that contains a status wrapper block
529  int length, // length of the buffer
530  csw_t *csw
531 )
532 {
533  if( length < 13)
534  {
535  return USBH_STATUS_LENGTH;
536  }
537  csw->CSWSignatur=LENDIAN_BUFFER_TO_U32(buffer);
538  csw->CSWTag=LENDIAN_BUFFER_TO_U32(buffer+4); // 4: tag Same as original command
539  csw->CSWDataResidue=LENDIAN_BUFFER_TO_U32(buffer+8);// 8: residue, amount of bytes not transferred
540  csw->CSWStatus=(uint32_t)*(buffer+12);//12:status
541  return USBH_STATUS_SUCCESS;
542 }
543 
544 
545 static void ErrorHandler(uint16_t errCode, uint8_t *clrEponly )
546 {
547  //uint8_t InEpStatus=0, OutEpStatus=0;
548  if (errCode & USBHOST_ENDPOINTSTATUS_STALL)
549  {
550 
551  if(*clrEponly)
552  {
553  //UsbHostMassStorage_GetEPStatus(&InEpStatus, &OutEpStatus);
554  if((EndpointIN->u8Status) & USBHOST_ENDPOINTSTATUS_STALL)
555  {
556 
559  EndpointIN->u8Status &= ~USBHOST_ENDPOINTSTATUS_STALL;
560  }
561  if((EndpointOUT->u8Status) & USBHOST_ENDPOINTSTATUS_STALL)
562  {
564  EndpointOUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_STALL;
565  }
566  *clrEponly=0;
567  }
568  return;
569  //UsbHostMassStorage_ResetRecovery();
570  }
572 }
573 
574 void UsbHostMassStorage_CheckForCompletion(volatile uint8_t *NAKDetected)
575 {
576  volatile uint32_t u32Timeout = 20000000;
577  *NAKDetected = 0;
578 
579  while((IS_SET(u8Status,USBMASSSTORAGE_FLAG_TRANSFERDONE_OUT)) || (IS_SET(u8Status,USBMASSSTORAGE_FLAG_TRANSFERDONE_IN))) // Wait for status information of previous transmission
580  {
581  if ((u32Timeout < 5) || (UsbHost_GetDeviceStatus(pstcUsbHandle) == USBHOST_DEVICE_IDLE)) // if there is no Handshake, something went wrong!
582  {
585  EndpointOUT->bAbortTransfer = TRUE;
586  EndpointIN->bAbortTransfer = TRUE;
587  *NAKDetected = 1; // There must be a circular NAK Handshake
588  u32Timeout = 1000000;
589  while((EndpointIN->bAbortTransfer) && (EndpointOUT->bAbortTransfer) && (u32Timeout > 5) && (UsbHost_GetDeviceStatus(pstcUsbHandle) != USBHOST_DEVICE_IDLE)) u32Timeout--;
590 
591  }
592  u32Timeout--;
593  }
594 
595 }
596 
597 
598 
600 {
601  uint16_t status;
602  volatile uint8_t NAKDetected=0;
603  uint8_t clrEponly=0;
604  uint16_t cnt=0;
605  cnt = cnt;
606 
608  SET_MASK(u8Status,USBMASSSTORAGE_FLAG_TRANSFERDONE_OUT); // Block transfer
609  status = UsbHostMassStorage_SendCBW(pstcStorageInfo);
610  if(status)
611  {
612  return status;
613  }
615  if(pstcStorageInfo->STATUS || NAKDetected)
616  {
617  ErrorHandler(pstcStorageInfo->STATUS, &clrEponly );
618  return USBH_STATUS_ERROR;
619  }
620 
621  if(cbw.dCBWDataTransferLength != 0) // Jump in, if there is a data Transfer
622  {
623 
624  if(pstcStorageInfo->STORAGE_CMD == WRITE_MASS_STORAGE)
625  {
627  }
628  else
629  {
631  }
632  status = UsbHostMassStorage_Transmit_Data(pstcStorageInfo);
633  if(status)
634  {
635  return status;
636  }
638 
639  if((pstcStorageInfo->STATUS) || (NAKDetected))
640  {
641  clrEponly=1;
642  ErrorHandler(pstcStorageInfo->STATUS, &clrEponly) ;
643  NAKDetected=0;
644  }
645  }
647  status = UsbHostMassStorage_GetCSW(pstcStorageInfo);
648  if(status)
649  {
650  return status;
651  }
652 
654 
655  if((pstcStorageInfo->STATUS) || (NAKDetected))
656  {
657 
658  clrEponly=1;
659  ErrorHandler(pstcStorageInfo->STATUS, &clrEponly);
660  NAKDetected=0;
661  status = UsbHostMassStorage_GetCSW(pstcStorageInfo);
662  if(status)
663  {
664  return status;
665  }
667  if(pstcStorageInfo->STATUS || NAKDetected)
668  {
669  ErrorHandler(pstcStorageInfo->STATUS, &clrEponly);
670  return USBH_STATUS_ERROR;
671  }
672  }
673  status = UsbHostMassStorage_CheckCSW(pstcStorageInfo);
674  return status;
675 }
676 
677 int16_t UsbHostMassStorage_SendCBW(StorageInfo_t *pstcStorageInfo)
678 {
679  uint16_t status;
680  ZERO_STRUCT(cbw);
681  pstcStorageInfo->SEND_CBW = 1;
682  pstcStorageInfo->BUFFER_SIZE = 31; // CBW Size must always be 31 byte
683 
684  cbw.dCBWSignature = 0x43425355;
685  cbw.dCBWTag = CBW_TAG;
686  cbw.bCBWLUN = 0x00;
687  cbw.CBWCB[1] = 0x00;
688  cbw.CBWCB[2] = (0xff & (pstcStorageInfo->BLOCK_START >> 24));
689  cbw.CBWCB[3] = (0xff & (pstcStorageInfo->BLOCK_START >> 16));
690  cbw.CBWCB[4] = (0xff & (pstcStorageInfo->BLOCK_START >> 8));
691  cbw.CBWCB[5] = (0xff & (pstcStorageInfo->BLOCK_START));
692  cbw.CBWCB[6] = 0x00;
693  cbw.CBWCB[7] = (uint8_t)(((uint16_t)(pstcStorageInfo->BLOCK_CNT) >> 8) & 0xff);
694  cbw.CBWCB[8] = (uint8_t)(((uint16_t)(pstcStorageInfo->BLOCK_CNT) & 0xff));
695  cbw.CBWCB[9] = 0x00;
696 
697  switch(pstcStorageInfo->STORAGE_CMD)
698  {
699  case READ_MASS_STORAGE:
700  cbw.CBWCB[0] = pstcStorageInfo->STORAGE_CMD;
701  cbw.dCBWDataTransferLength = 0x00000200;
702  cbw.bCBWFlags = 0x80;
703  cbw.bCBWCBLength = 0x0A;
704  break;
705 
706  case WRITE_MASS_STORAGE:
707  cbw.CBWCB[0] = pstcStorageInfo->STORAGE_CMD;
708  cbw.dCBWDataTransferLength = 0x00000200;
709  cbw.bCBWFlags = 0x00;
710  cbw.bCBWCBLength = 0x0A;
711  break;
712 
714  cbw.CBWCB[0] = pstcStorageInfo->STORAGE_CMD;
715  cbw.dCBWDataTransferLength = 0x00000024;
716  cbw.bCBWFlags = 0x80;
717  cbw.bCBWCBLength = 0x06;
718  cbw.CBWCB[4] = 0x24;
719  break;
720 
722  cbw.CBWCB[0] = pstcStorageInfo->STORAGE_CMD;
723  cbw.dCBWDataTransferLength = 0x00000008;
724  cbw.bCBWFlags = 0x80;
725  cbw.bCBWCBLength = 0x0A ;
726  break;
727 
728  case TEST_UNIT_READY:
729  cbw.CBWCB[0] = pstcStorageInfo->STORAGE_CMD;
730  cbw.dCBWDataTransferLength = 0x00000000;
731  cbw.bCBWFlags = 0x00;
732  cbw.bCBWCBLength = 0x06;
733  break;
734 
736  cbw.CBWCB[0] = pstcStorageInfo->STORAGE_CMD;
737  cbw.dCBWDataTransferLength = 0x00000012;
738  cbw.bCBWFlags = 0x80;
739  cbw.bCBWCBLength = 0x0C;
740  cbw.CBWCB[4] = 0x12;
741  break;
742 
743  case SEND_DIAGNOSTIC:
744  cbw.CBWCB[0] = pstcStorageInfo->STORAGE_CMD;
745  cbw.CBWCB[1] = 0x04;
746  cbw.dCBWDataTransferLength = 0x00000000;
747  cbw.bCBWCBLength = 0x0A;
748  break;
749 
750  case MODE_SENSE:
751  cbw.CBWCB[0] = pstcStorageInfo->STORAGE_CMD;
752  cbw.dCBWDataTransferLength = 0x00000004;
753  cbw.bCBWFlags = 0x80;
754  cbw.bCBWCBLength = 0x06;
755  cbw.CBWCB[2] = 0x3F;
756  cbw.CBWCB[4] = 0x04;
757  break;
758  }
759  #if (ENDIANTYPE == LITTLEENDIAN)
760  status = UsbHostMassStorage_Transmit_MSDC_Data((uint8_t*)&cbw, pstcStorageInfo);
761  #else
762  convCBWtoBuffer(&cbw,CbwBuffer);
763  status = UsbHostMassStorage_Transmit_MSDC_Data((uint8_t*)CbwBuffer, pstcStorageInfo);
764  if (status)
765  {
766  //status = UsbHostMassStorage_Transmit_MSDC_Data((uint8_t*)CbwBuffer, pstcStorageInfo);
767  }
768 
769  #endif
770  pstcStorageInfo->SEND_CBW = 0;
771  return status;
772 
773 }
774 
775 int16_t UsbHostMassStorage_Transmit_MSDC_Data(uint8_t *pu8Buffer, StorageInfo_t *pstcStorageInfo)
776 {
777  if (pstcStorageInfo == 0)
778  {
779 
780  return 2;
781  }
782  //SwapBuffer(pu8Buffer,pstcStorageInfo->BUFFER_SIZE);
783  if((EndpointOUT != 0) && (EndpointIN != 0))
784  {
785 
786  if(pstcStorageInfo->SEND_CBW)
787  {
788 
789  UsbHost_TransferData(pstcUsbHandle,EndpointOUT,pu8Buffer, pstcStorageInfo->BUFFER_SIZE, UsbHostMassStorage_BulkOutCompletion);
790  return USBH_STATUS_SUCCESS;
791  }
792 
793  if(pstcStorageInfo->GET_CSW)
794  {
795 
796  UsbHost_TransferData(pstcUsbHandle,EndpointIN,pu8Buffer, pstcStorageInfo->BUFFER_SIZE, UsbHostMassStorage_BulkInCompletion);
797  return USBH_STATUS_SUCCESS;
798  }
799 
800  switch(pstcStorageInfo->STORAGE_CMD)
801  {
802 
803  case WRITE_MASS_STORAGE:
804  case TEST_UNIT_READY:
805 
806  UsbHost_TransferData(pstcUsbHandle,EndpointOUT,pu8Buffer, pstcStorageInfo->BUFFER_SIZE, UsbHostMassStorage_BulkOutCompletion);
807  break;
808 
809 
811  case MODE_SENSE:
813  case INQUIRY_MASS_STORAGE:
814  case READ_MASS_STORAGE:
815 
816  UsbHost_TransferData(pstcUsbHandle,EndpointIN,pu8Buffer, pstcStorageInfo->BUFFER_SIZE, UsbHostMassStorage_BulkInCompletion);
817  break;
818  }
819  return USBH_STATUS_SUCCESS;
820  }
821 
822 
823  return USBH_STATUS_ERROR;
824 
825 }
826 
828 {
829  uint16_t status;
830  pstcStorageInfo->BUFFER_SIZE = cbw.dCBWDataTransferLength;
831 
832  status = UsbHostMassStorage_Transmit_MSDC_Data(pstcStorageInfo->BUFFER, pstcStorageInfo);
833  return status;
834 }
835 
836 int16_t UsbHostMassStorage_GetCSW(StorageInfo_t *pstcStorageInfo)
837 {
838  uint16_t status;
839  volatile uint8_t NAKDetected=0;
840  ZERO_STRUCT(csw);
841  pstcStorageInfo->GET_CSW = 1;
842  pstcStorageInfo->BUFFER_SIZE = 13; //CBW Size must always be 13 byte
843  #if (ENDIANTYPE == LITTLEENDIAN)
844  status = UsbHostMassStorage_Transmit_MSDC_Data((uint8_t*)&csw, pstcStorageInfo);
845  #else
846  status = UsbHostMassStorage_Transmit_MSDC_Data((uint8_t*)CswBuffer, pstcStorageInfo);
848  if ((status == 0) && (NAKDetected == 1))
849  {
850  status = 3;
851  }
852  if (!status)
853  {
854  status = convBufferToCSW(CswBuffer,13,&csw);
855  }
856  #endif
857  pstcStorageInfo->GET_CSW = 0;
858  return status;
859 }
860 
862 { // Check if CSW is valid
863  uint8_t clrEponly=0;
864 
865  if((csw.CSWTag != CBW_TAG) || (csw.CSWSignatur != 0x53425355) ||((pstcStorageInfo->BYTES_TRANSFERRED) != 13 ))
866  {
867  ErrorHandler((uint16_t)NULL, &clrEponly);
868  return USBH_STATUS_ERROR;
869  } // Check if CSW is meaningful
870  if((csw.CSWStatus == 2) || (csw.CSWDataResidue >> cbw.dCBWDataTransferLength))
871  {
872 
873  ErrorHandler((uint16_t)NULL, &clrEponly);
874  return USBH_STATUS_ERROR;
875  }
876  if(csw.CSWStatus == 1)
877  {
878  pstcStorageInfo->CSW_STATUS = 1;
879  }
880  return USBH_STATUS_SUCCESS;
881 }
882 
883 
885 {
886  ZERO_STRUCT(setup);
887  setup.bmRequestType=0x21;
888  setup.bRequest=0xFF;
889  setup.wValue=0;
890  setup.wIndex=0;
891  setup.wLength=0;
892  UsbHost_SetupRequest(pstcUsbHandle,&setup,SetupComplete);
894  return USBH_STATUS_SUCCESS;
895 }
896 
898 {
899  if( UsbHost_GetDeviceStatus(pstcUsbHandle) == USBHOST_DEVICE_CONFIGURED)
900  {
901 
902  return 0;
903  }
904  else
905  {
906 
907  return 1;
908  }
909 }
910 
912 {
913  uint16_t status;
917  status = UsbHostMassStorage_TestUnitReady(&stcGlobalStorageInfo);
918  return status;
919 
920 }
921 
922 
924 {
925  UsbHost_ClearEndpointPoll(pstcUsbHandle, EndpointIN);
926  /*
927  ZERO_STRUCT(setup);
928  setup.bmRequestType=0x02;
929  setup.bRequest=0x01;
930  setup.wValue=0x00;
931  setup.wIndex = USB_IN_DIRECTION | u8EndpointIN;
932  setup.wLength=0x00;
933  //UMH_AbortTransfer(umhInHandle); TBD
934  UsbHost_SetupRequest(pstcUsbHandle,&setup,SetupComplete);
935  WaitForSetupComplete();
936  //UMH_ResetDataToggleBit(umhInHandle); TBD*/
937  return USBH_STATUS_SUCCESS;
938 }
939 
941 {
942  UsbHost_ClearEndpointPoll(pstcUsbHandle, EndpointOUT);
943  /*
944  ZERO_STRUCT(setup);
945  setup.bmRequestType=0x02;
946  setup.bRequest=0x01;
947  setup.wValue=0x00;
948  setup.wIndex=USB_OUT_DIRECTION | u8EndpointOUT;
949  setup.wLength=0x00;
950  //UMH_AbortTransfer(umhOutHandle); TBD
951  UsbHost_SetupRequest(pstcUsbHandle,&setup,SetupComplete);
952  WaitForSetupComplete();
953  //UMH_ResetDataToggleBit(umhOutHandle); TBD */
954  return USBH_STATUS_SUCCESS;
955 }
956 
958 {
959  uint16_t status;
960  ZERO_STRUCT(pstcStorageInfo);
961  pstcStorageInfo->STORAGE_CMD = TEST_UNIT_READY;
962 
963  status = UsbHostMassStorage_SendCBW(pstcStorageInfo);
964  //UMH_StallExecutionUserCallback(200);
965  UsbHostMassStorage_GetCSW(pstcStorageInfo);
966 
967  return status;
968 }
969 
970 /*
971 void UsbHostMassStorage_GetEPStatus(uint8_t* InEpStatus, uint8_t* OutEpStatus)
972 {
973  SETUP_DATA setup;
974  int16_t status;
975  uint16_t transf=0;
976 
977  ZERO_STRUCT(setup);
978  setup.Bytes.Type=0x82;
979  setup.Bytes.Request=0x00;
980  setup.Words.Value=0x00;
981  setup.Words.Index=USB_IN_DIRECTION | u8EndpointIN;
982  setup.Words.Length=0x02;
983  status = UMH_SetupRequest((uint8_t*)&setup,(uint8_t*)InEpStatus,(unsigned int*)&transf);
984 
985  ZERO_STRUCT(setup);
986  setup.Bytes.Type=0x82;
987  setup.Bytes.Request=0x00;
988  setup.Words.Value=0x00;
989  setup.Words.Index=USB_OUT_DIRECTION | u8EndpointOUT;
990  setup.Words.Length=0x02;
991  status = UMH_SetupRequest((uint8_t*)&setup,(uint8_t*)OutEpStatus,(unsigned int*)&transf);
992 }
993 */
994 
995 // max. execution time is 100us
996 /*static void Idle(void)
997 {
998  return;
999 }*/
1000 
1001 // this function is called from the library to wait
1002 // a specified time. This function must be implemented by the user
1003 // with the fixed function name UmhStallExecutionCallback.
1004 // Return: nothing
1005 // parameter: time in 0.1 milliseconds units
1006 
1007 /*void UMH_StallExecutionUserCallback(unsigned long time)
1008 {
1009  unsigned long startTime=GetCurrentTime();
1010 
1011  while(1)
1012  {
1013  if( IsTimeOver(time,startTime) )
1014  break;
1015  Idle();
1016  }
1017 }*/
1018 
1020 {
1021  return (pstcUsbHandle != NULL);
1022 }
1023 
1024 #endif /* (USE_USB_HOST == 1) */