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.
Fork of gr-peach-opencv-project-sd-card by
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 #include "rtos.h" 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 protected: 00131 class SPI_HS : public SPI { 00132 public: 00133 SPI_HS(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) : SPI(mosi, miso, sclk, ssel) {} 00134 00135 virtual int write(int value) { 00136 aquire(); 00137 int ret = spi_master_write(&_spi, value); 00138 return ret; 00139 } 00140 protected: 00141 virtual void aquire(void) { 00142 if (_owner != this) { 00143 spi_format(&_spi, _bits, _mode, 0); 00144 spi_frequency(&_spi, _hz); 00145 _owner = this; 00146 } 00147 } 00148 }; 00149 int _cmd(int cmd, int arg); 00150 int _cmdx(int cmd, int arg); 00151 int _cmd8(); 00152 int _cmd58(); 00153 int _initialise_card(); 00154 int _initialise_card_v1(); 00155 int _initialise_card_v2(); 00156 00157 int _read(uint8_t * buffer, uint32_t length); 00158 int _write(const uint8_t *buffer, uint32_t length); 00159 uint32_t _sd_sectors(); 00160 uint32_t _sectors; 00161 00162 uint32_t _init_sck; 00163 uint32_t _transfer_sck; 00164 00165 SPI_HS _spi; 00166 DigitalOut _cs; 00167 unsigned _block_size; 00168 bool _is_initialized; 00169 bool _dbg; 00170 rtos::Mutex _lock; 00171 }; 00172 00173 00174 #endif /* DEVICE_SPI */ 00175 00176 #endif /* MBED_SD_BLOCK_DEVICE_H */
Generated on Tue Jul 12 2022 14:47:35 by
1.7.2
