CMSIS-Pack: Flash Programming

| CMSIS-Pack Version 1.5.0 Delivery Mechanism for Software Packs

- Main Page
- Usage and Description

All Pages
Flash Programming

Flash Programming Algorithms are a piece of software to erase or download applications to Flash devices. A Pack with Device Support usually contains predefined Flash algorithms for programming the devices that are supported by the DFP. A template for creating new algorithms are available in the ARM:CMSIS Pack. The following section describes the process in more detail.

# Creating a new Algorithm

Flash programming algorithms are defined with functions to erase and program the Flash device. Special compiler and linker settings are required. Follow these steps to create and configure a new Flash programming algorithm:

1. Copy the content from the ARM:CMSIS Pack folder (usually C:\Keil\ARM\Pack\ARM\CMSIS\version\Device\_Template_Flash) to a new folder.
2. Rename the project file NewDevice.uvproj to represent the new Flash ROM device name, for example MyDevice.uvproj.
3. Open the project with uVision. From the toolbar, use the drop-down Select Target to define the processor architecture. Cortex-M fits for all Cortex-M0/M0+, M3, and M4 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.
4. Open the dialog Project - Options for Target - Output and change the content of the field Name of Executable to represent the device, for example MyDevice.
5. Adapt the programming algorithms in the file FlashPrg.
6. Adapt the device parameters in the file FlashDev.
7. Use Project - Build Target to generate the new Flash programming algorithm. The output file (for example MyDevice.FLM) has to be added to the DFP.

Note
  - Creating a Flash programming algorithm with MDK-Lite is not supported.
  - 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`.
  - The Flash Algorithm Functions section contains reference for all the available functions.

## FlashPrg.c

The file FlashPrg.c contains the mandatory Flash programming functions Init, UnInit, EraseSector, and ProgramPage. Optionally, depending on the device features (or to speed-up execution), the functions EraseChip, BlankCheck, and Verify can be implemented.

## FlashDev.c

The file FlashDev.c contains parameter definitions for:

1. the Flash programming functions.
2. 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
   };

Note
  The Device Name will usually be shown in tools to identify the Flash algorithm. Make sure that this is changed to the correct device name from the DFP.

## Testing Algorithms

The _Template_Flash\Test-folder contains a project that shows how to test a new Flash programming algorithm on behalf of a STM32F10x device. Adapt the code to your needs.

# Adding an Algorithm to a Pack

The generated *.FLM file needs to be added to the Pack with Device Support, so that it is available to the tool user for programming his device. Usually, a directory Flash is created and the algorithm is saved in this directory.

The algorithm is specified within the the /package/devices/family level:

<family Dfamily="STM32F4" Dvendor="STMicroelectronics:13">
...
<algorithm name="Flash/STM32F2xx_512.flm" start=0x08000000 size=0x10000 default="1"/> <!-- valid for all devices of the family -->
<subFamily DsubFamily="STM32F405">
<algorithm name="Flash/STM32F2xx_1024.flm" start=0x08000000 size=0x20000 default="1"/> <!-- valid for all devices of a subFamily -->
<device Dname="STM32F405OE">
<algorithm name="Flash/STM32F2xx_2048.flm" start=0x08000000 size=0x40000 default="1"/> <!-- finally, this is the default for the device -->
</device>
...
</family>

The argument start specifies the base address for the Flash programming algorithm.

The argument size specifies the size covered by the Flash programming algorithm. End address = start + size - 1.

The argument default specifies whether a Flash programming algorithm is set as the default algorithm in a project (when true). If default is not set or false, the Flash programming algorithm can be configured on a lower level. However, the Flash programming algorithm of a project can be changed manually at any time during development.