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