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.
SDHIBlockDevice.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_RZ_SDHI_BLOCK_DEVICE_HPP 00023 #define MBED_RZ_SDHI_BLOCK_DEVICE_HPP 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 "platform/PlatformMutex.h" 00031 #include <map> 00032 00033 //#include "r_typedefs.h" 00034 00035 #include "sdif.h" 00036 #include "sd_cfg.h" 00037 00038 #define SDHI_DBG 1 /*!< 1 - Enable debugging */ 00039 00040 /** Access an SD Card using SDHI 00041 * 00042 * @code 00043 * #include "mbed.h" 00044 * #include "SDHIBlockDevice.h" 00045 * 00046 * SDHIBlockDevice rz_sdhi(p5, p6, p7, p12, p13, p14, p15, p2); // CLK, CMD, CD, WP, D0, D1, D2, D3 00047 * -- or -- 00048 * #define SDHI_CHANNEL 0 00049 * SDHIBlockDevice rz_sdhi(SDHI_CHANNEL); // use channel 0 00050 * 00051 * uint8_t block[512] = "Hello World!\n"; 00052 * 00053 * int main() { 00054 * rz_sdhi.init(); 00055 * rz_sdhi.write(block, 0, 512); 00056 * rz_sdhi.read(block, 0, 512); 00057 * printf("%s", block); 00058 * rz_sdhi.deinit(); 00059 * } 00060 */ 00061 00062 class vMutex 00063 { 00064 public: 00065 vMutex(PlatformMutex * mtx) : 00066 _mutex(mtx) 00067 { 00068 if (_mutex) 00069 _mutex->lock(); 00070 } 00071 virtual ~vMutex() 00072 { 00073 if(_mutex) 00074 _mutex->unlock(); 00075 } 00076 00077 private: 00078 PlatformMutex * _mutex; 00079 }; 00080 00081 typedef struct { 00082 const char *msg; 00083 int32_t errorno; 00084 } sderr_t; 00085 00086 00087 00088 class SDHIBlockDevice : public BlockDevice { 00089 public: 00090 /** Lifetime of an SD card 00091 */ 00092 SDHIBlockDevice(uint32_t sdport); 00093 SDHIBlockDevice(PinName sd_CLK, PinName sd_CMD, PinName sd_CD, PinName sd_WP, 00094 PinName sd_D0, PinName sd_D1, PinName sd_D2, PinName sd_D3 ); 00095 virtual ~SDHIBlockDevice(); 00096 00097 /** Initialize a block device 00098 * 00099 * @return 0 on success or a negative error code on failure 00100 */ 00101 virtual int init(); 00102 00103 /** Deinitialize a block device 00104 * 00105 * @return 0 on success or a negative error code on failure 00106 */ 00107 virtual int deinit(); 00108 00109 /** Read blocks from a block device 00110 * 00111 * @param buffer Buffer to write blocks to 00112 * @param addr Address of block to begin reading from 00113 * @param size Size to read in bytes, must be a multiple of read block size 00114 * @return 0 on success, negative error code on failure 00115 */ 00116 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); 00117 00118 /** Program blocks to a block device 00119 * 00120 * The blocks must have been erased prior to being programmed 00121 * 00122 * @param buffer Buffer of data to write to blocks 00123 * @param addr Address of block to begin writing to 00124 * @param size Size to write in bytes, must be a multiple of program block size 00125 * @return 0 on success, negative error code on failure 00126 */ 00127 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); 00128 00129 /** Erase blocks on a block device 00130 * 00131 * The state of an erased block is undefined until it has been programmed 00132 * 00133 * @param addr Address of block to begin erasing 00134 * @param size Size to erase in bytes, must be a multiple of erase block size 00135 * @return 0 on success, negative error code on failure 00136 */ 00137 virtual int erase(bd_addr_t addr, bd_size_t size); 00138 00139 00140 /** Mark blocks as no longer in use 00141 * 00142 * This function provides a hint to the underlying block device that a region of blocks 00143 * is no longer in use and may be erased without side effects. Erase must still be called 00144 * before programming, but trimming allows flash-translation-layers to schedule erases when 00145 * the device is not busy. 00146 * 00147 * @param addr Address of block to mark as unused 00148 * @param size Size to mark as unused in bytes, must be a multiple of erase block size 00149 * @return 0 on success, negative error code on failure 00150 */ 00151 virtual int trim(bd_addr_t addr, bd_size_t size); 00152 00153 /** Get the size of a readable block 00154 * 00155 * @return Size of a readable block in bytes 00156 */ 00157 virtual bd_size_t get_read_size() const; 00158 00159 /** Get the size of a programable block 00160 * 00161 * @return Size of a programable block in bytes 00162 * @note Must be a multiple of the read size 00163 */ 00164 virtual bd_size_t get_program_size() const; 00165 00166 /** Get the size of a eraseable block 00167 * 00168 * @return Size of a eraseable block in bytes 00169 * @note Must be a multiple of the program size 00170 */ 00171 /*virtual bd_size_t get_erase_size() const; */ 00172 00173 00174 /** Get the total size of the underlying device 00175 * 00176 * @return Size of the underlying device in bytes 00177 */ 00178 virtual bd_size_t size() const; 00179 00180 /** Enable or disable debugging 00181 * 00182 * @param State of debugging 00183 */ 00184 virtual void debug(bool dbg); 00185 00186 /** Get the BlockDevice class type. 00187 * 00188 * @return A string represent the BlockDevice class type. 00189 */ 00190 00191 virtual const char *get_type() const; 00192 00193 protected: 00194 void Initialize( uint32_t sdport ); 00195 00196 private: 00197 00198 uint8_t _card_type; 00199 00200 /* Move the SDCard into the SPI Mode idle state 00201 * 00202 * The card is transitioned from SDCard mode to SPI mode by sending the 00203 * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the 00204 * "SPI Startup" section of the comments at the head of the 00205 * implementation file for further details and specification references. 00206 * 00207 * @return Response form the card. R1_IDLE_STATE (0x1), the successful 00208 * response from CMD0. R1_XXX_XXX for more response 00209 */ 00210 // uint32_t _go_idle_state(); 00211 int _initialise_card(); 00212 00213 bd_size_t _sectors; 00214 bd_size_t _sd_sectors(); 00215 00216 bool _is_valid_trim(bd_addr_t addr, bd_size_t size); 00217 00218 const char * _sderr_msg(int32_t errorno); 00219 00220 int _error(int32_t errcode); 00221 00222 PlatformMutex _mutex; 00223 00224 bd_size_t _block_size; 00225 bd_size_t _erase_size; 00226 bool _is_initialized; 00227 bool _dbg; 00228 uint32_t _init_ref_count; 00229 00230 int32_t _sd_channel; 00231 uint32_t _regbase; 00232 #if SDHI_DBG 00233 static const sderr_t _sd_error_msg[]; 00234 00235 map<int32_t, const char *> _sd_err_map; 00236 #endif 00237 }; 00238 00239 #endif /* DEVICE_SPI */ 00240 00241 #endif /* MBED_RZ_SDHI_BLOCK_DEVICE_HPP */ 00242
Generated on Mon Jul 18 2022 23:15:42 by
1.7.2