ec521
flashwrite.cpp@0:e2f78ce2a5cf, 2018-04-22 (annotated)
- Committer:
- gaving
- Date:
- Sun Apr 22 19:20:10 2018 +0000
- Revision:
- 0:e2f78ce2a5cf
For EC521 only
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gaving | 0:e2f78ce2a5cf | 1 | #include "mbed.h" |
gaving | 0:e2f78ce2a5cf | 2 | #include "FreescaleIAP.h" |
gaving | 0:e2f78ce2a5cf | 3 | |
gaving | 0:e2f78ce2a5cf | 4 | int flashwrite() { |
gaving | 0:e2f78ce2a5cf | 5 | |
gaving | 0:e2f78ce2a5cf | 6 | int address = flash_size() - SECTOR_SIZE; //Write in last sector |
gaving | 0:e2f78ce2a5cf | 7 | int *data = (int*)address; |
gaving | 0:e2f78ce2a5cf | 8 | |
gaving | 0:e2f78ce2a5cf | 9 | //By default flash is initialized at 0xFF, this is signed -1, so now we know |
gaving | 0:e2f78ce2a5cf | 10 | //the program runs for the first time. You of course need to make sure your program |
gaving | 0:e2f78ce2a5cf | 11 | //never writes -1 to this variable if you use this method |
gaving | 0:e2f78ce2a5cf | 12 | |
gaving | 0:e2f78ce2a5cf | 13 | //Alternatively you could also do the same, but with a seperate "initial run" variable added, |
gaving | 0:e2f78ce2a5cf | 14 | //so your other variables can take any value |
gaving | 0:e2f78ce2a5cf | 15 | if (data[0] == -1) { |
gaving | 0:e2f78ce2a5cf | 16 | printf("Initial run\r\n"); |
gaving | 0:e2f78ce2a5cf | 17 | printf("Writing 42 and 42\r\n"); |
gaving | 0:e2f78ce2a5cf | 18 | erase_sector(address); |
gaving | 0:e2f78ce2a5cf | 19 | int newvalues[2] = {42, 42}; |
gaving | 0:e2f78ce2a5cf | 20 | program_flash(address,(char*) newvalues, 8); //Two integers of 4 bytes = 8 bytes |
gaving | 0:e2f78ce2a5cf | 21 | while(1); |
gaving | 0:e2f78ce2a5cf | 22 | } |
gaving | 0:e2f78ce2a5cf | 23 | printf("Current = %d and %d, new is %d and %d\r\n", data[0], data[1], data[0]+1, data[1]-1); |
gaving | 0:e2f78ce2a5cf | 24 | int newvalues[2] = {data[0]+1, data[1]-1}; |
gaving | 0:e2f78ce2a5cf | 25 | erase_sector(address); |
gaving | 0:e2f78ce2a5cf | 26 | program_flash(address, (char*) newvalues, 8); |
gaving | 0:e2f78ce2a5cf | 27 | while(1); |
gaving | 0:e2f78ce2a5cf | 28 | } |