Base station firmware

Committer:
Perijah
Date:
Tue May 24 13:16:55 2016 +0000
Revision:
0:ecc3925d3f8c
Base station firmware

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Perijah 0:ecc3925d3f8c 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Perijah 0:ecc3925d3f8c 2 *
Perijah 0:ecc3925d3f8c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Perijah 0:ecc3925d3f8c 4 * and associated documentation files (the "Software"), to deal in the Software without
Perijah 0:ecc3925d3f8c 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Perijah 0:ecc3925d3f8c 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Perijah 0:ecc3925d3f8c 7 * Software is furnished to do so, subject to the following conditions:
Perijah 0:ecc3925d3f8c 8 *
Perijah 0:ecc3925d3f8c 9 * The above copyright notice and this permission notice shall be included in all copies or
Perijah 0:ecc3925d3f8c 10 * substantial portions of the Software.
Perijah 0:ecc3925d3f8c 11 *
Perijah 0:ecc3925d3f8c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Perijah 0:ecc3925d3f8c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Perijah 0:ecc3925d3f8c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Perijah 0:ecc3925d3f8c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Perijah 0:ecc3925d3f8c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Perijah 0:ecc3925d3f8c 17 */
Perijah 0:ecc3925d3f8c 18
Perijah 0:ecc3925d3f8c 19 #include "stdint.h"
Perijah 0:ecc3925d3f8c 20 #include "USBMSD.h"
Perijah 0:ecc3925d3f8c 21
Perijah 0:ecc3925d3f8c 22 #define DISK_OK 0x00
Perijah 0:ecc3925d3f8c 23 #define NO_INIT 0x01
Perijah 0:ecc3925d3f8c 24 #define NO_DISK 0x02
Perijah 0:ecc3925d3f8c 25 #define WRITE_PROTECT 0x04
Perijah 0:ecc3925d3f8c 26
Perijah 0:ecc3925d3f8c 27 #define CBW_Signature 0x43425355
Perijah 0:ecc3925d3f8c 28 #define CSW_Signature 0x53425355
Perijah 0:ecc3925d3f8c 29
Perijah 0:ecc3925d3f8c 30 // SCSI Commands
Perijah 0:ecc3925d3f8c 31 #define TEST_UNIT_READY 0x00
Perijah 0:ecc3925d3f8c 32 #define REQUEST_SENSE 0x03
Perijah 0:ecc3925d3f8c 33 #define FORMAT_UNIT 0x04
Perijah 0:ecc3925d3f8c 34 #define INQUIRY 0x12
Perijah 0:ecc3925d3f8c 35 #define MODE_SELECT6 0x15
Perijah 0:ecc3925d3f8c 36 #define MODE_SENSE6 0x1A
Perijah 0:ecc3925d3f8c 37 #define START_STOP_UNIT 0x1B
Perijah 0:ecc3925d3f8c 38 #define MEDIA_REMOVAL 0x1E
Perijah 0:ecc3925d3f8c 39 #define READ_FORMAT_CAPACITIES 0x23
Perijah 0:ecc3925d3f8c 40 #define READ_CAPACITY 0x25
Perijah 0:ecc3925d3f8c 41 #define READ10 0x28
Perijah 0:ecc3925d3f8c 42 #define WRITE10 0x2A
Perijah 0:ecc3925d3f8c 43 #define VERIFY10 0x2F
Perijah 0:ecc3925d3f8c 44 #define READ12 0xA8
Perijah 0:ecc3925d3f8c 45 #define WRITE12 0xAA
Perijah 0:ecc3925d3f8c 46 #define MODE_SELECT10 0x55
Perijah 0:ecc3925d3f8c 47 #define MODE_SENSE10 0x5A
Perijah 0:ecc3925d3f8c 48
Perijah 0:ecc3925d3f8c 49 // MSC class specific requests
Perijah 0:ecc3925d3f8c 50 #define MSC_REQUEST_RESET 0xFF
Perijah 0:ecc3925d3f8c 51 #define MSC_REQUEST_GET_MAX_LUN 0xFE
Perijah 0:ecc3925d3f8c 52
Perijah 0:ecc3925d3f8c 53 #define DEFAULT_CONFIGURATION (1)
Perijah 0:ecc3925d3f8c 54
Perijah 0:ecc3925d3f8c 55 // max packet size
Perijah 0:ecc3925d3f8c 56 #define MAX_PACKET MAX_PACKET_SIZE_EPBULK
Perijah 0:ecc3925d3f8c 57
Perijah 0:ecc3925d3f8c 58 // CSW Status
Perijah 0:ecc3925d3f8c 59 enum Status {
Perijah 0:ecc3925d3f8c 60 CSW_PASSED,
Perijah 0:ecc3925d3f8c 61 CSW_FAILED,
Perijah 0:ecc3925d3f8c 62 CSW_ERROR,
Perijah 0:ecc3925d3f8c 63 };
Perijah 0:ecc3925d3f8c 64
Perijah 0:ecc3925d3f8c 65
Perijah 0:ecc3925d3f8c 66 USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
Perijah 0:ecc3925d3f8c 67 stage = READ_CBW;
Perijah 0:ecc3925d3f8c 68 memset((void *)&cbw, 0, sizeof(CBW));
Perijah 0:ecc3925d3f8c 69 memset((void *)&csw, 0, sizeof(CSW));
Perijah 0:ecc3925d3f8c 70 }
Perijah 0:ecc3925d3f8c 71
Perijah 0:ecc3925d3f8c 72
Perijah 0:ecc3925d3f8c 73
Perijah 0:ecc3925d3f8c 74 // Called in ISR context to process a class specific request
Perijah 0:ecc3925d3f8c 75 bool USBMSD::USBCallback_request(void) {
Perijah 0:ecc3925d3f8c 76
Perijah 0:ecc3925d3f8c 77 bool success = false;
Perijah 0:ecc3925d3f8c 78 CONTROL_TRANSFER * transfer = getTransferPtr();
Perijah 0:ecc3925d3f8c 79 static uint8_t maxLUN[1] = {0};
Perijah 0:ecc3925d3f8c 80
Perijah 0:ecc3925d3f8c 81 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
Perijah 0:ecc3925d3f8c 82 switch (transfer->setup.bRequest) {
Perijah 0:ecc3925d3f8c 83 case MSC_REQUEST_RESET:
Perijah 0:ecc3925d3f8c 84 reset();
Perijah 0:ecc3925d3f8c 85 success = true;
Perijah 0:ecc3925d3f8c 86 break;
Perijah 0:ecc3925d3f8c 87 case MSC_REQUEST_GET_MAX_LUN:
Perijah 0:ecc3925d3f8c 88 transfer->remaining = 1;
Perijah 0:ecc3925d3f8c 89 transfer->ptr = maxLUN;
Perijah 0:ecc3925d3f8c 90 transfer->direction = DEVICE_TO_HOST;
Perijah 0:ecc3925d3f8c 91 success = true;
Perijah 0:ecc3925d3f8c 92 break;
Perijah 0:ecc3925d3f8c 93 default:
Perijah 0:ecc3925d3f8c 94 break;
Perijah 0:ecc3925d3f8c 95 }
Perijah 0:ecc3925d3f8c 96 }
Perijah 0:ecc3925d3f8c 97
Perijah 0:ecc3925d3f8c 98 return success;
Perijah 0:ecc3925d3f8c 99 }
Perijah 0:ecc3925d3f8c 100
Perijah 0:ecc3925d3f8c 101
Perijah 0:ecc3925d3f8c 102 bool USBMSD::connect() {
Perijah 0:ecc3925d3f8c 103
Perijah 0:ecc3925d3f8c 104 //disk initialization
Perijah 0:ecc3925d3f8c 105 if (disk_status() & NO_INIT) {
Perijah 0:ecc3925d3f8c 106 if (disk_initialize()) {
Perijah 0:ecc3925d3f8c 107 return false;
Perijah 0:ecc3925d3f8c 108 }
Perijah 0:ecc3925d3f8c 109 }
Perijah 0:ecc3925d3f8c 110
Perijah 0:ecc3925d3f8c 111 // get number of blocks
Perijah 0:ecc3925d3f8c 112 BlockCount = disk_sectors();
Perijah 0:ecc3925d3f8c 113
Perijah 0:ecc3925d3f8c 114 // get memory size
Perijah 0:ecc3925d3f8c 115 MemorySize = disk_size();
Perijah 0:ecc3925d3f8c 116
Perijah 0:ecc3925d3f8c 117 if (BlockCount > 0) {
Perijah 0:ecc3925d3f8c 118 BlockSize = MemorySize / BlockCount;
Perijah 0:ecc3925d3f8c 119 if (BlockSize != 0) {
Perijah 0:ecc3925d3f8c 120 page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
Perijah 0:ecc3925d3f8c 121 if (page == NULL)
Perijah 0:ecc3925d3f8c 122 return false;
Perijah 0:ecc3925d3f8c 123 }
Perijah 0:ecc3925d3f8c 124 } else {
Perijah 0:ecc3925d3f8c 125 return false;
Perijah 0:ecc3925d3f8c 126 }
Perijah 0:ecc3925d3f8c 127
Perijah 0:ecc3925d3f8c 128 //connect the device
Perijah 0:ecc3925d3f8c 129 USBDevice::connect();
Perijah 0:ecc3925d3f8c 130 return true;
Perijah 0:ecc3925d3f8c 131 }
Perijah 0:ecc3925d3f8c 132
Perijah 0:ecc3925d3f8c 133
Perijah 0:ecc3925d3f8c 134 void USBMSD::reset() {
Perijah 0:ecc3925d3f8c 135 stage = READ_CBW;
Perijah 0:ecc3925d3f8c 136 }
Perijah 0:ecc3925d3f8c 137
Perijah 0:ecc3925d3f8c 138
Perijah 0:ecc3925d3f8c 139 // Called in ISR context called when a data is received
Perijah 0:ecc3925d3f8c 140 bool USBMSD::EP2_OUT_callback() {
Perijah 0:ecc3925d3f8c 141 uint32_t size = 0;
Perijah 0:ecc3925d3f8c 142 uint8_t buf[MAX_PACKET_SIZE_EPBULK];
Perijah 0:ecc3925d3f8c 143 readEP(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 144 switch (stage) {
Perijah 0:ecc3925d3f8c 145 // the device has to decode the CBW received
Perijah 0:ecc3925d3f8c 146 case READ_CBW:
Perijah 0:ecc3925d3f8c 147 CBWDecode(buf, size);
Perijah 0:ecc3925d3f8c 148 break;
Perijah 0:ecc3925d3f8c 149
Perijah 0:ecc3925d3f8c 150 // the device has to receive data from the host
Perijah 0:ecc3925d3f8c 151 case PROCESS_CBW:
Perijah 0:ecc3925d3f8c 152 switch (cbw.CB[0]) {
Perijah 0:ecc3925d3f8c 153 case WRITE10:
Perijah 0:ecc3925d3f8c 154 case WRITE12:
Perijah 0:ecc3925d3f8c 155 memoryWrite(buf, size);
Perijah 0:ecc3925d3f8c 156 break;
Perijah 0:ecc3925d3f8c 157 case VERIFY10:
Perijah 0:ecc3925d3f8c 158 memoryVerify(buf, size);
Perijah 0:ecc3925d3f8c 159 break;
Perijah 0:ecc3925d3f8c 160 }
Perijah 0:ecc3925d3f8c 161 break;
Perijah 0:ecc3925d3f8c 162
Perijah 0:ecc3925d3f8c 163 // an error has occured: stall endpoint and send CSW
Perijah 0:ecc3925d3f8c 164 default:
Perijah 0:ecc3925d3f8c 165 stallEndpoint(EPBULK_OUT);
Perijah 0:ecc3925d3f8c 166 csw.Status = CSW_ERROR;
Perijah 0:ecc3925d3f8c 167 sendCSW();
Perijah 0:ecc3925d3f8c 168 break;
Perijah 0:ecc3925d3f8c 169 }
Perijah 0:ecc3925d3f8c 170
Perijah 0:ecc3925d3f8c 171 //reactivate readings on the OUT bulk endpoint
Perijah 0:ecc3925d3f8c 172 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 173 return true;
Perijah 0:ecc3925d3f8c 174 }
Perijah 0:ecc3925d3f8c 175
Perijah 0:ecc3925d3f8c 176 // Called in ISR context when a data has been transferred
Perijah 0:ecc3925d3f8c 177 bool USBMSD::EP2_IN_callback() {
Perijah 0:ecc3925d3f8c 178 switch (stage) {
Perijah 0:ecc3925d3f8c 179
Perijah 0:ecc3925d3f8c 180 // the device has to send data to the host
Perijah 0:ecc3925d3f8c 181 case PROCESS_CBW:
Perijah 0:ecc3925d3f8c 182 switch (cbw.CB[0]) {
Perijah 0:ecc3925d3f8c 183 case READ10:
Perijah 0:ecc3925d3f8c 184 case READ12:
Perijah 0:ecc3925d3f8c 185 memoryRead();
Perijah 0:ecc3925d3f8c 186 break;
Perijah 0:ecc3925d3f8c 187 }
Perijah 0:ecc3925d3f8c 188 break;
Perijah 0:ecc3925d3f8c 189
Perijah 0:ecc3925d3f8c 190 //the device has to send a CSW
Perijah 0:ecc3925d3f8c 191 case SEND_CSW:
Perijah 0:ecc3925d3f8c 192 sendCSW();
Perijah 0:ecc3925d3f8c 193 break;
Perijah 0:ecc3925d3f8c 194
Perijah 0:ecc3925d3f8c 195 // an error has occured
Perijah 0:ecc3925d3f8c 196 case ERROR:
Perijah 0:ecc3925d3f8c 197 stallEndpoint(EPBULK_IN);
Perijah 0:ecc3925d3f8c 198 sendCSW();
Perijah 0:ecc3925d3f8c 199 break;
Perijah 0:ecc3925d3f8c 200
Perijah 0:ecc3925d3f8c 201 // the host has received the CSW -> we wait a CBW
Perijah 0:ecc3925d3f8c 202 case WAIT_CSW:
Perijah 0:ecc3925d3f8c 203 stage = READ_CBW;
Perijah 0:ecc3925d3f8c 204 break;
Perijah 0:ecc3925d3f8c 205 }
Perijah 0:ecc3925d3f8c 206 return true;
Perijah 0:ecc3925d3f8c 207 }
Perijah 0:ecc3925d3f8c 208
Perijah 0:ecc3925d3f8c 209
Perijah 0:ecc3925d3f8c 210 void USBMSD::memoryWrite (uint8_t * buf, uint16_t size) {
Perijah 0:ecc3925d3f8c 211
Perijah 0:ecc3925d3f8c 212 if ((addr + size) > MemorySize) {
Perijah 0:ecc3925d3f8c 213 size = MemorySize - addr;
Perijah 0:ecc3925d3f8c 214 stage = ERROR;
Perijah 0:ecc3925d3f8c 215 stallEndpoint(EPBULK_OUT);
Perijah 0:ecc3925d3f8c 216 }
Perijah 0:ecc3925d3f8c 217
Perijah 0:ecc3925d3f8c 218 // we fill an array in RAM of 1 block before writing it in memory
Perijah 0:ecc3925d3f8c 219 for (int i = 0; i < size; i++)
Perijah 0:ecc3925d3f8c 220 page[addr%BlockSize + i] = buf[i];
Perijah 0:ecc3925d3f8c 221
Perijah 0:ecc3925d3f8c 222 // if the array is filled, write it in memory
Perijah 0:ecc3925d3f8c 223 if (!((addr + size)%BlockSize)) {
Perijah 0:ecc3925d3f8c 224 if (!(disk_status() & WRITE_PROTECT)) {
Perijah 0:ecc3925d3f8c 225 disk_write(page, addr/BlockSize);
Perijah 0:ecc3925d3f8c 226 }
Perijah 0:ecc3925d3f8c 227 }
Perijah 0:ecc3925d3f8c 228
Perijah 0:ecc3925d3f8c 229 addr += size;
Perijah 0:ecc3925d3f8c 230 length -= size;
Perijah 0:ecc3925d3f8c 231 csw.DataResidue -= size;
Perijah 0:ecc3925d3f8c 232
Perijah 0:ecc3925d3f8c 233 if ((!length) || (stage != PROCESS_CBW)) {
Perijah 0:ecc3925d3f8c 234 csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED;
Perijah 0:ecc3925d3f8c 235 sendCSW();
Perijah 0:ecc3925d3f8c 236 }
Perijah 0:ecc3925d3f8c 237 }
Perijah 0:ecc3925d3f8c 238
Perijah 0:ecc3925d3f8c 239 void USBMSD::memoryVerify (uint8_t * buf, uint16_t size) {
Perijah 0:ecc3925d3f8c 240 uint32_t n;
Perijah 0:ecc3925d3f8c 241
Perijah 0:ecc3925d3f8c 242 if ((addr + size) > MemorySize) {
Perijah 0:ecc3925d3f8c 243 size = MemorySize - addr;
Perijah 0:ecc3925d3f8c 244 stage = ERROR;
Perijah 0:ecc3925d3f8c 245 stallEndpoint(EPBULK_OUT);
Perijah 0:ecc3925d3f8c 246 }
Perijah 0:ecc3925d3f8c 247
Perijah 0:ecc3925d3f8c 248 // beginning of a new block -> load a whole block in RAM
Perijah 0:ecc3925d3f8c 249 if (!(addr%BlockSize))
Perijah 0:ecc3925d3f8c 250 disk_read(page, addr/BlockSize);
Perijah 0:ecc3925d3f8c 251
Perijah 0:ecc3925d3f8c 252 // info are in RAM -> no need to re-read memory
Perijah 0:ecc3925d3f8c 253 for (n = 0; n < size; n++) {
Perijah 0:ecc3925d3f8c 254 if (page[addr%BlockSize + n] != buf[n]) {
Perijah 0:ecc3925d3f8c 255 memOK = false;
Perijah 0:ecc3925d3f8c 256 break;
Perijah 0:ecc3925d3f8c 257 }
Perijah 0:ecc3925d3f8c 258 }
Perijah 0:ecc3925d3f8c 259
Perijah 0:ecc3925d3f8c 260 addr += size;
Perijah 0:ecc3925d3f8c 261 length -= size;
Perijah 0:ecc3925d3f8c 262 csw.DataResidue -= size;
Perijah 0:ecc3925d3f8c 263
Perijah 0:ecc3925d3f8c 264 if ( !length || (stage != PROCESS_CBW)) {
Perijah 0:ecc3925d3f8c 265 csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED;
Perijah 0:ecc3925d3f8c 266 sendCSW();
Perijah 0:ecc3925d3f8c 267 }
Perijah 0:ecc3925d3f8c 268 }
Perijah 0:ecc3925d3f8c 269
Perijah 0:ecc3925d3f8c 270
Perijah 0:ecc3925d3f8c 271 bool USBMSD::inquiryRequest (void) {
Perijah 0:ecc3925d3f8c 272 uint8_t inquiry[] = { 0x00, 0x80, 0x00, 0x01,
Perijah 0:ecc3925d3f8c 273 36 - 4, 0x80, 0x00, 0x00,
Perijah 0:ecc3925d3f8c 274 'M', 'B', 'E', 'D', '.', 'O', 'R', 'G',
Perijah 0:ecc3925d3f8c 275 'M', 'B', 'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 'K', ' ', ' ', ' ',
Perijah 0:ecc3925d3f8c 276 '1', '.', '0', ' ',
Perijah 0:ecc3925d3f8c 277 };
Perijah 0:ecc3925d3f8c 278 if (!write(inquiry, sizeof(inquiry))) {
Perijah 0:ecc3925d3f8c 279 return false;
Perijah 0:ecc3925d3f8c 280 }
Perijah 0:ecc3925d3f8c 281 return true;
Perijah 0:ecc3925d3f8c 282 }
Perijah 0:ecc3925d3f8c 283
Perijah 0:ecc3925d3f8c 284
Perijah 0:ecc3925d3f8c 285 bool USBMSD::readFormatCapacity() {
Perijah 0:ecc3925d3f8c 286 uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08,
Perijah 0:ecc3925d3f8c 287 (BlockCount >> 24) & 0xff,
Perijah 0:ecc3925d3f8c 288 (BlockCount >> 16) & 0xff,
Perijah 0:ecc3925d3f8c 289 (BlockCount >> 8) & 0xff,
Perijah 0:ecc3925d3f8c 290 (BlockCount >> 0) & 0xff,
Perijah 0:ecc3925d3f8c 291
Perijah 0:ecc3925d3f8c 292 0x02,
Perijah 0:ecc3925d3f8c 293 (BlockSize >> 16) & 0xff,
Perijah 0:ecc3925d3f8c 294 (BlockSize >> 8) & 0xff,
Perijah 0:ecc3925d3f8c 295 (BlockSize >> 0) & 0xff,
Perijah 0:ecc3925d3f8c 296 };
Perijah 0:ecc3925d3f8c 297 if (!write(capacity, sizeof(capacity))) {
Perijah 0:ecc3925d3f8c 298 return false;
Perijah 0:ecc3925d3f8c 299 }
Perijah 0:ecc3925d3f8c 300 return true;
Perijah 0:ecc3925d3f8c 301 }
Perijah 0:ecc3925d3f8c 302
Perijah 0:ecc3925d3f8c 303
Perijah 0:ecc3925d3f8c 304 bool USBMSD::readCapacity (void) {
Perijah 0:ecc3925d3f8c 305 uint8_t capacity[] = {
Perijah 0:ecc3925d3f8c 306 ((BlockCount - 1) >> 24) & 0xff,
Perijah 0:ecc3925d3f8c 307 ((BlockCount - 1) >> 16) & 0xff,
Perijah 0:ecc3925d3f8c 308 ((BlockCount - 1) >> 8) & 0xff,
Perijah 0:ecc3925d3f8c 309 ((BlockCount - 1) >> 0) & 0xff,
Perijah 0:ecc3925d3f8c 310
Perijah 0:ecc3925d3f8c 311 (BlockSize >> 24) & 0xff,
Perijah 0:ecc3925d3f8c 312 (BlockSize >> 16) & 0xff,
Perijah 0:ecc3925d3f8c 313 (BlockSize >> 8) & 0xff,
Perijah 0:ecc3925d3f8c 314 (BlockSize >> 0) & 0xff,
Perijah 0:ecc3925d3f8c 315 };
Perijah 0:ecc3925d3f8c 316 if (!write(capacity, sizeof(capacity))) {
Perijah 0:ecc3925d3f8c 317 return false;
Perijah 0:ecc3925d3f8c 318 }
Perijah 0:ecc3925d3f8c 319 return true;
Perijah 0:ecc3925d3f8c 320 }
Perijah 0:ecc3925d3f8c 321
Perijah 0:ecc3925d3f8c 322 bool USBMSD::write (uint8_t * buf, uint16_t size) {
Perijah 0:ecc3925d3f8c 323
Perijah 0:ecc3925d3f8c 324 if (size >= cbw.DataLength) {
Perijah 0:ecc3925d3f8c 325 size = cbw.DataLength;
Perijah 0:ecc3925d3f8c 326 }
Perijah 0:ecc3925d3f8c 327 stage = SEND_CSW;
Perijah 0:ecc3925d3f8c 328
Perijah 0:ecc3925d3f8c 329 if (!writeNB(EPBULK_IN, buf, size, MAX_PACKET_SIZE_EPBULK)) {
Perijah 0:ecc3925d3f8c 330 return false;
Perijah 0:ecc3925d3f8c 331 }
Perijah 0:ecc3925d3f8c 332
Perijah 0:ecc3925d3f8c 333 csw.DataResidue -= size;
Perijah 0:ecc3925d3f8c 334 csw.Status = CSW_PASSED;
Perijah 0:ecc3925d3f8c 335 return true;
Perijah 0:ecc3925d3f8c 336 }
Perijah 0:ecc3925d3f8c 337
Perijah 0:ecc3925d3f8c 338
Perijah 0:ecc3925d3f8c 339 bool USBMSD::modeSense6 (void) {
Perijah 0:ecc3925d3f8c 340 uint8_t sense6[] = { 0x03, 0x00, 0x00, 0x00 };
Perijah 0:ecc3925d3f8c 341 if (!write(sense6, sizeof(sense6))) {
Perijah 0:ecc3925d3f8c 342 return false;
Perijah 0:ecc3925d3f8c 343 }
Perijah 0:ecc3925d3f8c 344 return true;
Perijah 0:ecc3925d3f8c 345 }
Perijah 0:ecc3925d3f8c 346
Perijah 0:ecc3925d3f8c 347 void USBMSD::sendCSW() {
Perijah 0:ecc3925d3f8c 348 csw.Signature = CSW_Signature;
Perijah 0:ecc3925d3f8c 349 writeNB(EPBULK_IN, (uint8_t *)&csw, sizeof(CSW), MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 350 stage = WAIT_CSW;
Perijah 0:ecc3925d3f8c 351 }
Perijah 0:ecc3925d3f8c 352
Perijah 0:ecc3925d3f8c 353 bool USBMSD::requestSense (void) {
Perijah 0:ecc3925d3f8c 354 uint8_t request_sense[] = {
Perijah 0:ecc3925d3f8c 355 0x70,
Perijah 0:ecc3925d3f8c 356 0x00,
Perijah 0:ecc3925d3f8c 357 0x05, // Sense Key: illegal request
Perijah 0:ecc3925d3f8c 358 0x00,
Perijah 0:ecc3925d3f8c 359 0x00,
Perijah 0:ecc3925d3f8c 360 0x00,
Perijah 0:ecc3925d3f8c 361 0x00,
Perijah 0:ecc3925d3f8c 362 0x0A,
Perijah 0:ecc3925d3f8c 363 0x00,
Perijah 0:ecc3925d3f8c 364 0x00,
Perijah 0:ecc3925d3f8c 365 0x00,
Perijah 0:ecc3925d3f8c 366 0x00,
Perijah 0:ecc3925d3f8c 367 0x30,
Perijah 0:ecc3925d3f8c 368 0x01,
Perijah 0:ecc3925d3f8c 369 0x00,
Perijah 0:ecc3925d3f8c 370 0x00,
Perijah 0:ecc3925d3f8c 371 0x00,
Perijah 0:ecc3925d3f8c 372 0x00,
Perijah 0:ecc3925d3f8c 373 };
Perijah 0:ecc3925d3f8c 374
Perijah 0:ecc3925d3f8c 375 if (!write(request_sense, sizeof(request_sense))) {
Perijah 0:ecc3925d3f8c 376 return false;
Perijah 0:ecc3925d3f8c 377 }
Perijah 0:ecc3925d3f8c 378
Perijah 0:ecc3925d3f8c 379 return true;
Perijah 0:ecc3925d3f8c 380 }
Perijah 0:ecc3925d3f8c 381
Perijah 0:ecc3925d3f8c 382 void USBMSD::fail() {
Perijah 0:ecc3925d3f8c 383 csw.Status = CSW_FAILED;
Perijah 0:ecc3925d3f8c 384 sendCSW();
Perijah 0:ecc3925d3f8c 385 }
Perijah 0:ecc3925d3f8c 386
Perijah 0:ecc3925d3f8c 387
Perijah 0:ecc3925d3f8c 388 void USBMSD::CBWDecode(uint8_t * buf, uint16_t size) {
Perijah 0:ecc3925d3f8c 389 if (size == sizeof(cbw)) {
Perijah 0:ecc3925d3f8c 390 memcpy((uint8_t *)&cbw, buf, size);
Perijah 0:ecc3925d3f8c 391 if (cbw.Signature == CBW_Signature) {
Perijah 0:ecc3925d3f8c 392 csw.Tag = cbw.Tag;
Perijah 0:ecc3925d3f8c 393 csw.DataResidue = cbw.DataLength;
Perijah 0:ecc3925d3f8c 394 if ((cbw.CBLength < 1) || (cbw.CBLength > 16) ) {
Perijah 0:ecc3925d3f8c 395 fail();
Perijah 0:ecc3925d3f8c 396 } else {
Perijah 0:ecc3925d3f8c 397 switch (cbw.CB[0]) {
Perijah 0:ecc3925d3f8c 398 case TEST_UNIT_READY:
Perijah 0:ecc3925d3f8c 399 testUnitReady();
Perijah 0:ecc3925d3f8c 400 break;
Perijah 0:ecc3925d3f8c 401 case REQUEST_SENSE:
Perijah 0:ecc3925d3f8c 402 requestSense();
Perijah 0:ecc3925d3f8c 403 break;
Perijah 0:ecc3925d3f8c 404 case INQUIRY:
Perijah 0:ecc3925d3f8c 405 inquiryRequest();
Perijah 0:ecc3925d3f8c 406 break;
Perijah 0:ecc3925d3f8c 407 case MODE_SENSE6:
Perijah 0:ecc3925d3f8c 408 modeSense6();
Perijah 0:ecc3925d3f8c 409 break;
Perijah 0:ecc3925d3f8c 410 case READ_FORMAT_CAPACITIES:
Perijah 0:ecc3925d3f8c 411 readFormatCapacity();
Perijah 0:ecc3925d3f8c 412 break;
Perijah 0:ecc3925d3f8c 413 case READ_CAPACITY:
Perijah 0:ecc3925d3f8c 414 readCapacity();
Perijah 0:ecc3925d3f8c 415 break;
Perijah 0:ecc3925d3f8c 416 case READ10:
Perijah 0:ecc3925d3f8c 417 case READ12:
Perijah 0:ecc3925d3f8c 418 if (infoTransfer()) {
Perijah 0:ecc3925d3f8c 419 if ((cbw.Flags & 0x80)) {
Perijah 0:ecc3925d3f8c 420 stage = PROCESS_CBW;
Perijah 0:ecc3925d3f8c 421 memoryRead();
Perijah 0:ecc3925d3f8c 422 } else {
Perijah 0:ecc3925d3f8c 423 stallEndpoint(EPBULK_OUT);
Perijah 0:ecc3925d3f8c 424 csw.Status = CSW_ERROR;
Perijah 0:ecc3925d3f8c 425 sendCSW();
Perijah 0:ecc3925d3f8c 426 }
Perijah 0:ecc3925d3f8c 427 }
Perijah 0:ecc3925d3f8c 428 break;
Perijah 0:ecc3925d3f8c 429 case WRITE10:
Perijah 0:ecc3925d3f8c 430 case WRITE12:
Perijah 0:ecc3925d3f8c 431 if (infoTransfer()) {
Perijah 0:ecc3925d3f8c 432 if (!(cbw.Flags & 0x80)) {
Perijah 0:ecc3925d3f8c 433 stage = PROCESS_CBW;
Perijah 0:ecc3925d3f8c 434 } else {
Perijah 0:ecc3925d3f8c 435 stallEndpoint(EPBULK_IN);
Perijah 0:ecc3925d3f8c 436 csw.Status = CSW_ERROR;
Perijah 0:ecc3925d3f8c 437 sendCSW();
Perijah 0:ecc3925d3f8c 438 }
Perijah 0:ecc3925d3f8c 439 }
Perijah 0:ecc3925d3f8c 440 break;
Perijah 0:ecc3925d3f8c 441 case VERIFY10:
Perijah 0:ecc3925d3f8c 442 if (!(cbw.CB[1] & 0x02)) {
Perijah 0:ecc3925d3f8c 443 csw.Status = CSW_PASSED;
Perijah 0:ecc3925d3f8c 444 sendCSW();
Perijah 0:ecc3925d3f8c 445 break;
Perijah 0:ecc3925d3f8c 446 }
Perijah 0:ecc3925d3f8c 447 if (infoTransfer()) {
Perijah 0:ecc3925d3f8c 448 if (!(cbw.Flags & 0x80)) {
Perijah 0:ecc3925d3f8c 449 stage = PROCESS_CBW;
Perijah 0:ecc3925d3f8c 450 memOK = true;
Perijah 0:ecc3925d3f8c 451 } else {
Perijah 0:ecc3925d3f8c 452 stallEndpoint(EPBULK_IN);
Perijah 0:ecc3925d3f8c 453 csw.Status = CSW_ERROR;
Perijah 0:ecc3925d3f8c 454 sendCSW();
Perijah 0:ecc3925d3f8c 455 }
Perijah 0:ecc3925d3f8c 456 }
Perijah 0:ecc3925d3f8c 457 break;
Perijah 0:ecc3925d3f8c 458 case MEDIA_REMOVAL:
Perijah 0:ecc3925d3f8c 459 csw.Status = CSW_PASSED;
Perijah 0:ecc3925d3f8c 460 sendCSW();
Perijah 0:ecc3925d3f8c 461 break;
Perijah 0:ecc3925d3f8c 462 default:
Perijah 0:ecc3925d3f8c 463 fail();
Perijah 0:ecc3925d3f8c 464 break;
Perijah 0:ecc3925d3f8c 465 }
Perijah 0:ecc3925d3f8c 466 }
Perijah 0:ecc3925d3f8c 467 }
Perijah 0:ecc3925d3f8c 468 }
Perijah 0:ecc3925d3f8c 469 }
Perijah 0:ecc3925d3f8c 470
Perijah 0:ecc3925d3f8c 471 void USBMSD::testUnitReady (void) {
Perijah 0:ecc3925d3f8c 472
Perijah 0:ecc3925d3f8c 473 if (cbw.DataLength != 0) {
Perijah 0:ecc3925d3f8c 474 if ((cbw.Flags & 0x80) != 0) {
Perijah 0:ecc3925d3f8c 475 stallEndpoint(EPBULK_IN);
Perijah 0:ecc3925d3f8c 476 } else {
Perijah 0:ecc3925d3f8c 477 stallEndpoint(EPBULK_OUT);
Perijah 0:ecc3925d3f8c 478 }
Perijah 0:ecc3925d3f8c 479 }
Perijah 0:ecc3925d3f8c 480
Perijah 0:ecc3925d3f8c 481 csw.Status = CSW_PASSED;
Perijah 0:ecc3925d3f8c 482 sendCSW();
Perijah 0:ecc3925d3f8c 483 }
Perijah 0:ecc3925d3f8c 484
Perijah 0:ecc3925d3f8c 485
Perijah 0:ecc3925d3f8c 486 void USBMSD::memoryRead (void) {
Perijah 0:ecc3925d3f8c 487 uint32_t n;
Perijah 0:ecc3925d3f8c 488
Perijah 0:ecc3925d3f8c 489 n = (length > MAX_PACKET) ? MAX_PACKET : length;
Perijah 0:ecc3925d3f8c 490
Perijah 0:ecc3925d3f8c 491 if ((addr + n) > MemorySize) {
Perijah 0:ecc3925d3f8c 492 n = MemorySize - addr;
Perijah 0:ecc3925d3f8c 493 stage = ERROR;
Perijah 0:ecc3925d3f8c 494 }
Perijah 0:ecc3925d3f8c 495
Perijah 0:ecc3925d3f8c 496 // we read an entire block
Perijah 0:ecc3925d3f8c 497 if (!(addr%BlockSize))
Perijah 0:ecc3925d3f8c 498 disk_read(page, addr/BlockSize);
Perijah 0:ecc3925d3f8c 499
Perijah 0:ecc3925d3f8c 500 // write data which are in RAM
Perijah 0:ecc3925d3f8c 501 writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 502
Perijah 0:ecc3925d3f8c 503 addr += n;
Perijah 0:ecc3925d3f8c 504 length -= n;
Perijah 0:ecc3925d3f8c 505
Perijah 0:ecc3925d3f8c 506 csw.DataResidue -= n;
Perijah 0:ecc3925d3f8c 507
Perijah 0:ecc3925d3f8c 508 if ( !length || (stage != PROCESS_CBW)) {
Perijah 0:ecc3925d3f8c 509 csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;
Perijah 0:ecc3925d3f8c 510 stage = (stage == PROCESS_CBW) ? SEND_CSW : stage;
Perijah 0:ecc3925d3f8c 511 }
Perijah 0:ecc3925d3f8c 512 }
Perijah 0:ecc3925d3f8c 513
Perijah 0:ecc3925d3f8c 514
Perijah 0:ecc3925d3f8c 515 bool USBMSD::infoTransfer (void) {
Perijah 0:ecc3925d3f8c 516 uint32_t n;
Perijah 0:ecc3925d3f8c 517
Perijah 0:ecc3925d3f8c 518 // Logical Block Address of First Block
Perijah 0:ecc3925d3f8c 519 n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) | (cbw.CB[5] << 0);
Perijah 0:ecc3925d3f8c 520
Perijah 0:ecc3925d3f8c 521 addr = n * BlockSize;
Perijah 0:ecc3925d3f8c 522
Perijah 0:ecc3925d3f8c 523 // Number of Blocks to transfer
Perijah 0:ecc3925d3f8c 524 switch (cbw.CB[0]) {
Perijah 0:ecc3925d3f8c 525 case READ10:
Perijah 0:ecc3925d3f8c 526 case WRITE10:
Perijah 0:ecc3925d3f8c 527 case VERIFY10:
Perijah 0:ecc3925d3f8c 528 n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0);
Perijah 0:ecc3925d3f8c 529 break;
Perijah 0:ecc3925d3f8c 530
Perijah 0:ecc3925d3f8c 531 case READ12:
Perijah 0:ecc3925d3f8c 532 case WRITE12:
Perijah 0:ecc3925d3f8c 533 n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) | (cbw.CB[8] << 8) | (cbw.CB[9] << 0);
Perijah 0:ecc3925d3f8c 534 break;
Perijah 0:ecc3925d3f8c 535 }
Perijah 0:ecc3925d3f8c 536
Perijah 0:ecc3925d3f8c 537 length = n * BlockSize;
Perijah 0:ecc3925d3f8c 538
Perijah 0:ecc3925d3f8c 539 if (!cbw.DataLength) { // host requests no data
Perijah 0:ecc3925d3f8c 540 csw.Status = CSW_FAILED;
Perijah 0:ecc3925d3f8c 541 sendCSW();
Perijah 0:ecc3925d3f8c 542 return false;
Perijah 0:ecc3925d3f8c 543 }
Perijah 0:ecc3925d3f8c 544
Perijah 0:ecc3925d3f8c 545 if (cbw.DataLength != length) {
Perijah 0:ecc3925d3f8c 546 if ((cbw.Flags & 0x80) != 0) {
Perijah 0:ecc3925d3f8c 547 stallEndpoint(EPBULK_IN);
Perijah 0:ecc3925d3f8c 548 } else {
Perijah 0:ecc3925d3f8c 549 stallEndpoint(EPBULK_OUT);
Perijah 0:ecc3925d3f8c 550 }
Perijah 0:ecc3925d3f8c 551
Perijah 0:ecc3925d3f8c 552 csw.Status = CSW_FAILED;
Perijah 0:ecc3925d3f8c 553 sendCSW();
Perijah 0:ecc3925d3f8c 554 return false;
Perijah 0:ecc3925d3f8c 555 }
Perijah 0:ecc3925d3f8c 556
Perijah 0:ecc3925d3f8c 557 return true;
Perijah 0:ecc3925d3f8c 558 }
Perijah 0:ecc3925d3f8c 559
Perijah 0:ecc3925d3f8c 560
Perijah 0:ecc3925d3f8c 561
Perijah 0:ecc3925d3f8c 562
Perijah 0:ecc3925d3f8c 563
Perijah 0:ecc3925d3f8c 564 // Called in ISR context
Perijah 0:ecc3925d3f8c 565 // Set configuration. Return false if the
Perijah 0:ecc3925d3f8c 566 // configuration is not supported.
Perijah 0:ecc3925d3f8c 567 bool USBMSD::USBCallback_setConfiguration(uint8_t configuration) {
Perijah 0:ecc3925d3f8c 568 if (configuration != DEFAULT_CONFIGURATION) {
Perijah 0:ecc3925d3f8c 569 return false;
Perijah 0:ecc3925d3f8c 570 }
Perijah 0:ecc3925d3f8c 571
Perijah 0:ecc3925d3f8c 572 // Configure endpoints > 0
Perijah 0:ecc3925d3f8c 573 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 574 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 575
Perijah 0:ecc3925d3f8c 576 //activate readings
Perijah 0:ecc3925d3f8c 577 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Perijah 0:ecc3925d3f8c 578 return true;
Perijah 0:ecc3925d3f8c 579 }
Perijah 0:ecc3925d3f8c 580
Perijah 0:ecc3925d3f8c 581
Perijah 0:ecc3925d3f8c 582 uint8_t * USBMSD::stringIinterfaceDesc() {
Perijah 0:ecc3925d3f8c 583 static uint8_t stringIinterfaceDescriptor[] = {
Perijah 0:ecc3925d3f8c 584 0x08, //bLength
Perijah 0:ecc3925d3f8c 585 STRING_DESCRIPTOR, //bDescriptorType 0x03
Perijah 0:ecc3925d3f8c 586 'M',0,'S',0,'D',0 //bString iInterface - MSD
Perijah 0:ecc3925d3f8c 587 };
Perijah 0:ecc3925d3f8c 588 return stringIinterfaceDescriptor;
Perijah 0:ecc3925d3f8c 589 }
Perijah 0:ecc3925d3f8c 590
Perijah 0:ecc3925d3f8c 591 uint8_t * USBMSD::stringIproductDesc() {
Perijah 0:ecc3925d3f8c 592 static uint8_t stringIproductDescriptor[] = {
Perijah 0:ecc3925d3f8c 593 0x12, //bLength
Perijah 0:ecc3925d3f8c 594 STRING_DESCRIPTOR, //bDescriptorType 0x03
Perijah 0:ecc3925d3f8c 595 'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio
Perijah 0:ecc3925d3f8c 596 };
Perijah 0:ecc3925d3f8c 597 return stringIproductDescriptor;
Perijah 0:ecc3925d3f8c 598 }
Perijah 0:ecc3925d3f8c 599
Perijah 0:ecc3925d3f8c 600
Perijah 0:ecc3925d3f8c 601 uint8_t * USBMSD::configurationDesc() {
Perijah 0:ecc3925d3f8c 602 static uint8_t configDescriptor[] = {
Perijah 0:ecc3925d3f8c 603
Perijah 0:ecc3925d3f8c 604 // Configuration 1
Perijah 0:ecc3925d3f8c 605 9, // bLength
Perijah 0:ecc3925d3f8c 606 2, // bDescriptorType
Perijah 0:ecc3925d3f8c 607 LSB(9 + 9 + 7 + 7), // wTotalLength
Perijah 0:ecc3925d3f8c 608 MSB(9 + 9 + 7 + 7),
Perijah 0:ecc3925d3f8c 609 0x01, // bNumInterfaces
Perijah 0:ecc3925d3f8c 610 0x01, // bConfigurationValue: 0x01 is used to select this configuration
Perijah 0:ecc3925d3f8c 611 0x00, // iConfiguration: no string to describe this configuration
Perijah 0:ecc3925d3f8c 612 0xC0, // bmAttributes
Perijah 0:ecc3925d3f8c 613 100, // bMaxPower, device power consumption is 100 mA
Perijah 0:ecc3925d3f8c 614
Perijah 0:ecc3925d3f8c 615 // Interface 0, Alternate Setting 0, MSC Class
Perijah 0:ecc3925d3f8c 616 9, // bLength
Perijah 0:ecc3925d3f8c 617 4, // bDescriptorType
Perijah 0:ecc3925d3f8c 618 0x00, // bInterfaceNumber
Perijah 0:ecc3925d3f8c 619 0x00, // bAlternateSetting
Perijah 0:ecc3925d3f8c 620 0x02, // bNumEndpoints
Perijah 0:ecc3925d3f8c 621 0x08, // bInterfaceClass
Perijah 0:ecc3925d3f8c 622 0x06, // bInterfaceSubClass
Perijah 0:ecc3925d3f8c 623 0x50, // bInterfaceProtocol
Perijah 0:ecc3925d3f8c 624 0x04, // iInterface
Perijah 0:ecc3925d3f8c 625
Perijah 0:ecc3925d3f8c 626 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Perijah 0:ecc3925d3f8c 627 7, // bLength
Perijah 0:ecc3925d3f8c 628 5, // bDescriptorType
Perijah 0:ecc3925d3f8c 629 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
Perijah 0:ecc3925d3f8c 630 0x02, // bmAttributes (0x02=bulk)
Perijah 0:ecc3925d3f8c 631 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Perijah 0:ecc3925d3f8c 632 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Perijah 0:ecc3925d3f8c 633 0, // bInterval
Perijah 0:ecc3925d3f8c 634
Perijah 0:ecc3925d3f8c 635 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Perijah 0:ecc3925d3f8c 636 7, // bLength
Perijah 0:ecc3925d3f8c 637 5, // bDescriptorType
Perijah 0:ecc3925d3f8c 638 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
Perijah 0:ecc3925d3f8c 639 0x02, // bmAttributes (0x02=bulk)
Perijah 0:ecc3925d3f8c 640 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Perijah 0:ecc3925d3f8c 641 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Perijah 0:ecc3925d3f8c 642 0 // bInterval
Perijah 0:ecc3925d3f8c 643 };
Perijah 0:ecc3925d3f8c 644 return configDescriptor;
Perijah 0:ecc3925d3f8c 645 }