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 @@
*
*/
#include "modules/filesystem.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);
#include "fs/internal.h"
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(
'cdcacm.c',
'main.c',
'support.c',
'fs/fileops.c',
module_sources,
l0der_sources,
ble_sources,
......
......@@ -6,18 +6,6 @@
#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
#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
* have been started
......@@ -34,13 +22,4 @@ int fatfs_attach(void);
/** close all opened FDs, sync and deinitialize FLASH layer */
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
......@@ -17,6 +17,7 @@
#include <FreeRTOS.h>
#include <semphr.h>
#include "fs/internal.h"
#include "modules/filesystem.h"
#include "epicardium.h"
#include "card10.h"
......@@ -30,6 +31,7 @@
#endif
/* 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_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)
......@@ -79,16 +81,6 @@ static StaticSemaphore_t s_globalLockBuffer;
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()
{
static volatile bool s_initCalled = false;
......@@ -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 =
_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)
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)
{
*fs = NULL;
......@@ -209,7 +211,7 @@ void efs_unlock_global(EpicFileSystem *fs)
globalLockRelease();
}
bool efs_get_opened(
static bool efs_get_opened(
EpicFileSystem *fs,
int fd,
enum epic_stat_type expected,
......
module_sources = files(
'display.c',
'filesystem_fat.c',
'filesystem_fileops.c',
'leds.c',
'light_sensor.c',
'log.c',
......
#ifndef MODULES_H
#define MODULES_H
#include <stdint.h>
/* ---------- Serial ------------------------------------------------------- */
#define SERIAL_READ_BUFFER_SIZE 128
void vSerialTask(void *pvParameters);
......
......@@ -7,9 +7,9 @@
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include "diskio.h" /* FatFs lower layer API */
#include <stdbool.h>
#include "diskio.h" /* FatFs lower layer API */
/* Definitions of physical drive number for each drive */
#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 */
......@@ -22,7 +22,7 @@
static uint8_t rtc_en;
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 */
#define INIT_CARD_RETRIES 10
......@@ -49,8 +49,15 @@ extern int mx25_ready(void);
/* 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;
if (!s_diskio_initialized) {
......@@ -72,13 +79,16 @@ DSTATUS disk_status(BYTE pdrv /* Physical drive nmuber to identify the drive */
return status;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS
disk_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive */
) {
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS status = STA_NOINIT;
rtc_en = 0;
......@@ -105,8 +115,7 @@ disk_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive */
#if SDHC
if(pdrv == 1) {
if (SDHC_Card_Inserted() &&
(SDHC_Lib_InitCard(INIT_CARD_RETRIES) == E_NO_ERROR)) {
if (SDHC_Card_Inserted() && (SDHC_Lib_InitCard(INIT_CARD_RETRIES) == E_NO_ERROR)) {
/* Card initialized and ready for work */
init_done = 1;
status = 0;
......@@ -137,7 +146,8 @@ DRESULT disk_read(
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
) {
)
{
DRESULT status = RES_ERROR;
if (!s_diskio_initialized) {
......@@ -145,12 +155,8 @@ DRESULT disk_read(
} else if (pdrv == 0) {
int sector_offset;
status = RES_OK;
for (sector_offset = 0; sector_offset < count;
sector_offset++) {
if (mx25_read(
sector + sector_offset,
(uint8_t *)buff +
SECTOR_SIZE * sector_offset) == 1) {
for(sector_offset = 0; sector_offset < count; sector_offset++) {
if(mx25_read(sector + sector_offset, (uint8_t*)buff + SECTOR_SIZE * sector_offset) == 1) {
status = RES_ERROR;
break;
}
......@@ -158,8 +164,7 @@ DRESULT disk_read(
}
#if SDHC
if(pdrv == 1) {
if (SDHC_Lib_Read(buff, sector, count, SDHC_LIB_SINGLE_DATA) ==
E_NO_ERROR) {
if (SDHC_Lib_Read(buff, sector, count, SDHC_LIB_SINGLE_DATA) == E_NO_ERROR) {
status = RES_OK;
}
}
......@@ -168,6 +173,8 @@ DRESULT disk_read(
return status;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
......@@ -177,7 +184,8 @@ DRESULT disk_write(
const BYTE *buff, /* Data to be written */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
) {
)
{
DRESULT status = RES_ERROR;
if (!s_diskio_initialized) {
......@@ -185,12 +193,8 @@ DRESULT disk_write(
} else if (pdrv == 0) {
int sector_offset;
status = RES_OK;
for (sector_offset = 0; sector_offset < count;
sector_offset++) {
if (mx25_write(
sector + sector_offset,
(uint8_t *)buff +
SECTOR_SIZE * sector_offset) == 1) {
for(sector_offset = 0; sector_offset < count; sector_offset++) {
if(mx25_write(sector + sector_offset, (uint8_t*)buff + SECTOR_SIZE * sector_offset) == 1) {
status = RES_ERROR;
break;
}
......@@ -199,9 +203,7 @@ DRESULT disk_write(
#if SDHC
if(pdrv == 1) {
if (SDHC_Lib_Write(
sector, (void *)buff, count, SDHC_LIB_SINGLE_DATA) !=
E_NO_ERROR) {
if (SDHC_Lib_Write(sector, (void *)buff, count, SDHC_LIB_SINGLE_DATA) != E_NO_ERROR) {
status = RES_ERROR;
} else {
status = RES_OK;
......@@ -212,6 +214,8 @@ DRESULT disk_write(
return status;
}
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
......@@ -220,7 +224,8 @@ DRESULT disk_ioctl(
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
) {
)
{
DRESULT status = RES_PARERR;
if (!s_diskio_initialized) {
......@@ -275,8 +280,7 @@ DRESULT disk_ioctl(
return status;
}
DWORD get_fattime(void)
{
DWORD get_fattime(void) {
if(rtc_en) {
DWORD result;
uint32_t seconds;
......@@ -312,7 +316,8 @@ DWORD get_fattime(void)
result |= minute<<5;
result |= half_seconds;
return result;
} else {
}
else {
return RES_NOTRDY;
}
}
......@@ -331,8 +336,7 @@ static DRESULT get_sector_count(void *buff)
if (init_done) {
if (SDHC_Lib_GetCSD(&csd) == E_NO_ERROR) {
*((DWORD *)buff) =
SDHC_Lib_GetCapacity(&csd) / FF_MIN_SS;
*((DWORD *)buff) = SDHC_Lib_GetCapacity(&csd) / FF_MIN_SS;
status = RES_OK;
}
} else {
......
......@@ -61,7 +61,6 @@ Q(encoding)
Q(file)
Q(FileIO)
Q(flush)
Q(usbstorage)
Q(mode)
Q(r)
Q(read)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment