Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SDBlockDevice.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2012 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #ifndef MBED_SD_BLOCK_DEVICE_H 00023 #define MBED_SD_BLOCK_DEVICE_H 00024 00025 /* If the target has no SPI support then SDCard is not supported */ 00026 #ifdef DEVICE_SPI 00027 00028 #include "BlockDevice.h" 00029 #include "mbed.h" 00030 00031 00032 /** Access an SD Card using SPI 00033 * 00034 * @code 00035 * #include "mbed.h" 00036 * #include "SDBlockDevice.h" 00037 * 00038 * SDBlockDevice sd(p5, p6, p7, p12); // mosi, miso, sclk, cs 00039 * uint8_t block[512] = "Hello World!\n"; 00040 * 00041 * int main() { 00042 * sd.init(); 00043 * sd.write(block, 0, 512); 00044 * sd.read(block, 0, 512); 00045 * printf("%s", block); 00046 * sd.deinit(); 00047 * } 00048 */ 00049 class SDBlockDevice : public BlockDevice { 00050 public: 00051 /** Lifetime of an SD card 00052 */ 00053 SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs); 00054 virtual ~SDBlockDevice(); 00055 00056 /** Initialize a block device 00057 * 00058 * @return 0 on success or a negative error code on failure 00059 */ 00060 virtual int init(); 00061 00062 /** Deinitialize a block device 00063 * 00064 * @return 0 on success or a negative error code on failure 00065 */ 00066 virtual int deinit(); 00067 00068 /** Read blocks from a block device 00069 * 00070 * @param buffer Buffer to write blocks to 00071 * @param addr Address of block to begin reading from 00072 * @param size Size to read in bytes, must be a multiple of read block size 00073 * @return 0 on success, negative error code on failure 00074 */ 00075 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); 00076 00077 /** Program blocks to a block device 00078 * 00079 * The blocks must have been erased prior to being programmed 00080 * 00081 * @param buffer Buffer of data to write to blocks 00082 * @param addr Address of block to begin writing to 00083 * @param size Size to write in bytes, must be a multiple of program block size 00084 * @return 0 on success, negative error code on failure 00085 */ 00086 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); 00087 00088 /** Erase blocks on a block device 00089 * 00090 * The state of an erased block is undefined until it has been programmed 00091 * 00092 * @param addr Address of block to begin erasing 00093 * @param size Size to erase in bytes, must be a multiple of erase block size 00094 * @return 0 on success, negative error code on failure 00095 */ 00096 virtual int erase(bd_addr_t addr, bd_size_t size); 00097 00098 /** Get the size of a readable block 00099 * 00100 * @return Size of a readable block in bytes 00101 */ 00102 virtual bd_size_t get_read_size() const; 00103 00104 /** Get the size of a programable block 00105 * 00106 * @return Size of a programable block in bytes 00107 * @note Must be a multiple of the read size 00108 */ 00109 virtual bd_size_t get_program_size() const; 00110 00111 /** Get the size of a eraseable block 00112 * 00113 * @return Size of a eraseable block in bytes 00114 * @note Must be a multiple of the program size 00115 */ 00116 virtual bd_size_t get_erase_size() const; 00117 00118 /** Get the total size of the underlying device 00119 * 00120 * @return Size of the underlying device in bytes 00121 */ 00122 virtual bd_size_t size() const; 00123 00124 /** Enable or disable debugging 00125 * 00126 * @param State of debugging 00127 */ 00128 virtual void debug(bool dbg); 00129 00130 virtual const char *get_type() const; 00131 00132 00133 private: 00134 int _cmd(int cmd, int arg); 00135 int _cmdx(int cmd, int arg); 00136 int _cmd8(); 00137 int _cmd58(); 00138 int _initialise_card(); 00139 int _initialise_card_v1(); 00140 int _initialise_card_v2(); 00141 00142 int _read(uint8_t * buffer, uint32_t length); 00143 int _write(const uint8_t *buffer, uint32_t length); 00144 uint32_t _sd_sectors(); 00145 uint32_t _sectors; 00146 00147 uint32_t _init_sck; 00148 uint32_t _transfer_sck; 00149 00150 SPI _spi; 00151 DigitalOut _cs; 00152 unsigned _block_size; 00153 bool _is_initialized; 00154 bool _dbg; 00155 Mutex _lock; 00156 }; 00157 00158 00159 #endif /* DEVICE_SPI */ 00160 00161 #endif /* MBED_SD_BLOCK_DEVICE_H */ 00162
Generated on Tue Jul 26 2022 07:27:42 by
