http://www.keil.com/support/man/docs/ulink2/ulink2_su_newalgorithms.htm
Creating New Algorithms
Home ? Setup Flash Programming ? Creating New Algorithms
μVision allows creating Flash programming algorithms forunsupported devices. The algorithm source code is implemented as aμVision project with special compiler and linker settings.
Follow these steps to create a new Flash programmingalgorithm:
- Create a sub-folder in KEILARMFLASH.
- Copy the content from KEILARMFLASH_Template to this new folder.
- Rename the project file NewDevice.uvproj to represent the new Flash ROM device name,for example 29F400.uvproj.
- Open the project with μVision. From the toolbar,use the drop-down Select Target to define the processor architecture:
-
Cortex-M fits for Cortex-M0,M1,M3,and M4 processor-based devices.
-
ARM7/ARM9 fits for ARM7 and ARM9 processor-based devices.
The configuration assumes a little-endian microcontroller. In case of a big-endian microcontroller,select the correct processor core with Project - Options for Target - Device.
- Open the dialog Project - Options for Target - Output and change the content of the field Name of Executable to represent the device,for example 29F400.
- Adapt the programming algorithms in the file FlashPrg.c (see below).
- Adapt the device parameters in the file FlashDev.c (see below).
- Use Project - Build Target to generate the new Flash programming algorithm. The output file,*.FLX - for ARM7/ARM9 devices,or *.FLM - for Cortex-M devices,is copied to the folder KEILARMFLASH and is now available in the dialog Add Flash Programming Algorithm.

Note
- Flash programming algorithms use Read-Only Position Independent and Read-Write Position Independent program code. These options are set in the dialogs Project - Options for Target - C/C++ and Project - Options for Target - Asm.
- The dialog Project - Options for Target - Linker defines the linker scatter file Target.lin. The error L6305 is disabled with --diag_suppress L6305.
FlashPrg.c
The file FlashPrg.c contains the mandatory FlashAlgorithm Functions Init,UnInit,EraseSector,and ProgramPage.Optionally,and in dependency of the device features,or to speed-upexecution,the functions EraseChip,BlankCheck,andVerify can be implemented.
The file provides a #define STACK_SIZE n. Defaultvalue for n is 64. Refer ARM:setting flashloader stack size with STACK_SIZE define not workingfor details.

Note
- Interrupts can be used,but we do not recommend using them.
- Debugging information can be output,but will not be picked up by the debugger. For example: LEDs or UARTs can be used from the code to provide debug information (needs to be interpreted by the user).
FlashDev.c
The file FlashDev.c contains parameterdefinitions for:
- the mandatory Flash Programming Functions Init,and ProgramPage. Depending on the device,the optional Flash Programming Functions EraseChip,and Verify might need programming.
- the FlashDevice structure.
struct FlashDevice const FlashDevice = {
FLASH_DRV_VERS,// Driver Version,do not modify!
"STM32Fxxx High-density Flash",// Device Name (512kB/384kB/256kB)
ONCHIP,// Device Type
0x08000000,// Device Start Address
0x00080000,// Device Size in Bytes (512kB)
1024,// Programming Page Size
0,// Reserved,must be 0
0xFF,// Initial Content of Erased Memory
100,// Program Page Timeout 100 mSec
500,// Erase Sector Timeout 500 mSec
// Specify Size and Address of Sectors
0x0800,0x000000,// Sector Size 2kB (256 Sectors)
SECTOR_END
};
Testing Algorithms
The KEILARMFLASH_TemplateTest folder contains aproject that shows how to test a new Flash Programming Algorithm onbehalf of an STM32 device.