SDHI_driver patch (mbedOS 5.11.5)
mbed-os-program/mbed-os/components/storage/blockdevice/COMPONENT_SD/SDHIBlockDevice.h@0:e1f465d87307, 2019-03-18 (annotated)
- Committer:
- tvendov
- Date:
- Mon Mar 18 16:54:40 2019 +0000
- Revision:
- 0:e1f465d87307
Initial_II
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tvendov | 0:e1f465d87307 | 1 | /* mbed Microcontroller Library |
| tvendov | 0:e1f465d87307 | 2 | * Copyright (c) 2006-2012 ARM Limited |
| tvendov | 0:e1f465d87307 | 3 | * |
| tvendov | 0:e1f465d87307 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| tvendov | 0:e1f465d87307 | 5 | * of this software and associated documentation files (the "Software"), to deal |
| tvendov | 0:e1f465d87307 | 6 | * in the Software without restriction, including without limitation the rights |
| tvendov | 0:e1f465d87307 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| tvendov | 0:e1f465d87307 | 8 | * copies of the Software, and to permit persons to whom the Software is |
| tvendov | 0:e1f465d87307 | 9 | * furnished to do so, subject to the following conditions: |
| tvendov | 0:e1f465d87307 | 10 | * |
| tvendov | 0:e1f465d87307 | 11 | * The above copyright notice and this permission notice shall be included in |
| tvendov | 0:e1f465d87307 | 12 | * all copies or substantial portions of the Software. |
| tvendov | 0:e1f465d87307 | 13 | * |
| tvendov | 0:e1f465d87307 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| tvendov | 0:e1f465d87307 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| tvendov | 0:e1f465d87307 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| tvendov | 0:e1f465d87307 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| tvendov | 0:e1f465d87307 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| tvendov | 0:e1f465d87307 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| tvendov | 0:e1f465d87307 | 20 | * SOFTWARE. |
| tvendov | 0:e1f465d87307 | 21 | */ |
| tvendov | 0:e1f465d87307 | 22 | #ifndef MBED_RZ_SDHI_BLOCK_DEVICE_HPP |
| tvendov | 0:e1f465d87307 | 23 | #define MBED_RZ_SDHI_BLOCK_DEVICE_HPP |
| tvendov | 0:e1f465d87307 | 24 | |
| tvendov | 0:e1f465d87307 | 25 | /* If the target has no SPI support then SDCard is not supported */ |
| tvendov | 0:e1f465d87307 | 26 | #ifdef DEVICE_SPI |
| tvendov | 0:e1f465d87307 | 27 | |
| tvendov | 0:e1f465d87307 | 28 | #include "BlockDevice.h" |
| tvendov | 0:e1f465d87307 | 29 | #include "mbed.h" |
| tvendov | 0:e1f465d87307 | 30 | #include "platform/PlatformMutex.h" |
| tvendov | 0:e1f465d87307 | 31 | #include <map> |
| tvendov | 0:e1f465d87307 | 32 | |
| tvendov | 0:e1f465d87307 | 33 | //#include "r_typedefs.h" |
| tvendov | 0:e1f465d87307 | 34 | |
| tvendov | 0:e1f465d87307 | 35 | #include "sdif.h" |
| tvendov | 0:e1f465d87307 | 36 | #include "sd_cfg.h" |
| tvendov | 0:e1f465d87307 | 37 | |
| tvendov | 0:e1f465d87307 | 38 | #define SDHI_DBG 1 /*!< 1 - Enable debugging */ |
| tvendov | 0:e1f465d87307 | 39 | |
| tvendov | 0:e1f465d87307 | 40 | /** Access an SD Card using SDHI |
| tvendov | 0:e1f465d87307 | 41 | * |
| tvendov | 0:e1f465d87307 | 42 | * @code |
| tvendov | 0:e1f465d87307 | 43 | * #include "mbed.h" |
| tvendov | 0:e1f465d87307 | 44 | * #include "SDHIBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 45 | * |
| tvendov | 0:e1f465d87307 | 46 | * SDHIBlockDevice rz_sdhi(p5, p6, p7, p12, p13, p14, p15, p2); // CLK, CMD, CD, WP, D0, D1, D2, D3 |
| tvendov | 0:e1f465d87307 | 47 | * -- or -- |
| tvendov | 0:e1f465d87307 | 48 | * #define SDHI_CHANNEL 0 |
| tvendov | 0:e1f465d87307 | 49 | * SDHIBlockDevice rz_sdhi(SDHI_CHANNEL); // use channel 0 |
| tvendov | 0:e1f465d87307 | 50 | * |
| tvendov | 0:e1f465d87307 | 51 | * uint8_t block[512] = "Hello World!\n"; |
| tvendov | 0:e1f465d87307 | 52 | * |
| tvendov | 0:e1f465d87307 | 53 | * int main() { |
| tvendov | 0:e1f465d87307 | 54 | * rz_sdhi.init(); |
| tvendov | 0:e1f465d87307 | 55 | * rz_sdhi.write(block, 0, 512); |
| tvendov | 0:e1f465d87307 | 56 | * rz_sdhi.read(block, 0, 512); |
| tvendov | 0:e1f465d87307 | 57 | * printf("%s", block); |
| tvendov | 0:e1f465d87307 | 58 | * rz_sdhi.deinit(); |
| tvendov | 0:e1f465d87307 | 59 | * } |
| tvendov | 0:e1f465d87307 | 60 | */ |
| tvendov | 0:e1f465d87307 | 61 | |
| tvendov | 0:e1f465d87307 | 62 | class vMutex |
| tvendov | 0:e1f465d87307 | 63 | { |
| tvendov | 0:e1f465d87307 | 64 | public: |
| tvendov | 0:e1f465d87307 | 65 | vMutex(PlatformMutex * mtx) : |
| tvendov | 0:e1f465d87307 | 66 | _mutex(mtx) |
| tvendov | 0:e1f465d87307 | 67 | { |
| tvendov | 0:e1f465d87307 | 68 | if (_mutex) |
| tvendov | 0:e1f465d87307 | 69 | _mutex->lock(); |
| tvendov | 0:e1f465d87307 | 70 | } |
| tvendov | 0:e1f465d87307 | 71 | virtual ~vMutex() |
| tvendov | 0:e1f465d87307 | 72 | { |
| tvendov | 0:e1f465d87307 | 73 | if(_mutex) |
| tvendov | 0:e1f465d87307 | 74 | _mutex->unlock(); |
| tvendov | 0:e1f465d87307 | 75 | } |
| tvendov | 0:e1f465d87307 | 76 | |
| tvendov | 0:e1f465d87307 | 77 | private: |
| tvendov | 0:e1f465d87307 | 78 | PlatformMutex * _mutex; |
| tvendov | 0:e1f465d87307 | 79 | }; |
| tvendov | 0:e1f465d87307 | 80 | |
| tvendov | 0:e1f465d87307 | 81 | typedef struct { |
| tvendov | 0:e1f465d87307 | 82 | const char *msg; |
| tvendov | 0:e1f465d87307 | 83 | int32_t errorno; |
| tvendov | 0:e1f465d87307 | 84 | } sderr_t; |
| tvendov | 0:e1f465d87307 | 85 | |
| tvendov | 0:e1f465d87307 | 86 | |
| tvendov | 0:e1f465d87307 | 87 | |
| tvendov | 0:e1f465d87307 | 88 | class SDHIBlockDevice : public BlockDevice { |
| tvendov | 0:e1f465d87307 | 89 | public: |
| tvendov | 0:e1f465d87307 | 90 | /** Lifetime of an SD card |
| tvendov | 0:e1f465d87307 | 91 | */ |
| tvendov | 0:e1f465d87307 | 92 | SDHIBlockDevice(uint32_t sdport); |
| tvendov | 0:e1f465d87307 | 93 | SDHIBlockDevice(PinName sd_CLK, PinName sd_CMD, PinName sd_CD, PinName sd_WP, |
| tvendov | 0:e1f465d87307 | 94 | PinName sd_D0, PinName sd_D1, PinName sd_D2, PinName sd_D3 ); |
| tvendov | 0:e1f465d87307 | 95 | virtual ~SDHIBlockDevice(); |
| tvendov | 0:e1f465d87307 | 96 | |
| tvendov | 0:e1f465d87307 | 97 | /** Initialize a block device |
| tvendov | 0:e1f465d87307 | 98 | * |
| tvendov | 0:e1f465d87307 | 99 | * @return 0 on success or a negative error code on failure |
| tvendov | 0:e1f465d87307 | 100 | */ |
| tvendov | 0:e1f465d87307 | 101 | virtual int init(); |
| tvendov | 0:e1f465d87307 | 102 | |
| tvendov | 0:e1f465d87307 | 103 | /** Deinitialize a block device |
| tvendov | 0:e1f465d87307 | 104 | * |
| tvendov | 0:e1f465d87307 | 105 | * @return 0 on success or a negative error code on failure |
| tvendov | 0:e1f465d87307 | 106 | */ |
| tvendov | 0:e1f465d87307 | 107 | virtual int deinit(); |
| tvendov | 0:e1f465d87307 | 108 | |
| tvendov | 0:e1f465d87307 | 109 | /** Read blocks from a block device |
| tvendov | 0:e1f465d87307 | 110 | * |
| tvendov | 0:e1f465d87307 | 111 | * @param buffer Buffer to write blocks to |
| tvendov | 0:e1f465d87307 | 112 | * @param addr Address of block to begin reading from |
| tvendov | 0:e1f465d87307 | 113 | * @param size Size to read in bytes, must be a multiple of read block size |
| tvendov | 0:e1f465d87307 | 114 | * @return 0 on success, negative error code on failure |
| tvendov | 0:e1f465d87307 | 115 | */ |
| tvendov | 0:e1f465d87307 | 116 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
| tvendov | 0:e1f465d87307 | 117 | |
| tvendov | 0:e1f465d87307 | 118 | /** Program blocks to a block device |
| tvendov | 0:e1f465d87307 | 119 | * |
| tvendov | 0:e1f465d87307 | 120 | * The blocks must have been erased prior to being programmed |
| tvendov | 0:e1f465d87307 | 121 | * |
| tvendov | 0:e1f465d87307 | 122 | * @param buffer Buffer of data to write to blocks |
| tvendov | 0:e1f465d87307 | 123 | * @param addr Address of block to begin writing to |
| tvendov | 0:e1f465d87307 | 124 | * @param size Size to write in bytes, must be a multiple of program block size |
| tvendov | 0:e1f465d87307 | 125 | * @return 0 on success, negative error code on failure |
| tvendov | 0:e1f465d87307 | 126 | */ |
| tvendov | 0:e1f465d87307 | 127 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
| tvendov | 0:e1f465d87307 | 128 | |
| tvendov | 0:e1f465d87307 | 129 | /** Erase blocks on a block device |
| tvendov | 0:e1f465d87307 | 130 | * |
| tvendov | 0:e1f465d87307 | 131 | * The state of an erased block is undefined until it has been programmed |
| tvendov | 0:e1f465d87307 | 132 | * |
| tvendov | 0:e1f465d87307 | 133 | * @param addr Address of block to begin erasing |
| tvendov | 0:e1f465d87307 | 134 | * @param size Size to erase in bytes, must be a multiple of erase block size |
| tvendov | 0:e1f465d87307 | 135 | * @return 0 on success, negative error code on failure |
| tvendov | 0:e1f465d87307 | 136 | */ |
| tvendov | 0:e1f465d87307 | 137 | virtual int erase(bd_addr_t addr, bd_size_t size); |
| tvendov | 0:e1f465d87307 | 138 | |
| tvendov | 0:e1f465d87307 | 139 | |
| tvendov | 0:e1f465d87307 | 140 | /** Mark blocks as no longer in use |
| tvendov | 0:e1f465d87307 | 141 | * |
| tvendov | 0:e1f465d87307 | 142 | * This function provides a hint to the underlying block device that a region of blocks |
| tvendov | 0:e1f465d87307 | 143 | * is no longer in use and may be erased without side effects. Erase must still be called |
| tvendov | 0:e1f465d87307 | 144 | * before programming, but trimming allows flash-translation-layers to schedule erases when |
| tvendov | 0:e1f465d87307 | 145 | * the device is not busy. |
| tvendov | 0:e1f465d87307 | 146 | * |
| tvendov | 0:e1f465d87307 | 147 | * @param addr Address of block to mark as unused |
| tvendov | 0:e1f465d87307 | 148 | * @param size Size to mark as unused in bytes, must be a multiple of erase block size |
| tvendov | 0:e1f465d87307 | 149 | * @return 0 on success, negative error code on failure |
| tvendov | 0:e1f465d87307 | 150 | */ |
| tvendov | 0:e1f465d87307 | 151 | virtual int trim(bd_addr_t addr, bd_size_t size); |
| tvendov | 0:e1f465d87307 | 152 | |
| tvendov | 0:e1f465d87307 | 153 | /** Get the size of a readable block |
| tvendov | 0:e1f465d87307 | 154 | * |
| tvendov | 0:e1f465d87307 | 155 | * @return Size of a readable block in bytes |
| tvendov | 0:e1f465d87307 | 156 | */ |
| tvendov | 0:e1f465d87307 | 157 | virtual bd_size_t get_read_size() const; |
| tvendov | 0:e1f465d87307 | 158 | |
| tvendov | 0:e1f465d87307 | 159 | /** Get the size of a programable block |
| tvendov | 0:e1f465d87307 | 160 | * |
| tvendov | 0:e1f465d87307 | 161 | * @return Size of a programable block in bytes |
| tvendov | 0:e1f465d87307 | 162 | * @note Must be a multiple of the read size |
| tvendov | 0:e1f465d87307 | 163 | */ |
| tvendov | 0:e1f465d87307 | 164 | virtual bd_size_t get_program_size() const; |
| tvendov | 0:e1f465d87307 | 165 | |
| tvendov | 0:e1f465d87307 | 166 | /** Get the size of a eraseable block |
| tvendov | 0:e1f465d87307 | 167 | * |
| tvendov | 0:e1f465d87307 | 168 | * @return Size of a eraseable block in bytes |
| tvendov | 0:e1f465d87307 | 169 | * @note Must be a multiple of the program size |
| tvendov | 0:e1f465d87307 | 170 | */ |
| tvendov | 0:e1f465d87307 | 171 | /*virtual bd_size_t get_erase_size() const; */ |
| tvendov | 0:e1f465d87307 | 172 | |
| tvendov | 0:e1f465d87307 | 173 | |
| tvendov | 0:e1f465d87307 | 174 | /** Get the total size of the underlying device |
| tvendov | 0:e1f465d87307 | 175 | * |
| tvendov | 0:e1f465d87307 | 176 | * @return Size of the underlying device in bytes |
| tvendov | 0:e1f465d87307 | 177 | */ |
| tvendov | 0:e1f465d87307 | 178 | virtual bd_size_t size() const; |
| tvendov | 0:e1f465d87307 | 179 | |
| tvendov | 0:e1f465d87307 | 180 | /** Enable or disable debugging |
| tvendov | 0:e1f465d87307 | 181 | * |
| tvendov | 0:e1f465d87307 | 182 | * @param State of debugging |
| tvendov | 0:e1f465d87307 | 183 | */ |
| tvendov | 0:e1f465d87307 | 184 | virtual void debug(bool dbg); |
| tvendov | 0:e1f465d87307 | 185 | |
| tvendov | 0:e1f465d87307 | 186 | /** Get the BlockDevice class type. |
| tvendov | 0:e1f465d87307 | 187 | * |
| tvendov | 0:e1f465d87307 | 188 | * @return A string represent the BlockDevice class type. |
| tvendov | 0:e1f465d87307 | 189 | */ |
| tvendov | 0:e1f465d87307 | 190 | |
| tvendov | 0:e1f465d87307 | 191 | virtual const char *get_type() const; |
| tvendov | 0:e1f465d87307 | 192 | |
| tvendov | 0:e1f465d87307 | 193 | protected: |
| tvendov | 0:e1f465d87307 | 194 | void Initialize( uint32_t sdport ); |
| tvendov | 0:e1f465d87307 | 195 | |
| tvendov | 0:e1f465d87307 | 196 | private: |
| tvendov | 0:e1f465d87307 | 197 | |
| tvendov | 0:e1f465d87307 | 198 | uint8_t _card_type; |
| tvendov | 0:e1f465d87307 | 199 | |
| tvendov | 0:e1f465d87307 | 200 | /* Move the SDCard into the SPI Mode idle state |
| tvendov | 0:e1f465d87307 | 201 | * |
| tvendov | 0:e1f465d87307 | 202 | * The card is transitioned from SDCard mode to SPI mode by sending the |
| tvendov | 0:e1f465d87307 | 203 | * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the |
| tvendov | 0:e1f465d87307 | 204 | * "SPI Startup" section of the comments at the head of the |
| tvendov | 0:e1f465d87307 | 205 | * implementation file for further details and specification references. |
| tvendov | 0:e1f465d87307 | 206 | * |
| tvendov | 0:e1f465d87307 | 207 | * @return Response form the card. R1_IDLE_STATE (0x1), the successful |
| tvendov | 0:e1f465d87307 | 208 | * response from CMD0. R1_XXX_XXX for more response |
| tvendov | 0:e1f465d87307 | 209 | */ |
| tvendov | 0:e1f465d87307 | 210 | // uint32_t _go_idle_state(); |
| tvendov | 0:e1f465d87307 | 211 | int _initialise_card(); |
| tvendov | 0:e1f465d87307 | 212 | |
| tvendov | 0:e1f465d87307 | 213 | bd_size_t _sectors; |
| tvendov | 0:e1f465d87307 | 214 | bd_size_t _sd_sectors(); |
| tvendov | 0:e1f465d87307 | 215 | |
| tvendov | 0:e1f465d87307 | 216 | bool _is_valid_trim(bd_addr_t addr, bd_size_t size); |
| tvendov | 0:e1f465d87307 | 217 | |
| tvendov | 0:e1f465d87307 | 218 | const char * _sderr_msg(int32_t errorno); |
| tvendov | 0:e1f465d87307 | 219 | |
| tvendov | 0:e1f465d87307 | 220 | int _error(int32_t errcode); |
| tvendov | 0:e1f465d87307 | 221 | |
| tvendov | 0:e1f465d87307 | 222 | PlatformMutex _mutex; |
| tvendov | 0:e1f465d87307 | 223 | |
| tvendov | 0:e1f465d87307 | 224 | bd_size_t _block_size; |
| tvendov | 0:e1f465d87307 | 225 | bd_size_t _erase_size; |
| tvendov | 0:e1f465d87307 | 226 | bool _is_initialized; |
| tvendov | 0:e1f465d87307 | 227 | bool _dbg; |
| tvendov | 0:e1f465d87307 | 228 | uint32_t _init_ref_count; |
| tvendov | 0:e1f465d87307 | 229 | |
| tvendov | 0:e1f465d87307 | 230 | int32_t _sd_channel; |
| tvendov | 0:e1f465d87307 | 231 | uint32_t _regbase; |
| tvendov | 0:e1f465d87307 | 232 | #if SDHI_DBG |
| tvendov | 0:e1f465d87307 | 233 | static const sderr_t _sd_error_msg[]; |
| tvendov | 0:e1f465d87307 | 234 | |
| tvendov | 0:e1f465d87307 | 235 | map<int32_t, const char *> _sd_err_map; |
| tvendov | 0:e1f465d87307 | 236 | #endif |
| tvendov | 0:e1f465d87307 | 237 | }; |
| tvendov | 0:e1f465d87307 | 238 | |
| tvendov | 0:e1f465d87307 | 239 | #endif /* DEVICE_SPI */ |
| tvendov | 0:e1f465d87307 | 240 | |
| tvendov | 0:e1f465d87307 | 241 | #endif /* MBED_RZ_SDHI_BLOCK_DEVICE_HPP */ |
| tvendov | 0:e1f465d87307 | 242 |