3.4.13 Non-Volatile Memory Controller (NVMCTRL)

The Non-Volatile Memory (NVM) module provides an interface to the device's Non-Volatile Memory controller, so that memory pages can be written, read, erased, and reconfigured in a standardized manner.

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.

NVM APIs are implemented to be non-blocking, the API will return immediately unless stalled by Flash operation. The user application can either use polling or callback method to indicate the transfer status.

  • 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.

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 row boundary and size has to be in multiple of rows
const uint8_t nvm_user_start_address[NVMCTRL_FLASH_ROWSIZE] __attribute__((aligned(NVMCTRL_FLASH_ROWSIZE),keep,externally_visible,space(prog)))= {0};

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

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

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

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

    while(NVMCTRL_IsBusy());

    /* Erase the row */
    NVMCTRL_RowErase((uint32_t)nvm_user_start_address);

    /* Wait for row erase  to complete */
    while(NVMCTRL_IsBusy());

    /* Program a page of data */
    NVMCTRL_PageWrite((uint32_t *)pageBuffer, (uint32_t)nvm_user_start_address);

    /* Wait for page program to compete */
    while(NVMCTRL_IsBusy());
}

Library Interface

Non-Volatile Memory Controller peripheral library provides the following interfaces:

Functions

NameDescription
NVMCTRL_InitializeInitializes given instance of the NVMCTRL peripheral
NVMCTRL_ReadReads length number of bytes from a given address in FLASH memory
NVMCTRL_PageWriteWrites one page of data to given NVM address
NVMCTRL_RowEraseErases a Row in the NVM
NVMCTRL_ErrorGetReturns the error state of NVM controller
NVMCTRL_IsBusyReturns the current status of NVM controller
NVMCTRL_RegionLockLocks a NVMCTRL region
NVMCTRL_RegionUnlockUnlocks a NVMCTRL region
NVMCTRL_SecureRegionLockLocks a NVMCTRL secure region
NVMCTRL_SecureRegionUnlockUnlocks a NVMCTRL secure region
NVMCTRL_DataScrambleEnableEnable or Disable data scrambling of the Secure Data Flash
NVMCTRL_DataScrambleKeySetSets the key for data scrambling of the Secure Data Flash
NVMCTRL_CallbackRegisterSets the pointer to the function (and it is context) to be called when the operation is complete
NVMCTRL_CacheInvalidateInvalidates all cache lines
NVMCTRL_PageBufferWriteWrites data to the internal buffer of NVM known as the page buffer
NVMCTRL_PageBufferCommitCommits the data present in NVM internal page buffer to flash memory

Data types and constants

NameTypeDescription
NVMCTRL_FLASH_START_ADDRESSMacroDefines the start address of NVMCTRL Flash
NVMCTRL_FLASH_SIZEMacroDefines the size (in bytes) of Flash
NVMCTRL_FLASH_PAGESIZEMacroDefines the size (in bytes) of a NVMCTRL Page
NVMCTRL_FLASH_ROWSIZEMacroDefines the size (in bytes) of a NVMCTRL Row
NVMCTRL_ERROREnumDefines the NVMCTRL Error Type
NVMCTRL_MEMORY_REGIONEnumDefines the NVMCTRL Memory Region
NVMCTRL_SECURE_MEMORY_REGIONEnumDefines the NVMCTRL Secure Memory Region
NVMCTRL_CALLBACKTypedefDefines the data type and function signature for the NVMCTRL peripheral callback function