Sonder Design Team
/
QSB_SPI_MEM
This code progams and reads a flash memory chip over spi
spimem.cpp
- Committer:
- ThomasSonderDesign
- Date:
- 2016-02-25
- Revision:
- 0:67a2a0e8c475
File content as of revision 0:67a2a0e8c475:
/* Test Writes to spi flash, all works */ #include "mbed.h" #include "USBSerial.h" //DigitalOut myled(LED1); USBSerial pc; //Timer t; DigitalOut cs(P0_22); DigitalIn button(P1_15); int dummy = 0; //Serial pc(USBTX, USBRX); // tx, rx int main() { while(1) { char c = 0; while(c==0) { c = pc.getc(); pc.printf("Waiting"); wait(1); } pc.printf("\n\rtime = "); for(int i = 0; i<5; i++) { pc.printf("%.d", 5-i); wait(1); pc.putc(8); } SPI my_spi(P0_9,P0_8,P0_10); // mosi, miso, sclk // Chip must be deselected cs = 1; my_spi.format(8,3);// Setup the spi for 8 bit data, low steady state clock, my_spi.frequency(10000);// second edge capture, with a 1MHz clock rate /* cs = 0; int v1 = my_spi.write(0x05); // Send Read status reg command int v2 = my_spi.write(0x1); int v3 = my_spi.write(0x1); pc.printf("Staus reg = %.x %.x %.x\n\r",v1, v2, v3); cs = 1; wait_ms(2); cs = 0; v1 = my_spi.write(0x9f); // Send Read status reg command v2 = my_spi.write(0x1); v3 = my_spi.write(0x1); v1 = my_spi.write(0x1); pc.printf("RID = %.x %.x %.x \n\r",v1, v2, v3); cs = 1; */ cs=1; // while (1){//(button ==1) { pc.printf("\n\rStart___________________________________\n\r"); //t.start(); cs = 0; // Bring chip select low to write to FeRAM my_spi.write(0x06); // Send WREN command cs = 1; // Bring chip select high to stop writing // Thought I would wait a bit to get a cleaner scope trace wait_us(10); /**** START OF WRITE SEQUENCE ****/ for(int i = 0; i>256; i++) { cs = 0; // Bring chip select low to write to FeRAM my_spi.write(0x02); // Send write command my_spi.write(0x00); // Send top address byte to wrtie to my_spi.write(0x00); // Send Middle address byte to wrtie to my_spi.write(i); // Send Bottom address byte to wrtie to // Send data to be write my_spi.write(i+1); cs = 1; wait_us(5); } //pc.printf("last i %.d", i); /**** END OF WRITE SEQUENCE ****/ // Thought I would wait a bit again wait_us(10); /**** START OF READ SEQUENCE ****/ int my_val[1200]; // Variable to store the read data cs = 0; my_spi.write(0x03); // Send read command my_spi.write(0x00); // Send top address byte to wrtie to my_spi.write(0x00); // Send Middle address byte to wrtie to my_spi.write(0x00); // Send Bottom address byte to wrtie to //pc.printf("\n\r %.d", my_spi.write(dummy)); for(int i=0; i<256; i++) { wait_us(10); my_spi.write(0); } cs=1; //t.stop(); //float time = t.read(); //pc.printf("The time taken was %f seconds\n", time); /* int counter = 0; for(int i=0; i<1200; i++) { wait_us(5); pc.printf("%x",my_val[i]); /* if(my_val[i]>00){ pc.printf("#"); counter++; } else{ pc.printf(" "); counter = 0; } if ((i%60)==0) { pc.printf("\n "); } } int val = my_spi.write(0); pc.printf("\n\rDone %.x",val); cs=1; //} } */ } }