Record audio data to a .wav file, complete with header, using the TLV320 CODEC and I2S port

Dependencies:   mbed

Committer:
d_worrall
Date:
Fri Aug 05 15:00:51 2011 +0000
Revision:
0:e7efc8468066
version 2.0

Who changed what in which revision?

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