for rex

Dependencies:   FatFileSystem mbed

Fork of SnakeGame by Pujun Bhatnagar

Committer:
superlova
Date:
Sat Aug 08 09:32:56 2015 +0000
Revision:
1:4222a8f9ca88
for rexry

Who changed what in which revision?

UserRevisionLine numberNew contents of line
superlova 1:4222a8f9ca88 1 /*
superlova 1:4222a8f9ca88 2 **************************************************************************************************************
superlova 1:4222a8f9ca88 3 * NXP USB Host Stack
superlova 1:4222a8f9ca88 4 *
superlova 1:4222a8f9ca88 5 * (c) Copyright 2008, NXP SemiConductors
superlova 1:4222a8f9ca88 6 * (c) Copyright 2008, OnChip Technologies LLC
superlova 1:4222a8f9ca88 7 * All Rights Reserved
superlova 1:4222a8f9ca88 8 *
superlova 1:4222a8f9ca88 9 * www.nxp.com
superlova 1:4222a8f9ca88 10 * www.onchiptech.com
superlova 1:4222a8f9ca88 11 *
superlova 1:4222a8f9ca88 12 * File : usbhost_ms.c
superlova 1:4222a8f9ca88 13 * Programmer(s) : Ravikanth.P
superlova 1:4222a8f9ca88 14 * Version :
superlova 1:4222a8f9ca88 15 *
superlova 1:4222a8f9ca88 16 **************************************************************************************************************
superlova 1:4222a8f9ca88 17 */
superlova 1:4222a8f9ca88 18
superlova 1:4222a8f9ca88 19 /*
superlova 1:4222a8f9ca88 20 **************************************************************************************************************
superlova 1:4222a8f9ca88 21 * INCLUDE HEADER FILES
superlova 1:4222a8f9ca88 22 **************************************************************************************************************
superlova 1:4222a8f9ca88 23 */
superlova 1:4222a8f9ca88 24
superlova 1:4222a8f9ca88 25 #include "usbhost_ms.h"
superlova 1:4222a8f9ca88 26
superlova 1:4222a8f9ca88 27 /*
superlova 1:4222a8f9ca88 28 **************************************************************************************************************
superlova 1:4222a8f9ca88 29 * GLOBAL VARIABLES
superlova 1:4222a8f9ca88 30 **************************************************************************************************************
superlova 1:4222a8f9ca88 31 */
superlova 1:4222a8f9ca88 32
superlova 1:4222a8f9ca88 33 USB_INT32U MS_BlkSize;
superlova 1:4222a8f9ca88 34
superlova 1:4222a8f9ca88 35 /*
superlova 1:4222a8f9ca88 36 **************************************************************************************************************
superlova 1:4222a8f9ca88 37 * INITIALIZE MASS STORAGE INTERFACE
superlova 1:4222a8f9ca88 38 *
superlova 1:4222a8f9ca88 39 * Description: This function initializes the mass storage interface
superlova 1:4222a8f9ca88 40 *
superlova 1:4222a8f9ca88 41 * Arguments : None
superlova 1:4222a8f9ca88 42 *
superlova 1:4222a8f9ca88 43 * Returns : OK if Success
superlova 1:4222a8f9ca88 44 * ERR_INVALID_BOOTSIG if Failed
superlova 1:4222a8f9ca88 45 *
superlova 1:4222a8f9ca88 46 **************************************************************************************************************
superlova 1:4222a8f9ca88 47 */
superlova 1:4222a8f9ca88 48
superlova 1:4222a8f9ca88 49 USB_INT32S MS_Init (USB_INT32U *blkSize, USB_INT32U *numBlks, USB_INT08U *inquiryResult)
superlova 1:4222a8f9ca88 50 {
superlova 1:4222a8f9ca88 51 USB_INT08U retry;
superlova 1:4222a8f9ca88 52 USB_INT32S rc;
superlova 1:4222a8f9ca88 53
superlova 1:4222a8f9ca88 54 MS_GetMaxLUN(); /* Get maximum logical unit number */
superlova 1:4222a8f9ca88 55 retry = 80;
superlova 1:4222a8f9ca88 56 while(retry) {
superlova 1:4222a8f9ca88 57 rc = MS_TestUnitReady(); /* Test whether the unit is ready */
superlova 1:4222a8f9ca88 58 if (rc == OK) {
superlova 1:4222a8f9ca88 59 break;
superlova 1:4222a8f9ca88 60 }
superlova 1:4222a8f9ca88 61 MS_GetSenseInfo(); /* Get sense information */
superlova 1:4222a8f9ca88 62 retry--;
superlova 1:4222a8f9ca88 63 }
superlova 1:4222a8f9ca88 64 if (rc != OK) {
superlova 1:4222a8f9ca88 65 PRINT_Err(rc);
superlova 1:4222a8f9ca88 66 return (rc);
superlova 1:4222a8f9ca88 67 }
superlova 1:4222a8f9ca88 68 rc = MS_ReadCapacity(numBlks, blkSize); /* Read capacity of the disk */
superlova 1:4222a8f9ca88 69 MS_BlkSize = *blkSize; // Set global
superlova 1:4222a8f9ca88 70 rc = MS_Inquire (inquiryResult);
superlova 1:4222a8f9ca88 71 return (rc);
superlova 1:4222a8f9ca88 72 }
superlova 1:4222a8f9ca88 73 /*
superlova 1:4222a8f9ca88 74 **************************************************************************************************************
superlova 1:4222a8f9ca88 75 * PARSE THE CONFIGURATION
superlova 1:4222a8f9ca88 76 *
superlova 1:4222a8f9ca88 77 * Description: This function is used to parse the configuration
superlova 1:4222a8f9ca88 78 *
superlova 1:4222a8f9ca88 79 * Arguments : None
superlova 1:4222a8f9ca88 80 *
superlova 1:4222a8f9ca88 81 * Returns : OK if Success
superlova 1:4222a8f9ca88 82 * ERR_INVALID_BOOTSIG if Failed
superlova 1:4222a8f9ca88 83 *
superlova 1:4222a8f9ca88 84 **************************************************************************************************************
superlova 1:4222a8f9ca88 85 */
superlova 1:4222a8f9ca88 86
superlova 1:4222a8f9ca88 87 USB_INT32S MS_ParseConfiguration (void)
superlova 1:4222a8f9ca88 88 {
superlova 1:4222a8f9ca88 89 volatile USB_INT08U *desc_ptr;
superlova 1:4222a8f9ca88 90 USB_INT08U ms_int_found;
superlova 1:4222a8f9ca88 91
superlova 1:4222a8f9ca88 92
superlova 1:4222a8f9ca88 93 desc_ptr = TDBuffer;
superlova 1:4222a8f9ca88 94 ms_int_found = 0;
superlova 1:4222a8f9ca88 95
superlova 1:4222a8f9ca88 96 if (desc_ptr[1] != USB_DESCRIPTOR_TYPE_CONFIGURATION) {
superlova 1:4222a8f9ca88 97 return (ERR_BAD_CONFIGURATION);
superlova 1:4222a8f9ca88 98 }
superlova 1:4222a8f9ca88 99 desc_ptr += desc_ptr[0];
superlova 1:4222a8f9ca88 100
superlova 1:4222a8f9ca88 101 while (desc_ptr != TDBuffer + ReadLE16U(&TDBuffer[2])) {
superlova 1:4222a8f9ca88 102
superlova 1:4222a8f9ca88 103 switch (desc_ptr[1]) {
superlova 1:4222a8f9ca88 104
superlova 1:4222a8f9ca88 105 case USB_DESCRIPTOR_TYPE_INTERFACE: /* If it is an interface descriptor */
superlova 1:4222a8f9ca88 106 if (desc_ptr[5] == MASS_STORAGE_CLASS && /* check if the class is mass storage */
superlova 1:4222a8f9ca88 107 desc_ptr[6] == MASS_STORAGE_SUBCLASS_SCSI && /* check if the subclass is SCSI */
superlova 1:4222a8f9ca88 108 desc_ptr[7] == MASS_STORAGE_PROTOCOL_BO) { /* check if the protocol is Bulk only */
superlova 1:4222a8f9ca88 109 ms_int_found = 1;
superlova 1:4222a8f9ca88 110 desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
superlova 1:4222a8f9ca88 111 }
superlova 1:4222a8f9ca88 112 break;
superlova 1:4222a8f9ca88 113
superlova 1:4222a8f9ca88 114 case USB_DESCRIPTOR_TYPE_ENDPOINT: /* If it is an endpoint descriptor */
superlova 1:4222a8f9ca88 115 if ((desc_ptr[3] & 0x03) == 0x02) { /* If it is Bulk endpoint */
superlova 1:4222a8f9ca88 116 if (desc_ptr[2] & 0x80) { /* If it is In endpoint */
superlova 1:4222a8f9ca88 117 EDBulkIn->Control = 1 | /* USB address */
superlova 1:4222a8f9ca88 118 ((desc_ptr[2] & 0x7F) << 7) | /* Endpoint address */
superlova 1:4222a8f9ca88 119 (2 << 11) | /* direction */
superlova 1:4222a8f9ca88 120 (ReadLE16U(&desc_ptr[4]) << 16); /* MaxPkt Size */
superlova 1:4222a8f9ca88 121 desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
superlova 1:4222a8f9ca88 122 } else { /* If it is Out endpoint */
superlova 1:4222a8f9ca88 123 EDBulkOut->Control = 1 | /* USB address */
superlova 1:4222a8f9ca88 124 ((desc_ptr[2] & 0x7F) << 7) | /* Endpoint address */
superlova 1:4222a8f9ca88 125 (1 << 11) | /* direction */
superlova 1:4222a8f9ca88 126 (ReadLE16U(&desc_ptr[4]) << 16); /* MaxPkt Size */
superlova 1:4222a8f9ca88 127 desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
superlova 1:4222a8f9ca88 128 }
superlova 1:4222a8f9ca88 129 } else { /* If it is not bulk end point */
superlova 1:4222a8f9ca88 130 desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
superlova 1:4222a8f9ca88 131 }
superlova 1:4222a8f9ca88 132 break;
superlova 1:4222a8f9ca88 133
superlova 1:4222a8f9ca88 134 default: /* If the descriptor is neither interface nor endpoint */
superlova 1:4222a8f9ca88 135 desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
superlova 1:4222a8f9ca88 136 break;
superlova 1:4222a8f9ca88 137 }
superlova 1:4222a8f9ca88 138 }
superlova 1:4222a8f9ca88 139 if (ms_int_found) {
superlova 1:4222a8f9ca88 140 PRINT_Log("Mass Storage device connected\n");
superlova 1:4222a8f9ca88 141 return (OK);
superlova 1:4222a8f9ca88 142 } else {
superlova 1:4222a8f9ca88 143 PRINT_Log("Not a Mass Storage device\n");
superlova 1:4222a8f9ca88 144 return (ERR_NO_MS_INTERFACE);
superlova 1:4222a8f9ca88 145 }
superlova 1:4222a8f9ca88 146 }
superlova 1:4222a8f9ca88 147
superlova 1:4222a8f9ca88 148 /*
superlova 1:4222a8f9ca88 149 **************************************************************************************************************
superlova 1:4222a8f9ca88 150 * GET MAXIMUM LOGICAL UNIT
superlova 1:4222a8f9ca88 151 *
superlova 1:4222a8f9ca88 152 * Description: This function returns the maximum logical unit from the device
superlova 1:4222a8f9ca88 153 *
superlova 1:4222a8f9ca88 154 * Arguments : None
superlova 1:4222a8f9ca88 155 *
superlova 1:4222a8f9ca88 156 * Returns : OK if Success
superlova 1:4222a8f9ca88 157 * ERR_INVALID_BOOTSIG if Failed
superlova 1:4222a8f9ca88 158 *
superlova 1:4222a8f9ca88 159 **************************************************************************************************************
superlova 1:4222a8f9ca88 160 */
superlova 1:4222a8f9ca88 161
superlova 1:4222a8f9ca88 162 USB_INT32S MS_GetMaxLUN (void)
superlova 1:4222a8f9ca88 163 {
superlova 1:4222a8f9ca88 164 USB_INT32S rc;
superlova 1:4222a8f9ca88 165
superlova 1:4222a8f9ca88 166
superlova 1:4222a8f9ca88 167 rc = Host_CtrlRecv(USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE,
superlova 1:4222a8f9ca88 168 MS_GET_MAX_LUN_REQ,
superlova 1:4222a8f9ca88 169 0,
superlova 1:4222a8f9ca88 170 0,
superlova 1:4222a8f9ca88 171 1,
superlova 1:4222a8f9ca88 172 TDBuffer);
superlova 1:4222a8f9ca88 173 return (rc);
superlova 1:4222a8f9ca88 174 }
superlova 1:4222a8f9ca88 175
superlova 1:4222a8f9ca88 176 /*
superlova 1:4222a8f9ca88 177 **************************************************************************************************************
superlova 1:4222a8f9ca88 178 * GET SENSE INFORMATION
superlova 1:4222a8f9ca88 179 *
superlova 1:4222a8f9ca88 180 * Description: This function is used to get sense information from the device
superlova 1:4222a8f9ca88 181 *
superlova 1:4222a8f9ca88 182 * Arguments : None
superlova 1:4222a8f9ca88 183 *
superlova 1:4222a8f9ca88 184 * Returns : OK if Success
superlova 1:4222a8f9ca88 185 * ERROR if Failed
superlova 1:4222a8f9ca88 186 *
superlova 1:4222a8f9ca88 187 **************************************************************************************************************
superlova 1:4222a8f9ca88 188 */
superlova 1:4222a8f9ca88 189
superlova 1:4222a8f9ca88 190 USB_INT32S MS_GetSenseInfo (void)
superlova 1:4222a8f9ca88 191 {
superlova 1:4222a8f9ca88 192 USB_INT32S rc;
superlova 1:4222a8f9ca88 193
superlova 1:4222a8f9ca88 194
superlova 1:4222a8f9ca88 195 Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_REQUEST_SENSE, 6);
superlova 1:4222a8f9ca88 196 rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE);
superlova 1:4222a8f9ca88 197 if (rc == OK) {
superlova 1:4222a8f9ca88 198 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, 18);
superlova 1:4222a8f9ca88 199 if (rc == OK) {
superlova 1:4222a8f9ca88 200 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE);
superlova 1:4222a8f9ca88 201 if (rc == OK) {
superlova 1:4222a8f9ca88 202 if (TDBuffer[12] != 0) {
superlova 1:4222a8f9ca88 203 rc = ERR_MS_CMD_FAILED;
superlova 1:4222a8f9ca88 204 }
superlova 1:4222a8f9ca88 205 }
superlova 1:4222a8f9ca88 206 }
superlova 1:4222a8f9ca88 207 }
superlova 1:4222a8f9ca88 208 return (rc);
superlova 1:4222a8f9ca88 209 }
superlova 1:4222a8f9ca88 210
superlova 1:4222a8f9ca88 211 /*
superlova 1:4222a8f9ca88 212 **************************************************************************************************************
superlova 1:4222a8f9ca88 213 * TEST UNIT READY
superlova 1:4222a8f9ca88 214 *
superlova 1:4222a8f9ca88 215 * Description: This function is used to test whether the unit is ready or not
superlova 1:4222a8f9ca88 216 *
superlova 1:4222a8f9ca88 217 * Arguments : None
superlova 1:4222a8f9ca88 218 *
superlova 1:4222a8f9ca88 219 * Returns : OK if Success
superlova 1:4222a8f9ca88 220 * ERROR if Failed
superlova 1:4222a8f9ca88 221 *
superlova 1:4222a8f9ca88 222 **************************************************************************************************************
superlova 1:4222a8f9ca88 223 */
superlova 1:4222a8f9ca88 224
superlova 1:4222a8f9ca88 225 USB_INT32S MS_TestUnitReady (void)
superlova 1:4222a8f9ca88 226 {
superlova 1:4222a8f9ca88 227 USB_INT32S rc;
superlova 1:4222a8f9ca88 228
superlova 1:4222a8f9ca88 229
superlova 1:4222a8f9ca88 230 Fill_MSCommand(0, 0, 0, MS_DATA_DIR_NONE, SCSI_CMD_TEST_UNIT_READY, 6);
superlova 1:4222a8f9ca88 231 rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE);
superlova 1:4222a8f9ca88 232 if (rc == OK) {
superlova 1:4222a8f9ca88 233 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE);
superlova 1:4222a8f9ca88 234 if (rc == OK) {
superlova 1:4222a8f9ca88 235 if (TDBuffer[12] != 0) {
superlova 1:4222a8f9ca88 236 rc = ERR_MS_CMD_FAILED;
superlova 1:4222a8f9ca88 237 }
superlova 1:4222a8f9ca88 238 }
superlova 1:4222a8f9ca88 239 }
superlova 1:4222a8f9ca88 240 return (rc);
superlova 1:4222a8f9ca88 241 }
superlova 1:4222a8f9ca88 242
superlova 1:4222a8f9ca88 243 /*
superlova 1:4222a8f9ca88 244 **************************************************************************************************************
superlova 1:4222a8f9ca88 245 * READ CAPACITY
superlova 1:4222a8f9ca88 246 *
superlova 1:4222a8f9ca88 247 * Description: This function is used to read the capacity of the mass storage device
superlova 1:4222a8f9ca88 248 *
superlova 1:4222a8f9ca88 249 * Arguments : None
superlova 1:4222a8f9ca88 250 *
superlova 1:4222a8f9ca88 251 * Returns : OK if Success
superlova 1:4222a8f9ca88 252 * ERROR if Failed
superlova 1:4222a8f9ca88 253 *
superlova 1:4222a8f9ca88 254 **************************************************************************************************************
superlova 1:4222a8f9ca88 255 */
superlova 1:4222a8f9ca88 256
superlova 1:4222a8f9ca88 257 USB_INT32S MS_ReadCapacity (USB_INT32U *numBlks, USB_INT32U *blkSize)
superlova 1:4222a8f9ca88 258 {
superlova 1:4222a8f9ca88 259 USB_INT32S rc;
superlova 1:4222a8f9ca88 260
superlova 1:4222a8f9ca88 261
superlova 1:4222a8f9ca88 262 Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_READ_CAPACITY, 10);
superlova 1:4222a8f9ca88 263 rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE);
superlova 1:4222a8f9ca88 264 if (rc == OK) {
superlova 1:4222a8f9ca88 265 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, 8);
superlova 1:4222a8f9ca88 266 if (rc == OK) {
superlova 1:4222a8f9ca88 267 if (numBlks)
superlova 1:4222a8f9ca88 268 *numBlks = ReadBE32U(&TDBuffer[0]);
superlova 1:4222a8f9ca88 269 if (blkSize)
superlova 1:4222a8f9ca88 270 *blkSize = ReadBE32U(&TDBuffer[4]);
superlova 1:4222a8f9ca88 271 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE);
superlova 1:4222a8f9ca88 272 if (rc == OK) {
superlova 1:4222a8f9ca88 273 if (TDBuffer[12] != 0) {
superlova 1:4222a8f9ca88 274 rc = ERR_MS_CMD_FAILED;
superlova 1:4222a8f9ca88 275 }
superlova 1:4222a8f9ca88 276 }
superlova 1:4222a8f9ca88 277 }
superlova 1:4222a8f9ca88 278 }
superlova 1:4222a8f9ca88 279 return (rc);
superlova 1:4222a8f9ca88 280 }
superlova 1:4222a8f9ca88 281
superlova 1:4222a8f9ca88 282
superlova 1:4222a8f9ca88 283
superlova 1:4222a8f9ca88 284 USB_INT32S MS_Inquire (USB_INT08U *response)
superlova 1:4222a8f9ca88 285 {
superlova 1:4222a8f9ca88 286 USB_INT32S rc;
superlova 1:4222a8f9ca88 287 USB_INT32U i;
superlova 1:4222a8f9ca88 288
superlova 1:4222a8f9ca88 289 Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_INQUIRY, 6);
superlova 1:4222a8f9ca88 290 rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE);
superlova 1:4222a8f9ca88 291 if (rc == OK) {
superlova 1:4222a8f9ca88 292 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, INQUIRY_LENGTH);
superlova 1:4222a8f9ca88 293 if (rc == OK) {
superlova 1:4222a8f9ca88 294 if (response) {
superlova 1:4222a8f9ca88 295 for ( i = 0; i < INQUIRY_LENGTH; i++ )
superlova 1:4222a8f9ca88 296 *response++ = *TDBuffer++;
superlova 1:4222a8f9ca88 297 #if 0
superlova 1:4222a8f9ca88 298 MemCpy (response, TDBuffer, INQUIRY_LENGTH);
superlova 1:4222a8f9ca88 299 StrNullTrailingSpace (response->vendorID, SCSI_INQUIRY_VENDORCHARS);
superlova 1:4222a8f9ca88 300 StrNullTrailingSpace (response->productID, SCSI_INQUIRY_PRODUCTCHARS);
superlova 1:4222a8f9ca88 301 StrNullTrailingSpace (response->productRev, SCSI_INQUIRY_REVCHARS);
superlova 1:4222a8f9ca88 302 #endif
superlova 1:4222a8f9ca88 303 }
superlova 1:4222a8f9ca88 304 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE);
superlova 1:4222a8f9ca88 305 if (rc == OK) {
superlova 1:4222a8f9ca88 306 if (TDBuffer[12] != 0) { // bCSWStatus byte
superlova 1:4222a8f9ca88 307 rc = ERR_MS_CMD_FAILED;
superlova 1:4222a8f9ca88 308 }
superlova 1:4222a8f9ca88 309 }
superlova 1:4222a8f9ca88 310 }
superlova 1:4222a8f9ca88 311 }
superlova 1:4222a8f9ca88 312 return (rc);
superlova 1:4222a8f9ca88 313 }
superlova 1:4222a8f9ca88 314
superlova 1:4222a8f9ca88 315 /*
superlova 1:4222a8f9ca88 316 **************************************************************************************************************
superlova 1:4222a8f9ca88 317 * RECEIVE THE BULK DATA
superlova 1:4222a8f9ca88 318 *
superlova 1:4222a8f9ca88 319 * Description: This function is used to receive the bulk data
superlova 1:4222a8f9ca88 320 *
superlova 1:4222a8f9ca88 321 * Arguments : None
superlova 1:4222a8f9ca88 322 *
superlova 1:4222a8f9ca88 323 * Returns : OK if Success
superlova 1:4222a8f9ca88 324 * ERR_INVALID_BOOTSIG if Failed
superlova 1:4222a8f9ca88 325 *
superlova 1:4222a8f9ca88 326 **************************************************************************************************************
superlova 1:4222a8f9ca88 327 */
superlova 1:4222a8f9ca88 328
superlova 1:4222a8f9ca88 329 USB_INT32S MS_BulkRecv ( USB_INT32U block_number,
superlova 1:4222a8f9ca88 330 USB_INT16U num_blocks,
superlova 1:4222a8f9ca88 331 volatile USB_INT08U *user_buffer)
superlova 1:4222a8f9ca88 332 {
superlova 1:4222a8f9ca88 333 USB_INT32S rc;
superlova 1:4222a8f9ca88 334 int i;
superlova 1:4222a8f9ca88 335 volatile USB_INT08U *c = user_buffer;
superlova 1:4222a8f9ca88 336 for (i=0;i<MS_BlkSize*num_blocks;i++)
superlova 1:4222a8f9ca88 337 *c++ = 0;
superlova 1:4222a8f9ca88 338
superlova 1:4222a8f9ca88 339
superlova 1:4222a8f9ca88 340 Fill_MSCommand(block_number, MS_BlkSize, num_blocks, MS_DATA_DIR_IN, SCSI_CMD_READ_10, 10);
superlova 1:4222a8f9ca88 341
superlova 1:4222a8f9ca88 342 rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE);
superlova 1:4222a8f9ca88 343 if (rc == OK) {
superlova 1:4222a8f9ca88 344 rc = Host_ProcessTD(EDBulkIn, TD_IN, user_buffer, MS_BlkSize * num_blocks);
superlova 1:4222a8f9ca88 345 if (rc == OK) {
superlova 1:4222a8f9ca88 346 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE);
superlova 1:4222a8f9ca88 347 if (rc == OK) {
superlova 1:4222a8f9ca88 348 if (TDBuffer[12] != 0) {
superlova 1:4222a8f9ca88 349 rc = ERR_MS_CMD_FAILED;
superlova 1:4222a8f9ca88 350 }
superlova 1:4222a8f9ca88 351 }
superlova 1:4222a8f9ca88 352 }
superlova 1:4222a8f9ca88 353 }
superlova 1:4222a8f9ca88 354 return (rc);
superlova 1:4222a8f9ca88 355 }
superlova 1:4222a8f9ca88 356
superlova 1:4222a8f9ca88 357 /*
superlova 1:4222a8f9ca88 358 **************************************************************************************************************
superlova 1:4222a8f9ca88 359 * SEND BULK DATA
superlova 1:4222a8f9ca88 360 *
superlova 1:4222a8f9ca88 361 * Description: This function is used to send the bulk data
superlova 1:4222a8f9ca88 362 *
superlova 1:4222a8f9ca88 363 * Arguments : None
superlova 1:4222a8f9ca88 364 *
superlova 1:4222a8f9ca88 365 * Returns : OK if Success
superlova 1:4222a8f9ca88 366 * ERR_INVALID_BOOTSIG if Failed
superlova 1:4222a8f9ca88 367 *
superlova 1:4222a8f9ca88 368 **************************************************************************************************************
superlova 1:4222a8f9ca88 369 */
superlova 1:4222a8f9ca88 370
superlova 1:4222a8f9ca88 371 USB_INT32S MS_BulkSend ( USB_INT32U block_number,
superlova 1:4222a8f9ca88 372 USB_INT16U num_blocks,
superlova 1:4222a8f9ca88 373 volatile USB_INT08U *user_buffer)
superlova 1:4222a8f9ca88 374 {
superlova 1:4222a8f9ca88 375 USB_INT32S rc;
superlova 1:4222a8f9ca88 376
superlova 1:4222a8f9ca88 377
superlova 1:4222a8f9ca88 378 Fill_MSCommand(block_number, MS_BlkSize, num_blocks, MS_DATA_DIR_OUT, SCSI_CMD_WRITE_10, 10);
superlova 1:4222a8f9ca88 379
superlova 1:4222a8f9ca88 380 rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE);
superlova 1:4222a8f9ca88 381 if (rc == OK) {
superlova 1:4222a8f9ca88 382 rc = Host_ProcessTD(EDBulkOut, TD_OUT, user_buffer, MS_BlkSize * num_blocks);
superlova 1:4222a8f9ca88 383 if (rc == OK) {
superlova 1:4222a8f9ca88 384 rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE);
superlova 1:4222a8f9ca88 385 if (rc == OK) {
superlova 1:4222a8f9ca88 386 if (TDBuffer[12] != 0) {
superlova 1:4222a8f9ca88 387 rc = ERR_MS_CMD_FAILED;
superlova 1:4222a8f9ca88 388 }
superlova 1:4222a8f9ca88 389 }
superlova 1:4222a8f9ca88 390 }
superlova 1:4222a8f9ca88 391 }
superlova 1:4222a8f9ca88 392 return (rc);
superlova 1:4222a8f9ca88 393 }
superlova 1:4222a8f9ca88 394
superlova 1:4222a8f9ca88 395 /*
superlova 1:4222a8f9ca88 396 **************************************************************************************************************
superlova 1:4222a8f9ca88 397 * FILL MASS STORAGE COMMAND
superlova 1:4222a8f9ca88 398 *
superlova 1:4222a8f9ca88 399 * Description: This function is used to fill the mass storage command
superlova 1:4222a8f9ca88 400 *
superlova 1:4222a8f9ca88 401 * Arguments : None
superlova 1:4222a8f9ca88 402 *
superlova 1:4222a8f9ca88 403 * Returns : OK if Success
superlova 1:4222a8f9ca88 404 * ERR_INVALID_BOOTSIG if Failed
superlova 1:4222a8f9ca88 405 *
superlova 1:4222a8f9ca88 406 **************************************************************************************************************
superlova 1:4222a8f9ca88 407 */
superlova 1:4222a8f9ca88 408
superlova 1:4222a8f9ca88 409 void Fill_MSCommand (USB_INT32U block_number,
superlova 1:4222a8f9ca88 410 USB_INT32U block_size,
superlova 1:4222a8f9ca88 411 USB_INT16U num_blocks,
superlova 1:4222a8f9ca88 412 MS_DATA_DIR direction,
superlova 1:4222a8f9ca88 413 USB_INT08U scsi_cmd,
superlova 1:4222a8f9ca88 414 USB_INT08U scsi_cmd_len)
superlova 1:4222a8f9ca88 415 {
superlova 1:4222a8f9ca88 416 USB_INT32U data_len;
superlova 1:4222a8f9ca88 417 static USB_INT32U tag_cnt = 0;
superlova 1:4222a8f9ca88 418 USB_INT32U cnt;
superlova 1:4222a8f9ca88 419
superlova 1:4222a8f9ca88 420
superlova 1:4222a8f9ca88 421 for (cnt = 0; cnt < CBW_SIZE; cnt++) {
superlova 1:4222a8f9ca88 422 TDBuffer[cnt] = 0;
superlova 1:4222a8f9ca88 423 }
superlova 1:4222a8f9ca88 424 switch(scsi_cmd) {
superlova 1:4222a8f9ca88 425
superlova 1:4222a8f9ca88 426 case SCSI_CMD_TEST_UNIT_READY:
superlova 1:4222a8f9ca88 427 data_len = 0;
superlova 1:4222a8f9ca88 428 break;
superlova 1:4222a8f9ca88 429 case SCSI_CMD_READ_CAPACITY:
superlova 1:4222a8f9ca88 430 data_len = 8;
superlova 1:4222a8f9ca88 431 break;
superlova 1:4222a8f9ca88 432 case SCSI_CMD_REQUEST_SENSE:
superlova 1:4222a8f9ca88 433 data_len = 18;
superlova 1:4222a8f9ca88 434 break;
superlova 1:4222a8f9ca88 435 case SCSI_CMD_INQUIRY:
superlova 1:4222a8f9ca88 436 data_len = 36;
superlova 1:4222a8f9ca88 437 break;
superlova 1:4222a8f9ca88 438 default:
superlova 1:4222a8f9ca88 439 data_len = block_size * num_blocks;
superlova 1:4222a8f9ca88 440 break;
superlova 1:4222a8f9ca88 441 }
superlova 1:4222a8f9ca88 442 WriteLE32U(TDBuffer, CBW_SIGNATURE);
superlova 1:4222a8f9ca88 443 WriteLE32U(&TDBuffer[4], tag_cnt);
superlova 1:4222a8f9ca88 444 WriteLE32U(&TDBuffer[8], data_len);
superlova 1:4222a8f9ca88 445 TDBuffer[12] = (direction == MS_DATA_DIR_NONE) ? 0 : direction;
superlova 1:4222a8f9ca88 446 TDBuffer[14] = scsi_cmd_len; /* Length of the CBW */
superlova 1:4222a8f9ca88 447 TDBuffer[15] = scsi_cmd;
superlova 1:4222a8f9ca88 448 if ((scsi_cmd == SCSI_CMD_REQUEST_SENSE)
superlova 1:4222a8f9ca88 449 || (scsi_cmd == SCSI_CMD_INQUIRY)) {
superlova 1:4222a8f9ca88 450 TDBuffer[19] = (USB_INT08U)data_len;
superlova 1:4222a8f9ca88 451 } else {
superlova 1:4222a8f9ca88 452 WriteBE32U(&TDBuffer[17], block_number);
superlova 1:4222a8f9ca88 453 }
superlova 1:4222a8f9ca88 454 WriteBE16U(&TDBuffer[22], num_blocks);
superlova 1:4222a8f9ca88 455 }