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); }
History
Disabled debug
2016-03-16, by Sissors [Wed, 16 Mar 2016 20:20:17 +0000] rev 11
Disabled debug
Whoops, was per 16-byte, not 8-byte
2016-03-16, by Sissors [Wed, 16 Mar 2016 20:19:18 +0000] rev 10
Whoops, was per 16-byte, not 8-byte
Make verify erased work for writes of less than alignment size.
2016-03-16, by Sissors [Wed, 16 Mar 2016 20:09:08 +0000] rev 9
Make verify erased work for writes of less than alignment size.
Added Teensy in defines.
2015-10-18, by Sissors [Sun, 18 Oct 2015 17:03:52 +0000] rev 8
Added Teensy in defines.
Only compile when Freescale device is used, this makes it possible to integrate it within code which also needs to work on other platforms.
2015-06-22, by Sissors [Mon, 22 Jun 2015 12:15:47 +0000] rev 7
Only compile when Freescale device is used, this makes it possible to integrate it within code which also needs to work on other platforms.
Modification of FRDM-K64F ; add ProgramPhrase FCMD;
2014-09-30, by nyatla [Tue, 30 Sep 2014 14:00:43 +0000] rev 6
Modification of FRDM-K64F ; add ProgramPhrase FCMD;
Correct #if defined() in .h file for header size, added K22F (which does not work as of yet)
2014-09-30, by Sissors [Tue, 30 Sep 2014 13:18:52 +0000] rev 5
Correct #if defined() in .h file for header size, added K22F (which does not work as of yet)
Disable IRQs during flash commands
2014-07-18, by Sissors [Fri, 18 Jul 2014 20:18:21 +0000] rev 4
Disable IRQs during flash commands
Added K20D50M support
2014-07-18, by Sissors [Fri, 18 Jul 2014 06:17:00 +0000] rev 3
Added K20D50M support
Added flash_size function
2014-05-12, by Sissors [Mon, 12 May 2014 19:45:04 +0000] rev 2
Added flash_size function