2.102 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

Secure Embedded Flash Controller peripheral library provides the following interfaces:

Functions

NameDescription
SEFCx_InitializeInitializes given instance of the SEFCx peripheral
SEFCx_ReadReads length number of bytes from a given address in FLASH memory
SEFCx_QuadWordWriteWrites a 128-bit data to a given address in FLASH memory
SEFCx_PageWriteWrites data of size equivalent to page size to a given FLASH address
SEFCx_SectorEraseErases a Sector in the FLASH
SEFCx_ErrorGetReturns the error encountered by SEFCx controller
SEFCx_IsBusyReturns the current status of SEFCx controller
SEFCx_RegionLockLocks a Flash region
SEFCx_RegionUnlockUnlocks a Flash region
SEFCx_CallbackRegisterSets the pointer to the function (and it's context) to be called when the operation is complete
SEFCx_PageBufferWriteWrites data to the internal buffer of SEFCx known as the latch buffer
SEFCx_PageBufferCommitCommits the data present in SEFCx internal latch buffer to flash memory
SEFCx_GpnvmBitClearClears the given bit number of the GPNVM
SEFCx_GpnvmBitReadReads the given bit number of the GPNVM
SEFCx_GpnvmBitSetSets the given bit number of the GPNVM
SEFCx_UniqueIdentifierReadReads the unique identifier of length number of bytes
SEFCx_UserSignatureReadReads the user signature from the given block number and page number
SEFCx_UserSignatureWriteWrites the user signature at the given page number and block number
SEFCx_UserSignatureEraseErases the given user signature block
SEFCx_UserSignatureRightsGetReads the rights information of the user signature
SEFCx_UserSignatureRightsSetWrites the given rights information of the user signature
SEFCx_WriteProtectionSetWrites the mode of the write protection
SEFCx_WriteProtectionGetReads the mode of the write protection

Data types and constants

NameTypeDescription
SEFC_ERROREnumDefines the data type for the SEFCx Error
SEFC_CALLBACKTypedefDefines the data type and function signature for the SEFCx peripheral callback function
SEFC_USERSIGNATURE_PAGEenumDefines the data type for the user signature page numbers
SEFC_USERSIGNATURE_BLOCK enumDefines the data type for the user signature block number
Note: Not all APIs maybe implemented. See the specific device family section for available APIs.