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