pffs->data_ptr=daddr;
if(found) {
- pffs->index_ptr=pffs->sec_addr[sector]+iaddr;
+ pffs->index_ptr[0]=pffs->sec_addr[sector];
+ pffs->index_ptr[1]=pffs->sec_addr[sector]+iaddr;
return 0;
}
else
#define pffs_sec_erase(pffs,sec) pffs->fe(pffs->base_addr|pffs->sec_addr[sec])
-int pffs_reorientate(t_pffs *pffs) {
+int pffs_orientate(t_pffs *pffs) {
u8 sec,sec0,sec1;
return 0;
}
-int pffs_flash_register(t_pffs *pffs,u32 base_addr,u8 sec_num,u32 *sec_addr,
+int pffs_flash_register(t_pffs *pffs,u32 base_addr,u32 *sec_addr,
u8 sec_num_data_min,u8 sec_num_data_max,
u8 sec_num_index0,u8 sec_num_index1,
int (*fw)(u32 addr,u16 *buf,int len),
/* assign physical flash specs */
pffs->base_addr=base_addr;
- pffs->sec_num=sec_num;
pffs->sec_addr=sec_addr;
/* specified index and data sectors */
pffs->fr=fr;
pffs->fe=fe;
- /* orientate */
- pffs_reorientate(pffs);
-
pffs->state|=PFFS_REGISTERED;
return 0;
int pffs_init(t_pffs *pffs) {
+ /* check whether a flash is registered */
if(!(pffs->state&PFFS_REGISTERED))
return -1;
+ /* orientate */
+ pffs_orientate(pffs);
+
+ return 0;
+}
+
+#define pffs_check_magic(data) (((data)&PFFS_INDEX_MAGIC_MASK)==PFFS_INDEX_MAGIC)
+#define pffs_fnlen(data) (((data)&PFFS_FNLEN_MASK)>>4)
+#define pffs_daddr_msb(data) (((data)&))
+
+int pffs_find_file(t_pffs *pffs,char *file,u32 *iaddr,u32 *daddr,u16 *len) {
+
+ u8 fnl;
+ u16 data[PFFS_MAX_FILENAME_SIZE+PFFS_HEADER_SIZE];
+
+ pffs->index_ptr[2]=pffs->base_addr|pffs->index_ptr[0];
+
+ while(pffs->index_ptr[2]<pffs->index_ptr[1])
+ pffs->fr(iaddr,data,3);
+
+ if(!pffs_check_magic(data[0]))
+ break;
+
+ fnl=pffs_fnlen(data[0]);
+ pffs->fr(iaddr+6,data+3,fnl+fnl);
+
+ if(!strncmp(fd->file,(char *)(data+3),fnl+fnl)) {
+ *daddr=((data[0]&0x000f)<<16)|data[1];
+ *len=data[2];
+ pffs->index_ptr[2]=*iaddr;
+ return PFFS_FILE_EXISTS;
+ }
+ pffs->fr(iaddr,data,3);
+ }
+
+ return PFFS_FILE_NOT_FOUND;
+}
+
+int pffs_open_read(t_pffs *pffs,t_pffs_fd *fd) {
+
+ u32 iaddr;
+ u32 daddr;
+ u16 data[PFFS_MAX_FILENAME+PFFS_HEADER_SIZE];
+
+ iaddr=pffs->base_addr|pffs->index_ptr[0];
+
+ return 0x23;
+}
+
+int pffs_open(t_pffs *pffs,t_pffs_fd *fd,char *file,u8 mode) {
+
+ /* the pffs struct */
+ fd->pffs=pffs;
+
+ /* filename */
+ fd->fn_size=strlen(file);
+ if(fd->fn_size>30)
+ return PFFS_FILENAME_TOO_LONG;
+ strncpy(fd->file,file,fd->fn_size);
+ if(fd->fn_size&1)
+ fd->file[fd->fn_size++]='\0';
+
+ /* mode */
+ fd->mode=mode;
+
+ /* action */
+ switch(mode) {
+ case PFFS_READ:
+ pffs_open_read(pffs,fd);
+ break;
+ case PFFS_WRITE:
+ break;
+ case PFFS_RDWR:
+ default:
+ return PFFS_MODE_UNSUPPORTED;
+ }
+
return 0;
}
#include "lpc2xxx.h"
#include "types.h"
+#include "functions.h"
/* defines */
+#define PFFS_MAX_FILENAME_SIZE 15 // in words
+#define PFFS_HEADER_SIZE 3 // in words
#define PFFS_REGISTERED (1<<0)
#define PFFS_INDEX_MAGIC 0x7000
+#define PFFS_INDEX_MAGIC_MASK 0xf000
+#define PFFS_RESERVED_MASK 0x0f00
+#define PFFS_FNLEN_MASK 0x00f0
+#define PFFS_LEN_MSB_MASK 0x000f
+
+/* file modes */
+#define PFFS_READ 0x01
+#define PFFS_WRITE 0x02
+#define PFFS_RDWR 0x03
+
+/* pffs write / read return codes */
+#define PFFS_FILE_EXISTS 0x01
+#define PFFS_FILE_NOT_FOUND 0x02
+#define PFFS_NO_SPACE_LEFT 0x04
+#define PFFS_FILENAME_TOO_LONG 0x08
+
/* type definitions */
typedef struct s_pffs {
/* flash specs */
u32 base_addr;
- u8 sec_num;
u32 *sec_addr;
/* flash write, read and sector erase function pointers */
int (*fw)(u32 addr,u16 *buf,int len);
int (*fe)(u32 addr);
/* pffs internal variables */
u8 state;
- u32 data_ptr;
- u32 index_ptr;
- u8 sec_num_data[2];
- u8 sec_num_index[2];
+ u32 data_ptr; // pointer where new data goes
+ u32 index_ptr[3]; // 0: start, 1: new, 2: current
+ u8 sec_num_data[2]; // data start/end sectors
+ u8 sec_num_index[2]; // 2 index sectors
} t_pffs;
+typedef struct s_pffs_fd {
+ u32 daddr;
+ u32 iaddr;
+ char file[PFFS_MAX_FILENAME_SIZE+PFFS_MAX_FILENAME_SIZE];
+ u8 fn_size;
+ u8 mode;
+ t_pffs *pffs;
+} t_pffs_fd;
+
/* function prototypes */
+int pffs_open(t_pffs *pffs,char *file,u8 mode);
+int pffs_write(t_pffs *pffs,int fd,u8 *buf,int len);
+int pffs_read(t_pffs *pffs,int fd,u8 *buf,int len);
+int pffs_close(t_pffs *pffs,int fd);
+int pffs_unlink(t_pffs *pffs,char *file);
#endif