Jump to main content

##

# 3.42.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

Name | Description
--- | ---
NVMCTRL_Initialize | Initializes given instance of the NVMCTRL peripheral
NVMCTRL_Read | Reads length number of bytes from a given address in FLASH memory
NVMCTRL_PageWrite | Writes one page of data to given NVM address
NVMCTRL_RowErase | Erases a Row in the NVM
NVMCTRL_ErrorGet | Returns the error state of NVM controller
NVMCTRL_IsBusy | Returns the current status of NVM controller
NVMCTRL_RegionLock | Locks a NVMCTRL region
NVMCTRL_RegionUnlock | Unlocks a NVMCTRL region
NVMCTRL_SecureRegionLock | Locks a NVMCTRL secure region
NVMCTRL_SecureRegionUnlock | Unlocks a NVMCTRL secure region
NVMCTRL_DataScrambleEnable | Enable or Disable data scrambling of the Secure Data Flash
NVMCTRL_DataScrambleKeySet | Sets the key for data scrambling of the Secure Data Flash
NVMCTRL_CallbackRegister | Sets the pointer to the function (and it is context) to be called when the operation is complete
NVMCTRL_CacheInvalidate | Invalidates all cache lines
NVMCTRL_PageBufferWrite | Writes data to the internal buffer of NVM known as the page buffer
NVMCTRL_PageBufferCommit | Commits the data present in NVM internal page buffer to flash memory

Data types and constants

Name | Type | Description
--- | --- | ---
NVMCTRL_FLASH_START_ADDRESS | Macro | Defines the start address of NVMCTRL Flash
NVMCTRL_FLASH_SIZE | Macro | Defines the size (in bytes) of Flash
NVMCTRL_FLASH_PAGESIZE | Macro | Defines the size (in bytes) of a NVMCTRL Page
NVMCTRL_FLASH_ROWSIZE | Macro | Defines the size (in bytes) of a NVMCTRL Row
NVMCTRL_ERROR | Enum | Defines the NVMCTRL Error Type
NVMCTRL_MEMORY_REGION | Enum | Defines the NVMCTRL Memory Region
NVMCTRL_SECURE_MEMORY_REGION | Enum | Defines the NVMCTRL Secure Memory Region
NVMCTRL_CALLBACK | Typedef | Defines the data type and function signature for the NVMCTRL peripheral callback function