* functions
*/
-int strlen(char *string) {
+#define modulo(d,x,y) (d)=(x); \
+ while((d)>=(y)) \
+ (d)-=(y);
- int cnt;
+int strlen(char *s) {
- cnt=0;
+ int n;
- while(string[cnt])
- cnt++;
+ n=0;
- return cnt;
+ while(s[n])
+ n++;
+
+ return n;
}
int strncmp(char *a,char *b,int n) {
return 0;
}
+int pffs_get_data_sec(t_pffs_fd *fd) {
+
+ t_pffs *pffs;
+ u8 sec,sec0,sec1;
+
+ pffs->fd->pffs;
+ sec0=pffs->sec_num_data[0];
+ sec1=pffs->sec_num_data[1];
+
+ for(sec=sec0;sec<sec1;sec++) {
+ if((addr<pffs->sec_addr[sec+1])&(addr>=pffs->sec_addr[sec])) {
+ fd->data_sec=sec;
+ break;
+ }
+ }
+
+ return 0;
+}
+
+int pffs_rearrange(t_pffs *pffs) {
+
+
+
+ return 0;
+}
+
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,
*iaddr=pffs->base_addr|pffs->sec_addr[pffs->index_sec];
- while(*iaddr<pffs->sec_addr[pffs->index_sec]) {
+ while(*iaddr<pffs->sec_addr[pffs->index_sec+1]) {
pffs->fr(*iaddr,data,3);
if(!pffs_check_magic(data[0]))
return ret;
}
+int pffs_write_index_init(t_pffs_fd *fd) {
+
+ t_pffs *pffs;
+ u16 data[PFFS_HEADER_SIZE+PFFS_MAX_FILENAME_SIZE];
+
+ pffs=fd->pffs;
+ fns=fd->fn-size;
+
+ if(fd->iaddr+6+fns+fns>=pffs->sec_addr[pffs->index_sec+1])
+ return PFFS_NO_INDEX_SPACE_LEFT;
+
+ data[0]=0x7f00|(fns<<4)|((fd->daddr>>16)&0xf);
+ data[1]=fd->daddr&0xffff;
+ pffs->fw(fd->iaddr,data,4);
+ pffs->fw(fd->iaddr+6,fd->file,fns+fns);
+
+ return PFFS_INDEX_WROTE_INIT;
+}
+
+int pffs_write_finish(t_pffs_fd *fd) {
+
+ fd->pffs->fw(fd->iaddr+4,&(fd->len),2);
+
+ return 0;
+}
+
int pffs_open_write(t_pffs_fd *fd) {
int ret;
switch(ret) {
case PFFS_FILE_NOT_FOUND:
- fd->mode=PFFS_WRITE;
- pffs_write_index_init();
+ ret=pffs_write_index_init();
+ if(ret!=PFFS_INDEX_WROTE_INIT)
+ break;
fd->dptr=fd->daddr;
+ fd->len=0;
+ fd->mode=PFFS_WRITE;
break;
case PFFS_FILE_FOUND:
default:
/* filename */
fd->fn_size=strlen(file);
- if(fd->fn_size>PFFS_MAX_FILENAME_SIZE+PFFS_MAX_FILENAME_SIZE)
- return PFFS_FILENAME_TOO_LONG;
- strncpy(fd->file,file,fd->fn_size);
if(fd->fn_size&1)
fd->file[fd->fn_size++]='\0';
+ fd->fn_size>>1;
+ if(fd->fn_size>PFFS_MAX_FILENAME_SIZE)
+ return PFFS_FILENAME_TOO_LONG;
+ strncpy(fd->file,file,fd->fn_size+fd->fn_size);
/* clear fd mode */
fd->mode=0;
int pffs_read(t_pffs_fd *fd,u8 *buf,int len) {
int missing;
+ int sec_end;
/* check whether valid */
if(!(fd->mode&PFFS_READ))
return PFFS_EINVAL;
- /* checl len */
+ /* check len */
if(len&1)
return PFFS_INVALID_LEN;
+ /* check how much we read */
missing=fd->len-(fd->dptr-fd->daddr);
if(len>missing)
len=missing;
+ if((fd->dptr+len)>=pffs->sec_addr[pffs->sec_num_data[1]+1])
+ sec_end=pffs->sec_addr[pffs->sec_num_data[1]+1]-fd->dptr;
+
+
/* read */
fd->pffs->fr(fd->dptr,(u16 *)buf,len);
fd->dptr+=len;;
return len;
}
+int pffs_write(t_pffs_fd *fd,u8 buf,int len) {
+
+ /* check whether valid */
+ if(!(fd->mode&PFFS_WRITE))
+ return PFFS_EINVAL;
+
+ /* check len */
+ if(len&1)
+ return PFFS_INVALID_LEN;
+
+ missing=PFFS_MAX_FILE_SIZE-fd->len;
+ if(len>missing)
+ len=missing;
+
+ /* check for */
+ if(fd->dptr+len>=pffs->)
+}
+
/* defines */
#define PFFS_MAX_FILENAME_SIZE 15 // in words
#define PFFS_HEADER_SIZE 3 // in words
+#define PFFS_MAX_FILE_SIZE 0xffff
/* general pffs system */
#define PFFS_INDEX_FOUND 0x00
#define PFFS_INDEX_MAGIC_MASK 0xf000
#define PFFS_STATE_MASK 0x0f00
#define PFFS_FNLEN_MASK 0x00f0
-#define PFFS_LEN_MSB_MASK 0x000f
+#define PFFS_ADDR_MSB_MASK 0x000f
#define PFFS_INDEX_MAGIC 0x7000
#define PFFS_INDEX_REMOVED 0x0700
+#define PFFS_ADDR_MSB
+
/* file modes */
#define PFFS_READ 0x01
#define PFFS_WRITE 0x02
#define PFFS_SEEK_END 0x03
/* pffs open / write / read return codes */
-#define PFFS_FILE_FOUND 0x01
-#define PFFS_FILE_NOT_FOUND 0x02
-#define PFFS_NO_SPACE_LEFT 0x04
-#define PFFS_FILENAME_TOO_LONG 0x08
-#define PFFS_MODE_UNSUPPORTED 0x10
+#define PFFS_FILE_FOUND 0x01
+#define PFFS_FILE_NOT_FOUND 0x02
+#define PFFS_NO_SPACE_LEFT 0x04
+#define PFFS_FILENAME_TOO_LONG 0x08
+#define PFFS_MODE_UNSUPPORTED 0x10
+#define PFFS_NO_INDEX_SPACE_LEFT 0x20
+#define PFFS_INDEX_WROTE_INIT 0x40
#define PFFS_INVALID_LEN 0x01
#define PFFS_EINVAL 0x02
} t_pffs;
typedef struct s_pffs_fd {
- u32 daddr;
- u32 dptr;
- u32 iaddr;
- u16 len;
- char file[PFFS_MAX_FILENAME_SIZE+PFFS_MAX_FILENAME_SIZE];
- u8 fn_size;
- u8 mode;
- t_pffs *pffs;
+ u32 daddr; // data start address
+ u8 data_sec; // current data sector
+ u32 dptr; // data pointer
+ u32 iaddr; // index address
+ u16 len; // file size
+ char file[PFFS_MAX_FILENAME_SIZE+PFFS_MAX_FILENAME_SIZE]; // the file
+ u8 fn_size; // file name size
+ u8 mode; // mode, eg: write, read
+ t_pffs *pffs; // the pffs main struct
} t_pffs_fd;
-/* function prototypes */
+/* function prototypes (only the ones applications should use!) */
+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),
+ int (*fr)(u32 addr,u16 *buf,int len),
+ int (*fe)(u32 addr));
+int pffs_init(t_pffs *pffs);
int pffs_open(t_pffs *pffs,t_pffs_fd *fd,char *file,u8 mode);
int pffs_write(t_pffs_fd *fd,u8 *buf,int len);
int pffs_read(t_pffs_fd *fd,u8 *buf,int len);