Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDIO support - WIP #10235

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
394 changes: 394 additions & 0 deletions components/storage/blockdevice/COMPONENT_SDIO/SDIOBlockDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,394 @@
/* mbed Microcontroller Library
* Copyright (c) 2019 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#include "sdio_api.h"
#include "us_ticker_api.h"
#include "SDIOBlockDevice.h"
#include "platform/mbed_debug.h"

using namespace mbed;

#if DEVICE_SDIO

/*
* defines
*/

#define SD_DBG 0 /*!< 1 - Enable debugging */
#define SD_CMD_TRACE 0 /*!< 1 - Enable SD command tracing */

#define SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK -5001 /*!< operation would block */
#define SD_BLOCK_DEVICE_ERROR_UNSUPPORTED -5002 /*!< unsupported operation */
#define SD_BLOCK_DEVICE_ERROR_PARAMETER -5003 /*!< invalid parameter */
#define SD_BLOCK_DEVICE_ERROR_NO_INIT -5004 /*!< uninitialized */
#define SD_BLOCK_DEVICE_ERROR_NO_DEVICE -5005 /*!< device is missing or not connected */
#define SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED -5006 /*!< write protected */
#define SD_BLOCK_DEVICE_ERROR_UNUSABLE -5007 /*!< unusable card */
#define SD_BLOCK_DEVICE_ERROR_NO_RESPONSE -5008 /*!< No response from device */
#define SD_BLOCK_DEVICE_ERROR_CRC -5009 /*!< CRC error */
#define SD_BLOCK_DEVICE_ERROR_ERASE -5010 /*!< Erase error: reset/sequence */
#define SD_BLOCK_DEVICE_ERROR_WRITE -5011 /*!< SPI Write error: !SPI_DATA_ACCEPTED */
#define SD_BLOCK_DEVICE_ERROR_UNSUPPORTED_BLOCKSIZE -5012 /*!< unsupported blocksize, only 512 byte supported */
#define SD_BLOCK_DEVICE_ERROR_READBLOCKS -5013 /*!< read data blocks from SD failed */
#define SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS -5014 /*!< write data blocks to SD failed */
#define SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS -5015 /*!< erase data blocks to SD failed */

#define BLOCK_SIZE_HC 512 /*!< Block size supported for SD card is 512 bytes */

// Types
#define SDCARD_NONE 0 /**< No card is present */
#define SDCARD_V1 1 /**< v1.x Standard Capacity */
#define SDCARD_V2 2 /**< v2.x Standard capacity SD card */
#define SDCARD_V2HC 3 /**< v2.x High capacity SD card */
#define CARD_UNKNOWN 4 /**< Unknown or unsupported card */

SDIOBlockDevice::SDIOBlockDevice(PinName cardDetect) : _cardDetect(cardDetect),
_is_initialized(0),
_sectors(0),
_init_ref_count(0)
{
// Only HC block size is supported.
_block_size = BLOCK_SIZE_HC;
_erase_size = BLOCK_SIZE_HC;
}

SDIOBlockDevice::~SDIOBlockDevice()
{
if (_is_initialized) {
deinit();
}
}

int SDIOBlockDevice::init()
{
debug_if(SD_DBG, "init Card...\r\n");

lock();

if (!_is_initialized) {
_init_ref_count = 0;
}

_init_ref_count++;

if (_init_ref_count != 1) {
unlock();
return BD_ERROR_OK;
}

if (isPresent() == false) {
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
}

int status = sdio_init();
if (BD_ERROR_OK != status) {
unlock();
return BD_ERROR_DEVICE_ERROR;
}

sdio_get_card_info(&_cardInfo);
_is_initialized = true;
debug_if(SD_DBG, "SD initialized: type: %d version: %d class: %d\n",
_cardInfo.CardType, _cardInfo.CardVersion, _cardInfo.Class);
debug_if(SD_DBG, "SD size: %d MB\n",
_cardInfo.LogBlockNbr / 2 / 1024);

// get sectors count from cardinfo
_sectors = _cardInfo.LogBlockNbr;
if (BLOCK_SIZE_HC != _cardInfo.BlockSize) {
unlock();
return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED_BLOCKSIZE;
}

unlock();
return status;
}

int SDIOBlockDevice::deinit()
{
debug_if(SD_DBG, "deinit Card...\r\n");
lock();

if (!_is_initialized) {
_init_ref_count = 0;
unlock();
return BD_ERROR_OK;
}

_init_ref_count--;

if (_init_ref_count) {
unlock();
return BD_ERROR_OK;
}

int status = sdio_deinit();
_is_initialized = false;

_sectors = 0;

unlock();
return status;
}

int SDIOBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
{
int status = 0;
lock();
if (isPresent() == false) {
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
}

if (!_is_initialized) {
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
}

if (!is_valid_read(addr, size)) {
unlock();
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
}

uint32_t *_buffer = static_cast<uint32_t *>(buffer);

// ReadBlocks uses byte unit address
// SDHC and SDXC Cards different addressing is handled in ReadBlocks()
bd_addr_t blockCnt = size / _block_size;
addr = addr / _block_size;

#if DEVICE_SDIO_ASYNC
// make sure card is ready
{
uint32_t tickstart = us_ticker_read();
while (sdio_get_card_state() != SD_TRANSFER_OK) {
// wait until SD ready
if ((us_ticker_read() - tickstart) >= MBED_CONF_SDIO_CMD_TIMEOUT) {
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
}
}

// receive the data : one block/ multiple blocks is handled in ReadBlocks()
status = sdio_readblocks_async(_buffer, addr, blockCnt);
debug_if(SD_DBG, "ReadBlocks dbgtest addr: %lld blockCnt: %lld \n", addr, blockCnt);

if (status == MSD_OK) {
// wait until DMA finished
uint32_t tickstart = us_ticker_read();
while (sdio_read_pending() != SD_TRANSFER_OK) {
if ((us_ticker_read() - tickstart) >= MBED_CONF_SDIO_CMD_TIMEOUT) {
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
}
// make sure card is ready
tickstart = us_ticker_read();
while (sdio_get_card_state() != SD_TRANSFER_OK) {
// wait until SD ready
if ((us_ticker_read() - tickstart) >= MBED_CONF_SDIO_CMD_TIMEOUT) {
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
}
} else {
debug_if(SD_DBG, "ReadBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
#else
status = sdio_readblocks(_buffer, addr, blockCnt);
debug_if(SD_DBG, "ReadBlocks dbgtest addr: %lld blockCnt: %lld \n", addr, blockCnt);

if (status != MSD_OK) {
debug_if(SD_DBG, "ReadBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
status = SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
#endif

unlock();
return status;
}

int SDIOBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
{
int status = 0;
lock();

if (isPresent() == false) {
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
}

if (!_is_initialized) {
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
}

if (!is_valid_program(addr, size)) {
unlock();
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
}

// HAL layer uses uint32_t for addr/size
uint32_t *_buffer = (uint32_t *)(buffer);

// Get block count
bd_size_t blockCnt = size / _block_size;
addr = addr / _block_size;

#if DEVICE_SDIO_ASYNC
// make sure card is ready
{
uint32_t tickstart = us_ticker_read();
while (sdio_get_card_state() != SD_TRANSFER_OK) {
// wait until SD ready
if ((us_ticker_read() - tickstart) >= MBED_CONF_SDIO_CMD_TIMEOUT) {
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
}

status = sdio_writeblocks_async(_buffer, addr, blockCnt);
debug_if(SD_DBG, "WriteBlocks dbgtest addr: %lld blockCnt: %lld \n", addr, blockCnt);

if (status == MSD_OK) {
// wait until DMA finished
uint32_t tickstart = us_ticker_read();
while (sdio_write_pending() != SD_TRANSFER_OK) {
if ((us_ticker_read() - tickstart) >= MBED_CONF_SDIO_CMD_TIMEOUT) {
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
// make sure card is ready
tickstart = us_ticker_read();
while (sdio_get_card_state() != SD_TRANSFER_OK) {
// wait until SD ready
if ((us_ticker_read() - tickstart) >= MBED_CONF_SDIO_CMD_TIMEOUT) {
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
} else {
debug_if(SD_DBG, "WriteBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
#else
status = sdio_writeblocks(_buffer, addr, blockCnt);

debug_if(SD_DBG, "WriteBlocks dbgtest addr: %lld blockCnt: %lld \n", addr, blockCnt);

if (status != MSD_OK) {
debug_if(SD_DBG, "WriteBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
status = SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
#endif

unlock();
return status;
}

int SDIOBlockDevice::trim(bd_addr_t addr, bd_size_t size)
{
debug_if(SD_DBG, "trim Card...\r\n");
lock();
if (isPresent() == false) {
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
}

if (!_is_initialized) {
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
}

if (!_is_valid_trim(addr, size)) {
unlock();
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
}

bd_size_t blockCnt = size / _block_size;
addr = addr / _block_size;

int status = sdio_erase(addr, blockCnt);
if (status != 0) {
debug_if(SD_DBG, "Erase blocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
unlock();
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
} else {
uint32_t tickstart = us_ticker_read();
while (sdio_get_card_state() != SD_TRANSFER_OK) {
// wait until SD ready
if ((us_ticker_read() - tickstart) >= MBED_CONF_SDIO_CMD_TIMEOUT) {
unlock();
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
}
}
}

unlock();
return status;
}

bd_size_t SDIOBlockDevice::get_read_size() const
{
return _block_size;
}

bd_size_t SDIOBlockDevice::get_program_size() const
{
return _block_size;
}

bd_size_t SDIOBlockDevice::size() const
{
return _block_size * _sectors;
}

void SDIOBlockDevice::debug(bool dbg)
{
}

bool SDIOBlockDevice::_is_valid_trim(bd_addr_t addr, bd_size_t size)
{
return (
addr % _erase_size == 0 &&
size % _erase_size == 0 &&
addr + size <= this->size());
}

bool SDIOBlockDevice::isPresent(void)
{
if (_cardDetect.is_connected()) {
return (_cardDetect.read() == 0);
} else {
return true;
}
}

const char *SDIOBlockDevice::get_type() const
{
return "SDIO";
}

#endif //DEVICE_SDIO
Loading