This program plays QuickTime movies on GR-Peach

Dependencies:   AsciiFont GR-PEACH_video GraphicsFramework LCD_shield_config R_BSP TLV320_RBSP mbed-rtos mbed

Requirements

  • GR-Peach
  • GR-Peach Audio Camera Shield or I²S compatible audio DAC
  • GR-Peach LCD Shield
  • USB memory stick

How to play movie files

  • Encode movie files

encode movies with ffmpeg

$ ffmpeg -i <input -ar 44100 -acodec pcm_s16le -s 480x270 -vcodec mjpeg -q:v 3 -movflags faststart -threads 4 -vf fps=30 <output>.mov
  • Copy movies to the root directory of USB memory
  • Build and upload this program
  • Run it
Committer:
mtkrtk
Date:
Sun Mar 12 02:01:46 2017 +0000
Revision:
1:3e638b9e91cd
Parent:
0:d0f130e27d32
fixed lcd contrast pin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mtkrtk 0:d0f130e27d32 1 /* mbed USBHost Library
mtkrtk 0:d0f130e27d32 2 * Copyright (c) 2006-2013 ARM Limited
mtkrtk 0:d0f130e27d32 3 *
mtkrtk 0:d0f130e27d32 4 * Licensed under the Apache License, Version 2.0 (the "License");
mtkrtk 0:d0f130e27d32 5 * you may not use this file except in compliance with the License.
mtkrtk 0:d0f130e27d32 6 * You may obtain a copy of the License at
mtkrtk 0:d0f130e27d32 7 *
mtkrtk 0:d0f130e27d32 8 * http://www.apache.org/licenses/LICENSE-2.0
mtkrtk 0:d0f130e27d32 9 *
mtkrtk 0:d0f130e27d32 10 * Unless required by applicable law or agreed to in writing, software
mtkrtk 0:d0f130e27d32 11 * distributed under the License is distributed on an "AS IS" BASIS,
mtkrtk 0:d0f130e27d32 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mtkrtk 0:d0f130e27d32 13 * See the License for the specific language governing permissions and
mtkrtk 0:d0f130e27d32 14 * limitations under the License.
mtkrtk 0:d0f130e27d32 15 */
mtkrtk 0:d0f130e27d32 16
mtkrtk 0:d0f130e27d32 17 #include "USBHostMSD.h"
mtkrtk 0:d0f130e27d32 18
mtkrtk 0:d0f130e27d32 19 #if USBHOST_MSD
mtkrtk 0:d0f130e27d32 20
mtkrtk 0:d0f130e27d32 21 #include "dbg.h"
mtkrtk 0:d0f130e27d32 22
mtkrtk 0:d0f130e27d32 23 #define CBW_SIGNATURE 0x43425355
mtkrtk 0:d0f130e27d32 24 #define CSW_SIGNATURE 0x53425355
mtkrtk 0:d0f130e27d32 25
mtkrtk 0:d0f130e27d32 26 #define DEVICE_TO_HOST 0x80
mtkrtk 0:d0f130e27d32 27 #define HOST_TO_DEVICE 0x00
mtkrtk 0:d0f130e27d32 28
mtkrtk 0:d0f130e27d32 29 #define GET_MAX_LUN (0xFE)
mtkrtk 0:d0f130e27d32 30 #define BO_MASS_STORAGE_RESET (0xFF)
mtkrtk 0:d0f130e27d32 31
mtkrtk 0:d0f130e27d32 32 USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir)
mtkrtk 0:d0f130e27d32 33 {
mtkrtk 0:d0f130e27d32 34 host = USBHost::getHostInst();
mtkrtk 0:d0f130e27d32 35 init();
mtkrtk 0:d0f130e27d32 36 }
mtkrtk 0:d0f130e27d32 37
mtkrtk 0:d0f130e27d32 38 void USBHostMSD::init() {
mtkrtk 0:d0f130e27d32 39 dev_connected = false;
mtkrtk 0:d0f130e27d32 40 dev = NULL;
mtkrtk 0:d0f130e27d32 41 bulk_in = NULL;
mtkrtk 0:d0f130e27d32 42 bulk_out = NULL;
mtkrtk 0:d0f130e27d32 43 dev_connected = false;
mtkrtk 0:d0f130e27d32 44 blockSize = 0;
mtkrtk 0:d0f130e27d32 45 blockCount = 0;
mtkrtk 0:d0f130e27d32 46 msd_intf = -1;
mtkrtk 0:d0f130e27d32 47 msd_device_found = false;
mtkrtk 0:d0f130e27d32 48 disk_init = false;
mtkrtk 0:d0f130e27d32 49 dev_connected = false;
mtkrtk 0:d0f130e27d32 50 nb_ep = 0;
mtkrtk 0:d0f130e27d32 51 }
mtkrtk 0:d0f130e27d32 52
mtkrtk 0:d0f130e27d32 53
mtkrtk 0:d0f130e27d32 54 bool USBHostMSD::connected()
mtkrtk 0:d0f130e27d32 55 {
mtkrtk 0:d0f130e27d32 56 return dev_connected;
mtkrtk 0:d0f130e27d32 57 }
mtkrtk 0:d0f130e27d32 58
mtkrtk 0:d0f130e27d32 59 bool USBHostMSD::connect()
mtkrtk 0:d0f130e27d32 60 {
mtkrtk 0:d0f130e27d32 61
mtkrtk 0:d0f130e27d32 62 if (dev_connected) {
mtkrtk 0:d0f130e27d32 63 return true;
mtkrtk 0:d0f130e27d32 64 }
mtkrtk 0:d0f130e27d32 65
mtkrtk 0:d0f130e27d32 66 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
mtkrtk 0:d0f130e27d32 67 if ((dev = host->getDevice(i)) != NULL) {
mtkrtk 0:d0f130e27d32 68
mtkrtk 0:d0f130e27d32 69 USB_DBG("Trying to connect MSD device\r\n");
mtkrtk 0:d0f130e27d32 70
mtkrtk 0:d0f130e27d32 71 if(host->enumerate(dev, this))
mtkrtk 0:d0f130e27d32 72 break;
mtkrtk 0:d0f130e27d32 73
mtkrtk 0:d0f130e27d32 74 if (msd_device_found) {
mtkrtk 0:d0f130e27d32 75 bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN);
mtkrtk 0:d0f130e27d32 76 bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT);
mtkrtk 0:d0f130e27d32 77
mtkrtk 0:d0f130e27d32 78 if (!bulk_in || !bulk_out)
mtkrtk 0:d0f130e27d32 79 continue;
mtkrtk 0:d0f130e27d32 80
mtkrtk 0:d0f130e27d32 81 USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf);
mtkrtk 0:d0f130e27d32 82 dev->setName("MSD", msd_intf);
mtkrtk 0:d0f130e27d32 83 host->registerDriver(dev, msd_intf, this, &USBHostMSD::init);
mtkrtk 0:d0f130e27d32 84
mtkrtk 0:d0f130e27d32 85 dev_connected = true;
mtkrtk 0:d0f130e27d32 86 return true;
mtkrtk 0:d0f130e27d32 87 }
mtkrtk 0:d0f130e27d32 88 } //if()
mtkrtk 0:d0f130e27d32 89 } //for()
mtkrtk 0:d0f130e27d32 90 init();
mtkrtk 0:d0f130e27d32 91 return false;
mtkrtk 0:d0f130e27d32 92 }
mtkrtk 0:d0f130e27d32 93
mtkrtk 0:d0f130e27d32 94 /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid)
mtkrtk 0:d0f130e27d32 95 {
mtkrtk 0:d0f130e27d32 96 // we don't check VID/PID for MSD driver
mtkrtk 0:d0f130e27d32 97 }
mtkrtk 0:d0f130e27d32 98
mtkrtk 0:d0f130e27d32 99 /*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
mtkrtk 0:d0f130e27d32 100 {
mtkrtk 0:d0f130e27d32 101 if ((msd_intf == -1) &&
mtkrtk 0:d0f130e27d32 102 (intf_class == MSD_CLASS) &&
mtkrtk 0:d0f130e27d32 103 (intf_subclass == 0x06) &&
mtkrtk 0:d0f130e27d32 104 (intf_protocol == 0x50)) {
mtkrtk 0:d0f130e27d32 105 msd_intf = intf_nb;
mtkrtk 0:d0f130e27d32 106 return true;
mtkrtk 0:d0f130e27d32 107 }
mtkrtk 0:d0f130e27d32 108 return false;
mtkrtk 0:d0f130e27d32 109 }
mtkrtk 0:d0f130e27d32 110
mtkrtk 0:d0f130e27d32 111 /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
mtkrtk 0:d0f130e27d32 112 {
mtkrtk 0:d0f130e27d32 113 if (intf_nb == msd_intf) {
mtkrtk 0:d0f130e27d32 114 if (type == BULK_ENDPOINT) {
mtkrtk 0:d0f130e27d32 115 nb_ep++;
mtkrtk 0:d0f130e27d32 116 if (nb_ep == 2)
mtkrtk 0:d0f130e27d32 117 msd_device_found = true;
mtkrtk 0:d0f130e27d32 118 return true;
mtkrtk 0:d0f130e27d32 119 }
mtkrtk 0:d0f130e27d32 120 }
mtkrtk 0:d0f130e27d32 121 return false;
mtkrtk 0:d0f130e27d32 122 }
mtkrtk 0:d0f130e27d32 123
mtkrtk 0:d0f130e27d32 124
mtkrtk 0:d0f130e27d32 125 int USBHostMSD::testUnitReady() {
mtkrtk 0:d0f130e27d32 126 USB_DBG("Test unit ready");
mtkrtk 0:d0f130e27d32 127 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
mtkrtk 0:d0f130e27d32 128 }
mtkrtk 0:d0f130e27d32 129
mtkrtk 0:d0f130e27d32 130
mtkrtk 0:d0f130e27d32 131 int USBHostMSD::readCapacity() {
mtkrtk 0:d0f130e27d32 132 USB_DBG("Read capacity");
mtkrtk 0:d0f130e27d32 133 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
mtkrtk 0:d0f130e27d32 134 uint8_t result[8];
mtkrtk 0:d0f130e27d32 135 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8);
mtkrtk 0:d0f130e27d32 136 if (status == 0) {
mtkrtk 0:d0f130e27d32 137 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3];
mtkrtk 0:d0f130e27d32 138 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7];
mtkrtk 0:d0f130e27d32 139 USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize);
mtkrtk 0:d0f130e27d32 140 }
mtkrtk 0:d0f130e27d32 141 return status;
mtkrtk 0:d0f130e27d32 142 }
mtkrtk 0:d0f130e27d32 143
mtkrtk 0:d0f130e27d32 144
mtkrtk 0:d0f130e27d32 145 int USBHostMSD::SCSIRequestSense() {
mtkrtk 0:d0f130e27d32 146 USB_DBG("Request sense");
mtkrtk 0:d0f130e27d32 147 uint8_t cmd[6] = {0x03,0,0,0,18,0};
mtkrtk 0:d0f130e27d32 148 uint8_t result[18];
mtkrtk 0:d0f130e27d32 149 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18);
mtkrtk 0:d0f130e27d32 150 return status;
mtkrtk 0:d0f130e27d32 151 }
mtkrtk 0:d0f130e27d32 152
mtkrtk 0:d0f130e27d32 153
mtkrtk 0:d0f130e27d32 154 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
mtkrtk 0:d0f130e27d32 155 USB_DBG("Inquiry");
mtkrtk 0:d0f130e27d32 156 uint8_t evpd = (page_code == 0) ? 0 : 1;
mtkrtk 0:d0f130e27d32 157 uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0};
mtkrtk 0:d0f130e27d32 158 uint8_t result[36];
mtkrtk 0:d0f130e27d32 159 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36);
mtkrtk 0:d0f130e27d32 160 if (status == 0) {
mtkrtk 0:d0f130e27d32 161 char vid_pid[17];
mtkrtk 0:d0f130e27d32 162 memcpy(vid_pid, &result[8], 8);
mtkrtk 0:d0f130e27d32 163 vid_pid[8] = 0;
mtkrtk 0:d0f130e27d32 164 USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid);
mtkrtk 0:d0f130e27d32 165
mtkrtk 0:d0f130e27d32 166 memcpy(vid_pid, &result[16], 16);
mtkrtk 0:d0f130e27d32 167 vid_pid[16] = 0;
mtkrtk 0:d0f130e27d32 168 USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid);
mtkrtk 0:d0f130e27d32 169
mtkrtk 0:d0f130e27d32 170 memcpy(vid_pid, &result[32], 4);
mtkrtk 0:d0f130e27d32 171 vid_pid[4] = 0;
mtkrtk 0:d0f130e27d32 172 USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid);
mtkrtk 0:d0f130e27d32 173 }
mtkrtk 0:d0f130e27d32 174 return status;
mtkrtk 0:d0f130e27d32 175 }
mtkrtk 0:d0f130e27d32 176
mtkrtk 0:d0f130e27d32 177 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
mtkrtk 0:d0f130e27d32 178 // if ep stalled: send clear feature
mtkrtk 0:d0f130e27d32 179 if (res == USB_TYPE_STALL_ERROR) {
mtkrtk 0:d0f130e27d32 180 res = host->controlWrite( dev,
mtkrtk 0:d0f130e27d32 181 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
mtkrtk 0:d0f130e27d32 182 CLEAR_FEATURE,
mtkrtk 0:d0f130e27d32 183 0, ep->getAddress(), NULL, 0);
mtkrtk 0:d0f130e27d32 184 // set state to IDLE if clear feature successful
mtkrtk 0:d0f130e27d32 185 if (res == USB_TYPE_OK) {
mtkrtk 0:d0f130e27d32 186 ep->setState(USB_TYPE_IDLE);
mtkrtk 0:d0f130e27d32 187 }
mtkrtk 0:d0f130e27d32 188 }
mtkrtk 0:d0f130e27d32 189
mtkrtk 0:d0f130e27d32 190 if (res != USB_TYPE_OK)
mtkrtk 0:d0f130e27d32 191 return -1;
mtkrtk 0:d0f130e27d32 192
mtkrtk 0:d0f130e27d32 193 return 0;
mtkrtk 0:d0f130e27d32 194 }
mtkrtk 0:d0f130e27d32 195
mtkrtk 0:d0f130e27d32 196
mtkrtk 0:d0f130e27d32 197 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
mtkrtk 0:d0f130e27d32 198
mtkrtk 0:d0f130e27d32 199 int res = 0;
mtkrtk 0:d0f130e27d32 200
mtkrtk 0:d0f130e27d32 201 cbw.Signature = CBW_SIGNATURE;
mtkrtk 0:d0f130e27d32 202 cbw.Tag = 0;
mtkrtk 0:d0f130e27d32 203 cbw.DataLength = transfer_len;
mtkrtk 0:d0f130e27d32 204 cbw.Flags = flags;
mtkrtk 0:d0f130e27d32 205 cbw.LUN = 0;
mtkrtk 0:d0f130e27d32 206 cbw.CBLength = cmd_len;
mtkrtk 0:d0f130e27d32 207 memset(cbw.CB,0,sizeof(cbw.CB));
mtkrtk 0:d0f130e27d32 208 if (cmd) {
mtkrtk 0:d0f130e27d32 209 memcpy(cbw.CB,cmd,cmd_len);
mtkrtk 0:d0f130e27d32 210 }
mtkrtk 0:d0f130e27d32 211
mtkrtk 0:d0f130e27d32 212 // send the cbw
mtkrtk 0:d0f130e27d32 213 USB_DBG("Send CBW");
mtkrtk 0:d0f130e27d32 214 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31);
mtkrtk 0:d0f130e27d32 215 if (checkResult(res, bulk_out))
mtkrtk 0:d0f130e27d32 216 return -1;
mtkrtk 0:d0f130e27d32 217
mtkrtk 0:d0f130e27d32 218 // data stage if needed
mtkrtk 0:d0f130e27d32 219 if (data) {
mtkrtk 0:d0f130e27d32 220 USB_DBG("data stage");
mtkrtk 0:d0f130e27d32 221 if (flags == HOST_TO_DEVICE) {
mtkrtk 0:d0f130e27d32 222
mtkrtk 0:d0f130e27d32 223 res = host->bulkWrite(dev, bulk_out, data, transfer_len);
mtkrtk 0:d0f130e27d32 224 if (checkResult(res, bulk_out))
mtkrtk 0:d0f130e27d32 225 return -1;
mtkrtk 0:d0f130e27d32 226
mtkrtk 0:d0f130e27d32 227 } else if (flags == DEVICE_TO_HOST) {
mtkrtk 0:d0f130e27d32 228
mtkrtk 0:d0f130e27d32 229 res = host->bulkRead(dev, bulk_in, data, transfer_len);
mtkrtk 0:d0f130e27d32 230 if (checkResult(res, bulk_in))
mtkrtk 0:d0f130e27d32 231 return -1;
mtkrtk 0:d0f130e27d32 232 }
mtkrtk 0:d0f130e27d32 233 }
mtkrtk 0:d0f130e27d32 234
mtkrtk 0:d0f130e27d32 235 // status stage
mtkrtk 0:d0f130e27d32 236 csw.Signature = 0;
mtkrtk 0:d0f130e27d32 237 USB_DBG("Read CSW");
mtkrtk 0:d0f130e27d32 238 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13);
mtkrtk 0:d0f130e27d32 239 if (checkResult(res, bulk_in))
mtkrtk 0:d0f130e27d32 240 return -1;
mtkrtk 0:d0f130e27d32 241
mtkrtk 0:d0f130e27d32 242 if (csw.Signature != CSW_SIGNATURE) {
mtkrtk 0:d0f130e27d32 243 return -1;
mtkrtk 0:d0f130e27d32 244 }
mtkrtk 0:d0f130e27d32 245
mtkrtk 0:d0f130e27d32 246 USB_DBG("recv csw: status: %d", csw.Status);
mtkrtk 0:d0f130e27d32 247
mtkrtk 0:d0f130e27d32 248 // ModeSense?
mtkrtk 0:d0f130e27d32 249 if ((csw.Status == 1) && (cmd[0] != 0x03)) {
mtkrtk 0:d0f130e27d32 250 USB_DBG("request mode sense");
mtkrtk 0:d0f130e27d32 251 return SCSIRequestSense();
mtkrtk 0:d0f130e27d32 252 }
mtkrtk 0:d0f130e27d32 253
mtkrtk 0:d0f130e27d32 254 // perform reset recovery
mtkrtk 0:d0f130e27d32 255 if ((csw.Status == 2) && (cmd[0] != 0x03)) {
mtkrtk 0:d0f130e27d32 256
mtkrtk 0:d0f130e27d32 257 // send Bulk-Only Mass Storage Reset request
mtkrtk 0:d0f130e27d32 258 res = host->controlWrite( dev,
mtkrtk 0:d0f130e27d32 259 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
mtkrtk 0:d0f130e27d32 260 BO_MASS_STORAGE_RESET,
mtkrtk 0:d0f130e27d32 261 0, msd_intf, NULL, 0);
mtkrtk 0:d0f130e27d32 262
mtkrtk 0:d0f130e27d32 263 // unstall both endpoints
mtkrtk 0:d0f130e27d32 264 res = host->controlWrite( dev,
mtkrtk 0:d0f130e27d32 265 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
mtkrtk 0:d0f130e27d32 266 CLEAR_FEATURE,
mtkrtk 0:d0f130e27d32 267 0, bulk_in->getAddress(), NULL, 0);
mtkrtk 0:d0f130e27d32 268
mtkrtk 0:d0f130e27d32 269 res = host->controlWrite( dev,
mtkrtk 0:d0f130e27d32 270 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
mtkrtk 0:d0f130e27d32 271 CLEAR_FEATURE,
mtkrtk 0:d0f130e27d32 272 0, bulk_out->getAddress(), NULL, 0);
mtkrtk 0:d0f130e27d32 273
mtkrtk 0:d0f130e27d32 274 }
mtkrtk 0:d0f130e27d32 275
mtkrtk 0:d0f130e27d32 276 return csw.Status;
mtkrtk 0:d0f130e27d32 277 }
mtkrtk 0:d0f130e27d32 278
mtkrtk 0:d0f130e27d32 279
mtkrtk 0:d0f130e27d32 280 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
mtkrtk 0:d0f130e27d32 281 uint8_t cmd[10];
mtkrtk 0:d0f130e27d32 282 memset(cmd,0,10);
mtkrtk 0:d0f130e27d32 283 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
mtkrtk 0:d0f130e27d32 284
mtkrtk 0:d0f130e27d32 285 cmd[2] = (block >> 24) & 0xff;
mtkrtk 0:d0f130e27d32 286 cmd[3] = (block >> 16) & 0xff;
mtkrtk 0:d0f130e27d32 287 cmd[4] = (block >> 8) & 0xff;
mtkrtk 0:d0f130e27d32 288 cmd[5] = block & 0xff;
mtkrtk 0:d0f130e27d32 289
mtkrtk 0:d0f130e27d32 290 cmd[7] = (nbBlock >> 8) & 0xff;
mtkrtk 0:d0f130e27d32 291 cmd[8] = nbBlock & 0xff;
mtkrtk 0:d0f130e27d32 292
mtkrtk 0:d0f130e27d32 293 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
mtkrtk 0:d0f130e27d32 294 }
mtkrtk 0:d0f130e27d32 295
mtkrtk 0:d0f130e27d32 296 int USBHostMSD::getMaxLun() {
mtkrtk 0:d0f130e27d32 297 uint8_t buf[1], res;
mtkrtk 0:d0f130e27d32 298 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
mtkrtk 0:d0f130e27d32 299 0xfe, 0, msd_intf, buf, 1);
mtkrtk 0:d0f130e27d32 300 USB_DBG("max lun: %d", buf[0]);
mtkrtk 0:d0f130e27d32 301 return res;
mtkrtk 0:d0f130e27d32 302 }
mtkrtk 0:d0f130e27d32 303
mtkrtk 0:d0f130e27d32 304 int USBHostMSD::disk_initialize() {
mtkrtk 0:d0f130e27d32 305 USB_DBG("FILESYSTEM: init");
mtkrtk 0:d0f130e27d32 306 uint16_t i, timeout = 10;
mtkrtk 0:d0f130e27d32 307
mtkrtk 0:d0f130e27d32 308 getMaxLun();
mtkrtk 0:d0f130e27d32 309
mtkrtk 0:d0f130e27d32 310 for (i = 0; i < timeout; i++) {
mtkrtk 0:d0f130e27d32 311 rtos::Thread::wait(100);
mtkrtk 0:d0f130e27d32 312 if (!testUnitReady())
mtkrtk 0:d0f130e27d32 313 break;
mtkrtk 0:d0f130e27d32 314 }
mtkrtk 0:d0f130e27d32 315
mtkrtk 0:d0f130e27d32 316 if (i == timeout) {
mtkrtk 0:d0f130e27d32 317 disk_init = false;
mtkrtk 0:d0f130e27d32 318 return -1;
mtkrtk 0:d0f130e27d32 319 }
mtkrtk 0:d0f130e27d32 320
mtkrtk 0:d0f130e27d32 321 inquiry(0, 0);
mtkrtk 0:d0f130e27d32 322 disk_init = 1;
mtkrtk 0:d0f130e27d32 323 return readCapacity();
mtkrtk 0:d0f130e27d32 324 }
mtkrtk 0:d0f130e27d32 325
mtkrtk 0:d0f130e27d32 326 int USBHostMSD::disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count) {
mtkrtk 0:d0f130e27d32 327 USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count);
mtkrtk 0:d0f130e27d32 328 if (!disk_init) {
mtkrtk 0:d0f130e27d32 329 disk_initialize();
mtkrtk 0:d0f130e27d32 330 }
mtkrtk 0:d0f130e27d32 331 if (!disk_init)
mtkrtk 0:d0f130e27d32 332 return -1;
mtkrtk 0:d0f130e27d32 333 for (uint32_t b = block_number; b < block_number + count; b++) {
mtkrtk 0:d0f130e27d32 334 if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE))
mtkrtk 0:d0f130e27d32 335 return -1;
mtkrtk 0:d0f130e27d32 336 buffer += 512;
mtkrtk 0:d0f130e27d32 337 }
mtkrtk 0:d0f130e27d32 338 return 0;
mtkrtk 0:d0f130e27d32 339 }
mtkrtk 0:d0f130e27d32 340
mtkrtk 0:d0f130e27d32 341 int USBHostMSD::disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count) {
mtkrtk 0:d0f130e27d32 342 USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count);
mtkrtk 0:d0f130e27d32 343 if (!disk_init) {
mtkrtk 0:d0f130e27d32 344 disk_initialize();
mtkrtk 0:d0f130e27d32 345 }
mtkrtk 0:d0f130e27d32 346 if (!disk_init)
mtkrtk 0:d0f130e27d32 347 return -1;
mtkrtk 0:d0f130e27d32 348 for (uint32_t b = block_number; b < block_number + count; b++) {
mtkrtk 0:d0f130e27d32 349 if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST))
mtkrtk 0:d0f130e27d32 350 return -1;
mtkrtk 0:d0f130e27d32 351 buffer += 512;
mtkrtk 0:d0f130e27d32 352 }
mtkrtk 0:d0f130e27d32 353 return 0;
mtkrtk 0:d0f130e27d32 354 }
mtkrtk 0:d0f130e27d32 355
mtkrtk 0:d0f130e27d32 356 uint32_t USBHostMSD::disk_sectors() {
mtkrtk 0:d0f130e27d32 357 USB_DBG("FILESYSTEM: sectors");
mtkrtk 0:d0f130e27d32 358 if (!disk_init) {
mtkrtk 0:d0f130e27d32 359 disk_initialize();
mtkrtk 0:d0f130e27d32 360 }
mtkrtk 0:d0f130e27d32 361 if (!disk_init)
mtkrtk 0:d0f130e27d32 362 return 0;
mtkrtk 0:d0f130e27d32 363 return blockCount;
mtkrtk 0:d0f130e27d32 364 }
mtkrtk 0:d0f130e27d32 365
mtkrtk 0:d0f130e27d32 366 #endif
mtkrtk 0:d0f130e27d32 367