Jump to main content

##

# 2.52 Harden Embedded Flash Controller (HEFC)

The Hardened Embedded Flash Controller (HEFC) manages the programming, erasing, locking and unlocking sequences of the Flash using a full set of commands.

## Using The Library

The main Flash memory can not be read while it is being erased or written, the CPU is stalled during the entire operation. All functions that modify the main Flash can be run from RAM memory to avoid CPU stall while main Flash is being erased or written.

The FLASH memory is divided into a number of physical rows, each containing four identically sized flash pages. Pages may be read or written to individually, however pages must be erased before being reprogrammed and the smallest granularity available for erasure is one single row.

HEFC APIs are implemented to be non-blocking, the API will return immediately if not stalled by Flash operation. The user application can either poll the status or get callback once the flash operation is completed.

- With polling, the application will need to continuously check if the flash operation is completed

- With callback, the registered callback function will be called once the flash operation is completed. This means the application do not have to poll continuously. The interrupt must be enabled in MCC for callback method

Here is an example code to erase a row and program a page of memory using polling method

    // Define a constant array in Flash.
    // It must be aligned to sector boundary and size has to be in multiple of sectors
    const uint8_t hefc_user_start_address[HEFC_SECTORSIZE] __attribute__((aligned(HEFC_SECTORSIZE),keep,externally_visible,space(prog)))= {0};

    void populate_buffer(uint8_t* data)
    {
        int i = 0;

        for (i = 0; i < (HEFC_PAGESIZE); i++)
        {
            *(data + i) = i;
        }
    }

    int main (void)
    {
        uint8_t pageBuffer[HEFC_PAGESIZE] = {0};

        /*Populate pageBuffer to programmed*/
        populate_buffer(pageBuffer);

        while(HEFC_IsBusy());

        /*Erase the sector*/
        HEFC_SectorErase(hefc_user_start_address);

        /* Wait for erase operation to complete */
        while(HEFC_IsBusy());

        /*Program a page*/
        HEFC_PageWrite(pageBuffer, hefc_user_start_address);

        /* Wait for page program to complete
        while(HEFC_IsBusy());
    }

## Library Interface

Harden Embedded Flash Controller peripheral library provides the following interfaces:

Functions

Name | Description
--- | ---
HEFC_Initialize | Initializes given instance of the HEFC peripheral
HEFC_Read | Reads length number of bytes from a given address in FLASH memory
HEFC_PageWrite | Writes data of size equivalent to page size to a given FLASH address
HEFC_SectorErase | Erases a Sector in the FLASH
HEFC_ErrorGet | Returns the error encountered by HEFC controller
HEFC_IsBusy | Returns the current status of HEFC controller
HEFC_RegionLock | Locks a Flash region
HEFC_RegionUnlock | Unlocks a Flash region
HEFC_CallbackRegister | Sets the pointer to the function (and it's context) to be called when the operation is complete

Data types and constants

Name | Type | Description
--- | --- | ---
HEFC_ERROR | Enum | Defines the data type for the HEFC Error
HEFC_CALLBACK | Typedef | Defines the data type and function signature for the HEFC peripheral callback function

Note: Not all APIs maybe implemented. See the specific device family section for available APIs.