* functions
*/
+u32 pffs_get_ptr(t_pffs *pffs,u8 sector) {
+
+ u16 data[3];
+ u32 iaddr;
+ u32 daddr;
+ u8 fnlen;
+ u8 found;
+
+ iaddr=0;
+ daddr=0;
+ found=0;
+
+ while(1) {
+ pffs->fr(pffs->base_addr+pffs->sec_addr[sector]+iaddr,data,6);
+ if(!((data[0]&0xff00)==PFFS_INDEX_MAGIC))
+ break;
+ found=1;
+ fnlen=(data[0]&0x00f0)>>4;
+ iaddr+=(6+fnlen+fnlen);
+ daddr=((data[0]&0x000f)<<16)|data[1];
+ }
+
+ pffs->data_ptr=daddr;
+
+ if(found) {
+ pffs->index_ptr=pffs->sec_addr[sector]+iaddr;
+ return 0;
+ }
+ else
+ return -1;
+}
+
+int pffs_sec_empty(t_pffs *pffs,u8 sec) {
+
+ u32 addr;
+ u16 data;
+
+ addr=pffs->base_addr|pffs->sec_addr[sec];
+
+ while(addr<(pffs->base_addr|pffs->sec_addr[sec+1])) {
+ data=*((u16 *)(addr));
+ if(data!=0xffff)
+ return 0;
+ addr+=2;
+ }
+
+ return 1;
+}
+
+#define pffs_sec_erase(pffs,sec) pffs->fe(pffs->base_addr|pffs->sec_addr[sec])
+
+int pffs_reorientate(t_pffs *pffs) {
+
+ u8 sec,sec0,sec1;
+
+ /* check index sectors */
+ sec0=pffs->sec_num_index[0];
+ sec1=pffs->sec_num_index[1];
+ if(!(pffs_get_ptr(pffs,sec0)&pffs_get_ptr(pffs,sec1)))
+ return 0;
+
+ /* initial run => no data + no index assumed => check whether erased! */
+ // erase index sectors
+ if(!pffs_sec_empty(pffs,sec0))
+ pffs_sec_erase(pffs,sec0);
+ if(!pffs_sec_empty(pffs,sec1))
+ pffs_sec_erase(pffs,sec1);
+ // erase data sectors
+ sec0=pffs->sec_num_data[0];
+ sec1=pffs->sec_num_data[1];
+ for(sec=sec0;sec<=sec1;sec++)
+ if(!pffs_sec_empty(pffs,sec))
+ pffs_sec_erase(pffs,sec);
+
+ return 0;
+}
+
int pffs_flash_register(t_pffs *pffs,u32 base_addr,u8 sec_num,u32 *sec_addr,
- u8 dmin,u8 dmax,u8 index1,u8 index2,
+ 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)) {
+ /* 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->sec_num_data[0]=sec_num_data_min;
+ pffs->sec_num_data[1]=sec_num_data_max;
+ pffs->sec_num_index[0]=sec_num_index0;
+ pffs->sec_num_index[1]=sec_num_index1;
+
+ /* flash acccess function pointers */
pffs->fw=fw;
pffs->fr=fr;
pffs->fe=fe;
+ /* orientate */
+ pffs_reorientate(pffs);
+
pffs->state|=PFFS_REGISTERED;
return 0;