EmbeddedArtists AB
/
lpc812_exp_solution_spi-e2prom
Solutions for the SPI E2PROM experiments for LPC812 MAX
main.cpp@1:819f21483c01, 2013-11-28 (annotated)
- Committer:
- embeddedartists
- Date:
- Thu Nov 28 12:17:45 2013 +0000
- Revision:
- 1:819f21483c01
- Parent:
- 0:aa38ad0bf51d
Renamed the SPI24LC080 class to SPI25LC080 as it is an interface to the 25LC080 chip.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:aa38ad0bf51d | 1 | #include "mbed.h" |
embeddedartists | 1:819f21483c01 | 2 | #include "SPI25LC080.h" |
embeddedartists | 0:aa38ad0bf51d | 3 | |
embeddedartists | 0:aa38ad0bf51d | 4 | Serial pc(USBTX, USBRX); |
embeddedartists | 0:aa38ad0bf51d | 5 | |
embeddedartists | 1:819f21483c01 | 6 | SPI25LC080 e2prom; |
embeddedartists | 0:aa38ad0bf51d | 7 | |
embeddedartists | 0:aa38ad0bf51d | 8 | #define E2PROM_START_ADDR 0 |
embeddedartists | 0:aa38ad0bf51d | 9 | #define BUF_SIZE 20 |
embeddedartists | 0:aa38ad0bf51d | 10 | |
embeddedartists | 0:aa38ad0bf51d | 11 | static void experiment2() |
embeddedartists | 0:aa38ad0bf51d | 12 | { |
embeddedartists | 0:aa38ad0bf51d | 13 | uint8_t buf[BUF_SIZE]; |
embeddedartists | 0:aa38ad0bf51d | 14 | char* magic = "Magic text - 654321"; |
embeddedartists | 0:aa38ad0bf51d | 15 | |
embeddedartists | 0:aa38ad0bf51d | 16 | pc.printf("Starting E2PROM test...\n"); |
embeddedartists | 0:aa38ad0bf51d | 17 | |
embeddedartists | 0:aa38ad0bf51d | 18 | memset(buf, 0, BUF_SIZE); |
embeddedartists | 0:aa38ad0bf51d | 19 | |
embeddedartists | 0:aa38ad0bf51d | 20 | //read from spi e2prom |
embeddedartists | 0:aa38ad0bf51d | 21 | e2prom.read(E2PROM_START_ADDR, buf, BUF_SIZE); |
embeddedartists | 0:aa38ad0bf51d | 22 | |
embeddedartists | 0:aa38ad0bf51d | 23 | //write read bytes on console |
embeddedartists | 0:aa38ad0bf51d | 24 | buf[19] = 0; //null terminate string in case it is missing |
embeddedartists | 0:aa38ad0bf51d | 25 | pc.printf("String in memory: '%s'\n", buf); |
embeddedartists | 0:aa38ad0bf51d | 26 | |
embeddedartists | 0:aa38ad0bf51d | 27 | //check if expected string |
embeddedartists | 0:aa38ad0bf51d | 28 | if (memcmp(buf, magic, strlen(magic)) != 0) { |
embeddedartists | 0:aa38ad0bf51d | 29 | memcpy(buf, magic, strlen(magic)); |
embeddedartists | 0:aa38ad0bf51d | 30 | |
embeddedartists | 0:aa38ad0bf51d | 31 | //write the correct string in memory |
embeddedartists | 0:aa38ad0bf51d | 32 | e2prom.write(E2PROM_START_ADDR, buf, 20); |
embeddedartists | 0:aa38ad0bf51d | 33 | |
embeddedartists | 0:aa38ad0bf51d | 34 | pc.printf("String has been written!"); |
embeddedartists | 0:aa38ad0bf51d | 35 | } |
embeddedartists | 0:aa38ad0bf51d | 36 | |
embeddedartists | 0:aa38ad0bf51d | 37 | printf("\nDone!"); |
embeddedartists | 0:aa38ad0bf51d | 38 | while(1) |
embeddedartists | 0:aa38ad0bf51d | 39 | ; |
embeddedartists | 0:aa38ad0bf51d | 40 | } |
embeddedartists | 0:aa38ad0bf51d | 41 | |
embeddedartists | 0:aa38ad0bf51d | 42 | int main() |
embeddedartists | 0:aa38ad0bf51d | 43 | { |
embeddedartists | 0:aa38ad0bf51d | 44 | experiment2(); // E2PROM |
embeddedartists | 0:aa38ad0bf51d | 45 | } |