Microchip® Advanced Software Framework

Quick Start Guide for NVM - Basic

In this use case, the NVM module is configured for:

  • Power reduction mode enabled after sleep mode until first NVM access
  • Automatic page write commands issued to commit data as pages are written to the internal buffer
  • Zero wait states when reading FLASH memory
  • No memory space for the EEPROM
  • No protected bootloader section

This use case sets up the NVM controller to write a page of data to flash, and then read it back into the same buffer.

Setup

Prerequisites

There are no special setup requirements for this use-case.

Code

Copy-paste the following setup code to your user application:

void configure_nvm(void)
{
struct nvm_config config_nvm;
config_nvm.manual_page_write = false;
nvm_set_config(&config_nvm);
}

Add to user application initialization (typically the start of main()):

configure_nvm();

Workflow

  1. Create an NVM module configuration struct, which can be filled out to adjust the configuration of the NVM controller.
    struct nvm_config config_nvm;
  2. Initialize the NVM configuration struct with the module's default values.
    Note
    This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
  3. Enable automatic page write mode. The new data will be written to NVM automaticly.
    config_nvm.manual_page_write = false;
    Note
    If automatic page write mode is disabled, the data will not write to NVM until the NVM write command has been invoked. For safe use of the NVM module, disable automatic page write mode and use write command to commit data is recommended.
  4. Configure NVM controller with the created configuration struct settings.
    nvm_set_config(&config_nvm);

Use Case

Code

Copy-paste the following code to your user application:

uint8_t page_buffer[NVMCTRL_PAGE_SIZE];
for (uint32_t i = 0; i < NVMCTRL_PAGE_SIZE; i++) {
page_buffer[i] = i;
}
enum status_code error_code;
do
{
error_code = nvm_erase_row(
100 * NVMCTRL_ROW_PAGES * NVMCTRL_PAGE_SIZE);
} while (error_code == STATUS_BUSY);
do
{
error_code = nvm_write_buffer(
100 * NVMCTRL_ROW_PAGES * NVMCTRL_PAGE_SIZE,
page_buffer, NVMCTRL_PAGE_SIZE);
} while (error_code == STATUS_BUSY);
do
{
error_code = nvm_read_buffer(
100 * NVMCTRL_ROW_PAGES * NVMCTRL_PAGE_SIZE,
page_buffer, NVMCTRL_PAGE_SIZE);
} while (error_code == STATUS_BUSY);

Workflow

  1. Set up a buffer, one NVM page in size, to hold data to read or write into NVM memory.
    uint8_t page_buffer[NVMCTRL_PAGE_SIZE];
  2. Fill the buffer with a pattern of data.
    for (uint32_t i = 0; i < NVMCTRL_PAGE_SIZE; i++) {
    page_buffer[i] = i;
    }
  3. Create a variable to hold the error status from the called NVM functions.
    enum status_code error_code;
  4. Erase a page of NVM data. As the NVM could be busy initializing or completing a previous operation, a loop is used to retry the command while the NVM controller is busy.
    do
    {
    error_code = nvm_erase_row(
    100 * NVMCTRL_ROW_PAGES * NVMCTRL_PAGE_SIZE);
    } while (error_code == STATUS_BUSY);
    Note
    This must be performed before writing new data into an NVM page.
  5. Write the data buffer to the previously erased page of the NVM.
    do
    {
    error_code = nvm_write_buffer(
    100 * NVMCTRL_ROW_PAGES * NVMCTRL_PAGE_SIZE,
    page_buffer, NVMCTRL_PAGE_SIZE);
    } while (error_code == STATUS_BUSY);
    Note
    The new data will be written to NVM memory automatically, as the NVM controller is configured in automatic page write mode.
  6. Read back the written page of page from the NVM into the buffer.
    do
    {
    error_code = nvm_read_buffer(
    100 * NVMCTRL_ROW_PAGES * NVMCTRL_PAGE_SIZE,
    page_buffer, NVMCTRL_PAGE_SIZE);
    } while (error_code == STATUS_BUSY);