1.13.14 Non-Volatile Memory (NVM)

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 main Flash is being erased or written.

The Program flash memory is divided into two Virtual regions pointing to the same physical memory location. Client can use any address from these virtual regions to perform erase/write/read operation

  • KSEG0 is cacheable region

  • KSEG1 is non-cacheable region

The program Flash array is built up of a series of rows to form a page. A page of Flash is the smallest unit of memory that can be erased at a single time. The program Flash array can be programmed in one of two ways:

  • Row programming, with 512 instruction words at a time

  • Word programming, with 1 instruction word at a time

  • Quad Word programming, with 4 instruction words at a time

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 page and program a row of memory using polling method

#define APP_FLASH_ADDRESS  (NVM_FLASH_START_ADDRESS + (NVM_FLASH_SIZE / 2))

uint8_t CACHE_ALIGN rowBuffer[NVM_FLASH_ROWSIZE] = {0};

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

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

int main (void)
{
    /*Populate rowBuffer to programmed*/
    populate_buffer(rowBuffer);

    while(NVM_IsBusy());

    /* Erase the page */
    NVM_PageErase(APP_FLASH_ADDRESS);

    /* Wait for page erase  to complete */
    while(NVM_IsBusy());

    /* Program a row of data */
    NVM_RowWrite((uint32_t *)rowBuffer, APP_FLASH_ADDRESS);

    /* Wait for row program to compete */
    while(NVM_IsBusy());
}

Library Interface

Peripheral library provides the following interfaces:

Functions

Name Description
NVM_Initialize Initializes given instance of the NVM peripheral
NVM_Read Reads length number of bytes from a given address in FLASH memory
NVM_RowWrite Writes one row of data to given NVM address
NVM_WordWrite Writes One word into the Flash
NVM_QuadWordWrite Writes four words into the Flash
NVM_PageErase Erases a Page in the NVM
NVM_ErrorGet Returns the error state of NVM controller
NVM_IsBusy Returns the current status of NVM controller
NVM_CallbackRegister Sets the pointer to the function (and it's context) to be called when the operation is complete
NVM_ProgramFlashSwapBank Swaps the Program Flash Bank 2
NVM_ProgramFlashWriteProtect Protect Program Flash Memory from Writes
NVM_ProgramFlashWriteProtectLock Disable Writes to Program Flash Write Protect Lock register
NVM_BootFlashWriteProtectEnable Protect Boot Flash Memory Page from Writes
NVM_BootFlashWriteProtectDisable Un-Protect Boot Flash Memory Page to perform Writes
NVM_BootFlashWriteProtectLock Disable Writes to Boot flash Write Protect Lock register

Data types and constants

Name Type Description
NVM_ERROR Enum Defines the NVM Error Type
NVM_BOOT_FLASH_WRITE_PROTECT Enum Defines the Boot flash write protect pages
NVM_BOOT_FLASH_WRITE_PROTECT_LOCK Enum Defines the Write protect lock pages
NVM_CALLBACK Typedef Defines the data type and function signature for the NVM peripheral callback function