exercise SPI SerialFlash library on k64f with winbond flash

Dependencies:   SerialFlash mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // SPI serial flash test from teensy SerialFlash
00002 //  SPI pins  CS on 6
00003 //  TODO SPI CS pins part of class definitions
00004 #include "mbed.h"
00005 #include "SerialFlash.h"
00006 
00007 void dump(int addr, int lines) {
00008     int i;
00009     char buf[16];
00010 
00011     while(lines--){
00012         SerialFlash.read(addr,buf,16);
00013         addr += 16;
00014         for(i=0;i<16;i++) printf("%02x ",buf[i]);
00015         printf("|");
00016         for(i=0;i<16;i++){
00017             if (buf[i] < ' ' | buf[i]  > '\177') printf("?");
00018             else printf("%c",buf[i]);
00019         }
00020         printf("|\n");
00021     }
00022 }
00023 
00024 void meta() {
00025   //  paul's file system meta data
00026   int i,files,deleted,capacity, avail;
00027   uint32_t maxfiles, stringsize, buff, dir[2];
00028   uint16_t hash;
00029   uint8_t id[3];
00030 
00031   printf("meta data: magic(4),strsize/4(2),maxfiles(2)  hashes*2\n");
00032   dump(0,2);
00033   SerialFlash.read(4,&buff,4);
00034   maxfiles = buff & 0xffff;
00035   stringsize = (buff & 0xFFFF0000) >> 14;
00036   printf("maxfiles %d  stringsize %d\n",maxfiles,stringsize);
00037   printf("dir: entry(10): addr(4),lth(4),str/4(2)\n");
00038   dump(8 + maxfiles*2,3);
00039   printf("strings\n");
00040   dump(8 + 12*maxfiles,3);
00041   printf("first data block\n");
00042   i = 8 + maxfiles*2 + maxfiles*10 + stringsize;
00043   dump(i,1);
00044   files=deleted=hash=0;
00045   for(i=0;i<maxfiles;i++) {
00046     SerialFlash.read(8+2*i,&hash,2);
00047     if (hash == 0xffff) break;
00048     if (hash) files++;
00049     else deleted++;
00050   }
00051   printf("files %d  deleted %d\n",files,deleted);
00052   SerialFlash.opendir();
00053   while (1) {
00054     char filename[64];
00055     uint32_t filesize;
00056 
00057     if (SerialFlash.readdir(filename, sizeof(filename), filesize)) {
00058       printf("%s  %d bytes\n",filename,filesize);
00059     } else {
00060       break; // no more files
00061     }
00062   }
00063   
00064   SerialFlash.readID(id);
00065   capacity = SerialFlash.capacity(id);
00066   printf("Capacity %d ",capacity);
00067   // read last dir entry  i-1, could round up to 256
00068   SerialFlash.read(8 + maxfiles*2 + (i-1)*10,dir,8);
00069   avail = capacity - (dir[0] + dir[1]);
00070   printf("   free %d\n",avail);
00071 }
00072 
00073 void erase() {
00074     printf("erasing ...");
00075     SerialFlash.eraseAll();
00076     while (SerialFlash.ready() == false);
00077     printf(" done\n");
00078 }
00079 
00080 void dofile(char *filename, unsigned long length) {
00081     printf("create %s %d bytes\n",filename,length);
00082 
00083     // check if this file is already on the Flash chip
00084     if (SerialFlash.exists(filename)) {
00085       printf("file exists, removing\n");
00086       SerialFlash.remove(filename);
00087     }
00088   
00089     // create the file on the Flash chip and copy data
00090     if (SerialFlash.create(filename, length)) {
00091       char buf[256];
00092       unsigned int n;
00093       int i;
00094       wait(.002);
00095       SerialFlashFile ff = SerialFlash.open(filename);
00096       if (ff) {
00097         // copy data loop
00098         while (length) {
00099 
00100 
00101           for(i=0;i<sizeof(buf);i++) buf[i]=i;
00102           if (length >= 256) n =256;
00103            else n = length;
00104           ff.write(buf, n);
00105           length -= n;
00106           printf(".");
00107         }
00108         ff.close();
00109         printf("\n");
00110         // check some data
00111         ff = SerialFlash.open("TST.DAT");
00112         printf("checking  flash addr %d\n",ff.getFlashAddress());
00113         dump(ff.getFlashAddress(),2);
00114         int n,errs=0;
00115         while(1) {
00116             n=ff.read(buf,sizeof(buf));
00117             if (n <= 0) break;
00118             for (i=0;i<sizeof(buf);i++) if (buf[i] != i) errs++;
00119         }
00120         ff.close();
00121         printf("errs %d\n",errs); 
00122       } else {
00123         printf("  error opening freshly created file!\n");
00124       }
00125     } else {
00126       printf("  unable to create file\n");
00127     }
00128 }
00129 
00130 int main()
00131 {
00132     uint8_t id[3];
00133     SerialFlash.begin();
00134     //erase();
00135     //dofile("TST.DAT",1024);
00136     while (true) {
00137         SerialFlash.readID(id);
00138         printf("%x %x %x\n",id[0],id[1],id[2]);
00139         dump(0,2);
00140         meta();
00141         wait(4.0);
00142     }
00143 }