Working Maveric

Committer:
mettrque
Date:
Sun Jun 11 17:14:19 2017 +0000
Revision:
9:b6a9d1c44ed9
Parent:
6:ef95300898b2
Working on 11.06.2017

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mettrque 9:b6a9d1c44ed9 1 /* mbed Microcontroller Library
mettrque 9:b6a9d1c44ed9 2 * Copyright (c) 2006-2012 ARM Limited
mettrque 9:b6a9d1c44ed9 3 *
mettrque 9:b6a9d1c44ed9 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
mettrque 9:b6a9d1c44ed9 5 * of this software and associated documentation files (the "Software"), to deal
mettrque 9:b6a9d1c44ed9 6 * in the Software without restriction, including without limitation the rights
mettrque 9:b6a9d1c44ed9 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mettrque 9:b6a9d1c44ed9 8 * copies of the Software, and to permit persons to whom the Software is
mettrque 9:b6a9d1c44ed9 9 * furnished to do so, subject to the following conditions:
mettrque 9:b6a9d1c44ed9 10 *
mettrque 9:b6a9d1c44ed9 11 * The above copyright notice and this permission notice shall be included in
mettrque 9:b6a9d1c44ed9 12 * all copies or substantial portions of the Software.
mettrque 9:b6a9d1c44ed9 13 *
mettrque 9:b6a9d1c44ed9 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mettrque 9:b6a9d1c44ed9 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mettrque 9:b6a9d1c44ed9 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mettrque 9:b6a9d1c44ed9 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mettrque 9:b6a9d1c44ed9 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mettrque 9:b6a9d1c44ed9 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
mettrque 9:b6a9d1c44ed9 20 * SOFTWARE.
mettrque 9:b6a9d1c44ed9 21 */
mettrque 9:b6a9d1c44ed9 22 #ifndef MBED_SD_BLOCK_DEVICE_H
mettrque 9:b6a9d1c44ed9 23 #define MBED_SD_BLOCK_DEVICE_H
mettrque 9:b6a9d1c44ed9 24
mettrque 9:b6a9d1c44ed9 25 /* If the target has no SPI support then SDCard is not supported */
mettrque 9:b6a9d1c44ed9 26 #ifdef DEVICE_SPI
mettrque 9:b6a9d1c44ed9 27
mettrque 9:b6a9d1c44ed9 28 #include "BlockDevice.h"
mettrque 9:b6a9d1c44ed9 29 #include "mbed.h"
mettrque 9:b6a9d1c44ed9 30
mettrque 9:b6a9d1c44ed9 31
mettrque 9:b6a9d1c44ed9 32 /** Access an SD Card using SPI
mettrque 9:b6a9d1c44ed9 33 *
mettrque 9:b6a9d1c44ed9 34 * @code
mettrque 9:b6a9d1c44ed9 35 * #include "mbed.h"
mettrque 9:b6a9d1c44ed9 36 * #include "SDBlockDevice.h"
mettrque 9:b6a9d1c44ed9 37 *
mettrque 9:b6a9d1c44ed9 38 * SDBlockDevice sd(p5, p6, p7, p12); // mosi, miso, sclk, cs
mettrque 9:b6a9d1c44ed9 39 * uint8_t block[512] = "Hello World!\n";
mettrque 9:b6a9d1c44ed9 40 *
mettrque 9:b6a9d1c44ed9 41 * int main() {
mettrque 9:b6a9d1c44ed9 42 * sd.init();
mettrque 9:b6a9d1c44ed9 43 * sd.write(block, 0, 512);
mettrque 9:b6a9d1c44ed9 44 * sd.read(block, 0, 512);
mettrque 9:b6a9d1c44ed9 45 * printf("%s", block);
mettrque 9:b6a9d1c44ed9 46 * sd.deinit();
mettrque 9:b6a9d1c44ed9 47 * }
mettrque 9:b6a9d1c44ed9 48 */
mettrque 9:b6a9d1c44ed9 49 class SDBlockDevice : public BlockDevice {
mettrque 9:b6a9d1c44ed9 50 public:
mettrque 9:b6a9d1c44ed9 51 /** Lifetime of an SD card
mettrque 9:b6a9d1c44ed9 52 */
mettrque 9:b6a9d1c44ed9 53 SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs);
mettrque 9:b6a9d1c44ed9 54 virtual ~SDBlockDevice();
mettrque 9:b6a9d1c44ed9 55
mettrque 9:b6a9d1c44ed9 56 /** Initialize a block device
mettrque 9:b6a9d1c44ed9 57 *
mettrque 9:b6a9d1c44ed9 58 * @return 0 on success or a negative error code on failure
mettrque 9:b6a9d1c44ed9 59 */
mettrque 9:b6a9d1c44ed9 60 virtual int init();
mettrque 9:b6a9d1c44ed9 61
mettrque 9:b6a9d1c44ed9 62 /** Deinitialize a block device
mettrque 9:b6a9d1c44ed9 63 *
mettrque 9:b6a9d1c44ed9 64 * @return 0 on success or a negative error code on failure
mettrque 9:b6a9d1c44ed9 65 */
mettrque 9:b6a9d1c44ed9 66 virtual int deinit();
mettrque 9:b6a9d1c44ed9 67
mettrque 9:b6a9d1c44ed9 68 /** Read blocks from a block device
mettrque 9:b6a9d1c44ed9 69 *
mettrque 9:b6a9d1c44ed9 70 * @param buffer Buffer to write blocks to
mettrque 9:b6a9d1c44ed9 71 * @param addr Address of block to begin reading from
mettrque 9:b6a9d1c44ed9 72 * @param size Size to read in bytes, must be a multiple of read block size
mettrque 9:b6a9d1c44ed9 73 * @return 0 on success, negative error code on failure
mettrque 9:b6a9d1c44ed9 74 */
mettrque 9:b6a9d1c44ed9 75 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
mettrque 6:ef95300898b2 76
mettrque 9:b6a9d1c44ed9 77 /** Program blocks to a block device
mettrque 9:b6a9d1c44ed9 78 *
mettrque 9:b6a9d1c44ed9 79 * The blocks must have been erased prior to being programmed
mettrque 9:b6a9d1c44ed9 80 *
mettrque 9:b6a9d1c44ed9 81 * @param buffer Buffer of data to write to blocks
mettrque 9:b6a9d1c44ed9 82 * @param addr Address of block to begin writing to
mettrque 9:b6a9d1c44ed9 83 * @param size Size to write in bytes, must be a multiple of program block size
mettrque 9:b6a9d1c44ed9 84 * @return 0 on success, negative error code on failure
mettrque 9:b6a9d1c44ed9 85 */
mettrque 9:b6a9d1c44ed9 86 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
mettrque 9:b6a9d1c44ed9 87
mettrque 9:b6a9d1c44ed9 88 /** Erase blocks on a block device
mettrque 9:b6a9d1c44ed9 89 *
mettrque 9:b6a9d1c44ed9 90 * The state of an erased block is undefined until it has been programmed
mettrque 9:b6a9d1c44ed9 91 *
mettrque 9:b6a9d1c44ed9 92 * @param addr Address of block to begin erasing
mettrque 9:b6a9d1c44ed9 93 * @param size Size to erase in bytes, must be a multiple of erase block size
mettrque 9:b6a9d1c44ed9 94 * @return 0 on success, negative error code on failure
mettrque 9:b6a9d1c44ed9 95 */
mettrque 9:b6a9d1c44ed9 96 virtual int erase(bd_addr_t addr, bd_size_t size);
mettrque 9:b6a9d1c44ed9 97
mettrque 9:b6a9d1c44ed9 98 /** Get the size of a readable block
mettrque 9:b6a9d1c44ed9 99 *
mettrque 9:b6a9d1c44ed9 100 * @return Size of a readable block in bytes
mettrque 9:b6a9d1c44ed9 101 */
mettrque 9:b6a9d1c44ed9 102 virtual bd_size_t get_read_size() const;
mettrque 9:b6a9d1c44ed9 103
mettrque 9:b6a9d1c44ed9 104 /** Get the size of a programable block
mettrque 9:b6a9d1c44ed9 105 *
mettrque 9:b6a9d1c44ed9 106 * @return Size of a programable block in bytes
mettrque 9:b6a9d1c44ed9 107 * @note Must be a multiple of the read size
mettrque 9:b6a9d1c44ed9 108 */
mettrque 9:b6a9d1c44ed9 109 virtual bd_size_t get_program_size() const;
mettrque 9:b6a9d1c44ed9 110
mettrque 9:b6a9d1c44ed9 111 /** Get the size of a eraseable block
mettrque 9:b6a9d1c44ed9 112 *
mettrque 9:b6a9d1c44ed9 113 * @return Size of a eraseable block in bytes
mettrque 9:b6a9d1c44ed9 114 * @note Must be a multiple of the program size
mettrque 9:b6a9d1c44ed9 115 */
mettrque 9:b6a9d1c44ed9 116 virtual bd_size_t get_erase_size() const;
mettrque 9:b6a9d1c44ed9 117
mettrque 9:b6a9d1c44ed9 118 /** Get the total size of the underlying device
mettrque 9:b6a9d1c44ed9 119 *
mettrque 9:b6a9d1c44ed9 120 * @return Size of the underlying device in bytes
mettrque 9:b6a9d1c44ed9 121 */
mettrque 9:b6a9d1c44ed9 122 virtual bd_size_t size() const;
mettrque 9:b6a9d1c44ed9 123
mettrque 9:b6a9d1c44ed9 124 /** Enable or disable debugging
mettrque 9:b6a9d1c44ed9 125 *
mettrque 9:b6a9d1c44ed9 126 * @param State of debugging
mettrque 9:b6a9d1c44ed9 127 */
mettrque 9:b6a9d1c44ed9 128 virtual void debug(bool dbg);
mettrque 9:b6a9d1c44ed9 129
mettrque 9:b6a9d1c44ed9 130 private:
mettrque 9:b6a9d1c44ed9 131 int _cmd(int cmd, int arg);
mettrque 9:b6a9d1c44ed9 132 int _cmdx(int cmd, int arg);
mettrque 9:b6a9d1c44ed9 133 int _cmd8();
mettrque 9:b6a9d1c44ed9 134 int _cmd58();
mettrque 9:b6a9d1c44ed9 135
mettrque 9:b6a9d1c44ed9 136 /* Move the SDCard into the SPI Mode idle state
mettrque 9:b6a9d1c44ed9 137 *
mettrque 9:b6a9d1c44ed9 138 * The card is transitioned from SDCard mode to SPI mode by sending the
mettrque 9:b6a9d1c44ed9 139 * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the
mettrque 9:b6a9d1c44ed9 140 * "SPI Startup" section of the comments at the head of the
mettrque 9:b6a9d1c44ed9 141 * implementation file for further details and specification references.
mettrque 9:b6a9d1c44ed9 142 *
mettrque 9:b6a9d1c44ed9 143 * @return -1 if an error occurred e.g. a valid response was not
mettrque 9:b6a9d1c44ed9 144 * received. Otherwise R1_IDLE_STATE (0x1), the successful
mettrque 9:b6a9d1c44ed9 145 * response from CMD0.
mettrque 9:b6a9d1c44ed9 146 */
mettrque 9:b6a9d1c44ed9 147 int _go_idle_state();
mettrque 9:b6a9d1c44ed9 148 int _initialise_card();
mettrque 9:b6a9d1c44ed9 149 int _initialise_card_v1();
mettrque 9:b6a9d1c44ed9 150 int _initialise_card_v2();
mettrque 9:b6a9d1c44ed9 151
mettrque 9:b6a9d1c44ed9 152 int _read(uint8_t * buffer, uint32_t length);
mettrque 9:b6a9d1c44ed9 153 int _write(const uint8_t *buffer, uint32_t length);
mettrque 9:b6a9d1c44ed9 154 uint32_t _sd_sectors();
mettrque 9:b6a9d1c44ed9 155 uint32_t _sectors;
mettrque 9:b6a9d1c44ed9 156
mettrque 9:b6a9d1c44ed9 157 uint32_t _init_sck;
mettrque 9:b6a9d1c44ed9 158 uint32_t _transfer_sck;
mettrque 9:b6a9d1c44ed9 159
mettrque 9:b6a9d1c44ed9 160 SPI _spi;
mettrque 9:b6a9d1c44ed9 161 DigitalOut _cs;
mettrque 9:b6a9d1c44ed9 162 unsigned _block_size;
mettrque 9:b6a9d1c44ed9 163 bool _is_initialized;
mettrque 9:b6a9d1c44ed9 164 bool _dbg;
mettrque 9:b6a9d1c44ed9 165 mutable Mutex _lock;
mettrque 9:b6a9d1c44ed9 166 };
mettrque 9:b6a9d1c44ed9 167
mettrque 9:b6a9d1c44ed9 168
mettrque 9:b6a9d1c44ed9 169 #endif /* DEVICE_SPI */
mettrque 9:b6a9d1c44ed9 170
mettrque 9:b6a9d1c44ed9 171 #endif /* MBED_SD_BLOCK_DEVICE_H */
mettrque 9:b6a9d1c44ed9 172