Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
spimem.cpp@0:67a2a0e8c475, 2016-02-25 (annotated)
- Committer:
- ThomasSonderDesign
- Date:
- Thu Feb 25 00:51:18 2016 +0000
- Revision:
- 0:67a2a0e8c475
Test od programing flash over SPI
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ThomasSonderDesign | 0:67a2a0e8c475 | 1 | /* |
| ThomasSonderDesign | 0:67a2a0e8c475 | 2 | Test Writes to spi flash, all works |
| ThomasSonderDesign | 0:67a2a0e8c475 | 3 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 4 | */ |
| ThomasSonderDesign | 0:67a2a0e8c475 | 5 | #include "mbed.h" |
| ThomasSonderDesign | 0:67a2a0e8c475 | 6 | #include "USBSerial.h" |
| ThomasSonderDesign | 0:67a2a0e8c475 | 7 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 8 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 9 | //DigitalOut myled(LED1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 10 | USBSerial pc; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 11 | //Timer t; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 12 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 13 | DigitalOut cs(P0_22); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 14 | DigitalIn button(P1_15); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 15 | int dummy = 0; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 16 | //Serial pc(USBTX, USBRX); // tx, rx |
| ThomasSonderDesign | 0:67a2a0e8c475 | 17 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 18 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 19 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 20 | int main() |
| ThomasSonderDesign | 0:67a2a0e8c475 | 21 | { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 22 | while(1) { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 23 | char c = 0; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 24 | while(c==0) { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 25 | c = pc.getc(); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 26 | pc.printf("Waiting"); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 27 | wait(1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 28 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 29 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 30 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 31 | pc.printf("\n\rtime = "); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 32 | for(int i = 0; i<5; i++) { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 33 | pc.printf("%.d", 5-i); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 34 | wait(1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 35 | pc.putc(8); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 36 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 37 | SPI my_spi(P0_9,P0_8,P0_10); // mosi, miso, sclk |
| ThomasSonderDesign | 0:67a2a0e8c475 | 38 | // Chip must be deselected |
| ThomasSonderDesign | 0:67a2a0e8c475 | 39 | cs = 1; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 40 | my_spi.format(8,3);// Setup the spi for 8 bit data, low steady state clock, |
| ThomasSonderDesign | 0:67a2a0e8c475 | 41 | my_spi.frequency(10000);// second edge capture, with a 1MHz clock rate |
| ThomasSonderDesign | 0:67a2a0e8c475 | 42 | /* |
| ThomasSonderDesign | 0:67a2a0e8c475 | 43 | cs = 0; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 44 | int v1 = my_spi.write(0x05); // Send Read status reg command |
| ThomasSonderDesign | 0:67a2a0e8c475 | 45 | int v2 = my_spi.write(0x1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 46 | int v3 = my_spi.write(0x1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 47 | pc.printf("Staus reg = %.x %.x %.x\n\r",v1, v2, v3); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 48 | cs = 1; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 49 | wait_ms(2); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 50 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 51 | cs = 0; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 52 | v1 = my_spi.write(0x9f); // Send Read status reg command |
| ThomasSonderDesign | 0:67a2a0e8c475 | 53 | v2 = my_spi.write(0x1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 54 | v3 = my_spi.write(0x1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 55 | v1 = my_spi.write(0x1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 56 | pc.printf("RID = %.x %.x %.x \n\r",v1, v2, v3); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 57 | cs = 1; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 58 | */ |
| ThomasSonderDesign | 0:67a2a0e8c475 | 59 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 60 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 61 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 62 | cs=1; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 63 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 64 | // while (1){//(button ==1) { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 65 | pc.printf("\n\rStart___________________________________\n\r"); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 66 | //t.start(); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 67 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 68 | cs = 0; // Bring chip select low to write to FeRAM |
| ThomasSonderDesign | 0:67a2a0e8c475 | 69 | my_spi.write(0x06); // Send WREN command |
| ThomasSonderDesign | 0:67a2a0e8c475 | 70 | cs = 1; // Bring chip select high to stop writing |
| ThomasSonderDesign | 0:67a2a0e8c475 | 71 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 72 | // Thought I would wait a bit to get a cleaner scope trace |
| ThomasSonderDesign | 0:67a2a0e8c475 | 73 | wait_us(10); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 74 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 75 | /**** START OF WRITE SEQUENCE ****/ |
| ThomasSonderDesign | 0:67a2a0e8c475 | 76 | for(int i = 0; i>256; i++) { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 77 | cs = 0; // Bring chip select low to write to FeRAM |
| ThomasSonderDesign | 0:67a2a0e8c475 | 78 | my_spi.write(0x02); // Send write command |
| ThomasSonderDesign | 0:67a2a0e8c475 | 79 | my_spi.write(0x00); // Send top address byte to wrtie to |
| ThomasSonderDesign | 0:67a2a0e8c475 | 80 | my_spi.write(0x00); // Send Middle address byte to wrtie to |
| ThomasSonderDesign | 0:67a2a0e8c475 | 81 | my_spi.write(i); // Send Bottom address byte to wrtie to |
| ThomasSonderDesign | 0:67a2a0e8c475 | 82 | // Send data to be write |
| ThomasSonderDesign | 0:67a2a0e8c475 | 83 | my_spi.write(i+1); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 84 | cs = 1; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 85 | wait_us(5); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 86 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 87 | //pc.printf("last i %.d", i); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 88 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 89 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 90 | /**** END OF WRITE SEQUENCE ****/ |
| ThomasSonderDesign | 0:67a2a0e8c475 | 91 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 92 | // Thought I would wait a bit again |
| ThomasSonderDesign | 0:67a2a0e8c475 | 93 | wait_us(10); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 94 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 95 | /**** START OF READ SEQUENCE ****/ |
| ThomasSonderDesign | 0:67a2a0e8c475 | 96 | int my_val[1200]; // Variable to store the read data |
| ThomasSonderDesign | 0:67a2a0e8c475 | 97 | cs = 0; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 98 | my_spi.write(0x03); // Send read command |
| ThomasSonderDesign | 0:67a2a0e8c475 | 99 | my_spi.write(0x00); // Send top address byte to wrtie to |
| ThomasSonderDesign | 0:67a2a0e8c475 | 100 | my_spi.write(0x00); // Send Middle address byte to wrtie to |
| ThomasSonderDesign | 0:67a2a0e8c475 | 101 | my_spi.write(0x00); // Send Bottom address byte to wrtie to |
| ThomasSonderDesign | 0:67a2a0e8c475 | 102 | //pc.printf("\n\r %.d", my_spi.write(dummy)); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 103 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 104 | for(int i=0; i<256; i++) { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 105 | wait_us(10); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 106 | my_spi.write(0); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 107 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 108 | cs=1; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 109 | //t.stop(); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 110 | //float time = t.read(); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 111 | //pc.printf("The time taken was %f seconds\n", time); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 112 | /* |
| ThomasSonderDesign | 0:67a2a0e8c475 | 113 | int counter = 0; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 114 | for(int i=0; i<1200; i++) { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 115 | wait_us(5); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 116 | pc.printf("%x",my_val[i]); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 117 | /* |
| ThomasSonderDesign | 0:67a2a0e8c475 | 118 | if(my_val[i]>00){ |
| ThomasSonderDesign | 0:67a2a0e8c475 | 119 | pc.printf("#"); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 120 | counter++; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 121 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 122 | else{ |
| ThomasSonderDesign | 0:67a2a0e8c475 | 123 | pc.printf(" "); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 124 | counter = 0; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 125 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 126 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 127 | if ((i%60)==0) { |
| ThomasSonderDesign | 0:67a2a0e8c475 | 128 | pc.printf("\n "); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 129 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 130 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 131 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 132 | |
| ThomasSonderDesign | 0:67a2a0e8c475 | 133 | int val = my_spi.write(0); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 134 | pc.printf("\n\rDone %.x",val); |
| ThomasSonderDesign | 0:67a2a0e8c475 | 135 | cs=1; |
| ThomasSonderDesign | 0:67a2a0e8c475 | 136 | //} |
| ThomasSonderDesign | 0:67a2a0e8c475 | 137 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 138 | */ |
| ThomasSonderDesign | 0:67a2a0e8c475 | 139 | } |
| ThomasSonderDesign | 0:67a2a0e8c475 | 140 | } |