test program for flash25spi library

Dependencies:   mbed

Committer:
stonie
Date:
Sun Feb 20 09:32:09 2011 +0000
Revision:
0:ceaf9292b25a
Initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stonie 0:ceaf9292b25a 1 /* This program is based on the Ser25LCxxx_test program by Hendrik Lipka
stonie 0:ceaf9292b25a 2 * It was adapted to flash25spi library on 19.2.2011 by Klaus Steinhammer
stonie 0:ceaf9292b25a 3 * the BSD license also applies to this code - have fun.
stonie 0:ceaf9292b25a 4 */
stonie 0:ceaf9292b25a 5
stonie 0:ceaf9292b25a 6 /*
stonie 0:ceaf9292b25a 7 * Ser25LCxxx_test program
stonie 0:ceaf9292b25a 8 * Copyright (c) 2010 Hendrik Lipka
stonie 0:ceaf9292b25a 9 *
stonie 0:ceaf9292b25a 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
stonie 0:ceaf9292b25a 11 * of this software and associated documentation files (the "Software"), to deal
stonie 0:ceaf9292b25a 12 * in the Software without restriction, including without limitation the rights
stonie 0:ceaf9292b25a 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
stonie 0:ceaf9292b25a 14 * copies of the Software, and to permit persons to whom the Software is
stonie 0:ceaf9292b25a 15 * furnished to do so, subject to the following conditions:
stonie 0:ceaf9292b25a 16 *
stonie 0:ceaf9292b25a 17 * The above copyright notice and this permission notice shall be included in
stonie 0:ceaf9292b25a 18 * all copies or substantial portions of the Software.
stonie 0:ceaf9292b25a 19 *
stonie 0:ceaf9292b25a 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
stonie 0:ceaf9292b25a 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
stonie 0:ceaf9292b25a 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
stonie 0:ceaf9292b25a 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
stonie 0:ceaf9292b25a 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
stonie 0:ceaf9292b25a 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
stonie 0:ceaf9292b25a 26 * THE SOFTWARE.
stonie 0:ceaf9292b25a 27 */
stonie 0:ceaf9292b25a 28
stonie 0:ceaf9292b25a 29
stonie 0:ceaf9292b25a 30
stonie 0:ceaf9292b25a 31 #include "mbed.h"
stonie 0:ceaf9292b25a 32 #include "flash25spi.h"
stonie 0:ceaf9292b25a 33
stonie 0:ceaf9292b25a 34 void dumpLine(char* s, int len)
stonie 0:ceaf9292b25a 35 {
stonie 0:ceaf9292b25a 36 for (int i=0;i<len;i++)
stonie 0:ceaf9292b25a 37 {
stonie 0:ceaf9292b25a 38 printf("%X ",s[i]);
stonie 0:ceaf9292b25a 39 }
stonie 0:ceaf9292b25a 40 printf("\n");
stonie 0:ceaf9292b25a 41 }
stonie 0:ceaf9292b25a 42
stonie 0:ceaf9292b25a 43 void dump(char *s, int len)
stonie 0:ceaf9292b25a 44 {
stonie 0:ceaf9292b25a 45 int i=0;
stonie 0:ceaf9292b25a 46 while (i<len)
stonie 0:ceaf9292b25a 47 {
stonie 0:ceaf9292b25a 48 if (i+16<len)
stonie 0:ceaf9292b25a 49 dumpLine(s+i,16);
stonie 0:ceaf9292b25a 50 else
stonie 0:ceaf9292b25a 51 dumpLine(s+i,len-i);
stonie 0:ceaf9292b25a 52 i+=16;
stonie 0:ceaf9292b25a 53 }
stonie 0:ceaf9292b25a 54 }
stonie 0:ceaf9292b25a 55
stonie 0:ceaf9292b25a 56 int main() {
stonie 0:ceaf9292b25a 57 printf("******** [%s] *********\n",__TIME__);
stonie 0:ceaf9292b25a 58 // setup hardware
stonie 0:ceaf9292b25a 59 SPI spi(p5,p6,p7);
stonie 0:ceaf9292b25a 60 spi.format(8,3);
stonie 0:ceaf9292b25a 61 spi.frequency(1000000);
stonie 0:ceaf9292b25a 62 // /CS is connected to p8, chip,in question is a en25q32 (4Mx8, 256-byte page, 4k sectorsize, 64k blocksize)
stonie 0:ceaf9292b25a 63 flash25spi flash(&spi, p8);
stonie 0:ceaf9292b25a 64
stonie 0:ceaf9292b25a 65 int len=512;
stonie 0:ceaf9292b25a 66
stonie 0:ceaf9292b25a 67 printf("dump initial memory content (2 pages)\n");
stonie 0:ceaf9292b25a 68 char* r=flash.read(0xff00,len);
stonie 0:ceaf9292b25a 69 dump(r,len);
stonie 0:ceaf9292b25a 70 printf("\n");
stonie 0:ceaf9292b25a 71
stonie 0:ceaf9292b25a 72 // write some stuff to the first and second page
stonie 0:ceaf9292b25a 73 char buf[64];
stonie 0:ceaf9292b25a 74 sprintf(buf,"******** [%s] *********\n",__TIME__);
stonie 0:ceaf9292b25a 75
stonie 0:ceaf9292b25a 76 bool b=flash.write(0xfff0,strlen(buf),buf);
stonie 0:ceaf9292b25a 77
stonie 0:ceaf9292b25a 78 printf("read written data back (2 pages)\n");
stonie 0:ceaf9292b25a 79 r=flash.read(0xff00,len);
stonie 0:ceaf9292b25a 80 dump(r,len);
stonie 0:ceaf9292b25a 81 printf("\n");
stonie 0:ceaf9292b25a 82
stonie 0:ceaf9292b25a 83 printf("clear first page, read 2 pages back\n");
stonie 0:ceaf9292b25a 84 flash.clearSector(0xff00);
stonie 0:ceaf9292b25a 85 r=flash.read(0xff00,len);
stonie 0:ceaf9292b25a 86 dump(r,len);
stonie 0:ceaf9292b25a 87 printf("\n");
stonie 0:ceaf9292b25a 88
stonie 0:ceaf9292b25a 89 printf("clear everything, read 2 pages back\n");
stonie 0:ceaf9292b25a 90 flash.clearMem();
stonie 0:ceaf9292b25a 91 r=flash.read(0xff00,len);
stonie 0:ceaf9292b25a 92 dump(r,len);
stonie 0:ceaf9292b25a 93 printf("\n");
stonie 0:ceaf9292b25a 94
stonie 0:ceaf9292b25a 95 }