Skip to content
Snippets Groups Projects
Verified Commit 9b106d2c authored by rahix's avatar rahix
Browse files

chore(fatfs): Fix style


Signed-off-by: default avatarRahix <rahix@rahix.de>
parent 1ef682b7
No related branches found
No related tags found
No related merge requests found
......@@ -21,42 +21,42 @@
#endif
static const TCHAR *rcstrings =
_T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
_T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
_T("NOT_ENABLED\0NO_FILESYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
_T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0INVALID_PARAMETER\0");
_T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
_T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
_T("NOT_ENABLED\0NO_FILESYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
_T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0INVALID_PARAMETER\0");
// this table converts from FRESULT to POSIX errno
const int fresult_to_errno_table[20] = {
[FR_OK] = 0,
[FR_DISK_ERR] = EIO,
[FR_INT_ERR] = EIO,
[FR_NOT_READY] = EBUSY,
[FR_NO_FILE] = ENOENT,
[FR_NO_PATH] = ENOENT,
[FR_INVALID_NAME] = EINVAL,
[FR_DENIED] = EACCES,
[FR_EXIST] = EEXIST,
[FR_INVALID_OBJECT] = EINVAL,
[FR_WRITE_PROTECTED] = EROFS,
[FR_INVALID_DRIVE] = ENODEV,
[FR_NOT_ENABLED] = ENODEV,
[FR_NO_FILESYSTEM] = ENODEV,
[FR_MKFS_ABORTED] = EIO,
[FR_TIMEOUT] = EIO,
[FR_LOCKED] = EIO,
[FR_NOT_ENOUGH_CORE] = ENOMEM,
[FR_TOO_MANY_OPEN_FILES] = EMFILE,
[FR_INVALID_PARAMETER] = EINVAL,
[FR_OK] = 0,
[FR_DISK_ERR] = EIO,
[FR_INT_ERR] = EIO,
[FR_NOT_READY] = EBUSY,
[FR_NO_FILE] = ENOENT,
[FR_NO_PATH] = ENOENT,
[FR_INVALID_NAME] = EINVAL,
[FR_DENIED] = EACCES,
[FR_EXIST] = EEXIST,
[FR_INVALID_OBJECT] = EINVAL,
[FR_WRITE_PROTECTED] = EROFS,
[FR_INVALID_DRIVE] = ENODEV,
[FR_NOT_ENABLED] = ENODEV,
[FR_NO_FILESYSTEM] = ENODEV,
[FR_MKFS_ABORTED] = EIO,
[FR_TIMEOUT] = EIO,
[FR_LOCKED] = EIO,
[FR_NOT_ENOUGH_CORE] = ENOMEM,
[FR_TOO_MANY_OPEN_FILES] = EMFILE,
[FR_INVALID_PARAMETER] = EINVAL,
};
enum FatObjectType { FO_Nil, FO_File, FO_Dir };
struct FatObject {
enum FatObjectType type;
union {
FIL file;
DIR dir;
};
enum FatObjectType type;
union {
FIL file;
DIR dir;
};
};
static bool mount(void);
......@@ -72,47 +72,47 @@ StaticSemaphore_t xSemaphoreBuffer;
#endif
static volatile struct {
bool initiaized;
bool initiaized;
} s_state = {
.initiaized = false,
.initiaized = false,
};
void fatfs_init()
{
if (mount()) {
s_state.initiaized = true;
printf("FatFs mounted\n");
}
if (mount()) {
s_state.initiaized = true;
printf("FatFs mounted\n");
}
}
const char *f_get_rc_string(FRESULT rc)
{
FRESULT i;
const char *p = rcstrings;
for (i = 0; i != rc && *p; i++) {
while (*p++)
;
}
return p;
FRESULT i;
const char *p = rcstrings;
for (i = 0; i != rc && *p; i++) {
while (*p++)
;
}
return p;
}
static bool mount()
{
FRESULT res;
res = f_mount(&FatFs, "/", 0);
if (res != FR_OK) {
printf("f_mount error %s\n", f_get_rc_string(res));
return false;
}
res = f_opendir(&dir, "0:");
if (res != FR_OK) {
printf("f_opendir error %s\n", f_get_rc_string(res));
return false;
}
return true;
FRESULT res;
res = f_mount(&FatFs, "/", 0);
if (res != FR_OK) {
printf("f_mount error %s\n", f_get_rc_string(res));
return false;
}
res = f_opendir(&dir, "0:");
if (res != FR_OK) {
printf("f_opendir error %s\n", f_get_rc_string(res));
return false;
}
return true;
}
/*------------------------------------------------------------------------*/
......@@ -131,12 +131,12 @@ static bool mount()
int ff_cre_syncobj(BYTE vol, FF_SYNC_t *sobj)
{
#if (EPIC_FAT_STATIC_SEMAPHORE == 1)
*sobj = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer);
*sobj = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer);
#else
*sobj = xSemaphoreCreateMutex();
*sobj = xSemaphoreCreateMutex();
#endif //EPIC_FAT_STATIC_SEMAPHORE
return (int)(*sobj != NULL);
return (int)(*sobj != NULL);
}
/*------------------------------------------------------------------------*/
......@@ -154,9 +154,9 @@ int ff_cre_syncobj(BYTE vol, FF_SYNC_t *sobj)
*/
int ff_del_syncobj(FF_SYNC_t sobj)
{
/* FreeRTOS */
vSemaphoreDelete(sobj);
return 1;
/* FreeRTOS */
vSemaphoreDelete(sobj);
return 1;
}
/*------------------------------------------------------------------------*/
......@@ -173,8 +173,8 @@ int ff_del_syncobj(FF_SYNC_t sobj)
*/
int ff_req_grant(FF_SYNC_t sobj)
{
/* FreeRTOS */
return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
/* FreeRTOS */
return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
}
/*------------------------------------------------------------------------*/
......@@ -185,163 +185,161 @@ int ff_req_grant(FF_SYNC_t sobj)
void ff_rel_grant(FF_SYNC_t sobj)
{
/* FreeRTOS */
xSemaphoreGive(sobj);
/* FreeRTOS */
xSemaphoreGive(sobj);
}
int get_fat_object(int i, enum FatObjectType expected, struct FatObject **res)
{
if (i < 0 || i >= EPIC_FAT_MAX_OPENED) {
*res = NULL;
return EBADF;
}
if (s_openedObjects[i].type != expected) {
*res = NULL;
return EBADF;
}
*res = &s_openedObjects[i];
return 0;
if (i < 0 || i >= EPIC_FAT_MAX_OPENED) {
*res = NULL;
return EBADF;
}
if (s_openedObjects[i].type != expected) {
*res = NULL;
return EBADF;
}
*res = &s_openedObjects[i];
return 0;
}
int32_t epic_open(const char *filename, const char *modeString)
{
struct FatObject *o = NULL;
const char *mode_s = modeString;
int i;
int mode = 0;
//find free object to use
for (i = 0; i < EPIC_FAT_MAX_OPENED; ++i) {
if (s_openedObjects[i].type == FO_Nil) {
break;
}
}
if (i == EPIC_FAT_MAX_OPENED) {
return -fresult_to_errno_table[FR_TOO_MANY_OPEN_FILES];
}
o = &s_openedObjects[i];
while (*mode_s) {
switch (*mode_s++) {
case 'r':
mode |= FA_READ;
break;
case 'w':
mode |= FA_WRITE | FA_CREATE_ALWAYS;
break;
case 'x':
mode |= FA_WRITE | FA_CREATE_NEW;
break;
case 'a':
mode |= FA_WRITE | FA_OPEN_ALWAYS;
break;
case '+':
mode |= FA_READ | FA_WRITE;
break;
}
}
int res = f_open(&o->file, filename, mode);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
o->type = FO_File;
// for 'a' mode, we must begin at the end of the file
if ((mode & FA_OPEN_ALWAYS) != 0) {
f_lseek(&o->file, f_size(&o->file));
}
return i;
struct FatObject *o = NULL;
const char *mode_s = modeString;
int i;
int mode = 0;
//find free object to use
for (i = 0; i < EPIC_FAT_MAX_OPENED; ++i) {
if (s_openedObjects[i].type == FO_Nil) {
break;
}
}
if (i == EPIC_FAT_MAX_OPENED) {
return -fresult_to_errno_table[FR_TOO_MANY_OPEN_FILES];
}
o = &s_openedObjects[i];
while (*mode_s) {
switch (*mode_s++) {
case 'r':
mode |= FA_READ;
break;
case 'w':
mode |= FA_WRITE | FA_CREATE_ALWAYS;
break;
case 'x':
mode |= FA_WRITE | FA_CREATE_NEW;
break;
case 'a':
mode |= FA_WRITE | FA_OPEN_ALWAYS;
break;
case '+':
mode |= FA_READ | FA_WRITE;
break;
}
}
int res = f_open(&o->file, filename, mode);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
o->type = FO_File;
// for 'a' mode, we must begin at the end of the file
if ((mode & FA_OPEN_ALWAYS) != 0) {
f_lseek(&o->file, f_size(&o->file));
}
return i;
}
int32_t epic_close(int32_t fd)
{
int res;
struct FatObject *o;
res = get_fat_object(fd, FO_File, &o);
if (res) {
return -res;
}
res = f_close(&o->file);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
o->type = FO_Nil;
return 0;
int res;
struct FatObject *o;
res = get_fat_object(fd, FO_File, &o);
if (res) {
return -res;
}
res = f_close(&o->file);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
o->type = FO_Nil;
return 0;
}
int32_t epic_read(int32_t fd, void *buf, uint32_t nbytes)
{
unsigned int nread = 0;
unsigned int nread = 0;
int res;
struct FatObject *o;
res = get_fat_object(fd, FO_File, &o);
if (res) {
return -res;
}
int res;
struct FatObject *o;
res = get_fat_object(fd, FO_File, &o);
if (res) {
return -res;
}
res = f_read(&o->file, buf, nbytes, &nread);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
res = f_read(&o->file, buf, nbytes, &nread);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
return nread;
return nread;
}
int32_t epic_write(int32_t fd, const void *buf, uint32_t nbytes)
{
unsigned int nwritten = 0;
int res;
struct FatObject *o;
res = get_fat_object(fd, FO_File, &o);
if (res) {
return -res;
}
res = f_write(&o->file, buf, nbytes, &nwritten);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
return nwritten;
unsigned int nwritten = 0;
int res;
struct FatObject *o;
res = get_fat_object(fd, FO_File, &o);
if (res) {
return -res;
}
res = f_write(&o->file, buf, nbytes, &nwritten);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
return nwritten;
}
int32_t epic_flush(int32_t fd) {
int res;
struct FatObject *o;
res = get_fat_object(fd, FO_File, &o);
if (res) {
return -res;
}
res = f_sync(&o->file);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
return 0;
int32_t epic_flush(int32_t fd)
{
int res;
struct FatObject *o;
res = get_fat_object(fd, FO_File, &o);
if (res) {
return -res;
}
res = f_sync(&o->file);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
return 0;
}
int32_t epic_stat(const char* filename, epic_stat_t* stat) {
int res;
FILINFO finfo;
res = f_stat(filename, &finfo);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
if(finfo.fattrib & AM_DIR) {
stat->type = EPICSTAT_DIR;
} else {
stat->type = EPICSTAT_FILE;
}
return 0;
int32_t epic_stat(const char *filename, epic_stat_t *stat)
{
int res;
FILINFO finfo;
res = f_stat(filename, &finfo);
if (res != FR_OK) {
return -fresult_to_errno_table[res];
}
if (finfo.fattrib & AM_DIR) {
stat->type = EPICSTAT_DIR;
} else {
stat->type = EPICSTAT_FILE;
}
return 0;
}
......@@ -93,26 +93,26 @@ STATIC mp_uint_t
file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode)
{
pyb_file_obj_t *self = MP_OBJ_TO_PTR(o_in);
int res;
switch (request) {
case MP_STREAM_FLUSH:
res = epic_flush(self->fd);
if (res < 0) {
*errcode = -res;
return MP_STREAM_ERROR;
}
return 0;
case MP_STREAM_CLOSE:
res = epic_close(self->fd);
if (res < 0) {
*errcode = -res;
return MP_STREAM_ERROR;
}
return 0;
}
//every valid case returns either success or error, so this is EINVAL land:
*errcode = MP_EINVAL;
return MP_STREAM_ERROR;
int res;
switch (request) {
case MP_STREAM_FLUSH:
res = epic_flush(self->fd);
if (res < 0) {
*errcode = -res;
return MP_STREAM_ERROR;
}
return 0;
case MP_STREAM_CLOSE:
res = epic_close(self->fd);
if (res < 0) {
*errcode = -res;
return MP_STREAM_ERROR;
}
return 0;
}
//every valid case returns either success or error, so this is EINVAL land:
*errcode = MP_EINVAL;
return MP_STREAM_ERROR;
// if (request == MP_STREAM_SEEK) {
// struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)(uintptr_t)arg;
......@@ -175,7 +175,7 @@ STATIC mp_obj_t file_open(const mp_obj_type_t *type, mp_arg_val_t *args)
m_del_obj(pyb_file_obj_t, o);
mp_raise_OSError(-res);
}
o->fd = res;
o->fd = res;
return MP_OBJ_FROM_PTR(o);
}
......@@ -272,4 +272,3 @@ mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs)
return file_open(&mp_type_fat_textio, arg_vals);
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
......@@ -7,73 +7,77 @@
/** ported from picropython's posic implementation */
typedef struct _mp_reader_epicfat_t {
bool close_fd;
int fd;
size_t len;
size_t pos;
byte buf[20];
bool close_fd;
int fd;
size_t len;
size_t pos;
byte buf[20];
} mp_reader_epicfat_t;
STATIC mp_uint_t mp_reader_epicfat_readbyte(void *data) {
mp_reader_epicfat_t *reader = (mp_reader_epicfat_t*)data;
if (reader->pos >= reader->len) {
if (reader->len == 0) {
return MP_READER_EOF;
} else {
int n = epic_read(reader->fd, reader->buf, sizeof(reader->buf));
if (n <= 0) {
reader->len = 0;
return MP_READER_EOF;
}
reader->len = n;
reader->pos = 0;
}
}
return reader->buf[reader->pos++];
STATIC mp_uint_t mp_reader_epicfat_readbyte(void *data)
{
mp_reader_epicfat_t *reader = (mp_reader_epicfat_t *)data;
if (reader->pos >= reader->len) {
if (reader->len == 0) {
return MP_READER_EOF;
} else {
int n = epic_read(
reader->fd, reader->buf, sizeof(reader->buf)
);
if (n <= 0) {
reader->len = 0;
return MP_READER_EOF;
}
reader->len = n;
reader->pos = 0;
}
}
return reader->buf[reader->pos++];
}
STATIC void mp_reader_epicfat_close(void *data) {
mp_reader_epicfat_t *reader = (mp_reader_epicfat_t*)data;
epic_close(reader->fd);
m_del_obj(mp_reader_epicfat_t, reader);
STATIC void mp_reader_epicfat_close(void *data)
{
mp_reader_epicfat_t *reader = (mp_reader_epicfat_t *)data;
epic_close(reader->fd);
m_del_obj(mp_reader_epicfat_t, reader);
}
void mp_reader_new_file(mp_reader_t* reader, const char *filename)
void mp_reader_new_file(mp_reader_t *reader, const char *filename)
{
int fd = epic_open(filename, "r");
if (fd < 0) {
mp_raise_OSError(-fd);
}
mp_reader_epicfat_t *rp = m_new_obj(mp_reader_epicfat_t);
rp->fd = fd;
int n = epic_read(rp->fd, rp->buf, sizeof(rp->buf));
if (n < 0) {
epic_close(fd);
}
rp->len = n;
rp->pos = 0;
reader->data = rp;
reader->readbyte = mp_reader_epicfat_readbyte;
reader->close = mp_reader_epicfat_close;
int fd = epic_open(filename, "r");
if (fd < 0) {
mp_raise_OSError(-fd);
}
mp_reader_epicfat_t *rp = m_new_obj(mp_reader_epicfat_t);
rp->fd = fd;
int n = epic_read(rp->fd, rp->buf, sizeof(rp->buf));
if (n < 0) {
epic_close(fd);
}
rp->len = n;
rp->pos = 0;
reader->data = rp;
reader->readbyte = mp_reader_epicfat_readbyte;
reader->close = mp_reader_epicfat_close;
}
mp_lexer_t *mp_lexer_new_from_file(const char *filename)
{
mp_reader_t reader;
mp_reader_new_file(&reader, filename);
return mp_lexer_new(qstr_from_str(filename), reader);
mp_reader_t reader;
mp_reader_new_file(&reader, filename);
return mp_lexer_new(qstr_from_str(filename), reader);
}
mp_import_stat_t mp_import_stat(const char *path)
{
struct epic_stat_t stat;
struct epic_stat_t stat;
if(epic_stat(path, &stat) == 0) {
if (stat.type == EPICSTAT_FILE) {
return MP_IMPORT_STAT_FILE;
} else {
return MP_IMPORT_STAT_DIR;
}
}
if (epic_stat(path, &stat) == 0) {
if (stat.type == EPICSTAT_FILE) {
return MP_IMPORT_STAT_FILE;
} else {
return MP_IMPORT_STAT_DIR;
}
}
return MP_IMPORT_STAT_NO_EXIST;
}
......@@ -116,8 +116,3 @@ void NORETURN nlr_jump_fail(void *val)
Reset_Handler();
}
/******************************************************************************
* Stubs
*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment