USB Mass Storage Device Block Device

Dependents:   mini_piano_player play_wav BlueCrutch FTHR_USBMSD_Demo_27

Fork of USBMSD_SD by Greg Steiert

Committer:
switches
Date:
Fri Feb 23 23:25:58 2018 +0000
Revision:
4:6b2f29de40d3
Parent:
USBMSD_SD.cpp@3:cce1e689c548
Child:
5:13fed87fb66f
Creating USBMSD Block Device implementation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 2:055119ccf5a7 1 /* mbed Microcontroller Library
samux 2:055119ccf5a7 2 * Copyright (c) 2006-2012 ARM Limited
samux 0:de50a209c5a9 3 *
samux 0:de50a209c5a9 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
samux 0:de50a209c5a9 5 * of this software and associated documentation files (the "Software"), to deal
samux 0:de50a209c5a9 6 * in the Software without restriction, including without limitation the rights
samux 0:de50a209c5a9 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
samux 0:de50a209c5a9 8 * copies of the Software, and to permit persons to whom the Software is
samux 0:de50a209c5a9 9 * furnished to do so, subject to the following conditions:
samux 0:de50a209c5a9 10 *
samux 0:de50a209c5a9 11 * The above copyright notice and this permission notice shall be included in
samux 0:de50a209c5a9 12 * all copies or substantial portions of the Software.
samux 0:de50a209c5a9 13 *
samux 0:de50a209c5a9 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
samux 0:de50a209c5a9 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
samux 0:de50a209c5a9 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
samux 0:de50a209c5a9 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
samux 0:de50a209c5a9 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 2:055119ccf5a7 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
samux 2:055119ccf5a7 20 * SOFTWARE.
samux 0:de50a209c5a9 21 */
samux 0:de50a209c5a9 22 /* Introduction
samux 0:de50a209c5a9 23 * ------------
samux 0:de50a209c5a9 24 * SD and MMC cards support a number of interfaces, but common to them all
samux 0:de50a209c5a9 25 * is one based on SPI. This is the one I'm implmenting because it means
samux 2:055119ccf5a7 26 * it is much more portable even though not so performant, and we already
samux 0:de50a209c5a9 27 * have the mbed SPI Interface!
samux 0:de50a209c5a9 28 *
samux 2:055119ccf5a7 29 * The main reference I'm using is Chapter 7, "SPI Mode" of:
samux 0:de50a209c5a9 30 * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
samux 0:de50a209c5a9 31 *
samux 0:de50a209c5a9 32 * SPI Startup
samux 0:de50a209c5a9 33 * -----------
samux 0:de50a209c5a9 34 * The SD card powers up in SD mode. The SPI interface mode is selected by
samux 2:055119ccf5a7 35 * asserting CS low and sending the reset command (CMD0). The card will
samux 0:de50a209c5a9 36 * respond with a (R1) response.
samux 0:de50a209c5a9 37 *
samux 2:055119ccf5a7 38 * CMD8 is optionally sent to determine the voltage range supported, and
samux 2:055119ccf5a7 39 * indirectly determine whether it is a version 1.x SD/non-SD card or
samux 0:de50a209c5a9 40 * version 2.x. I'll just ignore this for now.
samux 0:de50a209c5a9 41 *
samux 0:de50a209c5a9 42 * ACMD41 is repeatedly issued to initialise the card, until "in idle"
samux 0:de50a209c5a9 43 * (bit 0) of the R1 response goes to '0', indicating it is initialised.
samux 0:de50a209c5a9 44 *
samux 0:de50a209c5a9 45 * You should also indicate whether the host supports High Capicity cards,
samux 0:de50a209c5a9 46 * and check whether the card is high capacity - i'll also ignore this
samux 0:de50a209c5a9 47 *
samux 0:de50a209c5a9 48 * SPI Protocol
samux 0:de50a209c5a9 49 * ------------
samux 0:de50a209c5a9 50 * The SD SPI protocol is based on transactions made up of 8-bit words, with
samux 0:de50a209c5a9 51 * the host starting every bus transaction by asserting the CS signal low. The
samux 0:de50a209c5a9 52 * card always responds to commands, data blocks and errors.
samux 2:055119ccf5a7 53 *
samux 2:055119ccf5a7 54 * The protocol supports a CRC, but by default it is off (except for the
samux 0:de50a209c5a9 55 * first reset CMD0, where the CRC can just be pre-calculated, and CMD8)
samux 2:055119ccf5a7 56 * I'll leave the CRC off I think!
samux 2:055119ccf5a7 57 *
samux 2:055119ccf5a7 58 * Standard capacity cards have variable data block sizes, whereas High
samux 0:de50a209c5a9 59 * Capacity cards fix the size of data block to 512 bytes. I'll therefore
samux 0:de50a209c5a9 60 * just always use the Standard Capacity cards with a block size of 512 bytes.
samux 0:de50a209c5a9 61 * This is set with CMD16.
samux 0:de50a209c5a9 62 *
samux 2:055119ccf5a7 63 * You can read and write single blocks (CMD17, CMD25) or multiple blocks
samux 0:de50a209c5a9 64 * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When
samux 2:055119ccf5a7 65 * the card gets a read command, it responds with a response token, and then
samux 0:de50a209c5a9 66 * a data token or an error.
samux 2:055119ccf5a7 67 *
samux 0:de50a209c5a9 68 * SPI Command Format
samux 0:de50a209c5a9 69 * ------------------
samux 0:de50a209c5a9 70 * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC.
samux 0:de50a209c5a9 71 *
samux 0:de50a209c5a9 72 * +---------------+------------+------------+-----------+----------+--------------+
samux 0:de50a209c5a9 73 * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 |
samux 0:de50a209c5a9 74 * +---------------+------------+------------+-----------+----------+--------------+
samux 0:de50a209c5a9 75 *
samux 0:de50a209c5a9 76 * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95)
samux 0:de50a209c5a9 77 *
samux 0:de50a209c5a9 78 * All Application Specific commands shall be preceded with APP_CMD (CMD55).
samux 0:de50a209c5a9 79 *
samux 0:de50a209c5a9 80 * SPI Response Format
samux 0:de50a209c5a9 81 * -------------------
samux 0:de50a209c5a9 82 * The main response format (R1) is a status byte (normally zero). Key flags:
samux 2:055119ccf5a7 83 * idle - 1 if the card is in an idle state/initialising
samux 0:de50a209c5a9 84 * cmd - 1 if an illegal command code was detected
samux 0:de50a209c5a9 85 *
samux 0:de50a209c5a9 86 * +-------------------------------------------------+
samux 0:de50a209c5a9 87 * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle |
samux 0:de50a209c5a9 88 * +-------------------------------------------------+
samux 0:de50a209c5a9 89 *
samux 0:de50a209c5a9 90 * R1b is the same, except it is followed by a busy signal (zeros) until
samux 0:de50a209c5a9 91 * the first non-zero byte when it is ready again.
samux 0:de50a209c5a9 92 *
samux 0:de50a209c5a9 93 * Data Response Token
samux 0:de50a209c5a9 94 * -------------------
samux 2:055119ccf5a7 95 * Every data block written to the card is acknowledged by a byte
samux 0:de50a209c5a9 96 * response token
samux 0:de50a209c5a9 97 *
samux 0:de50a209c5a9 98 * +----------------------+
samux 0:de50a209c5a9 99 * | xxx | 0 | status | 1 |
samux 0:de50a209c5a9 100 * +----------------------+
samux 0:de50a209c5a9 101 * 010 - OK!
samux 0:de50a209c5a9 102 * 101 - CRC Error
samux 0:de50a209c5a9 103 * 110 - Write Error
samux 0:de50a209c5a9 104 *
samux 0:de50a209c5a9 105 * Single Block Read and Write
samux 0:de50a209c5a9 106 * ---------------------------
samux 0:de50a209c5a9 107 *
samux 0:de50a209c5a9 108 * Block transfers have a byte header, followed by the data, followed
samux 0:de50a209c5a9 109 * by a 16-bit CRC. In our case, the data will always be 512 bytes.
samux 2:055119ccf5a7 110 *
samux 0:de50a209c5a9 111 * +------+---------+---------+- - - -+---------+-----------+----------+
samux 2:055119ccf5a7 112 * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] |
samux 0:de50a209c5a9 113 * +------+---------+---------+- - - -+---------+-----------+----------+
samux 0:de50a209c5a9 114 */
switches 4:6b2f29de40d3 115 #include "USBMSD_BD.h"
samux 2:055119ccf5a7 116 #include "mbed_debug.h"
samux 0:de50a209c5a9 117
samux 0:de50a209c5a9 118 #define SD_COMMAND_TIMEOUT 5000
samux 0:de50a209c5a9 119
samux 2:055119ccf5a7 120 #define SD_DBG 0
samux 2:055119ccf5a7 121
switches 4:6b2f29de40d3 122 USBMSD_BD::USBMSD_BD(BlockDevice* bd, bd_size_t ssize) {
switches 4:6b2f29de40d3 123 _bd = bd;
switches 4:6b2f29de40d3 124 _ssize = ssize;
samux 2:055119ccf5a7 125 //no init
samux 2:055119ccf5a7 126 _status = 0x01;
samux 2:055119ccf5a7 127
switches 4:6b2f29de40d3 128 // connect();
samux 0:de50a209c5a9 129 }
samux 0:de50a209c5a9 130
switches 4:6b2f29de40d3 131 int USBMSD_BD::disk_initialize() {
switches 4:6b2f29de40d3 132 _sectors = _bd->size() / _ssize;
samux 2:055119ccf5a7 133
samux 1:923991b026e7 134 // OK
samux 1:923991b026e7 135 _status = 0x00;
samux 2:055119ccf5a7 136
samux 0:de50a209c5a9 137 return 0;
samux 0:de50a209c5a9 138 }
samux 0:de50a209c5a9 139
switches 4:6b2f29de40d3 140 int USBMSD_BD::disk_write(const uint8_t* buff, uint64_t sector, uint8_t count)
switches 4:6b2f29de40d3 141 {
switches 4:6b2f29de40d3 142 bd_addr_t addr = (bd_addr_t)sector*_ssize;
switches 4:6b2f29de40d3 143 bd_size_t size = (bd_size_t)count*_ssize;
switches 4:6b2f29de40d3 144 int err = _bd->program(buff, addr, size);
switches 4:6b2f29de40d3 145 return err;
samux 0:de50a209c5a9 146 }
samux 0:de50a209c5a9 147
switches 4:6b2f29de40d3 148 int USBMSD_BD::disk_read(uint8_t* buff, uint64_t sector, uint8_t count)
switches 4:6b2f29de40d3 149 {
switches 4:6b2f29de40d3 150 bd_addr_t addr = (bd_addr_t)sector*_ssize;
switches 4:6b2f29de40d3 151 bd_size_t size = (bd_size_t)count*_ssize;
switches 4:6b2f29de40d3 152 int err = _bd->read(buff, addr, size);
switches 4:6b2f29de40d3 153 return err;
samux 0:de50a209c5a9 154 }
samux 0:de50a209c5a9 155
switches 4:6b2f29de40d3 156 int USBMSD_BD::disk_status() { return _status; }
switches 4:6b2f29de40d3 157 int USBMSD_BD::disk_sync() { return 0; }
switches 4:6b2f29de40d3 158 uint64_t USBMSD_BD::disk_sectors() { return _sectors; }
switches 4:6b2f29de40d3 159 uint64_t USBMSD_BD::disk_size() { return (*_bd).size(); }
samux 2:055119ccf5a7 160
samux 0:de50a209c5a9 161
samux 0:de50a209c5a9 162 // PRIVATE FUNCTIONS
samux 0:de50a209c5a9 163