IAP code for Freescale platforms
Dependents: 18_PT1000 RDA5807M-FM-Radio flashaccess TF_conops_BAEFLAGIMAN ... more
K22F
Due to the default clock setup of the K22F, flash write access is there disabled. In the future I might add a workaround, but for now see: https://developer.mbed.org/questions/52738/Error-with-FreescaleIAP-code-with-K22F/
Be careful with which flash you are erasing/overwriting!
Example code:
#include "mbed.h" #include "FreescaleIAP.h" int main() { int address = flash_size() - SECTOR_SIZE; //Write in last sector int *data = (int*)address; printf("Starting\r\n"); erase_sector(address); int numbers[10] = {0, 1, 10, 100, 1000, 10000, 1000000, 10000000, 100000000, 1000000000}; program_flash(address, (char*)&numbers, 40); //10 integers of 4 bytes each: 40 bytes length printf("Resulting flash: \r\n"); for (int i = 0; i<10; i++) printf("%d\r\n", data[i]); printf("Done\r\n\n"); while (true) { } }
For an example on using this for a bootloader, check out: http://developer.mbed.org/users/Sissors/code/Bootloader_K64F/
If you want to permanently store a variable between resets, you can run into the problem of how to define the value the first time. Since the mbed drag-and-drop loader seems to issue a full-chip erase, you cannot first upload a program to set the initial value, and then switch to the regular program: The full-chip erase will also erase your initial value. One option is to use the same statements as used in the bootloader example to force it to program initial values for your variables on your memory address. This should work fine, however it is target dependent where you want to program it (generally your last sector), so it makes for a less nice example program. You can also try to detect if it is the initial run by looking at the state of the flash, by default this is all '1's. The following example does this:
#include "mbed.h" #include "FreescaleIAP.h" int main() { int address = flash_size() - SECTOR_SIZE; //Write in last sector int *data = (int*)address; //By default flash is initialized at 0xFF, this is signed -1, so now we know //the program runs for the first time. You of course need to make sure your program //never writes -1 to this variable if you use this method //Alternatively you could also do the same, but with a seperate "initial run" variable added, //so your other variables can take any value if (data[0] == -1) { printf("Initial run\r\n"); printf("Writing 42 and 42\r\n"); erase_sector(address); int newvalues[2] = {42, 42}; program_flash(address,(char*) newvalues, 8); //Two integers of 4 bytes = 8 bytes while(1); } printf("Current = %d and %d, new is %d and %d\r\n", data[0], data[1], data[0]+1, data[1]-1); int newvalues[2] = {data[0]+1, data[1]-1}; erase_sector(address); program_flash(address, (char*) newvalues, 8); while(1); }
Revision 2:70ade1638644, committed 2014-05-12
- Comitter:
- Sissors
- Date:
- Mon May 12 19:45:04 2014 +0000
- Parent:
- 1:702fd2d53c17
- Child:
- 3:0f127a8ba79b
- Commit message:
- Added flash_size function
Changed in this revision
FreescaleIAP.cpp | Show annotated file Show diff for this revision Revisions of this file |
FreescaleIAP.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/FreescaleIAP.cpp Sun May 11 13:01:44 2014 +0000 +++ b/FreescaleIAP.cpp Mon May 12 19:45:04 2014 +0000 @@ -1,6 +1,6 @@ #include "FreescaleIAP.h" -#define IAPDEBUG +//#define IAPDEBUG enum FCMD { Read1s = 0x01, @@ -61,6 +61,13 @@ return Success; } +uint32_t flash_size(void) { + uint32_t retval = (SIM->FCFG2 & 0x7F000000u) >> (24-13); + if (SIM->FCFG2 & (1<<23)) //Possible second flash bank + retval += (SIM->FCFG2 & 0x007F0000u) >> (16-13); + return retval; +} + IAPCode program_word(int address, char *data) { #ifdef IAPDEBUG printf("IAP: Programming word at %x, %d - %d - %d - %d\r\n", address, data[0], data[1], data[2], data[3]); @@ -96,8 +103,8 @@ /* Check if no flash boundary is violated Returns true on violation */ bool check_boundary(int address, unsigned int length) { - address &= ~1023; - int temp = (address+length - 1) & (~1023); + int temp = (address+length - 1) / SECTOR_SIZE; + address /= SECTOR_SIZE; bool retval = (address != temp); #ifdef IAPDEBUG if (retval)
--- a/FreescaleIAP.h Sun May 11 13:01:44 2014 +0000 +++ b/FreescaleIAP.h Mon May 12 19:45:04 2014 +0000 @@ -3,6 +3,15 @@ #include "mbed.h" +#ifdef TARGET_KLXX +#define SECTOR_SIZE 1024 +#elif TARGET_K20D5M +#define SECTOR_SIZE 2048 +#elif TARGET_K64F +#define SECTOR_SIZE 4096 +#else +#define SECTOR_SIZE 1024 +#endif enum IAPCode { BoundaryError = -99, //Commands may not span several sectors @@ -16,7 +25,34 @@ Success = 0 }; +/** Erase a flash sector + * + * The size erased depends on the used device + * + * @param address address in the sector which needs to be erased + * @param return Success if no errors were encountered, otherwise one of the error states + */ IAPCode erase_sector(int address); + +/** Program flash + * + * Before programming the used area needs to be erased. The erase state is checked + * before programming, and will return an error if not erased. + * + * @param address starting address where the data needs to be programmed (must be longword alligned: two LSBs must be zero) + * @param data pointer to array with the data to program + * @param length number of bytes to program (must be a multiple of 4) + * @param return Success if no errors were encountered, otherwise one of the error states + */ IAPCode program_flash(int address, char *data, unsigned int length); +/** + * Returns size of flash memory + * + * This is the first address which is not flash + * + * @param return length of flash memory in bytes + */ +uint32_t flash_size(void); + #endif \ No newline at end of file