0

Committer:
roykrikke
Date:
Wed Apr 04 19:43:06 2018 +0000
Revision:
1:d1d99cfe13df
Code Style & Formatting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roykrikke 1:d1d99cfe13df 1 /* SD/MMC Block device Library for MBED-OS
roykrikke 1:d1d99cfe13df 2 * Copyright 2017 Roy Krikke
roykrikke 1:d1d99cfe13df 3 *
roykrikke 1:d1d99cfe13df 4 * Licensed under the Apache License, Version 2.0 (the "License");
roykrikke 1:d1d99cfe13df 5 * you may not use this file except in compliance with the License.
roykrikke 1:d1d99cfe13df 6 * You may obtain a copy of the License at
roykrikke 1:d1d99cfe13df 7 *
roykrikke 1:d1d99cfe13df 8 * http://www.apache.org/licenses/LICENSE-2.0
roykrikke 1:d1d99cfe13df 9 *
roykrikke 1:d1d99cfe13df 10 * Unless required by applicable law or agreed to in writing, software
roykrikke 1:d1d99cfe13df 11 * distributed under the License is distributed on an "AS IS" BASIS,
roykrikke 1:d1d99cfe13df 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
roykrikke 1:d1d99cfe13df 13 * See the License for the specific language governing permissions and
roykrikke 1:d1d99cfe13df 14 * limitations under the License.
roykrikke 1:d1d99cfe13df 15 *
roykrikke 1:d1d99cfe13df 16 */
roykrikke 1:d1d99cfe13df 17
roykrikke 1:d1d99cfe13df 18 #include "SDBlockDeviceDISCOF746NG.h"
roykrikke 1:d1d99cfe13df 19 #include "mbed_debug.h"
roykrikke 1:d1d99cfe13df 20
roykrikke 1:d1d99cfe13df 21 /* Required version: 5.5.0 and above */
roykrikke 1:d1d99cfe13df 22 #if defined(MBED_MAJOR_VERSION) && MBED_MAJOR_VERSION >= 5
roykrikke 1:d1d99cfe13df 23 #if (MBED_VERSION < MBED_ENCODE_VERSION(5,5,0))
roykrikke 1:d1d99cfe13df 24 #error "Incompatible mbed-os version detected! Required 5.5.0 and above"
roykrikke 1:d1d99cfe13df 25 #endif
roykrikke 1:d1d99cfe13df 26 #else
roykrikke 1:d1d99cfe13df 27 #warning "mbed-os version 5.5.0 or above required"
roykrikke 1:d1d99cfe13df 28 #endif
roykrikke 1:d1d99cfe13df 29
roykrikke 1:d1d99cfe13df 30 /* Required version: 5.5.0 and above */
roykrikke 1:d1d99cfe13df 31 #warning "Block device class BD_SD_DISCO_F746NG is depending on BSP_DISCO_F746NG libary (Revision 9:df2ea349c37a with date 06 Jul 2017)"
roykrikke 1:d1d99cfe13df 32
roykrikke 1:d1d99cfe13df 33 #define SD_DBG 0 /*!< 1 - Enable debugging */
roykrikke 1:d1d99cfe13df 34
roykrikke 1:d1d99cfe13df 35 /** Enum of standard error codes
roykrikke 1:d1d99cfe13df 36 *
roykrikke 1:d1d99cfe13df 37 * @enum bd_sd_error
roykrikke 1:d1d99cfe13df 38 */
roykrikke 1:d1d99cfe13df 39 enum bd_sd_error {
roykrikke 1:d1d99cfe13df 40 SD_BLOCK_DEVICE_OK = 0, /*!< no error */
roykrikke 1:d1d99cfe13df 41 SD_BLOCK_DEVICE_ERROR = -5000, /*!< device specific error */
roykrikke 1:d1d99cfe13df 42 //SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK = -5001, /*!< operation would block */
roykrikke 1:d1d99cfe13df 43 //SD_BLOCK_DEVICE_ERROR_UNSUPPORTED = -5002, /*!< unsupported operation */
roykrikke 1:d1d99cfe13df 44 SD_BLOCK_DEVICE_ERROR_PARAMETER = -5003, /*!< invalid parameter */
roykrikke 1:d1d99cfe13df 45 SD_BLOCK_DEVICE_ERROR_NO_INIT = -5004, /*!< uninitialized */
roykrikke 1:d1d99cfe13df 46 SD_BLOCK_DEVICE_ERROR_NO_DEVICE = -5005, /*!< device is missing or not connected */
roykrikke 1:d1d99cfe13df 47 //SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED = -5006, /*!< write protected */
roykrikke 1:d1d99cfe13df 48 //SD_BLOCK_DEVICE_ERROR_UNUSABLE = -5007, /*!< unusable card */
roykrikke 1:d1d99cfe13df 49 //SD_BLOCK_DEVICE_ERROR_NO_RESPONSE = -5008, /*!< No response from device */
roykrikke 1:d1d99cfe13df 50 //SD_BLOCK_DEVICE_ERROR_CRC = -5009, /*!< CRC error */
roykrikke 1:d1d99cfe13df 51 SD_BLOCK_DEVICE_ERROR_ERASE = -5010, /*!< Erase error */
roykrikke 1:d1d99cfe13df 52 SD_BLOCK_DEVICE_ERROR_READ = -5011, /*!< Read error */
roykrikke 1:d1d99cfe13df 53 SD_BLOCK_DEVICE_ERROR_PROGRAM = -5012, /*!< Program error */
roykrikke 1:d1d99cfe13df 54 };
roykrikke 1:d1d99cfe13df 55
roykrikke 1:d1d99cfe13df 56 #define BLOCK_SIZE_HC 512 /*!< Block size supported for SD card is 512 bytes */
roykrikke 1:d1d99cfe13df 57
roykrikke 1:d1d99cfe13df 58 SDBlockDeviceDISCOF746NG::SDBlockDeviceDISCOF746NG() :
roykrikke 1:d1d99cfe13df 59 _read_size (BLOCK_SIZE_HC), _program_size (BLOCK_SIZE_HC),
roykrikke 1:d1d99cfe13df 60 _erase_size(BLOCK_SIZE_HC), _block_size (BLOCK_SIZE_HC),
roykrikke 1:d1d99cfe13df 61 _capacity_in_blocks (0)
roykrikke 1:d1d99cfe13df 62 {
roykrikke 1:d1d99cfe13df 63 _timeout = 1000;
roykrikke 1:d1d99cfe13df 64 }
roykrikke 1:d1d99cfe13df 65
roykrikke 1:d1d99cfe13df 66 SDBlockDeviceDISCOF746NG::~SDBlockDeviceDISCOF746NG()
roykrikke 1:d1d99cfe13df 67 {
roykrikke 1:d1d99cfe13df 68 if(_is_initialized) {
roykrikke 1:d1d99cfe13df 69 deinit ();
roykrikke 1:d1d99cfe13df 70 }
roykrikke 1:d1d99cfe13df 71 }
roykrikke 1:d1d99cfe13df 72
roykrikke 1:d1d99cfe13df 73 int SDBlockDeviceDISCOF746NG::init()
roykrikke 1:d1d99cfe13df 74 {
roykrikke 1:d1d99cfe13df 75 lock();
roykrikke 1:d1d99cfe13df 76 _sd_state = BSP_SD_Init();
roykrikke 1:d1d99cfe13df 77
roykrikke 1:d1d99cfe13df 78 if(_sd_state != MSD_OK) {
roykrikke 1:d1d99cfe13df 79 if(_sd_state == MSD_ERROR_SD_NOT_PRESENT) {
roykrikke 1:d1d99cfe13df 80 debug_if(SD_DBG, "SD card is missing or not connected\n");
roykrikke 1:d1d99cfe13df 81 unlock ();
roykrikke 1:d1d99cfe13df 82 return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
roykrikke 1:d1d99cfe13df 83 } else {
roykrikke 1:d1d99cfe13df 84 debug_if(SD_DBG, "SD card initialization failed\n");
roykrikke 1:d1d99cfe13df 85 unlock();
roykrikke 1:d1d99cfe13df 86 return SD_BLOCK_DEVICE_ERROR_NO_INIT;
roykrikke 1:d1d99cfe13df 87 }
roykrikke 1:d1d99cfe13df 88 }
roykrikke 1:d1d99cfe13df 89 BSP_SD_GetCardInfo(&_current_card_info);
roykrikke 1:d1d99cfe13df 90
roykrikke 1:d1d99cfe13df 91 _card_type = _current_card_info.CardType;
roykrikke 1:d1d99cfe13df 92 _read_size = _current_card_info.BlockSize;
roykrikke 1:d1d99cfe13df 93 _program_size = _current_card_info.BlockSize;
roykrikke 1:d1d99cfe13df 94 _erase_size = _current_card_info.BlockSize;
roykrikke 1:d1d99cfe13df 95 _block_size = _current_card_info.BlockSize;
roykrikke 1:d1d99cfe13df 96 _capacity_in_blocks = _current_card_info.BlockNbr;
roykrikke 1:d1d99cfe13df 97
roykrikke 1:d1d99cfe13df 98 debug_if(SD_DBG, "Card Type: %i\n", _current_card_info.CardType);
roykrikke 1:d1d99cfe13df 99 debug_if(SD_DBG, "Card Version: %i\n", _current_card_info.CardVersion);
roykrikke 1:d1d99cfe13df 100 debug_if(SD_DBG, "Class: %i\n", _current_card_info.Class);
roykrikke 1:d1d99cfe13df 101 debug_if(SD_DBG, "Relative Card Address: %x\n", _current_card_info.RelCardAdd);
roykrikke 1:d1d99cfe13df 102 debug_if(SD_DBG, "Card Capacity in blocks: %i\n", _current_card_info.BlockNbr);
roykrikke 1:d1d99cfe13df 103 debug_if(SD_DBG, "One block size in bytes: %i\n", _current_card_info.BlockSize);
roykrikke 1:d1d99cfe13df 104 debug_if(SD_DBG, "Card logical Capacity in blocks: %i\n", _current_card_info.LogBlockNbr);
roykrikke 1:d1d99cfe13df 105 debug_if(SD_DBG, "Logical block size in bytes: %i\n", _current_card_info.LogBlockSize);
roykrikke 1:d1d99cfe13df 106 debug_if(SD_DBG, "Timeout: %i\n", _timeout);
roykrikke 1:d1d99cfe13df 107
roykrikke 1:d1d99cfe13df 108 _is_initialized = true;
roykrikke 1:d1d99cfe13df 109 unlock();
roykrikke 1:d1d99cfe13df 110 return SD_BLOCK_DEVICE_OK;
roykrikke 1:d1d99cfe13df 111 }
roykrikke 1:d1d99cfe13df 112
roykrikke 1:d1d99cfe13df 113 int SDBlockDeviceDISCOF746NG::deinit()
roykrikke 1:d1d99cfe13df 114 {
roykrikke 1:d1d99cfe13df 115 lock();
roykrikke 1:d1d99cfe13df 116 _sd_state = BSP_SD_DeInit ();
roykrikke 1:d1d99cfe13df 117 if(_sd_state != MSD_OK) {
roykrikke 1:d1d99cfe13df 118 debug_if (SD_DBG, "SD card deinitialization failed\n");
roykrikke 1:d1d99cfe13df 119 return SD_BLOCK_DEVICE_ERROR;
roykrikke 1:d1d99cfe13df 120 }
roykrikke 1:d1d99cfe13df 121 _is_initialized = false;
roykrikke 1:d1d99cfe13df 122 unlock();
roykrikke 1:d1d99cfe13df 123 return BD_ERROR_OK;
roykrikke 1:d1d99cfe13df 124 }
roykrikke 1:d1d99cfe13df 125
roykrikke 1:d1d99cfe13df 126 int SDBlockDeviceDISCOF746NG::read(void *b, bd_addr_t addr, bd_size_t size)
roykrikke 1:d1d99cfe13df 127 {
roykrikke 1:d1d99cfe13df 128 if(!is_valid_read (addr, size)) {
roykrikke 1:d1d99cfe13df 129 return SD_BLOCK_DEVICE_ERROR_PARAMETER;
roykrikke 1:d1d99cfe13df 130 }
roykrikke 1:d1d99cfe13df 131
roykrikke 1:d1d99cfe13df 132 lock();
roykrikke 1:d1d99cfe13df 133 if(!_is_initialized) {
roykrikke 1:d1d99cfe13df 134 unlock();
roykrikke 1:d1d99cfe13df 135 return SD_BLOCK_DEVICE_ERROR_NO_INIT;
roykrikke 1:d1d99cfe13df 136 }
roykrikke 1:d1d99cfe13df 137
roykrikke 1:d1d99cfe13df 138 uint32_t *buffer = static_cast<uint32_t *> (b);
roykrikke 1:d1d99cfe13df 139 int status = SD_BLOCK_DEVICE_OK;
roykrikke 1:d1d99cfe13df 140
roykrikke 1:d1d99cfe13df 141 // Get block address
roykrikke 1:d1d99cfe13df 142 uint32_t block_addr = addr / _block_size;
roykrikke 1:d1d99cfe13df 143 // Get block count
roykrikke 1:d1d99cfe13df 144 uint32_t block_cnt = size / _block_size;
roykrikke 1:d1d99cfe13df 145
roykrikke 1:d1d99cfe13df 146 debug_if(
roykrikke 1:d1d99cfe13df 147 SD_DBG,
roykrikke 1:d1d99cfe13df 148 "BD_SD_DISCO_F746NG::read addr: 0x%x, block_addr: %i size: %lu block count: %i\n",
roykrikke 1:d1d99cfe13df 149 addr, block_addr, size, block_cnt);
roykrikke 1:d1d99cfe13df 150
roykrikke 1:d1d99cfe13df 151 if(BSP_SD_ReadBlocks (buffer, block_addr, block_cnt, _timeout) != MSD_OK) {
roykrikke 1:d1d99cfe13df 152 status = SD_BLOCK_DEVICE_ERROR_READ;
roykrikke 1:d1d99cfe13df 153 }
roykrikke 1:d1d99cfe13df 154
roykrikke 1:d1d99cfe13df 155 // Wait until SD card is ready to use for new operation
roykrikke 1:d1d99cfe13df 156 while(BSP_SD_GetCardState() != SD_TRANSFER_OK) {
roykrikke 1:d1d99cfe13df 157 }
roykrikke 1:d1d99cfe13df 158
roykrikke 1:d1d99cfe13df 159 unlock ();
roykrikke 1:d1d99cfe13df 160 return status;
roykrikke 1:d1d99cfe13df 161 }
roykrikke 1:d1d99cfe13df 162
roykrikke 1:d1d99cfe13df 163 int SDBlockDeviceDISCOF746NG::program(const void *b, bd_addr_t addr, bd_size_t size)
roykrikke 1:d1d99cfe13df 164 {
roykrikke 1:d1d99cfe13df 165 if(!is_valid_program (addr, size)) {
roykrikke 1:d1d99cfe13df 166 return SD_BLOCK_DEVICE_ERROR_PARAMETER;
roykrikke 1:d1d99cfe13df 167 }
roykrikke 1:d1d99cfe13df 168
roykrikke 1:d1d99cfe13df 169 lock();
roykrikke 1:d1d99cfe13df 170 if(!_is_initialized) {
roykrikke 1:d1d99cfe13df 171 unlock ();
roykrikke 1:d1d99cfe13df 172 return SD_BLOCK_DEVICE_ERROR_NO_INIT;
roykrikke 1:d1d99cfe13df 173 }
roykrikke 1:d1d99cfe13df 174
roykrikke 1:d1d99cfe13df 175 uint32_t* buffer =
roykrikke 1:d1d99cfe13df 176 const_cast<uint32_t*> (reinterpret_cast<const uint32_t*> (b));
roykrikke 1:d1d99cfe13df 177 int status = SD_BLOCK_DEVICE_OK;
roykrikke 1:d1d99cfe13df 178
roykrikke 1:d1d99cfe13df 179 // Get block address
roykrikke 1:d1d99cfe13df 180 uint32_t block_addr = addr / _block_size;
roykrikke 1:d1d99cfe13df 181 // Get block count
roykrikke 1:d1d99cfe13df 182 uint32_t block_cnt = size / _block_size;
roykrikke 1:d1d99cfe13df 183
roykrikke 1:d1d99cfe13df 184 debug_if (
roykrikke 1:d1d99cfe13df 185 SD_DBG,
roykrikke 1:d1d99cfe13df 186 "BD_SD_DISCO_F746NG::program addr: 0x%x, block_addr: %i size: %lu block count: %i\n",
roykrikke 1:d1d99cfe13df 187 addr, block_addr, size, block_cnt);
roykrikke 1:d1d99cfe13df 188
roykrikke 1:d1d99cfe13df 189 if(BSP_SD_WriteBlocks (buffer, block_addr, block_cnt, _timeout) != MSD_OK) {
roykrikke 1:d1d99cfe13df 190 status = SD_BLOCK_DEVICE_ERROR_PROGRAM;
roykrikke 1:d1d99cfe13df 191 }
roykrikke 1:d1d99cfe13df 192
roykrikke 1:d1d99cfe13df 193 // Wait until SD card is ready to use for new operation
roykrikke 1:d1d99cfe13df 194 while(BSP_SD_GetCardState() != SD_TRANSFER_OK) {
roykrikke 1:d1d99cfe13df 195 }
roykrikke 1:d1d99cfe13df 196
roykrikke 1:d1d99cfe13df 197 unlock();
roykrikke 1:d1d99cfe13df 198 return status;
roykrikke 1:d1d99cfe13df 199 }
roykrikke 1:d1d99cfe13df 200
roykrikke 1:d1d99cfe13df 201 int SDBlockDeviceDISCOF746NG::erase(bd_addr_t addr, bd_size_t size)
roykrikke 1:d1d99cfe13df 202 {
roykrikke 1:d1d99cfe13df 203 if (!is_valid_erase(addr, size)) {
roykrikke 1:d1d99cfe13df 204 return SD_BLOCK_DEVICE_ERROR_PARAMETER;
roykrikke 1:d1d99cfe13df 205 }
roykrikke 1:d1d99cfe13df 206
roykrikke 1:d1d99cfe13df 207 lock();
roykrikke 1:d1d99cfe13df 208 if(!_is_initialized) {
roykrikke 1:d1d99cfe13df 209 unlock();
roykrikke 1:d1d99cfe13df 210 return SD_BLOCK_DEVICE_ERROR_NO_INIT;
roykrikke 1:d1d99cfe13df 211 }
roykrikke 1:d1d99cfe13df 212
roykrikke 1:d1d99cfe13df 213 size -= _block_size;
roykrikke 1:d1d99cfe13df 214
roykrikke 1:d1d99cfe13df 215 int status = SD_BLOCK_DEVICE_OK;
roykrikke 1:d1d99cfe13df 216 uint32_t start_addr = addr;
roykrikke 1:d1d99cfe13df 217 uint32_t end_addr = start_addr + size;
roykrikke 1:d1d99cfe13df 218
roykrikke 1:d1d99cfe13df 219 // Get block count
roykrikke 1:d1d99cfe13df 220 uint32_t block_start_addr = start_addr / _block_size;
roykrikke 1:d1d99cfe13df 221 // Get block address
roykrikke 1:d1d99cfe13df 222 uint32_t block_end_addr = end_addr / _block_size;
roykrikke 1:d1d99cfe13df 223
roykrikke 1:d1d99cfe13df 224 debug_if(
roykrikke 1:d1d99cfe13df 225 SD_DBG,
roykrikke 1:d1d99cfe13df 226 "BD_SD_DISCO_F746NG::erase start_addr: 0x%x, block_start_addr: %i | end_addr: 0x%x, block_end_addr: %i size: %lu\n",
roykrikke 1:d1d99cfe13df 227 start_addr, block_start_addr, end_addr, block_end_addr, size);
roykrikke 1:d1d99cfe13df 228
roykrikke 1:d1d99cfe13df 229 if(BSP_SD_Erase (block_start_addr, block_end_addr) != MSD_OK) {
roykrikke 1:d1d99cfe13df 230 status = SD_BLOCK_DEVICE_ERROR_ERASE;
roykrikke 1:d1d99cfe13df 231 }
roykrikke 1:d1d99cfe13df 232
roykrikke 1:d1d99cfe13df 233 /* Wait until SD card is ready to use for new operation */
roykrikke 1:d1d99cfe13df 234 while(BSP_SD_GetCardState() != SD_TRANSFER_OK) {
roykrikke 1:d1d99cfe13df 235 }
roykrikke 1:d1d99cfe13df 236
roykrikke 1:d1d99cfe13df 237 unlock();
roykrikke 1:d1d99cfe13df 238 return status;
roykrikke 1:d1d99cfe13df 239 }
roykrikke 1:d1d99cfe13df 240
roykrikke 1:d1d99cfe13df 241 bd_size_t SDBlockDeviceDISCOF746NG::get_read_size() const
roykrikke 1:d1d99cfe13df 242 {
roykrikke 1:d1d99cfe13df 243 return _read_size;
roykrikke 1:d1d99cfe13df 244 }
roykrikke 1:d1d99cfe13df 245
roykrikke 1:d1d99cfe13df 246 bd_size_t SDBlockDeviceDISCOF746NG::get_program_size() const
roykrikke 1:d1d99cfe13df 247 {
roykrikke 1:d1d99cfe13df 248 return _program_size;
roykrikke 1:d1d99cfe13df 249 }
roykrikke 1:d1d99cfe13df 250
roykrikke 1:d1d99cfe13df 251 bd_size_t SDBlockDeviceDISCOF746NG::get_erase_size() const
roykrikke 1:d1d99cfe13df 252 {
roykrikke 1:d1d99cfe13df 253 return _erase_size;
roykrikke 1:d1d99cfe13df 254 }
roykrikke 1:d1d99cfe13df 255
roykrikke 1:d1d99cfe13df 256 bd_size_t SDBlockDeviceDISCOF746NG::size() const
roykrikke 1:d1d99cfe13df 257 {
roykrikke 1:d1d99cfe13df 258 return (_block_size * _capacity_in_blocks);
roykrikke 1:d1d99cfe13df 259 }
roykrikke 1:d1d99cfe13df 260