132 lines
3.9 KiB
C
132 lines
3.9 KiB
C
/*-----------------------------------------------------------------------*/
|
|
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
|
|
/*-----------------------------------------------------------------------*/
|
|
/* If a working storage control module is available, it should be */
|
|
/* attached to the FatFs via a glue function rather than modifying it. */
|
|
/* This is an example of glue functions to attach various exsisting */
|
|
/* storage control modules to the FatFs module with a defined API. */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
#include <string.h>
|
|
#include "diskio.h" /* FatFs lower layer API */
|
|
#include "ffconf.h"
|
|
#include "ff.h"
|
|
#include "sdmmc_cmd.h"
|
|
#include "esp_log.h"
|
|
#include <time.h>
|
|
#include <sys/time.h>
|
|
|
|
static const char* TAG = "ff_diskio";
|
|
static ff_diskio_impl_t s_impls[_VOLUMES] = { { 0 } };
|
|
static sdmmc_card_t* s_cards[_VOLUMES] = { NULL };
|
|
|
|
PARTITION VolToPart[] = {
|
|
{0, 1}, /* Logical drive 0 ==> Physical drive 0, 1st partition */
|
|
{1, 0} /* Logical drive 1 ==> Physical drive 1, auto detection */
|
|
};
|
|
|
|
void ff_diskio_register(BYTE pdrv, const ff_diskio_impl_t* discio_impl)
|
|
{
|
|
assert(pdrv < _VOLUMES);
|
|
memcpy(&s_impls[pdrv], discio_impl, sizeof(ff_diskio_impl_t));
|
|
}
|
|
|
|
DSTATUS ff_disk_initialize (BYTE pdrv)
|
|
{
|
|
return s_impls[pdrv].init(pdrv);
|
|
}
|
|
DSTATUS ff_disk_status (BYTE pdrv)
|
|
{
|
|
return s_impls[pdrv].status(pdrv);
|
|
}
|
|
DRESULT ff_disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count)
|
|
{
|
|
return s_impls[pdrv].read(pdrv, buff, sector, count);
|
|
}
|
|
DRESULT ff_disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
|
|
{
|
|
return s_impls[pdrv].write(pdrv, buff, sector, count);
|
|
}
|
|
DRESULT ff_disk_ioctl (BYTE pdrv, BYTE cmd, void* buff)
|
|
{
|
|
return s_impls[pdrv].ioctl(pdrv, cmd, buff);
|
|
}
|
|
|
|
DWORD get_fattime(void)
|
|
{
|
|
time_t t = time(NULL);
|
|
struct tm *tmr = gmtime(&t);
|
|
return ((DWORD)(tmr->tm_year - 80) << 25)
|
|
| ((DWORD)(tmr->tm_mon + 1) << 21)
|
|
| ((DWORD)tmr->tm_mday << 16)
|
|
| (WORD)(tmr->tm_hour << 11)
|
|
| (WORD)(tmr->tm_min << 5)
|
|
| (WORD)(tmr->tm_sec >> 1);
|
|
}
|
|
|
|
DSTATUS ff_sdmmc_initialize (BYTE pdrv)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
DSTATUS ff_sdmmc_status (BYTE pdrv)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
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_blocks(card, buff, sector, count);
|
|
if (err != ESP_OK) {
|
|
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_blocks(card, buff, sector, count);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "sdmmc_write_blocks failed (%d)", err);
|
|
return RES_ERROR;
|
|
}
|
|
return RES_OK;
|
|
}
|
|
|
|
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:
|
|
*((uint32_t*) buff) = card->csd.capacity;
|
|
return RES_OK;
|
|
case GET_SECTOR_SIZE:
|
|
*((uint32_t*) buff) = card->csd.sector_size;
|
|
return RES_OK;
|
|
case GET_BLOCK_SIZE:
|
|
return RES_ERROR;
|
|
}
|
|
return RES_ERROR;
|
|
}
|
|
|
|
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;
|
|
ff_diskio_register(pdrv, &sdmmc_impl);
|
|
}
|
|
|