Code to take GPS, Accelerometer and Compass readings and print to USB for data analysis.

Dependencies:   CMPS03 FatFileSystemCpp GPS MMA7660 mbed C12832_lcd

Committer:
Alex4475
Date:
Wed May 07 09:49:54 2014 +0000
Revision:
0:2557081b4322
Code to take GPS, Accelerometer and Compass data and print to a USB for data analysis

Who changed what in which revision?

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