SDHI_driver patch (mbedOS 5.11.5)

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RZ_SDHIBlockDevice.cpp Source File

RZ_SDHIBlockDevice.cpp

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 #include "RZ_SDHIBlockDevice.hpp"
00023 #include "mbed_debug.h"
00024 #include <errno.h>
00025 
00026 #include "iodefine.h"
00027 #include "PeripheralPins.h"
00028 
00029 /* Required version: 5.6.1 and above */
00030 #ifdef MBED_MAJOR_VERSION
00031 #if (MBED_VERSION < MBED_ENCODE_VERSION(5,6,1))
00032 #error "Incompatible mbed-os version detected! Required 5.5.4 and above"
00033 #endif
00034 #else
00035 #warning "mbed-os version 5.6.1 or above required"
00036 #endif
00037 
00038 
00039 
00040 //#define SD_COMMAND_TIMEOUT                       5000   /*!< Timeout in ms for response */
00041 //#define SD_CMD0_GO_IDLE_STATE_RETRIES            5      /*!< Number of retries for sending CMDO */
00042 #define SD_CMD_TRACE                             0      /*!< 1 - Enable SD command tracing */
00043 
00044 //#define SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK        -5001    /*!< operation would block */
00045 #define SD_BLOCK_DEVICE_ERROR_UNSUPPORTED        -5002  /*!< unsupported operation */
00046 #define SD_BLOCK_DEVICE_ERROR_PARAMETER          -5003  /*!< invalid parameter */
00047 #define SD_BLOCK_DEVICE_ERROR_NO_INIT            -5004  /*!< uninitialized */
00048 #define SD_BLOCK_DEVICE_ERROR_NO_DEVICE          -5005  /*!< device is missing or not connected */
00049 #define SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED    -5006  /*!< write protected */
00050 #define SD_BLOCK_DEVICE_ERROR_UNUSABLE           -5007  /*!< unusable card */
00051 #define SD_BLOCK_DEVICE_ERROR_NO_RESPONSE        -5008  /*!< No response from device */
00052 #define SD_BLOCK_DEVICE_ERROR_CRC                -5009  /*!< CRC error */
00053 #define SD_BLOCK_DEVICE_ERROR_ERASE              -5010  /*!< Erase error: reset/sequence */
00054 #define SD_BLOCK_DEVICE_ERROR_WRITE              -5011  /*!< SPI Write error: !SPI_DATA_ACCEPTED */
00055 
00056 #define BLOCK_SIZE_HC                            512    /*!< Block size supported for SD card is 512 bytes  */
00057 #define WRITE_BL_PARTIAL                         0      /*!< Partial block write - Not supported */
00058 //
00059 // Types
00060 #define SDCARD_NONE              0           /**< No card is present */
00061 #define SDCARD_V1                1           /**< v1.x Standard Capacity */
00062 #define SDCARD_V2                2           /**< v2.x Standard capacity SD card */
00063 #define SDCARD_V2HC              3           /**< v2.x High capacity SD card */
00064 #define CARD_UNKNOWN             4           /**< Unknown or unsupported card */
00065 
00066 /* R3 Response : OCR Register */
00067 #define OCR_HCS_CCS             (0x1 << 30)
00068 #define OCR_LOW_VOLTAGE         (0x01 << 24)
00069 #define OCR_3_3V                (0x1 << 20)
00070 
00071 #define ESD_SECTOR_SIZE         BLOCK_SIZE_HC
00072 
00073 #define REG_CSD_HACK
00074 
00075 #define CSD_CID_LENGTH        16
00076 
00077 #define ARRAYSIZE(arr)  (size_t)(sizeof(arr)/sizeof(arr[0]))
00078 
00079 static uint32_t _sd_workbuf[SD_SIZE_OF_INIT/ sizeof(uint32_t)] __attribute__ ((section ("NC_BSS")));
00080 static uint32_t _sd_rw_buf[ESD_SECTOR_SIZE/sizeof(uint32_t)] __attribute__ ((section ("NC_BSS")));
00081 //static uint8_t  _sectorBuff[ESD_SECTOR_SIZE] __attribute__ ((section ("NC_BSS")));
00082 
00083 
00084 static uint32_t ext_bits(unsigned char *data, size_t datasize, unsigned int msb, unsigned int lsb) {
00085     uint32_t bits = 0;
00086 
00087     if ((datasize > 0) && (msb < (datasize<<3)))
00088     {
00089         uint32_t size = 1 + msb - lsb;
00090         for (uint32_t i = 0; i < size; i++)
00091         {
00092             uint32_t position = lsb + i;
00093             int32_t byte = (datasize-1) - (position >> 3);
00094             if( byte < 0 )
00095                 break;
00096             uint32_t bit = position & 0x7;
00097             uint32_t value = (data[byte] >> bit) & 1;
00098             bits |= value << i;
00099         }
00100     }
00101     return bits;
00102 }
00103 
00104 #if SDHI_DBG
00105 const sderr_t SDHIBlockDevice::_sd_error_msg[]= {
00106         {"SD_OK",                       0},        /* OK */
00107         {"SD_ERR",                     -1},        /* general error */
00108         {"SD_ERR_WP",                  -2},        /* write protect error */
00109         {"SD_ERR_RO",                  -3},        /* read only error */
00110         {"SD_ERR_RES_TOE",             -4},        /* response time out error */
00111         {"SD_ERR_CARD_TOE",            -5},        /* card time out error */
00112         {"SD_ERR_END_BIT",             -6},        /* end bit error */
00113         {"SD_ERR_CRC",                 -7},        /* CRC error */
00114         {"SD_ERR_CARD_RES",            -8},        /* card response error */
00115         {"SD_ERR_HOST_TOE",            -9},        /* host time out error */
00116         {"SD_ERR_CARD_ERASE",         -10},        /* card erase error */
00117         {"SD_ERR_CARD_LOCK",          -11},        /* card lock error */
00118         {"SD_ERR_CARD_UNLOCK",        -12},        /* card unlock error */
00119         {"SD_ERR_HOST_CRC",           -13},        /* host CRC error */
00120         {"SD_ERR_CARD_ECC",           -14},        /* card internal ECC error */
00121         {"SD_ERR_CARD_CC",            -15},        /* card internal error */
00122         {"SD_ERR_CARD_ERROR",         -16},        /* unknown card error */
00123         {"SD_ERR_CARD_TYPE",          -17},        /* non support card type */
00124         {"SD_ERR_NO_CARD",            -18},        /* no card */
00125         {"SD_ERR_ILL_READ",           -19},        /* illegal buffer read */
00126         {"SD_ERR_ILL_WRITE",          -20},        /* illegal buffer write */
00127         {"SD_ERR_AKE_SEQ",            -21},        /* the sequence of authentication process */
00128         {"SD_ERR_OVERWRITE",          -22},        /* CID/CSD overwrite error */
00129     /* 23-29 */
00130         {"SD_ERR_CPU_IF",             -30},        /* target CPU interface function error  */
00131         {"SD_ERR_STOP",               -31},        /* user stop */
00132     /* 32-49 */
00133         {"SD_ERR_CSD_VER",            -50},        /* CSD register version error */
00134         {"SD_ERR_SCR_VER",            -51},        /* SCR register version error */
00135         {"SD_ERR_FILE_FORMAT",        -52},        /* CSD register file format error  */
00136         {"SD_ERR_NOTSUP_CMD",         -53},        /* not supported command  */
00137     /* 54-59 */
00138         {"SD_ERR_ILL_FUNC",           -60},        /* invalid function request error */
00139         {"SD_ERR_IO_VERIFY",          -61},        /* direct write verify error */
00140         {"SD_ERR_IO_CAPAB",           -62},        /* IO capability error */
00141     /* 63-69 */
00142         {"SD_ERR_IFCOND_VER",         -70},        /* Interface condition version error */
00143         {"SD_ERR_IFCOND_VOLT",        -71},        /* Interface condition voltage error */
00144         {"SD_ERR_IFCOND_ECHO",        -72},        /* Interface condition echo back pattern error */
00145     /* 73-79 */
00146         {"SD_ERR_OUT_OF_RANGE",       -80},        /* the argument was out of range */
00147         {"SD_ERR_ADDRESS_ERROR",      -81},
00148         {"SD_ERR_BLOCK_LEN_ERROR",    -82},
00149         {"SD_ERR_ILLEGAL_COMMAND",    -83},
00150         {"SD_ERR_RESERVED_ERROR18",   -84},
00151         {"SD_ERR_RESERVED_ERROR17",   -85},
00152         {"SD_ERR_CMD_ERROR",          -86},
00153         {"SD_ERR_CBSY_ERROR",         -87},
00154         {"SD_ERR_NO_RESP_ERROR",      -88},
00155     /* 89 */
00156     /* 90-95 */
00157         {"SD_ERR_ERROR",              -96},
00158         {"SD_ERR_FUNCTION_NUMBER",    -97},
00159         {"SD_ERR_COM_CRC_ERROR",      -98},
00160         {"SD_ERR_INTERNAL",           -99},        /* driver software internal error */
00161 };
00162 
00163 #endif
00164 
00165 SDHIBlockDevice::SDHIBlockDevice(uint32_t sdport)
00166 {
00167     _init_ref_count = 0;
00168     Initialize(sdport);
00169 //    for( uint32_t n=0; n < ARRAYSIZE(_sd_error_msg); n++ )
00170 //    {
00171 //      _sd_err_map[_sd_error_msg[n].errorno] = _sd_error_msg[n].msg;
00172 //    }
00173 //
00174 //    if ( sdport < SDHI_COUNT )
00175 //    {
00176 //      _sd_channel = sdport;
00177 //      _regbase = (uint32_t)((_sd_channel == 0ul) ? &SDHI0 : &SDHI1);
00178 //
00179 //      _card_type = SDCARD_NONE;
00180 //
00181 //      // Set default to 100kHz for initialisation and 1MHz for data transfer
00182 //      _init_sck = 100000;
00183 //      _transfer_sck = hz;
00184 //
00185 //      // Only HC block size is supported.
00186 //      _block_size = BLOCK_SIZE_HC;
00187 //      _erase_size = BLOCK_SIZE_HC;
00188 //      _mtx_lock = false;
00189 //
00190 //      int32_t sd_err = sd_init( _sd_channel, _regbase, &_sd_workbuf[0], SD_CD_SOCKET );
00191 //      if ( sd_err != SD_OK)
00192 //      {
00193 //          _error(sd_err);
00194 ////            return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
00195 //      }
00196 //
00197 //      void *rw_buff = (void *)&_sd_rw_buf[0];
00198 //
00199 //      sd_err = sd_set_buffer( _sd_channel, rw_buff, (unsigned long)sizeof(_sd_rw_buf) );
00200 //      if ( sd_err != SD_OK )
00201 //      {
00202 //          _error(sd_err);
00203 //      }
00204 //
00205 //    }
00206 //    else
00207 //    {
00208 //      _sd_channel = -1;
00209 //      _regbase = 0ul;
00210 //
00211 //    }
00212 }
00213 
00214 SDHIBlockDevice::SDHIBlockDevice(PinName sd_CLK, PinName sd_CMD, PinName sd_CD, PinName sd_WP,
00215                                  PinName sd_D0, PinName sd_D1, PinName sd_D2, PinName sd_D3 )
00216 {
00217     _init_ref_count = 0;
00218 
00219     uint32_t sd_wp  = pinmap_peripheral(sd_WP, PinMap_SDHI_WP);
00220     uint32_t sd_cd  = pinmap_peripheral(sd_CD, PinMap_SDHI_CD);
00221     uint32_t sd_clk = pinmap_peripheral(sd_CLK, PinMap_SDHI_CLK);
00222     uint32_t sd_cmd = pinmap_peripheral(sd_CMD, PinMap_SDHI_CMD);
00223     uint32_t sd_d0  = pinmap_peripheral(sd_D0, PinMap_SDHI_D0);
00224     uint32_t sd_d1  = pinmap_peripheral(sd_D1, PinMap_SDHI_D1);
00225     uint32_t sd_d2  = pinmap_peripheral(sd_D2, PinMap_SDHI_D2);
00226     uint32_t sd_d3  = pinmap_peripheral(sd_D3, PinMap_SDHI_D3);
00227 
00228     uint32_t sd_cntl_1 = pinmap_merge(sd_wp, sd_cd);
00229     uint32_t sd_cntl_2 = pinmap_merge(sd_clk, sd_cmd);
00230     uint32_t sd_cntl = pinmap_merge(sd_cntl_1, sd_cntl_2);
00231 
00232     uint32_t sd_data_1 = pinmap_merge(sd_d0, sd_d1);
00233     uint32_t sd_data_2 = pinmap_merge(sd_d2, sd_d3);
00234     uint32_t sd_data = pinmap_merge(sd_data_1, sd_data_2);
00235 
00236     uint32_t sdport = pinmap_merge(sd_cntl, sd_data);
00237 
00238     MBED_ASSERT((int)sdport != NC);
00239 
00240     Initialize(sdport);
00241 }
00242 
00243 
00244 
00245 SDHIBlockDevice::~SDHIBlockDevice()
00246 {
00247     if (_is_initialized) {
00248         deinit();
00249     }
00250 }
00251 
00252 void SDHIBlockDevice::Initialize( uint32_t sdport )
00253 {
00254 #if SDHI_DBG
00255     for( uint32_t n=0; n < ARRAYSIZE(_sd_error_msg); n++ )
00256     {
00257         _sd_err_map[_sd_error_msg[n].errorno] = _sd_error_msg[n].msg;
00258     }
00259 #endif
00260     _sectors = 0;
00261     _is_initialized = 0;
00262     if ( sdport < SDHI_COUNT )
00263     {
00264         _sd_channel = sdport;
00265         _regbase = (uint32_t)((_sd_channel == SDHI_0) ? &SDHI0 : &SDHI1);
00266 
00267         _card_type = SDCARD_NONE;
00268 
00269         // Only HC block size is supported.
00270         _block_size = BLOCK_SIZE_HC;
00271         _erase_size = BLOCK_SIZE_HC;
00272 
00273         int32_t sd_err = sd_init( _sd_channel, _regbase, &_sd_workbuf[0], SD_CD_SOCKET );
00274         if ( sd_err != SD_OK)
00275         {
00276             _error(sd_err);
00277         }
00278 
00279         void *rw_buff = (void *)&_sd_rw_buf[0];
00280 
00281         sd_err = sd_set_buffer( _sd_channel, rw_buff, (unsigned long)sizeof(_sd_rw_buf) );
00282         if ( sd_err != SD_OK )
00283         {
00284             _error(sd_err);
00285         }
00286     }
00287     else
00288     {
00289         _sd_channel = -1;
00290         _regbase = 0ul;
00291 
00292     }
00293 
00294 }
00295 
00296 int SDHIBlockDevice::_initialise_card()
00297 {
00298     // Detail debugging is for commands
00299     _dbg = SDHI_DBG ? SD_CMD_TRACE : 0;
00300 
00301     int32_t status = BD_ERROR_OK;
00302 
00303     int32_t sd_err;
00304 
00305     if( _regbase )
00306     {
00307         sd_err = sd_mount(_sd_channel, SDCFG_DRIVER_MODE, SD_VOLT_3_3);
00308 
00309         if ( sd_err != SD_OK )
00310         {
00311             _error(sd_err);
00312             return SD_BLOCK_DEVICE_ERROR_UNUSABLE;
00313         }
00314         uint8_t    card_type;
00315         uint8_t    card_speed;
00316         uint8_t    card_capa;
00317 
00318         sd_err=sd_get_type(_sd_channel, &card_type, &card_speed, &card_capa);
00319         if( sd_err != SD_OK)
00320         {
00321             return _error(sd_err);
00322         }
00323 
00324         if( (card_type & SD_MEDIA_SD) != SD_MEDIA_SD )
00325         {
00326             return SD_BLOCK_DEVICE_ERROR_UNUSABLE;
00327         }
00328 
00329         uint32_t regOCR;
00330         uint8_t regCID[CSD_CID_LENGTH];
00331         uint8_t regCSD[CSD_CID_LENGTH];
00332         uint8_t regDSR[2];
00333         uint8_t regSCR[8];
00334 
00335         sd_err = sd_get_reg(_sd_channel, (uint8_t *)&regOCR, regCID, regCSD, regDSR, regSCR);
00336         if (sd_err != SD_OK)
00337         {
00338             return _error(sd_err);
00339         }
00340         regOCR = __REV(regOCR);
00341         // Check if card supports voltage range: 3.3V
00342         if (!(regOCR & OCR_3_3V)) {
00343             _card_type = CARD_UNKNOWN;
00344             status = SD_BLOCK_DEVICE_ERROR_UNUSABLE;
00345             return status;
00346         }
00347     }
00348     else
00349     {
00350         status = SD_BLOCK_DEVICE_ERROR_NO_INIT;
00351     }
00352 
00353     return status;
00354 }
00355 
00356 
00357 int SDHIBlockDevice::init()
00358 {
00359     vMutex l(&_mutex);
00360 
00361     if(!_is_initialized)
00362         _init_ref_count = 0;
00363 
00364     _init_ref_count++;
00365 
00366     if(_init_ref_count != 1)
00367         return BD_ERROR_OK;
00368 
00369     int err = _initialise_card();
00370     _is_initialized = (err == BD_ERROR_OK);
00371     if (!_is_initialized) {
00372         debug_if(SDHI_DBG, "Fail to initialize card\n");
00373         return err;
00374     }
00375     debug_if(SDHI_DBG, "init card = %d\r\n", _is_initialized);
00376     _sectors = _sd_sectors();
00377 
00378     if (0 == _sectors) {
00379         return BD_ERROR_DEVICE_ERROR;
00380     }
00381 
00382     return BD_ERROR_OK;
00383 }
00384 
00385 int SDHIBlockDevice::deinit()
00386 {
00387     vMutex l(&_mutex);
00388 
00389     if(!_is_initialized)
00390         _init_ref_count = 0;
00391 
00392     _init_ref_count--; //!!!
00393 
00394     if(_init_ref_count)
00395         return BD_ERROR_OK;
00396 
00397     _sectors = 0;
00398     if ( _is_initialized )
00399     {
00400         sd_unmount(_sd_channel);
00401         debug_if(SDHI_DBG, "card deinited![%d]\r\n", _is_initialized);
00402         _is_initialized = false;
00403     }
00404     return 0;
00405 }
00406 
00407 
00408 int SDHIBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
00409 {
00410     if (!is_valid_program(addr, size)) {
00411         return SD_BLOCK_DEVICE_ERROR_PARAMETER;
00412     }
00413 
00414     vMutex l(&_mutex);
00415 
00416     if (!_is_initialized) {
00417         return SD_BLOCK_DEVICE_ERROR_NO_INIT;
00418     }
00419 
00420     uint8_t* buffer = const_cast<uint8_t*>(static_cast<const uint8_t*>(b));
00421     int status = BD_ERROR_OK;
00422 
00423     // Get block count
00424     bd_addr_t blockCnt = size / _block_size;
00425 
00426     addr = addr / _block_size;
00427 
00428     // Send command to perform write operation
00429     int32_t sd_err = sd_write_sect( _sd_channel, buffer, addr, blockCnt, SD_WRITE_OVERWRITE);
00430 
00431     if (sd_err != SD_OK)
00432     {
00433         _error(sd_err);
00434         status = SD_BLOCK_DEVICE_ERROR_WRITE;
00435     }
00436 
00437     return status;
00438 }
00439 
00440 int SDHIBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
00441 {
00442     if (!is_valid_read(addr, size)) {
00443         return SD_BLOCK_DEVICE_ERROR_PARAMETER;
00444     }
00445 
00446     vMutex l(&_mutex);
00447 
00448     if (!_is_initialized) {
00449         return SD_BLOCK_DEVICE_ERROR_NO_INIT;
00450     }
00451     uint8_t *buffer = static_cast<uint8_t*>(b);
00452     int status = BD_ERROR_OK;
00453     bd_addr_t blockCnt =  size / _block_size;
00454 
00455     addr = addr / _block_size;
00456 
00457     int32_t sd_err = sd_read_sect(_sd_channel, buffer, addr, blockCnt);
00458 
00459     if (sd_err != SD_OK)
00460     {
00461         _error(sd_err);
00462         status = SD_BLOCK_DEVICE_ERROR_NO_RESPONSE;
00463     }
00464 
00465     return status;
00466 }
00467 
00468 int SDHIBlockDevice::erase(bd_addr_t addr, bd_size_t size)
00469 {
00470     return 0;
00471 }
00472 
00473 
00474 bool SDHIBlockDevice::_is_valid_trim(bd_addr_t addr, bd_size_t size)
00475 {
00476     return (
00477         addr % _erase_size == 0 &&
00478         size % _erase_size == 0 &&
00479         addr + size <= this->size());
00480 }
00481 
00482 int SDHIBlockDevice::trim(bd_addr_t addr, bd_size_t size)
00483 {
00484     if (!_is_valid_trim(addr, size)) {
00485         return SD_BLOCK_DEVICE_ERROR_PARAMETER;
00486     }
00487 
00488     vMutex l(&_mutex);
00489 
00490     if (!_is_initialized) {
00491         return SD_BLOCK_DEVICE_ERROR_NO_INIT;
00492     }
00493     int status = BD_ERROR_OK;
00494 
00495     size -= _block_size;
00496     // SDSC Card (CCS=0) uses byte unit address
00497     // SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit)
00498     if (SDCARD_V2HC == _card_type) {
00499         size = size / _block_size;
00500         addr = addr / _block_size;
00501     }
00502 
00503     return status;
00504 }
00505 
00506 bd_size_t SDHIBlockDevice::get_read_size() const
00507 {
00508     return _block_size;
00509 }
00510 
00511 bd_size_t SDHIBlockDevice::get_program_size() const
00512 {
00513     return _block_size;
00514 }
00515 
00516 /*
00517 bd_size_t SDHIBlockDevice::get_erase_size() const
00518 {
00519     return _block_size;
00520 }
00521 */
00522 
00523 bd_size_t SDHIBlockDevice::size() const
00524 {
00525     return _block_size*_sectors;
00526 }
00527 
00528 void SDHIBlockDevice::debug(bool dbg)
00529 {
00530     _dbg = dbg;
00531 }
00532 
00533 const char *SDHIBlockDevice::get_type() const
00534 {
00535     return "RZ-SDHI";
00536 }
00537 
00538 // PRIVATE FUNCTIONS
00539 
00540 bd_size_t SDHIBlockDevice::_sd_sectors() {
00541     uint32_t c_size, c_size_mult, read_bl_len;
00542     uint32_t block_len, mult, blocknr;
00543     uint32_t hc_c_size;
00544     bd_size_t blocks = 0, capacity = 0;
00545     uint8_t csd[CSD_CID_LENGTH];
00546 
00547     int32_t sd_err = sd_get_reg(_sd_channel, NULL, NULL, csd, NULL, NULL);
00548     if ( sd_err != SD_OK )
00549     {
00550         debug_if(SDHI_DBG, "Couldn't read csd response from disk\r\n");
00551         _error(sd_err);
00552         return 0;
00553     }
00554     for(int i = 0; i < (CSD_CID_LENGTH-1); i++)
00555     {
00556         csd[i] = csd[i+1];
00557     }
00558 
00559     debug_if(SDHI_DBG,"CSD is ");
00560     for(unsigned int i = 0; i < sizeof(csd); i++)
00561     {
00562         debug_if(SDHI_DBG, "%02X ", csd[i]);
00563     }
00564     debug_if(SDHI_DBG,"\r\n");
00565 
00566     // csd_structure : csd[127:126]
00567     int csd_structure = ext_bits(csd, CSD_CID_LENGTH, 127, 126);
00568     switch (csd_structure) {
00569         case 0:
00570             c_size = ext_bits(csd, CSD_CID_LENGTH, 73, 62);              // c_size        : csd[73:62]
00571             c_size_mult = ext_bits(csd, CSD_CID_LENGTH, 49, 47);         // c_size_mult   : csd[49:47]
00572             read_bl_len = ext_bits(csd, CSD_CID_LENGTH, 83, 80);         // read_bl_len   : csd[83:80] - the *maximum* read block length
00573             block_len = 1 << read_bl_len;                // BLOCK_LEN = 2^READ_BL_LEN
00574             mult = 1 << (c_size_mult + 2);               // MULT = 2^C_SIZE_MULT+2 (C_SIZE_MULT < 8)
00575             blocknr = (c_size + 1) * mult;               // BLOCKNR = (C_SIZE+1) * MULT
00576             capacity = blocknr * block_len;              // memory capacity = BLOCKNR * BLOCK_LEN
00577             blocks = capacity / _block_size;
00578             debug_if(SDHI_DBG, "Standard Capacity: c_size: %lu\r\n", c_size);
00579             debug_if(SDHI_DBG, "Sectors: 0x%llx : %llu\r\n", blocks, blocks);
00580             debug_if(SDHI_DBG, "Capacity: 0x%llx : %llu MB\r\n", capacity, (capacity/(1024U*1024U)));
00581 
00582             // ERASE_BLK_EN = 1: Erase in multiple of 512 bytes supported
00583             if (ext_bits(csd, CSD_CID_LENGTH, 46, 46)) {
00584                 _erase_size = BLOCK_SIZE_HC;
00585             } else {
00586                 // ERASE_BLK_EN = 1: Erase in multiple of SECTOR_SIZE supported
00587                 _erase_size = BLOCK_SIZE_HC * (ext_bits(csd, CSD_CID_LENGTH, 45, 39) + 1);
00588             }
00589             break;
00590 
00591         case 1:
00592             hc_c_size = ext_bits(csd, CSD_CID_LENGTH, 69, 48);            // device size : C_SIZE : [69:48]
00593             blocks = (hc_c_size+1) << 10;                 // block count = C_SIZE+1) * 1K byte (512B is block size)
00594             debug_if(SDHI_DBG, "SDHC/SDXC Card: hc_c_size: %lu\r\n", hc_c_size);
00595             debug_if(SDHI_DBG, "Sectors: 0x%llx : %llu\r\n", blocks, blocks);
00596             debug_if(SDHI_DBG, "Capacity: %llu MB\r\n", (blocks/(2048U)));
00597             // ERASE_BLK_EN is fixed to 1, which means host can erase one or multiple of 512 bytes.
00598             _erase_size = BLOCK_SIZE_HC;
00599             break;
00600 
00601         default:
00602             debug_if(SDHI_DBG, "CSD struct unsupported\r\n");
00603             return 0;
00604     };
00605     return blocks;
00606 }
00607 
00608 const char * SDHIBlockDevice::_sderr_msg(int32_t errorno)
00609 {
00610 #if SDHI_DBG
00611     map<int32_t, const char *>::iterator it = _sd_err_map.find(errorno);
00612 
00613     if ( it != _sd_err_map.end() )
00614         return it->second;
00615     else
00616         return "SD UNKNWON ERROR NO\n";
00617 #else
00618     return (const char *)0;
00619 #endif
00620 }
00621 
00622 int SDHIBlockDevice::_error(int32_t errcode)
00623 {
00624     int32_t sd_err = errcode;
00625     if(_sd_channel >= 0 && _sd_channel < SDHI_COUNT )
00626     {
00627         int32_t err = sd_get_error(_sd_channel);
00628         if ( err != SD_OK)
00629             sd_err = err;
00630         debug_if(SDHI_DBG, _sderr_msg(sd_err));
00631     }
00632     return sd_err;
00633 }
00634 
00635