1.36.6 Embedded Flash Controller (EFC)

The Enhanced Embedded Flash Controller (EEFC) 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 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.

EFC 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 MHC 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 efc_user_start_address[EFC_SECTORSIZE] __attribute__((aligned(EFC_SECTORSIZE),keep,externally_visible,space(prog)))= {0};

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

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

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

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

    while(EFC_IsBusy());

    /*Erase the sector*/
    EFC_SectorErase(efc_user_start_address);

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

    /*Program a page*/
    EFC_PageWrite(pageBuffer, efc_user_start_address);

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

Library Interface

Embedded Flash Controller peripheral library provides the following interfaces:

Functions

Name Description
EFC_Initialize Initializes given instance of the EFC peripheral
EFC_Read Reads length number of bytes from a given address in FLASH memory
EFC_QuadWordWrite Writes a 128-bit data to a given address in FLASH memory
EFC_PageWrite Writes data of size equivalent to page size to a given FLASH address
EFC_SectorErase Erases a Sector in the FLASH
EFC_ErrorGet Returns the error encountered by EFC controller
EFC_IsBusy Returns the current status of EFC controller
EFC_RegionLock Locks a Flash region
EFC_RegionUnlock Unlocks a Flash region
EFC_CallbackRegister Sets the pointer to the function (and it's context) to be called when the operation is complete
EFC_PageBufferWrite Writes data to the internal buffer of EFC known as the latch buffer
EFC_PageBufferCommit Commits the data present in EFC internal latch buffer to flash memory

Data types and constants

Name Type Description
EFC_ERROR Enum Defines the data type for the EFC Error
EFC_CALLBACK Typedef Defines the data type and function signature for the EFC peripheral callback function