Skip to content
Snippets Groups Projects
Commit bb431657 authored by swym's avatar swym
Browse files

cleanup(fatfs): undo formatting in libff's diskio.c, move private

implementation into epicardium/fs/
parent d350db5f
No related branches found
No related tags found
No related merge requests found
...@@ -6,20 +6,7 @@ ...@@ -6,20 +6,7 @@
* *
*/ */
#include "modules/filesystem.h" #include "fs/internal.h"
#include "epicardium.h"
extern int
efs_open(EpicFileSystem *fs, const char *filename, const char *modeString);
extern int efs_close(EpicFileSystem *fs, int fd);
extern int efs_read(EpicFileSystem *fs, int fd, void *buf, size_t nbytes);
extern int
efs_write(EpicFileSystem *fs, int fd, const void *buf, size_t nbytes);
extern int efs_flush(EpicFileSystem *fs, int fd);
extern int efs_seek(EpicFileSystem *fs, int fd, long offset, int whence);
extern int efs_tell(EpicFileSystem *fs, int fd);
extern int
efs_stat(EpicFileSystem *fs, const char *filename, struct epic_stat *stat);
int epic_file_open(const char *filename, const char *mode) int epic_file_open(const char *filename, const char *mode)
{ {
......
#ifndef EPICARCIUM_FS_INTERNAL_H_INCLUDED
#define EPICARCIUM_FS_INTERNAL_H_INCLUDED
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "epicardium.h"
/* Number of bits to use for indexing into our internal pool of files/directories
* This indirectly specifies the size of the pool as 1^EPIC_FAT_FD_INDEX_BITS
* Increase if number of open file descriptors is not enough, but be aware of
* memory usage of the pool!
*/
#define EPIC_FAT_FD_INDEX_BITS 4
#define EPIC_FAT_STATIC_SEMAPHORE 1
// forward declaration, actual definition is in filesystem_fat.c
typedef struct EpicFileSystem EpicFileSystem;
int efs_open(EpicFileSystem *fs, const char *filename, const char *modeString);
int efs_close(EpicFileSystem *fs, int fd);
int efs_read(EpicFileSystem *fs, int fd, void *buf, size_t nbytes);
int efs_write(EpicFileSystem *fs, int fd, const void *buf, size_t nbytes);
int efs_flush(EpicFileSystem *fs, int fd);
int efs_seek(EpicFileSystem *fs, int fd, long offset, int whence);
int efs_tell(EpicFileSystem *fs, int fd);
int efs_stat(EpicFileSystem *fs, const char *filename, struct epic_stat *stat);
/**
* lock global filesystem
*
* locks the global mutex and, if the global EpicFileSystem has been initialized correctly
* passes it to *fs
*
* Upon successful return, the filesystem has to be re-locked with epic_fs_unlock_global
* In case of error, the filesystem will be left in a locked state.
*/
bool efs_lock_global(EpicFileSystem** fs, int* ec);
void efs_unlock_global(EpicFileSystem* fs);
#endif// EPICARCIUM_FS_INTERNAL_H_INCLUDED
...@@ -74,6 +74,7 @@ elf = executable( ...@@ -74,6 +74,7 @@ elf = executable(
'cdcacm.c', 'cdcacm.c',
'main.c', 'main.c',
'support.c', 'support.c',
'fs/fileops.c',
module_sources, module_sources,
l0der_sources, l0der_sources,
ble_sources, ble_sources,
......
...@@ -6,18 +6,6 @@ ...@@ -6,18 +6,6 @@
#include <stdbool.h> #include <stdbool.h>
#include "epicardium.h" #include "epicardium.h"
/* Number of bits to use for indexing into our internal pool of files/directories
* This indirectly specifies the size of the pool as 1^EPIC_FAT_FD_INDEX_BITS
* Increase if number of open file descriptors is not enough, but be aware of
* memory usage of the pool!
*/
#define EPIC_FAT_FD_INDEX_BITS 4
#define EPIC_FAT_STATIC_SEMAPHORE 1
#define EPIC_FAT_MAX_OPENED (1 << (EPIC_FAT_FD_INDEX_BITS))
typedef struct EpicFileSystem EpicFileSystem;
/** /**
* module initialization - to be called once at startup before any FreeRTOS tasks * module initialization - to be called once at startup before any FreeRTOS tasks
* have been started * have been started
...@@ -34,13 +22,4 @@ int fatfs_attach(void); ...@@ -34,13 +22,4 @@ int fatfs_attach(void);
/** close all opened FDs, sync and deinitialize FLASH layer */ /** close all opened FDs, sync and deinitialize FLASH layer */
void fatfs_detach(void); void fatfs_detach(void);
/**
* lock global filesystem
*
* Upon successful return, the filesystem has to be re-locked with epic_fs_unlock_global
* In case of error, the filesystem will be left in a locked state.
*/
bool efs_lock_global(EpicFileSystem** fs, int* ec);
void efs_unlock_global(EpicFileSystem* fs);
#endif//EPICARDIUM_MODULE_FILESYSTEM_INCLUDED #endif//EPICARDIUM_MODULE_FILESYSTEM_INCLUDED
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <semphr.h> #include <semphr.h>
#include "fs/internal.h"
#include "modules/filesystem.h" #include "modules/filesystem.h"
#include "epicardium.h" #include "epicardium.h"
#include "card10.h" #include "card10.h"
...@@ -30,6 +31,7 @@ ...@@ -30,6 +31,7 @@
#endif #endif
/* clang-format off */ /* clang-format off */
#define EPIC_FAT_MAX_OPENED (1 << (EPIC_FAT_FD_INDEX_BITS))
#define EPIC_FAT_FD_GENERATION_BITS (31 - (EPIC_FAT_FD_INDEX_BITS)) #define EPIC_FAT_FD_GENERATION_BITS (31 - (EPIC_FAT_FD_INDEX_BITS))
#define EPIC_FAT_FD_INDEX_MASK (uint32_t)((1u << EPIC_FAT_FD_INDEX_BITS) - 1) #define EPIC_FAT_FD_INDEX_MASK (uint32_t)((1u << EPIC_FAT_FD_INDEX_BITS) - 1)
#define EPIC_FAT_FD_INDEX(fd) ((uint32_t)(fd)&EPIC_FAT_FD_INDEX_MASK) #define EPIC_FAT_FD_INDEX(fd) ((uint32_t)(fd)&EPIC_FAT_FD_INDEX_MASK)
...@@ -79,16 +81,6 @@ static StaticSemaphore_t s_globalLockBuffer; ...@@ -79,16 +81,6 @@ static StaticSemaphore_t s_globalLockBuffer;
static SemaphoreHandle_t s_globalLock = NULL; static SemaphoreHandle_t s_globalLock = NULL;
bool globalLockAccquire()
{
return (int)(xSemaphoreTake(s_globalLock, FF_FS_TIMEOUT) == pdTRUE);
}
void globalLockRelease()
{
xSemaphoreGive(s_globalLock);
}
void fatfs_init() void fatfs_init()
{ {
static volatile bool s_initCalled = false; static volatile bool s_initCalled = false;
...@@ -168,7 +160,7 @@ void fatfs_detach() ...@@ -168,7 +160,7 @@ void fatfs_detach()
} }
} }
const char *f_get_rc_string(FRESULT rc) static const char *f_get_rc_string(FRESULT rc)
{ {
static const TCHAR *rcstrings = static const TCHAR *rcstrings =
_T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0") _T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
...@@ -186,6 +178,16 @@ const char *f_get_rc_string(FRESULT rc) ...@@ -186,6 +178,16 @@ const char *f_get_rc_string(FRESULT rc)
return p; return p;
} }
static bool globalLockAccquire()
{
return (int)(xSemaphoreTake(s_globalLock, FF_FS_TIMEOUT) == pdTRUE);
}
static void globalLockRelease()
{
xSemaphoreGive(s_globalLock);
}
bool efs_lock_global(EpicFileSystem **fs, int *rc) bool efs_lock_global(EpicFileSystem **fs, int *rc)
{ {
*fs = NULL; *fs = NULL;
...@@ -209,7 +211,7 @@ void efs_unlock_global(EpicFileSystem *fs) ...@@ -209,7 +211,7 @@ void efs_unlock_global(EpicFileSystem *fs)
globalLockRelease(); globalLockRelease();
} }
bool efs_get_opened( static bool efs_get_opened(
EpicFileSystem *fs, EpicFileSystem *fs,
int fd, int fd,
enum epic_stat_type expected, enum epic_stat_type expected,
......
module_sources = files( module_sources = files(
'display.c', 'display.c',
'filesystem_fat.c', 'filesystem_fat.c',
'filesystem_fileops.c',
'leds.c', 'leds.c',
'light_sensor.c', 'light_sensor.c',
'log.c', 'log.c',
......
#ifndef MODULES_H #ifndef MODULES_H
#define MODULES_H #define MODULES_H
#include <stdint.h>
/* ---------- Serial ------------------------------------------------------- */ /* ---------- Serial ------------------------------------------------------- */
#define SERIAL_READ_BUFFER_SIZE 128 #define SERIAL_READ_BUFFER_SIZE 128
void vSerialTask(void *pvParameters); void vSerialTask(void *pvParameters);
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
/* storage control modules to the FatFs module with a defined API. */ /* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
#include "diskio.h" /* FatFs lower layer API */
#include <stdbool.h> #include <stdbool.h>
#include "diskio.h" /* FatFs lower layer API */
/* Definitions of physical drive number for each drive */ /* Definitions of physical drive number for each drive */
#define DEV_FLASH 0 /* Example: Map MMC/SD card to physical drive 1 */ #define DEV_FLASH 0 /* Example: Map MMC/SD card to physical drive 1 */
#define DEV_SD 1 /* Example: Map MMC/SD card to physical drive 1 */ #define DEV_SD 1 /* Example: Map MMC/SD card to physical drive 1 */
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
static uint8_t rtc_en; static uint8_t rtc_en;
static bool s_diskio_initialized = false; static bool s_diskio_initialized = false;
#if SDHC //TODO: implement deinitialization with SDHC as well #if SDHC
/* # of times to check for a card, should be > 1 to detect both SD and MMC */ /* # of times to check for a card, should be > 1 to detect both SD and MMC */
#define INIT_CARD_RETRIES 10 #define INIT_CARD_RETRIES 10
...@@ -49,8 +49,15 @@ extern int mx25_ready(void); ...@@ -49,8 +49,15 @@ extern int mx25_ready(void);
/* Get Drive Status */ /* Get Drive Status */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DSTATUS disk_status(BYTE pdrv /* Physical drive nmuber to identify the drive */ DSTATUS disk_status (
) { BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
#if 0
#define STA_NOINIT 0x01 /* Drive not initialized */
#define STA_NODISK 0x02 /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */
#endif
DSTATUS status = 0; DSTATUS status = 0;
if (!s_diskio_initialized) { if (!s_diskio_initialized) {
...@@ -72,13 +79,16 @@ DSTATUS disk_status(BYTE pdrv /* Physical drive nmuber to identify the drive */ ...@@ -72,13 +79,16 @@ DSTATUS disk_status(BYTE pdrv /* Physical drive nmuber to identify the drive */
return status; return status;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Inidialize a Drive */ /* Inidialize a Drive */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DSTATUS DSTATUS disk_initialize (
disk_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive */ BYTE pdrv /* Physical drive nmuber to identify the drive */
) { )
{
DSTATUS status = STA_NOINIT; DSTATUS status = STA_NOINIT;
rtc_en = 0; rtc_en = 0;
...@@ -105,8 +115,7 @@ disk_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive */ ...@@ -105,8 +115,7 @@ disk_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive */
#if SDHC #if SDHC
if(pdrv == 1) { if(pdrv == 1) {
if (SDHC_Card_Inserted() && if (SDHC_Card_Inserted() && (SDHC_Lib_InitCard(INIT_CARD_RETRIES) == E_NO_ERROR)) {
(SDHC_Lib_InitCard(INIT_CARD_RETRIES) == E_NO_ERROR)) {
/* Card initialized and ready for work */ /* Card initialized and ready for work */
init_done = 1; init_done = 1;
status = 0; status = 0;
...@@ -137,7 +146,8 @@ DRESULT disk_read( ...@@ -137,7 +146,8 @@ DRESULT disk_read(
BYTE *buff, /* Data buffer to store read data */ BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Start sector in LBA */ DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */ UINT count /* Number of sectors to read */
) { )
{
DRESULT status = RES_ERROR; DRESULT status = RES_ERROR;
if (!s_diskio_initialized) { if (!s_diskio_initialized) {
...@@ -145,12 +155,8 @@ DRESULT disk_read( ...@@ -145,12 +155,8 @@ DRESULT disk_read(
} else if (pdrv == 0) { } else if (pdrv == 0) {
int sector_offset; int sector_offset;
status = RES_OK; status = RES_OK;
for (sector_offset = 0; sector_offset < count; for(sector_offset = 0; sector_offset < count; sector_offset++) {
sector_offset++) { if(mx25_read(sector + sector_offset, (uint8_t*)buff + SECTOR_SIZE * sector_offset) == 1) {
if (mx25_read(
sector + sector_offset,
(uint8_t *)buff +
SECTOR_SIZE * sector_offset) == 1) {
status = RES_ERROR; status = RES_ERROR;
break; break;
} }
...@@ -158,8 +164,7 @@ DRESULT disk_read( ...@@ -158,8 +164,7 @@ DRESULT disk_read(
} }
#if SDHC #if SDHC
if(pdrv == 1) { if(pdrv == 1) {
if (SDHC_Lib_Read(buff, sector, count, SDHC_LIB_SINGLE_DATA) == if (SDHC_Lib_Read(buff, sector, count, SDHC_LIB_SINGLE_DATA) == E_NO_ERROR) {
E_NO_ERROR) {
status = RES_OK; status = RES_OK;
} }
} }
...@@ -168,6 +173,8 @@ DRESULT disk_read( ...@@ -168,6 +173,8 @@ DRESULT disk_read(
return status; return status;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Write Sector(s) */ /* Write Sector(s) */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
...@@ -177,7 +184,8 @@ DRESULT disk_write( ...@@ -177,7 +184,8 @@ DRESULT disk_write(
const BYTE *buff, /* Data to be written */ const BYTE *buff, /* Data to be written */
DWORD sector, /* Start sector in LBA */ DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */ UINT count /* Number of sectors to write */
) { )
{
DRESULT status = RES_ERROR; DRESULT status = RES_ERROR;
if (!s_diskio_initialized) { if (!s_diskio_initialized) {
...@@ -185,12 +193,8 @@ DRESULT disk_write( ...@@ -185,12 +193,8 @@ DRESULT disk_write(
} else if (pdrv == 0) { } else if (pdrv == 0) {
int sector_offset; int sector_offset;
status = RES_OK; status = RES_OK;
for (sector_offset = 0; sector_offset < count; for(sector_offset = 0; sector_offset < count; sector_offset++) {
sector_offset++) { if(mx25_write(sector + sector_offset, (uint8_t*)buff + SECTOR_SIZE * sector_offset) == 1) {
if (mx25_write(
sector + sector_offset,
(uint8_t *)buff +
SECTOR_SIZE * sector_offset) == 1) {
status = RES_ERROR; status = RES_ERROR;
break; break;
} }
...@@ -199,9 +203,7 @@ DRESULT disk_write( ...@@ -199,9 +203,7 @@ DRESULT disk_write(
#if SDHC #if SDHC
if(pdrv == 1) { if(pdrv == 1) {
if (SDHC_Lib_Write( if (SDHC_Lib_Write(sector, (void *)buff, count, SDHC_LIB_SINGLE_DATA) != E_NO_ERROR) {
sector, (void *)buff, count, SDHC_LIB_SINGLE_DATA) !=
E_NO_ERROR) {
status = RES_ERROR; status = RES_ERROR;
} else { } else {
status = RES_OK; status = RES_OK;
...@@ -212,6 +214,8 @@ DRESULT disk_write( ...@@ -212,6 +214,8 @@ DRESULT disk_write(
return status; return status;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */ /* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
...@@ -220,7 +224,8 @@ DRESULT disk_ioctl( ...@@ -220,7 +224,8 @@ DRESULT disk_ioctl(
BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */ BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */ void *buff /* Buffer to send/receive control data */
) { )
{
DRESULT status = RES_PARERR; DRESULT status = RES_PARERR;
if (!s_diskio_initialized) { if (!s_diskio_initialized) {
...@@ -275,8 +280,7 @@ DRESULT disk_ioctl( ...@@ -275,8 +280,7 @@ DRESULT disk_ioctl(
return status; return status;
} }
DWORD get_fattime(void) DWORD get_fattime(void) {
{
if(rtc_en) { if(rtc_en) {
DWORD result; DWORD result;
uint32_t seconds; uint32_t seconds;
...@@ -312,7 +316,8 @@ DWORD get_fattime(void) ...@@ -312,7 +316,8 @@ DWORD get_fattime(void)
result |= minute<<5; result |= minute<<5;
result |= half_seconds; result |= half_seconds;
return result; return result;
} else { }
else {
return RES_NOTRDY; return RES_NOTRDY;
} }
} }
...@@ -331,8 +336,7 @@ static DRESULT get_sector_count(void *buff) ...@@ -331,8 +336,7 @@ static DRESULT get_sector_count(void *buff)
if (init_done) { if (init_done) {
if (SDHC_Lib_GetCSD(&csd) == E_NO_ERROR) { if (SDHC_Lib_GetCSD(&csd) == E_NO_ERROR) {
*((DWORD *)buff) = *((DWORD *)buff) = SDHC_Lib_GetCapacity(&csd) / FF_MIN_SS;
SDHC_Lib_GetCapacity(&csd) / FF_MIN_SS;
status = RES_OK; status = RES_OK;
} }
} else { } else {
......
...@@ -61,7 +61,6 @@ Q(encoding) ...@@ -61,7 +61,6 @@ Q(encoding)
Q(file) Q(file)
Q(FileIO) Q(FileIO)
Q(flush) Q(flush)
Q(usbstorage)
Q(mode) Q(mode)
Q(r) Q(r)
Q(read) Q(read)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment