Programming block 0 of Kinetis K20

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

Programming block 0 of Kinetis K20

964 Views
wolfgangdietz
Contributor I

I have to replace a bootloader (in-application!) placed in the first 1k of the first sector of a K20-flash.  It does not work with the ProcessorExpert libs (I still have to use) without colliding with the flash configuration fields. Programming of  all other flash works as planned.

Can anybody give me a workflow how to flash the first sector?

Thanks in advance

Wolfgang

Labels (1)
0 Kudos
Reply
4 Replies

884 Views
mjbcswitzerland
Specialist V

Hi


I have used the following code in K20 (and K66) in-place boot-loader updating in uTasker based products. I am not sure what problems you have but this is a reference that has been used successfully and you may be able to replace its flash calls with whatever you have to do the same.

Beware that updating the first sector is risky since a power cycle during the process will leave the device in a state that can't recover.

Regards

Mark

Reference instructions - It can be called at any time but generally immediately after starting.

#include "serialArray.h"

static void fnUpdateSerialLoader(void)
{
    if (memcmp(serial_loader, fnGetFlashAdd((unsigned char *)0x00000000), sizeof(serial_loader)) != 0) { // if the loader in Flash is different to the one embedded in code
        unsigned char *ptrFlashDestination;
        unsigned char *ptrSerialLoader = serial_loader;
        if (memcmp(serial_loader, fnGetFlashAdd((unsigned char *)0x00000000), _FLASH_GRANULARITY) != 0) { // check whether the first sector requires modifying
            _fnEraseFlashSector((unsigned char *)0, _FLASH_GRANULARITY);  // delete first sector
            _fnWriteBytesFlash((unsigned char *)0x400, &serial_loader[0x400], 16); // program critical flash configuration
            _fnWriteBytesFlash((unsigned char *)0, &serial_loader[0], 0x400); // program start
            _fnWriteBytesFlash((unsigned char *)0x410, &serial_loader[0x410], (_FLASH_GRANULARITY - 0x410)); // program end
        }
        ptrFlashDestination = (unsigned char *)_FLASH_GRANULARITY;
        ptrSerialLoader += _FLASH_GRANULARITY;                           // after critical first sector has been programmed continue with the rest
        while (ptrFlashDestination < (unsigned char *)(4 * 1024)) {      // for each sector in the serial loader area
            _fnEraseFlashSector(ptrFlashDestination, _FLASH_GRANULARITY);// erase all boot loader sectors
            _fnWriteBytesFlash(ptrFlashDestination, ptrSerialLoader, _FLASH_GRANULARITY); // program sector
            ptrSerialLoader += _FLASH_GRANULARITY;
            ptrFlashDestination += _FLASH_GRANULARITY;
        }
    }
}

1. serialArray.h contains the new loader code to program:

serialArray.h can be created from the serial loader's binary file using

uTaskerFileCreate serialLoader.bin serialArray.h serial_loader

It assumes the input file is called serialLoader.bin
The output file is serialArray.h
and in this file an array is created

extern unsigned char serial_loader[] = {
    0xXX, 0xYY, ... // filled out by conversion program
    ....
};

Details of the conversion program at http://www.utasker.com/forum/index.php?topic=1445.0

0 Kudos
Reply

884 Views
wolfgangdietz
Contributor I

Hi Mark,

many thanks for your fast response.

It really seems to be quite easy, but unfortunately it doesn´t work.

Your three step solution shows the same results as my one step approach: Nothing :smileyalert: has been flashed and the readout protection of the K20 is set afterwards.

As far as I could see the Flash configuration fields and the Flash Security Register, FTFE_FSEC, FTFE_FPROT etc, are set correctly (no write protection etc.) before starting and after programming step two (config fields) though nothing has been flashed.

Is there any other FTFE-reg I have to configure before?

Note: I still use (have to us) the PE flash components Flash1_Write etc. which worked perfectly in the past.

Regards

Wolfgang

Von: mjbcswitzerland <admin@community.nxp.com>

Gesendet: Montag, 4. November 2019 23:26

An: Wolfgang Dietz <w.dietz@medatec.com>

Betreff: Re: - Re: Programming block 0 of Kinetis K20

NXP Community <https://community.freescale.com/resources/statics/1000/35400-NXP-Community-Email-banner-600x75.jpg>

Re: Programming block 0 of Kinetis K20

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

0 Kudos
Reply

884 Views
mjbcswitzerland
Specialist V

Wolfgang

If the processor is secured after the operation it means that the flash configuration (from 0x400) is programmed with 0xff 0xff, ..

This happens when the first sector is erased but nothing is written before a reset is performed.

This usually happens when there is a 'crash' after the initial erase operation, which can be due to - for example - watchdog timeout taking place or interrupt not being disabled (and vectors in the first sector being required to handle them).

You should be able to use normal debug techniques to find out what happens after the erase is performed and before the new code is written.

Regards

Mark

0 Kudos
Reply

884 Views
wolfgangdietz
Contributor I

Hi Mark,

thanks to your tips I found the cause of the problems, even thought I could not yet clarify the background of the behavior due to time problems.

I used to test the steps by in-circuit-debugging and the procedure crashed always, probably by the debugger-internal interrupt-processing.

It works perfectly running the application without debugger.

Once again thanks!

Wolfgang

Von: mjbcswitzerland <admin@community.nxp.com>

Gesendet: Dienstag, 5. November 2019 19:41

An: Wolfgang Dietz <w.dietz@medatec.com>

Betreff: Re: - Re: Programming block 0 of Kinetis K20

NXP Community <https://community.freescale.com/resources/statics/1000/35400-NXP-Community-Email-banner-600x75.jpg>

Re: Programming block 0 of Kinetis K20

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

0 Kudos
Reply