1.8.19 Secure Embedded Flash Controller (SEFC)

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

Security in the SEFC is based on access rights, secure key storage and a Private Key bus. The SEFC manages safety features, including Error correction code and a self-check mechanism reported by the Flash block.

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.

SEFC 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 sefc0_user_start_address[SEFC0_SECTORSIZE] __attribute__((aligned(SEFC0_SECTORSIZE),keep,externally_visible,space(prog)))= {0};

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

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

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

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

    while(SEFC0_IsBusy());

    /*Erase the sector*/
    SEFC0_SectorErase(sefc0_user_start_address);

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

    /*Program a page*/
    SEFC0_PageWrite(pageBuffer, sefc0_user_start_address);

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

Library Interface

Embedded Flash Controller peripheral library provides the following interfaces:

Functions

Name Description
SEFCx_Initialize Initializes given instance of the SEFCx peripheral
SEFCx_Read Reads length number of bytes from a given address in FLASH memory
SEFCx_QuadWordWrite Writes a 128-bit data to a given address in FLASH memory
SEFCx_PageWrite Writes data of size equivalent to page size to a given FLASH address
SEFCx_SectorErase Erases a Sector in the FLASH
SEFCx_ErrorGet Returns the error encountered by SEFCx controller
SEFCx_IsBusy Returns the current status of SEFCx controller
SEFCx_RegionLock Locks a Flash region
SEFCx_RegionUnlock Unlocks a Flash region
SEFCx_CallbackRegister Sets the pointer to the function (and it's context) to be called when the operation is complete
SEFCx_PageBufferWrite Writes data to the internal buffer of SEFCx known as the latch buffer
SEFCx_PageBufferCommit Commits the data present in SEFCx internal latch buffer to flash memory
SEFCx_GpnvmBitClear Clears the given bit number of the GPNVM
SEFCx_GpnvmBitRead Reads the given bit number of the GPNVM
SEFCx_GpnvmBitSet Sets the given bit number of the GPNVM
SEFCx_UniqueIdentifierRead Reads the unique identifier of length number of bytes
SEFCx_UserSignatureRead Reads the user signature from the given block number and page number
SEFCx_UserSignatureWrite Writes the user signature at the given page number and block number
SEFCx_UserSignatureErase Erases the given user signature block
SEFCx_UserSignatureRightsGet Reads the rights information of the user signature
SEFCx_UserSignatureRightsSet Writes the given rights information of the user signature
SEFCx_WriteProtectionSet Writes the mode of the write protection
SEFCx_WriteProtectionGet Reads the mode of the write protection

Data types and constants

Name Type Description
SEFC_ERROR Enum Defines the data type for the SEFCx Error
SEFC_CALLBACK Typedef Defines the data type and function signature for the SEFCx peripheral callback function
SEFC_USERSIGNATURE_PAGE enum Defines the data type for the user signature page numbers
SEFC_USERSIGNATURE_BLOCK enum Defines the data type for the user signature block number