Gorazd Kovacic
/
AT45_spi_flash_example
ported for ff-lpc546xx
main.cpp@0:cb29c01320b1, 2017-11-14 (annotated)
- Committer:
- maclobdell
- Date:
- Tue Nov 14 14:58:48 2017 +0000
- Revision:
- 0:cb29c01320b1
- Child:
- 1:1f267b9c975a
Very simple example application that extracts details from AT45 SPI Flash and writes/reads a string to it.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maclobdell | 0:cb29c01320b1 | 1 | #include "mbed.h" |
maclobdell | 0:cb29c01320b1 | 2 | |
maclobdell | 0:cb29c01320b1 | 3 | #include "AT45.h" |
maclobdell | 0:cb29c01320b1 | 4 | |
maclobdell | 0:cb29c01320b1 | 5 | /* Tested on FRDM-KW24D512 */ |
maclobdell | 0:cb29c01320b1 | 6 | |
maclobdell | 0:cb29c01320b1 | 7 | SPI spi(D11, D12, D13); |
maclobdell | 0:cb29c01320b1 | 8 | AT45 spif(&spi, D10); |
maclobdell | 0:cb29c01320b1 | 9 | |
maclobdell | 0:cb29c01320b1 | 10 | DigitalOut myled(LED1); |
maclobdell | 0:cb29c01320b1 | 11 | |
maclobdell | 0:cb29c01320b1 | 12 | int main() { |
maclobdell | 0:cb29c01320b1 | 13 | |
maclobdell | 0:cb29c01320b1 | 14 | printf("spif test\n\r"); |
maclobdell | 0:cb29c01320b1 | 15 | |
maclobdell | 0:cb29c01320b1 | 16 | printf("spif size: %d bytes\n\r", spif.device_size()); |
maclobdell | 0:cb29c01320b1 | 17 | printf("spif page erase size: %d bytes\n\r", spif.pagesize()); |
maclobdell | 0:cb29c01320b1 | 18 | printf("spif pages: %d\n\r", spif.pages()); |
maclobdell | 0:cb29c01320b1 | 19 | printf("spif block erase size: 4096 bytes\n\r"); |
maclobdell | 0:cb29c01320b1 | 20 | printf("spif blocks: %d\n\r", spif.blocks()); |
maclobdell | 0:cb29c01320b1 | 21 | printf("id: %d\n\r", spif.id()); |
maclobdell | 0:cb29c01320b1 | 22 | |
maclobdell | 0:cb29c01320b1 | 23 | int pagesize = spif.pagesize(); |
maclobdell | 0:cb29c01320b1 | 24 | |
maclobdell | 0:cb29c01320b1 | 25 | // Write "Hello World!" to a block |
maclobdell | 0:cb29c01320b1 | 26 | char *buffer = (char*) malloc(pagesize); |
maclobdell | 0:cb29c01320b1 | 27 | sprintf(buffer, "Hello World!\n"); |
maclobdell | 0:cb29c01320b1 | 28 | spif.page_erase(3); |
maclobdell | 0:cb29c01320b1 | 29 | spif.write_page(buffer, 3); |
maclobdell | 0:cb29c01320b1 | 30 | |
maclobdell | 0:cb29c01320b1 | 31 | char *buffer2 = (char*) malloc(pagesize); |
maclobdell | 0:cb29c01320b1 | 32 | // Read back what was stored |
maclobdell | 0:cb29c01320b1 | 33 | spif.read_page(buffer2, 3); |
maclobdell | 0:cb29c01320b1 | 34 | |
maclobdell | 0:cb29c01320b1 | 35 | printf("%s", buffer2); |
maclobdell | 0:cb29c01320b1 | 36 | |
maclobdell | 0:cb29c01320b1 | 37 | while(1) { |
maclobdell | 0:cb29c01320b1 | 38 | myled = 1; |
maclobdell | 0:cb29c01320b1 | 39 | wait(0.2); |
maclobdell | 0:cb29c01320b1 | 40 | myled = 0; |
maclobdell | 0:cb29c01320b1 | 41 | wait(0.2); |
maclobdell | 0:cb29c01320b1 | 42 | |
maclobdell | 0:cb29c01320b1 | 43 | } |
maclobdell | 0:cb29c01320b1 | 44 | } |