2021-10-12 09:30:13 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-05-04 07:50:13 -04:00
|
|
|
|
2019-06-19 12:07:51 -04:00
|
|
|
#include "diskio_impl.h"
|
2017-05-04 07:50:13 -04:00
|
|
|
#include "ffconf.h"
|
|
|
|
#include "ff.h"
|
|
|
|
#include "sdmmc_cmd.h"
|
|
|
|
#include "esp_log.h"
|
2019-12-04 09:01:25 -05:00
|
|
|
#include "esp_compiler.h"
|
2017-05-04 07:50:13 -04:00
|
|
|
|
2017-11-13 23:57:55 -05:00
|
|
|
static sdmmc_card_t* s_cards[FF_VOLUMES] = { NULL };
|
2022-06-05 08:37:21 -04:00
|
|
|
static bool s_disk_status_check_en[FF_VOLUMES] = { };
|
2017-05-04 07:50:13 -04:00
|
|
|
|
|
|
|
static const char* TAG = "diskio_sdmmc";
|
|
|
|
|
2021-10-12 09:30:13 -04:00
|
|
|
//Check if SD/MMC card is present
|
|
|
|
static DSTATUS ff_sdmmc_card_available(BYTE pdrv)
|
2017-05-04 07:50:13 -04:00
|
|
|
{
|
2021-10-12 09:30:13 -04:00
|
|
|
sdmmc_card_t* card = s_cards[pdrv];
|
|
|
|
assert(card);
|
|
|
|
esp_err_t err = sdmmc_get_status(card);
|
|
|
|
if (unlikely(err != ESP_OK)) {
|
|
|
|
ESP_LOGE(TAG, "Check status failed (0x%x)", err);
|
|
|
|
return STA_NOINIT;
|
|
|
|
}
|
2017-05-04 07:50:13 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-12 09:30:13 -04:00
|
|
|
/**
|
|
|
|
* ff_sdmmc_status() and ff_sdmmc_initialize() return STA_NOINIT when sdmmc_get_status()
|
|
|
|
* fails. This error value is checked throughout the FATFS code.
|
|
|
|
* Both functions return 0 on success.
|
|
|
|
*/
|
|
|
|
DSTATUS ff_sdmmc_initialize (BYTE pdrv)
|
|
|
|
{
|
|
|
|
return ff_sdmmc_card_available(pdrv);
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:37:21 -04:00
|
|
|
DSTATUS ff_sdmmc_status(BYTE pdrv)
|
2017-05-04 07:50:13 -04:00
|
|
|
{
|
2022-06-05 08:37:21 -04:00
|
|
|
if (s_disk_status_check_en[pdrv]) {
|
|
|
|
return ff_sdmmc_card_available(pdrv);
|
|
|
|
}
|
|
|
|
return 0;
|
2017-05-04 07:50:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DRESULT ff_sdmmc_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count)
|
|
|
|
{
|
|
|
|
sdmmc_card_t* card = s_cards[pdrv];
|
|
|
|
assert(card);
|
|
|
|
esp_err_t err = sdmmc_read_sectors(card, buff, sector, count);
|
2019-10-15 17:01:05 -04:00
|
|
|
if (unlikely(err != ESP_OK)) {
|
2017-05-04 07:50:13 -04:00
|
|
|
ESP_LOGE(TAG, "sdmmc_read_blocks failed (%d)", err);
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DRESULT ff_sdmmc_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
|
|
|
|
{
|
|
|
|
sdmmc_card_t* card = s_cards[pdrv];
|
|
|
|
assert(card);
|
|
|
|
esp_err_t err = sdmmc_write_sectors(card, buff, sector, count);
|
2019-10-15 17:01:05 -04:00
|
|
|
if (unlikely(err != ESP_OK)) {
|
2017-05-04 07:50:13 -04:00
|
|
|
ESP_LOGE(TAG, "sdmmc_write_blocks failed (%d)", err);
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-05 06:05:37 -05:00
|
|
|
#if FF_USE_TRIM
|
|
|
|
DRESULT ff_sdmmc_trim (BYTE pdrv, DWORD start_sector, DWORD sector_count)
|
|
|
|
{
|
|
|
|
sdmmc_card_t* card = s_cards[pdrv];
|
|
|
|
assert(card);
|
|
|
|
sdmmc_erase_arg_t arg;
|
|
|
|
|
|
|
|
arg = sdmmc_can_discard(card) == ESP_OK ? SDMMC_DISCARD_ARG : SDMMC_ERASE_ARG;
|
|
|
|
esp_err_t err = sdmmc_erase_sectors(card, start_sector, sector_count, arg);
|
|
|
|
if (unlikely(err != ESP_OK)) {
|
|
|
|
ESP_LOGE(TAG, "sdmmc_erase_sectors failed (%d)", err);
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
#endif //FF_USE_TRIM
|
|
|
|
|
2017-05-04 07:50:13 -04:00
|
|
|
DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff)
|
|
|
|
{
|
|
|
|
sdmmc_card_t* card = s_cards[pdrv];
|
|
|
|
assert(card);
|
|
|
|
switch(cmd) {
|
|
|
|
case CTRL_SYNC:
|
|
|
|
return RES_OK;
|
|
|
|
case GET_SECTOR_COUNT:
|
2017-10-09 00:36:21 -04:00
|
|
|
*((DWORD*) buff) = card->csd.capacity;
|
2017-05-04 07:50:13 -04:00
|
|
|
return RES_OK;
|
|
|
|
case GET_SECTOR_SIZE:
|
2017-10-09 00:36:21 -04:00
|
|
|
*((WORD*) buff) = card->csd.sector_size;
|
2017-05-04 07:50:13 -04:00
|
|
|
return RES_OK;
|
|
|
|
case GET_BLOCK_SIZE:
|
|
|
|
return RES_ERROR;
|
2022-03-05 06:05:37 -05:00
|
|
|
#if FF_USE_TRIM
|
|
|
|
case CTRL_TRIM:
|
|
|
|
return ff_sdmmc_trim (pdrv, *((DWORD*)buff), //start_sector
|
|
|
|
(*((DWORD*)buff + 1) - *((DWORD*)buff) + 1)); //sector_count
|
|
|
|
#endif //FF_USE_TRIM
|
2017-05-04 07:50:13 -04:00
|
|
|
}
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:37:21 -04:00
|
|
|
void ff_sdmmc_set_disk_status_check(BYTE pdrv, bool enable)
|
|
|
|
{
|
|
|
|
s_disk_status_check_en[pdrv] = enable;
|
|
|
|
}
|
|
|
|
|
2017-05-04 07:50:13 -04:00
|
|
|
void ff_diskio_register_sdmmc(BYTE pdrv, sdmmc_card_t* card)
|
|
|
|
{
|
|
|
|
static const ff_diskio_impl_t sdmmc_impl = {
|
|
|
|
.init = &ff_sdmmc_initialize,
|
|
|
|
.status = &ff_sdmmc_status,
|
|
|
|
.read = &ff_sdmmc_read,
|
|
|
|
.write = &ff_sdmmc_write,
|
|
|
|
.ioctl = &ff_sdmmc_ioctl
|
|
|
|
};
|
|
|
|
s_cards[pdrv] = card;
|
2022-06-05 08:37:21 -04:00
|
|
|
s_disk_status_check_en[pdrv] = false;
|
2017-05-04 07:50:13 -04:00
|
|
|
ff_diskio_register(pdrv, &sdmmc_impl);
|
|
|
|
}
|
|
|
|
|
2018-09-03 11:55:22 -04:00
|
|
|
BYTE ff_diskio_get_pdrv_card(const sdmmc_card_t* card)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < FF_VOLUMES; i++) {
|
|
|
|
if (card == s_cards[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0xff;
|
|
|
|
}
|