9 years, 5 months ago.

Error with FreescaleIAP code with K22F

Hi again!

I have a K22F platform. I found a problem with the example code of the FreescaleIAP library, i've just done an easier code from this:

--------------

  1. include "mbed.h"
  2. include "FreescaleIAP.h"

int main() { int sizeflash = flash_size(); printf(" Flash Size: %d\r\n",sizeflash);

int sizesector = SECTOR_SIZE; printf("Sector Size: %d\r\n",sizesector);

int address = flash_size() - SECTOR_SIZE; int *data = (int*)address; printf("Address: %d\r\n",address); printf(" \r\n");

printf("Starting\r\n"); erase_sector(address);

int numbers[2] = {5,6}; program_flash(address, (char*)&numbers, 8);

printf("Resulting flash: \r\n"); for (int i = 0; i<2; i++) printf("%d\r\n", data[i]);

printf("Done\r\n\n");

while (true) { } }

--------------

While i'm printing, "data[i]" returns me only "-1" values, whatever are the variables in the array.

I tried to change the bytes i program (4, 8 and 16) but it returns me always "-1".

Which could be the problem?

Thank you!!

Question relating to:

IAP code for Freescale platforms

Please use <<code>> and <</code>> around your code, this makes it easier readable and usable.

posted by Erik - 23 May 2015

#include "mbed.h"
#include "FreescaleIAP.h"
 
 
int main() {
    int sizeflash = flash_size();          
    printf(" Flash Size: %d\r\n",sizeflash);
    
    int sizesector = SECTOR_SIZE;  
    printf("Sector Size: %d\r\n",sizesector);
    
    int address = flash_size() - SECTOR_SIZE; 
    int *data = (int*)address;
    printf("Address: %d\r\n",address);
    printf(" \r\n");
  
    printf("Starting\r\n"); 
    erase_sector(address);
   
   int numbers[2] = {5,6};
   program_flash(address, (char*)&numbers, 8);  
   
   printf("Resulting flash: \r\n");
     for(int i=0;i<2;i++)
        printf("%d\r\n", data[i]);
        
        printf("Done\r\n\n");
        
         while (true) {
    }
}
posted by giacomo amici 23 May 2015

1 Answer

9 years, 5 months ago.

I could have sworn I also used this before on the K22F. But you are correct, it indeed does not work properly, it returns access violation error, which is of the flash controller the "I am not going to run that command but I am not telling you why" response.

But I found the problem, it is in the clock setup of the K22F, and I already checked, the K64F does not suffer from this. I might try to add a workaround in the library itself, but not sure about that yet, since I also don't want it to become too bloated. Something I will look into.

In the meantime for you: You can delete your mbed lib and import mbed-src. Navigate to this file: https://developer.mbed.org/users/mbed_official/code/mbed-src/file/1abac31e188e/targets/cmsis/TARGET_Freescale/TARGET_K22F/system_MK22F51212.h, and switch from clock setup 4 to clock setup 5. This is a bit slower one. The difference is that to reach the speed of clock setup 4 it enters HSRUN mode. In HSRUN mode all write access to flash memory is disabled.

Accepted Answer

Erik, thanks for the time you put in on this bootloader. Looks like the latest mbed lib defaulted the clock back to 4. Works well with the K22.

posted by Jon Buckman 15 Dec 2016

Is it possible to turn down the clock speed while doing the flash write. Then turn it up again? Or do you have to use RUN mode for all time instead of HSRUN mode?

Thanks

posted by Andrew Wade 16 Nov 2017

That is definately possible. I never implemented it, but it only needs to be turned down during flash writing, not permanently.

posted by Erik - 16 Nov 2017