fatfs: clean up diskio code

- clean up diskio_spiflash, fix the max number of volumes supported
- move SDMMC code into a separate file
This commit is contained in:
Ivan Grokhotkov 2017-05-04 19:50:13 +08:00
parent 4e3ea66ec4
commit 5ac28e843d
3 changed files with 102 additions and 102 deletions

View File

@ -9,18 +9,13 @@
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
#include <string.h> #include <string.h>
#include <time.h>
#include <sys/time.h>
#include "diskio.h" /* FatFs lower layer API */ #include "diskio.h" /* FatFs lower layer API */
#include "ffconf.h" #include "ffconf.h"
#include "ff.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] = { NULL };
static ff_diskio_impl_t * s_impls[_VOLUMES];
static sdmmc_card_t* s_cards[_VOLUMES] = { NULL };
static bool s_impls_initialized = false;
#if _MULTI_PARTITION /* Multiple partition configuration */ #if _MULTI_PARTITION /* Multiple partition configuration */
PARTITION VolToPart[] = { PARTITION VolToPart[] = {
@ -45,11 +40,6 @@ void ff_diskio_register(BYTE pdrv, const ff_diskio_impl_t* discio_impl)
{ {
assert(pdrv < _VOLUMES); assert(pdrv < _VOLUMES);
if (!s_impls_initialized) {
s_impls_initialized = true;
memset(s_impls, 0, _VOLUMES * sizeof(ff_diskio_impl_t*));
}
if (s_impls[pdrv]) { if (s_impls[pdrv]) {
ff_diskio_impl_t* im = s_impls[pdrv]; ff_diskio_impl_t* im = s_impls[pdrv];
s_impls[pdrv] = NULL; s_impls[pdrv] = NULL;
@ -99,70 +89,3 @@ DWORD get_fattime(void)
| (WORD)(tmr->tm_min << 5) | (WORD)(tmr->tm_min << 5)
| (WORD)(tmr->tm_sec >> 1); | (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_sectors(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_sectors(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);
}

View File

@ -0,0 +1,90 @@
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
//
// 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 "diskio.h"
#include "ffconf.h"
#include "ff.h"
#include "sdmmc_cmd.h"
#include "esp_log.h"
static sdmmc_card_t* s_cards[_VOLUMES] = { NULL };
static const char* TAG = "diskio_sdmmc";
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_sectors(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_sectors(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);
}

View File

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
@ -13,31 +13,18 @@
// limitations under the License. // limitations under the License.
#include <string.h> #include <string.h>
#include "diskio.h" /* FatFs lower layer API */ #include "diskio.h"
#include "ffconf.h" #include "ffconf.h"
#include "ff.h" #include "ff.h"
#include "sdmmc_cmd.h"
#include "esp_log.h" #include "esp_log.h"
#include <time.h>
#include <sys/time.h>
#include "diskio_spiflash.h" #include "diskio_spiflash.h"
#include "wear_levelling.h" #include "wear_levelling.h"
static const char* TAG = "ff_diskio_spiflash"; static const char* TAG = "ff_diskio_spiflash";
#ifndef MAX_FF_WL_DRIVES
#define MAX_FF_WL_DRIVES 8
#endif // MAX_FF_WL_DRIVES
wl_handle_t ff_wl_handles[MAX_FF_WL_DRIVES] = { wl_handle_t ff_wl_handles[_VOLUMES] = {
WL_INVALID_HANDLE, WL_INVALID_HANDLE,
WL_INVALID_HANDLE, WL_INVALID_HANDLE,
WL_INVALID_HANDLE,
WL_INVALID_HANDLE,
WL_INVALID_HANDLE,
WL_INVALID_HANDLE,
WL_INVALID_HANDLE,
WL_INVALID_HANDLE
}; };
DSTATUS ff_wl_initialize (BYTE pdrv) DSTATUS ff_wl_initialize (BYTE pdrv)
@ -104,7 +91,9 @@ DRESULT ff_wl_ioctl (BYTE pdrv, BYTE cmd, void *buff)
esp_err_t ff_diskio_register_wl_partition(BYTE pdrv, wl_handle_t flash_handle) esp_err_t ff_diskio_register_wl_partition(BYTE pdrv, wl_handle_t flash_handle)
{ {
if (pdrv >= MAX_FF_WL_DRIVES) return ESP_FAIL; if (pdrv >= _VOLUMES) {
return ESP_ERR_INVALID_ARG;
}
static const ff_diskio_impl_t wl_impl = { static const ff_diskio_impl_t wl_impl = {
.init = &ff_wl_initialize, .init = &ff_wl_initialize,
.status = &ff_wl_status, .status = &ff_wl_status,
@ -119,12 +108,10 @@ esp_err_t ff_diskio_register_wl_partition(BYTE pdrv, wl_handle_t flash_handle)
BYTE ff_diskio_get_pdrv_wl(wl_handle_t flash_handle) BYTE ff_diskio_get_pdrv_wl(wl_handle_t flash_handle)
{ {
for (int i=0 ; i< MAX_FF_WL_DRIVES ; i++) for (int i = 0; i < _VOLUMES; i++) {
{ if (flash_handle == ff_wl_handles[i]) {
if (flash_handle == ff_wl_handles[i])
{
return i; return i;
} }
} }
return -1; return 0xff;
} }