Kinetis over the air firmware update

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Kinetis over the air firmware update

Jump to solution
2,654 Views
oscargarciaabad
Contributor III

Hi all,

I'm planning to update the firmware in  my custom board from a file located in a FTP server.

My custom board has a Kinetis K60FN1M0VLQ12 microcontroller. I know I can do that with the swap option, but I don't know which

is the executable format I must upload to the FTP server. Right now I only have an elf file and this is too large to fit in the flash memory.

Thanks in advance.

Óscar.

Labels (1)
1 Solution
1,136 Views
mjbcswitzerland
Specialist V

Óscar

Typically a binary file will be used (in some cases encrypted). Depending on which build tool you use you can configure it to generate a binary output, otherwise you can use objcopy [objcopy - GNU Binary Utilities], eg.

arm-none-eabi-objcopy --output-target=binary sw.elf sw.bin

Regards

Mark

View solution in original post

0 Kudos
12 Replies
1,136 Views
mayursavaj
Contributor III

Hi Oscar,

I want to upgrade my Firmware Over Air. We are using MK22FX512VLL controller.

Can you help me? I am new in Kinetics Series (Freescale).

Thanks,

Mayur Savaj

0 Kudos
1,136 Views
oscargarciaabad
Contributor III

Hi Mayur,

Take a look at AN4533. Are you using MQX or are you working on bare-metal? Can you please be a little more specific? What are your doubts?

Regards,

Oscar.

0 Kudos
1,136 Views
kurtjordan
Contributor I

I have a similar question. Using the same part in freertos9. using an external WiFi module and AWS with MQTT to communicate to the outside world.

0 Kudos
1,137 Views
mjbcswitzerland
Specialist V

Óscar

Typically a binary file will be used (in some cases encrypted). Depending on which build tool you use you can configure it to generate a binary output, otherwise you can use objcopy [objcopy - GNU Binary Utilities], eg.

arm-none-eabi-objcopy --output-target=binary sw.elf sw.bin

Regards

Mark

0 Kudos
1,136 Views
oscargarciaabad
Contributor III

Hi Mark,

Indeed I'm using gcc. Thanks a lot for the answer!!

Regards.

Óscar.

0 Kudos
1,136 Views
oscargarciaabad
Contributor III

Hi all,

I converted my elf file to a bin file and put it in an FTP server. I'm trying to download this file to swap file 1 under MQX 4.1 the next way:

/************************************************************************************/

