fatfs: update diskio and vfs for FatFS changes

- do to not rely on integer.h types
- ffsystem.c does not define ff_memcalloc, replace with ff_memalloc +
  memset.
This commit is contained in:
Ivan Grokhotkov 2019-06-20 00:56:23 +08:00
parent 7724df407a
commit 66bdeca603
5 changed files with 28 additions and 32 deletions

View file

@ -18,6 +18,11 @@
extern "C" { extern "C" {
#endif #endif
#include <stdint.h>
typedef unsigned int UINT;
typedef unsigned char BYTE;
typedef uint32_t DWORD;
#include "diskio.h" #include "diskio.h"
#include "esp_err.h" #include "esp_err.h"
@ -27,11 +32,11 @@ extern "C" {
* See FatFs documentation for details about these functions * See FatFs documentation for details about these functions
*/ */
typedef struct { typedef struct {
DSTATUS (*init) (BYTE pdrv); /*!< disk initialization function */ DSTATUS (*init) (unsigned char pdrv); /*!< disk initialization function */
DSTATUS (*status) (BYTE pdrv); /*!< disk status check function */ DSTATUS (*status) (unsigned char pdrv); /*!< disk status check function */
DRESULT (*read) (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); /*!< sector read function */ DRESULT (*read) (unsigned char pdrv, unsigned char* buff, uint32_t sector, unsigned count); /*!< sector read function */
DRESULT (*write) (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); /*!< sector write function */ DRESULT (*write) (unsigned char pdrv, const unsigned char* buff, uint32_t sector, unsigned count); /*!< sector write function */
DRESULT (*ioctl) (BYTE pdrv, BYTE cmd, void* buff); /*!< function to get info about disk and do some misc operations */ DRESULT (*ioctl) (unsigned char pdrv, unsigned char cmd, void* buff); /*!< function to get info about disk and do some misc operations */
} ff_diskio_impl_t; } ff_diskio_impl_t;
/** /**

View file

@ -19,7 +19,6 @@
extern "C" { extern "C" {
#endif #endif
#include "integer.h"
#include "esp_partition.h" #include "esp_partition.h"
/** /**
@ -28,8 +27,8 @@ extern "C" {
* @param pdrv drive number * @param pdrv drive number
* @param part_handle pointer to raw flash partition. * @param part_handle pointer to raw flash partition.
*/ */
esp_err_t ff_diskio_register_raw_partition(BYTE pdrv, const esp_partition_t* part_handle); esp_err_t ff_diskio_register_raw_partition(unsigned char pdrv, const esp_partition_t* part_handle);
BYTE ff_diskio_get_pdrv_raw(const esp_partition_t* part_handle); unsigned char ff_diskio_get_pdrv_raw(const esp_partition_t* part_handle);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -19,7 +19,6 @@
extern "C" { extern "C" {
#endif #endif
#include "integer.h"
#include "wear_levelling.h" #include "wear_levelling.h"
@ -29,8 +28,8 @@ extern "C" {
* @param pdrv drive number * @param pdrv drive number
* @param flash_handle handle of the wear levelling partition. * @param flash_handle handle of the wear levelling partition.
*/ */
esp_err_t ff_diskio_register_wl_partition(BYTE pdrv, wl_handle_t flash_handle); esp_err_t ff_diskio_register_wl_partition(unsigned char pdrv, wl_handle_t flash_handle);
BYTE ff_diskio_get_pdrv_wl(wl_handle_t flash_handle); unsigned char ff_diskio_get_pdrv_wl(wl_handle_t flash_handle);
void ff_diskio_clear_pdrv_wl(wl_handle_t flash_handle); void ff_diskio_clear_pdrv_wl(wl_handle_t flash_handle);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "ff.h" #include "ff.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#ifdef CONFIG_FATFS_ALLOC_EXTRAM_FIRST #ifdef CONFIG_FATFS_ALLOC_EXTRAM_FIRST
@ -18,7 +19,7 @@
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
void* ff_memalloc ( /* Returns pointer to the allocated memory block (null on not enough core) */ void* ff_memalloc ( /* Returns pointer to the allocated memory block (null on not enough core) */
UINT msize /* Number of bytes to allocate */ unsigned msize /* Number of bytes to allocate */
) )
{ {
#ifdef CONFIG_FATFS_ALLOC_EXTRAM_FIRST #ifdef CONFIG_FATFS_ALLOC_EXTRAM_FIRST
@ -29,21 +30,6 @@ void* ff_memalloc ( /* Returns pointer to the allocated memory block (null on no
#endif #endif
} }
/*------------------------------------------------------------------------*/
/* Allocate and zero out memory block */
/*------------------------------------------------------------------------*/
void* ff_memcalloc (UINT num, UINT size)
{
#ifdef CONFIG_FATFS_ALLOC_EXTRAM_FIRST
return heap_caps_calloc_prefer(num, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM,
MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL);
#else
return calloc(num, size);
#endif
}
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Free a memory block */ /* Free a memory block */

View file

@ -150,15 +150,17 @@ esp_err_t esp_vfs_fat_register(const char* base_path, const char* fat_drive, siz
.utime_p = &vfs_fat_utime, .utime_p = &vfs_fat_utime,
}; };
size_t ctx_size = sizeof(vfs_fat_ctx_t) + max_files * sizeof(FIL); size_t ctx_size = sizeof(vfs_fat_ctx_t) + max_files * sizeof(FIL);
vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ff_memcalloc(1, ctx_size); vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ff_memalloc(ctx_size);
if (fat_ctx == NULL) { if (fat_ctx == NULL) {
return ESP_ERR_NO_MEM; return ESP_ERR_NO_MEM;
} }
memset(fat_ctx, 0, ctx_size);
fat_ctx->o_append = ff_memalloc(max_files * sizeof(bool)); fat_ctx->o_append = ff_memalloc(max_files * sizeof(bool));
if (fat_ctx->o_append == NULL) { if (fat_ctx->o_append == NULL) {
free(fat_ctx); free(fat_ctx);
return ESP_ERR_NO_MEM; return ESP_ERR_NO_MEM;
} }
memset(fat_ctx->o_append, 0, max_files * sizeof(bool));
fat_ctx->max_files = max_files; fat_ctx->max_files = max_files;
strlcpy(fat_ctx->fat_drive, fat_drive, sizeof(fat_ctx->fat_drive) - 1); strlcpy(fat_ctx->fat_drive, fat_drive, sizeof(fat_ctx->fat_drive) - 1);
strlcpy(fat_ctx->base_path, base_path, sizeof(fat_ctx->base_path) - 1); strlcpy(fat_ctx->base_path, base_path, sizeof(fat_ctx->base_path) - 1);
@ -512,8 +514,8 @@ static int vfs_fat_link(void* ctx, const char* n1, const char* n2)
prepend_drive_to_path(fat_ctx, &n1, &n2); prepend_drive_to_path(fat_ctx, &n1, &n2);
const size_t copy_buf_size = fat_ctx->fs.csize; const size_t copy_buf_size = fat_ctx->fs.csize;
FRESULT res; FRESULT res;
FIL* pf1 = ff_memcalloc(1, sizeof(FIL)); FIL* pf1 = (FIL*) ff_memalloc(sizeof(FIL));
FIL* pf2 = ff_memcalloc(1, sizeof(FIL)); FIL* pf2 = (FIL*) ff_memalloc(sizeof(FIL));
void* buf = ff_memalloc(copy_buf_size); void* buf = ff_memalloc(copy_buf_size);
if (buf == NULL || pf1 == NULL || pf2 == NULL) { if (buf == NULL || pf1 == NULL || pf2 == NULL) {
_lock_release(&fat_ctx->lock); _lock_release(&fat_ctx->lock);
@ -524,6 +526,8 @@ static int vfs_fat_link(void* ctx, const char* n1, const char* n2)
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
memset(pf1, 0, sizeof(*pf1));
memset(pf2, 0, sizeof(*pf2));
res = f_open(pf1, n1, FA_READ | FA_OPEN_EXISTING); res = f_open(pf1, n1, FA_READ | FA_OPEN_EXISTING);
if (res != FR_OK) { if (res != FR_OK) {
_lock_release(&fat_ctx->lock); _lock_release(&fat_ctx->lock);
@ -591,12 +595,14 @@ static DIR* vfs_fat_opendir(void* ctx, const char* name)
vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ctx; vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ctx;
_lock_acquire(&fat_ctx->lock); _lock_acquire(&fat_ctx->lock);
prepend_drive_to_path(fat_ctx, &name, NULL); prepend_drive_to_path(fat_ctx, &name, NULL);
vfs_fat_dir_t* fat_dir = ff_memcalloc(1, sizeof(vfs_fat_dir_t)); vfs_fat_dir_t* fat_dir = ff_memalloc(sizeof(vfs_fat_dir_t));
if (!fat_dir) { if (!fat_dir) {
_lock_release(&fat_ctx->lock); _lock_release(&fat_ctx->lock);
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;
} }
memset(fat_dir, 0, sizeof(*fat_dir));
FRESULT res = f_opendir(&fat_dir->ffdir, name); FRESULT res = f_opendir(&fat_dir->ffdir, name);
_lock_release(&fat_ctx->lock); _lock_release(&fat_ctx->lock);
if (res != FR_OK) { if (res != FR_OK) {
@ -766,7 +772,7 @@ static int vfs_fat_truncate(void* ctx, const char *path, off_t length)
_lock_acquire(&fat_ctx->lock); _lock_acquire(&fat_ctx->lock);
prepend_drive_to_path(fat_ctx, &path, NULL); prepend_drive_to_path(fat_ctx, &path, NULL);
file = (FIL*) ff_memcalloc(1, sizeof(FIL)); file = (FIL*) ff_memalloc(sizeof(FIL));
if (file == NULL) { if (file == NULL) {
_lock_release(&fat_ctx->lock); _lock_release(&fat_ctx->lock);
ESP_LOGD(TAG, "truncate alloc failed"); ESP_LOGD(TAG, "truncate alloc failed");
@ -774,6 +780,7 @@ static int vfs_fat_truncate(void* ctx, const char *path, off_t length)
ret = -1; ret = -1;
goto out; goto out;
} }
memset(file, 0, sizeof(*file));
res = f_open(file, path, FA_WRITE); res = f_open(file, path, FA_WRITE);