joseph adamu / Mbed OS CW_final_thr

Fork of CW_copy by Calvin Kalintra

Committer:
joseph_adamu
Date:
Wed Jan 10 09:50:29 2018 +0000
Revision:
1:dc648c5624b9
version for export

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joseph_adamu 1:dc648c5624b9 1 /* mbed Microcontroller Library
joseph_adamu 1:dc648c5624b9 2 * Copyright (c) 2006-2012 ARM Limited
joseph_adamu 1:dc648c5624b9 3 *
joseph_adamu 1:dc648c5624b9 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
joseph_adamu 1:dc648c5624b9 5 * of this software and associated documentation files (the "Software"), to deal
joseph_adamu 1:dc648c5624b9 6 * in the Software without restriction, including without limitation the rights
joseph_adamu 1:dc648c5624b9 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
joseph_adamu 1:dc648c5624b9 8 * copies of the Software, and to permit persons to whom the Software is
joseph_adamu 1:dc648c5624b9 9 * furnished to do so, subject to the following conditions:
joseph_adamu 1:dc648c5624b9 10 *
joseph_adamu 1:dc648c5624b9 11 * The above copyright notice and this permission notice shall be included in
joseph_adamu 1:dc648c5624b9 12 * all copies or substantial portions of the Software.
joseph_adamu 1:dc648c5624b9 13 *
joseph_adamu 1:dc648c5624b9 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
joseph_adamu 1:dc648c5624b9 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
joseph_adamu 1:dc648c5624b9 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
joseph_adamu 1:dc648c5624b9 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
joseph_adamu 1:dc648c5624b9 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
joseph_adamu 1:dc648c5624b9 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
joseph_adamu 1:dc648c5624b9 20 * SOFTWARE.
joseph_adamu 1:dc648c5624b9 21 */
joseph_adamu 1:dc648c5624b9 22 #ifndef MBED_SD_BLOCK_DEVICE_H
joseph_adamu 1:dc648c5624b9 23 #define MBED_SD_BLOCK_DEVICE_H
joseph_adamu 1:dc648c5624b9 24
joseph_adamu 1:dc648c5624b9 25 /* If the target has no SPI support then SDCard is not supported */
joseph_adamu 1:dc648c5624b9 26 #ifdef DEVICE_SPI
joseph_adamu 1:dc648c5624b9 27
joseph_adamu 1:dc648c5624b9 28 #include "BlockDevice.h"
joseph_adamu 1:dc648c5624b9 29 #include "mbed.h"
joseph_adamu 1:dc648c5624b9 30
joseph_adamu 1:dc648c5624b9 31
joseph_adamu 1:dc648c5624b9 32 /** Access an SD Card using SPI
joseph_adamu 1:dc648c5624b9 33 *
joseph_adamu 1:dc648c5624b9 34 * @code
joseph_adamu 1:dc648c5624b9 35 * #include "mbed.h"
joseph_adamu 1:dc648c5624b9 36 * #include "SDBlockDevice.h"
joseph_adamu 1:dc648c5624b9 37 *
joseph_adamu 1:dc648c5624b9 38 * SDBlockDevice sd(p5, p6, p7, p12); // mosi, miso, sclk, cs
joseph_adamu 1:dc648c5624b9 39 * uint8_t block[512] = "Hello World!\n";
joseph_adamu 1:dc648c5624b9 40 *
joseph_adamu 1:dc648c5624b9 41 * int main() {
joseph_adamu 1:dc648c5624b9 42 * sd.init();
joseph_adamu 1:dc648c5624b9 43 * sd.write(block, 0, 512);
joseph_adamu 1:dc648c5624b9 44 * sd.read(block, 0, 512);
joseph_adamu 1:dc648c5624b9 45 * printf("%s", block);
joseph_adamu 1:dc648c5624b9 46 * sd.deinit();
joseph_adamu 1:dc648c5624b9 47 * }
joseph_adamu 1:dc648c5624b9 48 */
joseph_adamu 1:dc648c5624b9 49 class SDBlockDevice : public BlockDevice {
joseph_adamu 1:dc648c5624b9 50 public:
joseph_adamu 1:dc648c5624b9 51 /** Lifetime of an SD card
joseph_adamu 1:dc648c5624b9 52 */
joseph_adamu 1:dc648c5624b9 53 SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz=1000000);
joseph_adamu 1:dc648c5624b9 54 virtual ~SDBlockDevice();
joseph_adamu 1:dc648c5624b9 55
joseph_adamu 1:dc648c5624b9 56 /** Initialize a block device
joseph_adamu 1:dc648c5624b9 57 *
joseph_adamu 1:dc648c5624b9 58 * @return 0 on success or a negative error code on failure
joseph_adamu 1:dc648c5624b9 59 */
joseph_adamu 1:dc648c5624b9 60 virtual int init();
joseph_adamu 1:dc648c5624b9 61
joseph_adamu 1:dc648c5624b9 62 /** Deinitialize a block device
joseph_adamu 1:dc648c5624b9 63 *
joseph_adamu 1:dc648c5624b9 64 * @return 0 on success or a negative error code on failure
joseph_adamu 1:dc648c5624b9 65 */
joseph_adamu 1:dc648c5624b9 66 virtual int deinit();
joseph_adamu 1:dc648c5624b9 67
joseph_adamu 1:dc648c5624b9 68 /** Read blocks from a block device
joseph_adamu 1:dc648c5624b9 69 *
joseph_adamu 1:dc648c5624b9 70 * @param buffer Buffer to write blocks to
joseph_adamu 1:dc648c5624b9 71 * @param addr Address of block to begin reading from
joseph_adamu 1:dc648c5624b9 72 * @param size Size to read in bytes, must be a multiple of read block size
joseph_adamu 1:dc648c5624b9 73 * @return 0 on success, negative error code on failure
joseph_adamu 1:dc648c5624b9 74 */
joseph_adamu 1:dc648c5624b9 75 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
joseph_adamu 1:dc648c5624b9 76
joseph_adamu 1:dc648c5624b9 77 /** Program blocks to a block device
joseph_adamu 1:dc648c5624b9 78 *
joseph_adamu 1:dc648c5624b9 79 * The blocks must have been erased prior to being programmed
joseph_adamu 1:dc648c5624b9 80 *
joseph_adamu 1:dc648c5624b9 81 * @param buffer Buffer of data to write to blocks
joseph_adamu 1:dc648c5624b9 82 * @param addr Address of block to begin writing to
joseph_adamu 1:dc648c5624b9 83 * @param size Size to write in bytes, must be a multiple of program block size
joseph_adamu 1:dc648c5624b9 84 * @return 0 on success, negative error code on failure
joseph_adamu 1:dc648c5624b9 85 */
joseph_adamu 1:dc648c5624b9 86 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
joseph_adamu 1:dc648c5624b9 87
joseph_adamu 1:dc648c5624b9 88 /** Mark blocks as no longer in use
joseph_adamu 1:dc648c5624b9 89 *
joseph_adamu 1:dc648c5624b9 90 * This function provides a hint to the underlying block device that a region of blocks
joseph_adamu 1:dc648c5624b9 91 * is no longer in use and may be erased without side effects. Erase must still be called
joseph_adamu 1:dc648c5624b9 92 * before programming, but trimming allows flash-translation-layers to schedule erases when
joseph_adamu 1:dc648c5624b9 93 * the device is not busy.
joseph_adamu 1:dc648c5624b9 94 *
joseph_adamu 1:dc648c5624b9 95 * @param addr Address of block to mark as unused
joseph_adamu 1:dc648c5624b9 96 * @param size Size to mark as unused in bytes, must be a multiple of erase block size
joseph_adamu 1:dc648c5624b9 97 * @return 0 on success, negative error code on failure
joseph_adamu 1:dc648c5624b9 98 */
joseph_adamu 1:dc648c5624b9 99 virtual int trim(bd_addr_t addr, bd_size_t size);
joseph_adamu 1:dc648c5624b9 100
joseph_adamu 1:dc648c5624b9 101 /** Get the size of a readable block
joseph_adamu 1:dc648c5624b9 102 *
joseph_adamu 1:dc648c5624b9 103 * @return Size of a readable block in bytes
joseph_adamu 1:dc648c5624b9 104 */
joseph_adamu 1:dc648c5624b9 105 virtual bd_size_t get_read_size() const;
joseph_adamu 1:dc648c5624b9 106
joseph_adamu 1:dc648c5624b9 107 /** Get the size of a programable block
joseph_adamu 1:dc648c5624b9 108 *
joseph_adamu 1:dc648c5624b9 109 * @return Size of a programable block in bytes
joseph_adamu 1:dc648c5624b9 110 * @note Must be a multiple of the read size
joseph_adamu 1:dc648c5624b9 111 */
joseph_adamu 1:dc648c5624b9 112 virtual bd_size_t get_program_size() const;
joseph_adamu 1:dc648c5624b9 113
joseph_adamu 1:dc648c5624b9 114 /** Get the total size of the underlying device
joseph_adamu 1:dc648c5624b9 115 *
joseph_adamu 1:dc648c5624b9 116 * @return Size of the underlying device in bytes
joseph_adamu 1:dc648c5624b9 117 */
joseph_adamu 1:dc648c5624b9 118 virtual bd_size_t size() const;
joseph_adamu 1:dc648c5624b9 119
joseph_adamu 1:dc648c5624b9 120 /** Enable or disable debugging
joseph_adamu 1:dc648c5624b9 121 *
joseph_adamu 1:dc648c5624b9 122 * @param State of debugging
joseph_adamu 1:dc648c5624b9 123 */
joseph_adamu 1:dc648c5624b9 124 virtual void debug(bool dbg);
joseph_adamu 1:dc648c5624b9 125
joseph_adamu 1:dc648c5624b9 126 /** Set the transfer frequency
joseph_adamu 1:dc648c5624b9 127 *
joseph_adamu 1:dc648c5624b9 128 * @param Transfer frequency
joseph_adamu 1:dc648c5624b9 129 * @note Max frequency supported is 25MHZ
joseph_adamu 1:dc648c5624b9 130 */
joseph_adamu 1:dc648c5624b9 131 virtual int frequency(uint64_t freq);
joseph_adamu 1:dc648c5624b9 132
joseph_adamu 1:dc648c5624b9 133
joseph_adamu 1:dc648c5624b9 134 private:
joseph_adamu 1:dc648c5624b9 135 /* Commands : Listed below are commands supported
joseph_adamu 1:dc648c5624b9 136 * in SPI mode for SD card : Only Mandatory ones
joseph_adamu 1:dc648c5624b9 137 */
joseph_adamu 1:dc648c5624b9 138 enum cmdSupported {
joseph_adamu 1:dc648c5624b9 139 CMD_NOT_SUPPORTED = -1, /**< Command not supported error */
joseph_adamu 1:dc648c5624b9 140 CMD0_GO_IDLE_STATE = 0, /**< Resets the SD Memory Card */
joseph_adamu 1:dc648c5624b9 141 CMD1_SEND_OP_COND = 1, /**< Sends host capacity support */
joseph_adamu 1:dc648c5624b9 142 CMD6_SWITCH_FUNC = 6, /**< Check and Switches card function */
joseph_adamu 1:dc648c5624b9 143 CMD8_SEND_IF_COND = 8, /**< Supply voltage info */
joseph_adamu 1:dc648c5624b9 144 CMD9_SEND_CSD = 9, /**< Provides Card Specific data */
joseph_adamu 1:dc648c5624b9 145 CMD10_SEND_CID = 10, /**< Provides Card Identification */
joseph_adamu 1:dc648c5624b9 146 CMD12_STOP_TRANSMISSION = 12, /**< Forces the card to stop transmission */
joseph_adamu 1:dc648c5624b9 147 CMD13_SEND_STATUS = 13, /**< Card responds with status */
joseph_adamu 1:dc648c5624b9 148 CMD16_SET_BLOCKLEN = 16, /**< Length for SC card is set */
joseph_adamu 1:dc648c5624b9 149 CMD17_READ_SINGLE_BLOCK = 17, /**< Read single block of data */
joseph_adamu 1:dc648c5624b9 150 CMD18_READ_MULTIPLE_BLOCK = 18, /**< Card transfers data blocks to host until interrupted
joseph_adamu 1:dc648c5624b9 151 by a STOP_TRANSMISSION command */
joseph_adamu 1:dc648c5624b9 152 CMD24_WRITE_BLOCK = 24, /**< Write single block of data */
joseph_adamu 1:dc648c5624b9 153 CMD25_WRITE_MULTIPLE_BLOCK = 25, /**< Continuously writes blocks of data until
joseph_adamu 1:dc648c5624b9 154 'Stop Tran' token is sent */
joseph_adamu 1:dc648c5624b9 155 CMD27_PROGRAM_CSD = 27, /**< Programming bits of CSD */
joseph_adamu 1:dc648c5624b9 156 CMD32_ERASE_WR_BLK_START_ADDR = 32, /**< Sets the address of the first write
joseph_adamu 1:dc648c5624b9 157 block to be erased. */
joseph_adamu 1:dc648c5624b9 158 CMD33_ERASE_WR_BLK_END_ADDR = 33, /**< Sets the address of the last write
joseph_adamu 1:dc648c5624b9 159 block of the continuous range to be erased.*/
joseph_adamu 1:dc648c5624b9 160 CMD38_ERASE = 38, /**< Erases all previously selected write blocks */
joseph_adamu 1:dc648c5624b9 161 CMD55_APP_CMD = 55, /**< Extend to Applications specific commands */
joseph_adamu 1:dc648c5624b9 162 CMD56_GEN_CMD = 56, /**< General Purpose Command */
joseph_adamu 1:dc648c5624b9 163 CMD58_READ_OCR = 58, /**< Read OCR register of card */
joseph_adamu 1:dc648c5624b9 164 CMD59_CRC_ON_OFF = 59, /**< Turns the CRC option on or off*/
joseph_adamu 1:dc648c5624b9 165 // App Commands
joseph_adamu 1:dc648c5624b9 166 ACMD6_SET_BUS_WIDTH = 6,
joseph_adamu 1:dc648c5624b9 167 ACMD13_SD_STATUS = 13,
joseph_adamu 1:dc648c5624b9 168 ACMD22_SEND_NUM_WR_BLOCKS = 22,
joseph_adamu 1:dc648c5624b9 169 ACMD23_SET_WR_BLK_ERASE_COUNT = 23,
joseph_adamu 1:dc648c5624b9 170 ACMD41_SD_SEND_OP_COND = 41,
joseph_adamu 1:dc648c5624b9 171 ACMD42_SET_CLR_CARD_DETECT = 42,
joseph_adamu 1:dc648c5624b9 172 ACMD51_SEND_SCR = 51,
joseph_adamu 1:dc648c5624b9 173 };
joseph_adamu 1:dc648c5624b9 174
joseph_adamu 1:dc648c5624b9 175 uint8_t _card_type;
joseph_adamu 1:dc648c5624b9 176 int _cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAcmd=0, uint32_t *resp=NULL);
joseph_adamu 1:dc648c5624b9 177 int _cmd8();
joseph_adamu 1:dc648c5624b9 178
joseph_adamu 1:dc648c5624b9 179 /* Move the SDCard into the SPI Mode idle state
joseph_adamu 1:dc648c5624b9 180 *
joseph_adamu 1:dc648c5624b9 181 * The card is transitioned from SDCard mode to SPI mode by sending the
joseph_adamu 1:dc648c5624b9 182 * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the
joseph_adamu 1:dc648c5624b9 183 * "SPI Startup" section of the comments at the head of the
joseph_adamu 1:dc648c5624b9 184 * implementation file for further details and specification references.
joseph_adamu 1:dc648c5624b9 185 *
joseph_adamu 1:dc648c5624b9 186 * @return Response form the card. R1_IDLE_STATE (0x1), the successful
joseph_adamu 1:dc648c5624b9 187 * response from CMD0. R1_XXX_XXX for more response
joseph_adamu 1:dc648c5624b9 188 */
joseph_adamu 1:dc648c5624b9 189 uint32_t _go_idle_state();
joseph_adamu 1:dc648c5624b9 190 int _initialise_card();
joseph_adamu 1:dc648c5624b9 191
joseph_adamu 1:dc648c5624b9 192 uint32_t _sectors;
joseph_adamu 1:dc648c5624b9 193 uint32_t _sd_sectors();
joseph_adamu 1:dc648c5624b9 194
joseph_adamu 1:dc648c5624b9 195 bool _is_valid_trim(bd_addr_t addr, bd_size_t size);
joseph_adamu 1:dc648c5624b9 196
joseph_adamu 1:dc648c5624b9 197 /* SPI functions */
joseph_adamu 1:dc648c5624b9 198 Timer _spi_timer; /**< Timer Class object used for busy wait */
joseph_adamu 1:dc648c5624b9 199 uint32_t _init_sck; /**< Intial SPI frequency */
joseph_adamu 1:dc648c5624b9 200 uint32_t _transfer_sck; /**< SPI frequency during data transfer/after initialization */
joseph_adamu 1:dc648c5624b9 201 SPI _spi; /**< SPI Class object */
joseph_adamu 1:dc648c5624b9 202
joseph_adamu 1:dc648c5624b9 203 /* SPI initialization function */
joseph_adamu 1:dc648c5624b9 204 void _spi_init();
joseph_adamu 1:dc648c5624b9 205 uint8_t _cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg);
joseph_adamu 1:dc648c5624b9 206 void _spi_wait(uint8_t count);
joseph_adamu 1:dc648c5624b9 207
joseph_adamu 1:dc648c5624b9 208 bool _wait_token(uint8_t token); /**< Wait for token */
joseph_adamu 1:dc648c5624b9 209 bool _wait_ready(uint16_t ms=300); /**< 300ms default wait for card to be ready */
joseph_adamu 1:dc648c5624b9 210 int _read(uint8_t * buffer, uint32_t length);
joseph_adamu 1:dc648c5624b9 211 int _read_bytes(uint8_t * buffer, uint32_t length);
joseph_adamu 1:dc648c5624b9 212 uint8_t _write(const uint8_t *buffer,uint8_t token, uint32_t length);
joseph_adamu 1:dc648c5624b9 213 int _freq(void);
joseph_adamu 1:dc648c5624b9 214
joseph_adamu 1:dc648c5624b9 215 /* Chip Select and SPI mode select */
joseph_adamu 1:dc648c5624b9 216 DigitalOut _cs;
joseph_adamu 1:dc648c5624b9 217 void _select();
joseph_adamu 1:dc648c5624b9 218 void _deselect();
joseph_adamu 1:dc648c5624b9 219
joseph_adamu 1:dc648c5624b9 220 mutable Mutex _lock;
joseph_adamu 1:dc648c5624b9 221 uint32_t _block_size;
joseph_adamu 1:dc648c5624b9 222 uint32_t _erase_size;
joseph_adamu 1:dc648c5624b9 223 bool _is_initialized;
joseph_adamu 1:dc648c5624b9 224 bool _dbg;
joseph_adamu 1:dc648c5624b9 225 };
joseph_adamu 1:dc648c5624b9 226
joseph_adamu 1:dc648c5624b9 227 #endif /* DEVICE_SPI */
joseph_adamu 1:dc648c5624b9 228
joseph_adamu 1:dc648c5624b9 229 #endif /* MBED_SD_BLOCK_DEVICE_H */