10 years ago.

how to write data into flash in mkl25z

I want to write some data into flash in kl25z, I've tried the function in https://community.freescale.com/message/404121#404121 , but in cw10.5 i can't configer user memory areas and I don't know what should be send to FLASH1Pointer, is there someone know how to do it by using cw ? mbed is ok too.

2 Answers

10 years ago.

I don't know if it will do everything you want, but for writing the KL25s flash from your own program, have a look at: http://mbed.org/users/Sissors/code/FreescaleIAP/ (It should maybe probably work on all freescale targets, but for sure on the KL25z). There is no code to 'reserve' flash area, might add it later, this library, while for the LPC1768/LPC11u24, does show you how you can do it yourself: http://mbed.org/users/okano/code/IAP/file/ff906ad52cf9/IAP.cpp

That said, it is useful if you need an automatic check if the flash is free during compilation, but as long as you write after your program it doesn't really need to be reserved.

Accepted Answer

hi there, very thanks for your help, but if i try to write some hex message to flash like this /media/uploads/senseHuge/2.png the address must be set to 12, when i use keil to debug it , it seems that it will put 2 '00' to the end of '0x10' , I don't know why this happened.

posted by Yanan Zhang 19 May 2014

Better use <<code>> and <</code>> to show code, than others can copy paste it.

How do you mean that the address must be set to 12? flash_size()-SECTOR_SIZE calculates the start of the last sector you have in the flash, so the one most likely to ne available.

Your result is (roughly) correct. Maybe I should add a check for it and return an error message, maybe I should keep the current behavior (please respond what you think is better).

The issue is the length. You can only write flash in blocks of 4 bytes (at least on the KL25). It was intended that if you try to write something which isn't a multiple of 4, it returns an error code, but it seems that never made it in. Now it simply writes to the closest multiple of 4, and does that by continuing reading after the end of the 'numbers variable, which by default is 0 (if you put another variable right after 'numbers' it will write that one).

So you need to write in multiples of 4 bytes. I can either make it refuse to write another length, or I can just keep it similar to this, where it adds dummy bytes to get it to a multiple of 4 bytes (I am leaning to that one).

That said, does it matter for you that it puts 2 times 0x00 at the end? It stores your 10 variables, at interfalls of 12, as you told it to do. What it does in between shouldn't matter I think?

posted by Erik - 19 May 2014

I put 2 '00' at the end finally, however, if i do it just like above, it has no problems. Another question is if i don't erase the flash at the beginning, theoretically the data will be stored eternally when the power went out. If i want to read the data and store it in a .txt file in my pc, how should i do it? it seems that if i flash a new project to the board to store the data to a .txt file, the data stored before will be erased this is my write function

#include "mbed.h"
#include "FreescaleIAP.h"

DigitalOut myled(LED_GREEN);
Serial pc(USBTX, USBRX);

int main()
{
    int i = 0;
    pc.baud(19200);
    int address = flash_size() - SECTOR_SIZE;           //Write in last sector
    char *data = (char*)address;
    char in[10] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10};
//    erase_sector(address);//don't erase old data

    while(true) {
        if(pc.readable()) {
            char c = pc.getc();//char or int is all right
//            pc.putc(c);
            if(c == (0x84)) {
                for(int k=0; k<10; k++) {
                    in[k] = c;
                    if(k!=9)
                        c = pc.getc();
                }
                for(int k =0; k<10; k++)
                    pc.putc(in[k]);
                program_flash(address+12*i, (char*)&in, 10);
                i+=1;
            }
        }
    }

}

received data are 84 A9 61 00 03 20 3F 00 06 00 84 A9 61 00 03 20 3F 00 06 01 84 A9 61 00 03 20 3F 00 06 01 84 A9 61 00 03 20 3F 00 06 01 which is predefined, and when i try to read it

#include "mbed.h"
#include "FreescaleIAP.h"
DigitalOut myled(LED_GREEN);
Serial pc(USBTX, USBRX);

int main()
{
    pc.baud(19200);
    int i = 0;
//    pc.printf("\nHello World!\n");

    int address = flash_size() - SECTOR_SIZE;           //Write in last sector
    char *data = (char*)address;
    for(i = 0; i<10; i++)
        pc.putc(data[i]);

}

it's not 84 A9 61 00 03 20 3F 00 06 00

posted by Yanan Zhang 19 May 2014

If you don't erase the flash data should stay there forever yes. (Within reason obviously). And you are correct about flashing a new file, I noticed the same behavior, apparantly it erases the complete flash (no idea why it does so, but not much I can do about it). I guess what works is simply sending the command to your board in another way that it should send the data to your PC. You could for example make an interrupt pin to do it. Or make it respond on a command from your PC via Serial to send the data back.

So yeah, it would be nice if it stayed when you upload a new program, but I guess the programmer uses the mass erase command to completely erase all data from flash. There is nothing I can do about it from my library, and integrating the send-to-pc function in your regular code is the only option.

posted by Erik - 19 May 2014
10 years ago.

What data are you trying to write to KL25Z? There's cmsis-dap repository on github, under mbedmicro. there are flash algo for kinetis devices, which you can use. Link https://github.com/mbedmicro/CMSIS-DAP/

For CW and PE questions, use Freescale community, you will get a proper answer here.

Regards,
0xc0170

I'm trying to write some hex message to it

posted by Yanan Zhang 19 May 2014