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