working pedometer code using tickers

Dependencies:   mbed PulseSensor2 SCP1000 4DGL-uLCD-SE LSM9DS1_Library_cal PinDetect FatFileSystemCpp GP-20U7

Committer:
memig3
Date:
Tue Apr 21 22:52:20 2020 +0000
Revision:
0:bcfec522ef98
working pedometer code using tickers;

Who changed what in which revision?

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