Block device class for creating a block device to access a SD/MMC card via SD/MMC interface on DISCO_F746NG
Dependencies: BSP_DISCO_F746NG
Dependents: DISCO-F746NG_BLOCK_DEVICE_SDCARD DISCO-F746NG_BLOCK_DEVICE_WITH_FAT_FILESYSTEM_ON_SDCARD SDReaderTest
SDBlockDeviceDISCOF746NG.h
00001 /* SD/MMC Block device Library for MBED-OS 00002 * Copyright 2017 Roy Krikke 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 * 00016 */ 00017 00018 #ifndef BD_SD_DISCO_F746NG_H 00019 #define BD_SD_DISCO_F746NG_H 00020 00021 #include "mbed.h" 00022 #include "BlockDevice.h" 00023 #include "platform/PlatformMutex.h" 00024 #include "stm32746g_discovery_sd.h" 00025 00026 #define BD_SD_DISCO_F746NG_MAJOR_VERSION 1 00027 #define BD_SD_DISCO_F746NG_MINOR_VERSION 0 00028 #define BD_SD_DISCO_F746NG_PATCH_VERSION 0 00029 00030 /** 00031 * BD_SD_DISCO_F746NG class. 00032 * Block device class for creating a block device to access a SD/MMC card via SD/MMC interface on DISCO_F746NG 00033 * 00034 * Example: 00035 * @code 00036 * #include "mbed.h" 00037 * #include "SDBlockDeviceDISCOF746NG.h" 00038 * 00039 * DigitalOut led (LED1); 00040 * 00041 * // Instantiate the Block Device for sd card on DISCO-F746NG 00042 * SDBlockDeviceDISCOF746NG bd; 00043 * uint8_t block[512] = "Hello World!\n"; 00044 * 00045 * int 00046 * main () { 00047 * Serial pc(SERIAL_TX, SERIAL_RX); 00048 * pc.baud (115200); 00049 * printf("Start\n"); 00050 * 00051 * // Call the SDBlockDevice-DISCO-F746NG instance initialisation method. 00052 * printf("sd card init...\n"); 00053 * if (0 != bd.init ()) { 00054 * printf("Init failed \n"); 00055 * return -1; 00056 * } 00057 * 00058 * printf("sd size: %llu\n", bd.size ()); 00059 * printf("sd read size: %llu\n", bd.get_read_size ()); 00060 * printf("sd program size: %llu\n", bd.get_program_size ()); 00061 * printf("sd erase size: %llu\n\n", bd.get_erase_size ()); 00062 * 00063 * printf("sd erase...\n"); 00064 * if (0 != bd.erase (0, bd.get_erase_size ())) { 00065 * printf ("Error Erasing block \n"); 00066 * } 00067 * 00068 * // Write some the data block to the device 00069 * printf("sd write: %s\n", block); 00070 * if (0 == bd.program (block, 0, 512)) { 00071 * // read the data block from the device 00072 * printf ("sd read: "); 00073 * if (0 == bd.read (block, 0, 512)) { 00074 * // print the contents of the block 00075 * printf ("%s", block); 00076 * } 00077 * } 00078 * 00079 * // Call the BD_SD_DISCO_F746NG instance de-initialisation method. 00080 * printf("sd card deinit...\n"); 00081 * if(0 != bd.deinit ()) { 00082 * printf ("Deinit failed \n"); 00083 * return -1; 00084 * } 00085 * 00086 * // Blink led with 2 Hz 00087 * while(true) { 00088 * led = !led; 00089 * wait (0.5); 00090 * } 00091 * } 00092 * @endcode 00093 * 00094 */ 00095 class SDBlockDeviceDISCOF746NG : public BlockDevice 00096 { 00097 public: 00098 00099 /** Lifetime of the memory block device 00100 * 00101 * Only a block size of 512 bytes is supported 00102 * 00103 */ 00104 SDBlockDeviceDISCOF746NG(); 00105 virtual ~SDBlockDeviceDISCOF746NG(); 00106 00107 /** Initialize a block device 00108 * 00109 * @return 0 on success or a negative error code on failure 00110 */ 00111 virtual int init(); 00112 00113 /** Deinitialize a block device 00114 * 00115 * @return 0 on success or a negative error code on failure 00116 */ 00117 virtual int deinit(); 00118 00119 /** Read blocks from a block device 00120 * 00121 * @param buffer Buffer to read blocks into 00122 * @param addr Address of block to begin reading from 00123 * @param size Size to read in bytes, must be a multiple of read block size 00124 * @return 0 on success, negative error code on failure 00125 */ 00126 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); 00127 00128 /** Program blocks to a block device 00129 * 00130 * The blocks must have been erased prior to being programmed 00131 * 00132 * @param buffer Buffer of data to write to blocks 00133 * @param addr Address of block to begin writing to 00134 * @param size Size to write in bytes, must be a multiple of program block size 00135 * @return 0 on success, negative error code on failure 00136 */ 00137 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); 00138 00139 /** Erase blocks on a block device 00140 * 00141 * The state of an erased block is undefined until it has been programmed 00142 * 00143 * @param addr Address of block to begin erasing 00144 * @param size Size to erase in bytes, must be a multiple of erase block size 00145 * @return 0 on success, negative error code on failure 00146 */ 00147 virtual int erase(bd_addr_t addr, bd_size_t size); 00148 00149 /** Get the size of a readable block 00150 * 00151 * @return Size of a readable block in bytes 00152 */ 00153 virtual bd_size_t get_read_size() const; 00154 00155 /** Get the size of a programable block 00156 * 00157 * @return Size of a programable block in bytes 00158 */ 00159 virtual bd_size_t get_program_size() const; 00160 00161 /** Get the size of a eraseable block 00162 * 00163 * @return Size of a eraseable block in bytes 00164 */ 00165 virtual bd_size_t get_erase_size() const; 00166 00167 /** Get the total size of the underlying device 00168 * 00169 * @return Size of the underlying device in bytes 00170 */ 00171 virtual bd_size_t size() const; 00172 00173 private: 00174 uint8_t _card_type; 00175 bd_size_t _read_size; 00176 bd_size_t _program_size; 00177 bd_size_t _erase_size; 00178 bd_size_t _block_size; 00179 bd_size_t _capacity_in_blocks; 00180 BSP_SD_CardInfo _current_card_info; 00181 uint8_t _sd_state; 00182 uint32_t _timeout; 00183 PlatformMutex _mutex; 00184 bool _is_initialized; 00185 00186 virtual void 00187 lock () { 00188 _mutex.lock(); 00189 } 00190 00191 virtual void 00192 unlock() { 00193 _mutex.unlock (); 00194 } 00195 00196 }; 00197 00198 #endif /* BD_SD_DISCO_F746NG_H */
Generated on Tue Jul 12 2022 19:08:01 by 1.7.2