void FirmwareUpdateTask(uint32_t PrivateData) {

   

    void *FTPHandle;

    int32_t Response;

    _ip_address ServerAddress;

    MQX_FILE_PTR SwapFile;

    uint32_t IOCTLParam;

    char Buffer[10];

   

    // set server address

    printf("This is firmwareUpdateTask speaking\n");

    memset(&ServerAddress, 0, sizeof(ServerAddress));

    ServerAddress = IPADDR(192, 168, 1, 25);

    //Open Swap file

    SwapFile = fopen("flashx:swap1", NULL);

    if(SwapFile == NULL) {

        printf("KO\n");

        _task_block();

    }

    else {

        printf("OK\n");

    }

   

    /* Enable sector cache */

    ioctl(SwapFile, FLASH_IOCTL_ENABLE_SECTOR_CACHE, NULL);

    printf("\nFlash sector cache enabled\n");

    /* Unprotecting the the FLASH might be required */

    IOCTLParam = 0;

    ioctl(SwapFile, FLASH_IOCTL_WRITE_PROTECT, &IOCTLParam);

    fseek(SwapFile, 0, IO_SEEK_SET);

    //open ftp session

    Response = FTP_open(&FTPHandle, ServerAddress, stdout);

    if (Response == -1) {

        printf("Couldn't open FTP session\n");

        return;

    } /* Endif */

    printf("FTP session opened\n");

    Response = FTP_command(FTPHandle, "USER user\r\n", stdout);

    /* Response 3xx means Password Required */

    if ((Response >= 300) && (Response < 400)) {

        printf("sending password\n");

        Response = FTP_command(FTPHandle, "PASS pass\r\n", stdout);

    } /* Endif */

    /* Response 2xx means Logged In */

    if ((Response >= 200) && (Response < 300)) {

        printf("Logged in\n");

        Response = FTP_command(FTPHandle, "CWD /ogarcia\r\n", stdout);

        printf("Response1 = %ld\n", Response);

        Response = FTP_command(FTPHandle, "TYPE I\r\n", stdout);

        printf("Response2 = %ld\n", Response);

        Response = FTP_command_data(FTPHandle, "RETR CptWifiNode.bin\r\n", stdout, SwapFile, FTPMODE_PORT | FTPDIR_RECV);

    } /* Endif */

    else {

        printf("Not logged in\n");

    }   

    FTP_close(FTPHandle, stdout);

    fclose(SwapFile);

    printf("Rebooting... \n");

    ioctl(SwapFile, FLASH_IOCTL_SWAP_FLASH_AND_RESET, NULL);

/*******************************************************************/

After reset my application has not been updated, and the old firmware runs again.

If I make

Response = FTP_command_data(FTPHandle, "RETR CptWifiNode.bin\r\n", stdout, stdout, FTPMODE_PORT | FTPDIR_RECV);

I can see the content of the file I'm downloading printed over the uart.

My binary file is 520416 bytes long and the swap block is 524288 bytes lonk (512 kB) Is this ok?

What I'm doing wrong with the swap?

Thanks in advance.

Óscar.

0 Kudos
1,136 Views
mjbcswitzerland
Specialist V

Hi

Check with a debugger to see whether the new code has been correctly saved to the second program flash area. If this is the case the swap commands will presumably require investigating. Ensure that the swap commands are executed from SRAM and that interrupts are disabled during this work.

Your code size is very close to the limit of swap operation so any code growth would possibly result in the technique from no longer being usable.

Note that in some cases a small boot loader may be a better choice that the swap technique. The uTasker project supports the following (for comparison):

- 2k encrypted boot loader allowing encrypted FTP uploading to be used (the FTP transfer is still managed by the original application and this doesn't need to be aware of the encrypted content) which doesn't need swap block support but enables encrypted data to be used and is still fail-safe

- as an alternative to internal intermediate storage it can be used together with cheap SPI Flash via SPI to allow SW uploads of almost full program flash size to be performed (about 1Meg in your case)

- a standalone FTP client boot loader can be configured (<20k space requirement) so that the board can update code from an FTP server without any intermediate storage space requirements (about 1004k possible in your case without any external storage space requirements).

- the uTasker loaders can also be used for applications that are not based on uTasker.

- the complete operation can be simulated in the uTasker simulator which allows simplifed testing and debugging, as well as support and remote error anaylsis

Regards

Mark

1,136 Views
oscargarciaabad
Contributor III

Hi Mark,

I have been doing some research and I found that I can download a little txt file to flashx:swap1 with no problems The problem appears when I want to download my binary file.

When I see the size of this file on windows it says the file is 520.416 bytes long, and this file is using 524.288 bytes wich actually is 2^19 bytes long. I managed to generate the binary file with codewarrior

And the result is the same.

I took a look to the swap file definition and I found this:

{ "swap1", BSP_INTERNAL_FLASH_BASE + (BSP_INTERNAL_FLASH_SIZE / 2), BSP_INTERNAL_FLASH_BASE + (BSP_INTERNAL_FLASH_SIZE) - (1 + BSP_INTERNAL_FLASH_SECTOR_SIZE) },

Which means that:

Swap1 start address = 0x00080000 (524288 dec)

Swap1 end address = 0x000FEFFF (1044479 dec)

Swap1 size = 0x0007F000 (520192 dec)

As you can see swap1 size is smaller than my binary file.

This is because last sector in MQX of the swap is used for swap flash indicators as you can see in the last message of this post:

https://community.freescale.com/message/374804#374804

On the other hand, size output is the following:

text data bss dec hex filename

171532 584 832 172948 2a394 CptWifiNode.bin

Why a file 172.948 bytes long ends in binary file 520.416 bytes long?

And how can I change the binary size to fit in the swap file?

Regards,

Óscar.

De: Mark Butcher

Enviado el: martes, 20 de mayo de 2014 17:55

Para: Oscar Garcia Abat

Asunto: Re: - Kinetis over the air firmware update

<https://community.freescale.com/>

Kinetis over the air firmware update

reply from Mark Butcher<https://community.freescale.com/people/mjbcswitzerland?et=watches.email.thread> in Kinetis Microcontrollers - View the full discussion<https://community.freescale.com/message/404682?et=watches.email.thread#404682>

0 Kudos
1,136 Views
mjbcswitzerland
Specialist V

Óscar

It sounds as though the binary conversion may not be correct (converted from the ELF file).

The ELF data contains debug and other infomration and is typically several times larger than the actual binary content.

- Compare the sizes of your ELF and binary files - if they are the same there is something going wrong with the conversion and you are just getting the same output as input

Another possible explanation is padding:

- Open the binary file with a binary editor and look through it (if it ELF format you will see the string "ELF" somewhere at the beginning). Check whether there are large blocks with 0xff; this could be the case when there is some data linked at a specific location.

Eg. if you have a small code at 0x00000000..0x00002000 (8k) but have forced consts to be at 0x7fc00 (say 10 bytes) the resulting binary will consist of

Program code (0x00000000..0x00002000 - 8kBytes)

Padding (503kBytes)

Const (10 bytes)

============

Total 512kBytes

Regards

Mark

0 Kudos
1,136 Views
oscargarciaabad
Contributor III

Hi Mark,

The elf files is about 70 MB in size. I made some findings:

- My code is using last 4KB flash sector for user data (Set up parameters for my device), I moved that data

To the previous 4KB block. Now the binary fits in the flash memory (Remember that block is used for swap indicators).

- FTP download is working well. I’m able to download the file to swap1 and print its content after the download.

The data is the same I can see in the binary file (checked with an hex viewer).

- After swap and reboot the swap is not done (Te downloaded file is still in swap1 rather in swap0).

As you can see the only part that is failing is the swap process. I’m going to take a look on swap process and as soon I make my findings I will let you know.

Thanks a lot for your help.

Best Regards.

Óscar.

0 Kudos
1,136 Views
mjbcswitzerland
Specialist V

Hi Óscar

OK - that would explain things.

A couple of points:

1. If you remove the parameters from the linker script and address them using pointers in the code (and flash routines to erase/program) then you will be able to reduce the code size to the real code size alone.

2. The swap mechanism will mean that your old parameters (when not already overwritten) are also swapped to a new location. This means that SW uploads will probaby result in parameter loss, which may be undesirable. I would recommend ensuring that the parameters don't get deleted and are recovered, or else to use a small boot loader rather than the swap mechanism. By avoiding the swap you also make the code more portable in case you decide to use a device with FlexNVM later, which would otherwise not be able to work.

Regards

Mark

0 Kudos
1,136 Views
oscargarciaabad
Contributor III

Hi Mark,

Te swap is already working. I made a HUGE mistake!

I closed the SwapFile descriptor before performing the swap!

Many thanks for your help.

Best regards.

Óscar.

0 Kudos