Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NVM driver flash handling

Functions for handling internal flash memory.

The internal flash memory on the XMEGA devices consists of the application section, the application table section and the bootloader section. All these sections can store program code for the MCU, but if there is available space, they can be used for storing other persistent data.

Writing the flash memory can only be done one page at a time. It consists of loading the data to the internal page buffer and then running one of the write commands. If the page has not been erased before writing, the data will not be written correctly.

In order to be able to write to flash memory the programming commands need to be run from the boot section.

Note
If using GCC and the flash sub-module, remember to configure the boot section in the make file.
The functions in this module are modifying the NVM.CMD register. If the application are using program space access in interrupts (__flash pointers in IAR EW or pgm_read_byte in GCC) interrupts needs to be disabled when running EEPROM access functions. If not the program space reads will be corrupted.

Macros

#define FLASH_PAGE_SIZE
 
#define FLASH_SIZE
 Size of a flash page in bytes. More...
 
#define IAR_FLASH_PTR   __farflash
 Flash pointer type to use for accessing flash memory with IAR. More...
 

Typedefs

typedef uint32_t flash_addr_t
 Data type for holding flash memory addresses. More...
 

Functions

static void nvm_flash_atomic_write_app_page (flash_addr_t page_addr)
 Erase and write a page within the application section. More...
 
static void nvm_flash_atomic_write_boot_page (flash_addr_t page_addr)
 Erase and write a page within the boot section. More...
 
void nvm_flash_erase_and_write_buffer (flash_addr_t address, const void *buf, uint16_t len, bool b_blank_check)
 Erase and write specific parts of application flash section. More...
 
static void nvm_flash_erase_app (void)
 Erase entire application section. More...
 
static void nvm_flash_erase_app_page (flash_addr_t page_addr)
 Erase a page within the application section. More...
 
static void nvm_flash_erase_boot_page (flash_addr_t page_addr)
 Erase a page within the boot section. More...
 
static void nvm_flash_erase_user_section (void)
 Erase the user calibration section page. More...
 
static void nvm_flash_flush_buffer (void)
 Flush flash page buffer. More...
 
void nvm_flash_load_word_to_buffer (uint32_t word_addr, uint16_t data)
 Load word into flash page buffer. More...
 
void nvm_flash_read_buffer (flash_addr_t address, void *buf, uint16_t len)
 Read buffer within the application section. More...
 
static uint8_t nvm_flash_read_byte (flash_addr_t addr)
 Load byte from flash memory. More...
 
static uint16_t nvm_flash_read_word (flash_addr_t addr)
 Load word from flash memory. More...
 
static void nvm_flash_split_write_app_page (flash_addr_t page_addr)
 Write a page within the application section. More...
 
static void nvm_flash_split_write_boot_page (flash_addr_t page_addr)
 Write a page within the boot section. More...
 
static void nvm_flash_write_user_page (void)
 Write the user calibration section page. More...
 
void nvm_issue_flash_range_crc (flash_addr_t start_addr, flash_addr_t end_addr)
 Issue flash range CRC command. More...
 
void nvm_user_sig_read_buffer (flash_addr_t address, void *buf, uint16_t len)
 Read buffer within the user section. More...
 
void nvm_user_sig_write_buffer (flash_addr_t address, const void *buf, uint16_t len, bool b_blank_check)
 Write specific parts of user flash section. More...
 

#define FLASH_SIZE

Size of a flash page in bytes.

The page size in bytes taken from the toolchain header files.

Note
Page size is currently missing from the IAR header files, so it needs to be defined in the driver until it is fixed.

Referenced by nvm_get_size().

#define IAR_FLASH_PTR   __farflash

Flash pointer type to use for accessing flash memory with IAR.

Referenced by nvm_flash_read_byte(), and nvm_flash_read_word().

typedef uint32_t flash_addr_t

Data type for holding flash memory addresses.

static void nvm_flash_atomic_write_app_page ( flash_addr_t  page_addr)
inlinestatic

Erase and write a page within the application section.

Erase and the write a page within the application section with the data stored in the page buffer. Erase and write is done in an atomic operation.

Parameters
page_addrByte address to the page to delete

References nvm_common_spm(), and nvm_wait_until_ready().

Referenced by nvm_flash_erase_and_write_buffer(), and test_atomic_write_app_table().

static void nvm_flash_atomic_write_boot_page ( flash_addr_t  page_addr)
inlinestatic

Erase and write a page within the boot section.

Erase and the write a page within the boot section with the data stored in the page buffer. Erase and write is done in an atomic operation.

Parameters
page_addrByte address to the page to delete

References nvm_common_spm(), and nvm_wait_until_ready().

Referenced by test_atomic_write_boot().

void nvm_flash_erase_and_write_buffer ( flash_addr_t  address,
const void *  buf,
uint16_t  len,
bool  b_blank_check 
)

Erase and write specific parts of application flash section.

Parameters
addressthe address to where to write
bufpointer to the data
lenthe number of bytes to write
b_blank_checkif True then the page flash is checked before write to run or not the erase page command.

Set b_blank_check to false if all application flash is erased before.

References FLASH_PAGE_SIZE, LSB, MSB, nvm_flash_atomic_write_app_page(), nvm_flash_load_word_to_buffer(), nvm_flash_read_word(), nvm_flash_split_write_app_page(), and nvm_wait_until_ready().

Referenced by nvm_write(), and nvm_write_char().

static void nvm_flash_erase_app ( void  )
inlinestatic

Erase entire application section.

Erase all of the application section.

References nvm_common_spm(), and nvm_wait_until_ready().

static void nvm_flash_erase_app_page ( flash_addr_t  page_addr)
inlinestatic

Erase a page within the application section.

Erase one page within the application section

Parameters
page_addrByte address to the page to delete

References nvm_common_spm(), and nvm_wait_until_ready().

Referenced by nvm_page_erase(), and test_split_write_app_table().

static void nvm_flash_erase_boot_page ( flash_addr_t  page_addr)
inlinestatic

Erase a page within the boot section.

Erase one page within the boot section

Parameters
page_addrByte address to the page to delete

References nvm_common_spm(), and nvm_wait_until_ready().

Referenced by nvm_page_erase(), and test_split_write_boot().

static void nvm_flash_erase_user_section ( void  )
inlinestatic

Erase the user calibration section page.

Erase the user calibration section page. There is only one page, so no parameters are needed.

References nvm_common_spm(), and nvm_wait_until_ready().

Referenced by nvm_page_erase(), and nvm_user_sig_write_buffer().

static void nvm_flash_flush_buffer ( void  )
inlinestatic

Flush flash page buffer.

Clear the NVM controller page buffer for flash. This needs to be called before using nvm_flash_load_word_to_buffer if it has not already been cleared.

References nvm_common_spm(), and nvm_wait_until_ready().

void nvm_flash_load_word_to_buffer ( uint32_t  word_addr,
uint16_t  data 
)

Load word into flash page buffer.

Clear the NVM controller page buffer for flash. This needs to be called before using nvm_flash_load_word_to_buffer if it has not already been cleared.

Parameters
word_addrAddress to store data. The upper bits beyond the page size is ignored. FLASH_PAGE_SIZE
dataData word to load into the page buffer

Referenced by fill_flash_page_buffer(), nvm_flash_erase_and_write_buffer(), and nvm_user_sig_write_buffer().

void nvm_flash_read_buffer ( flash_addr_t  address,
void *  buf,
uint16_t  len 
)

Read buffer within the application section.

Parameters
addressthe address to where to read
bufpointer to the data
lenthe number of bytes to read

References nvm_flash_read_byte(), and nvm_wait_until_ready().

Referenced by nvm_read().

static uint8_t nvm_flash_read_byte ( flash_addr_t  addr)
inlinestatic

Load byte from flash memory.

Load one word of flash using byte addressing. IAR has __flash pointers and GCC have pgm_read_byte_xx functions which load data from flash memory. This function used for compatibility between the compilers.

Parameters
addrByte address to load
Returns
Byte from program memory

References IAR_FLASH_PTR.

Referenced by nvm_flash_read_buffer(), nvm_read_char(), and sound_get_8_bit_sample().

static uint16_t nvm_flash_read_word ( flash_addr_t  addr)
inlinestatic

Load word from flash memory.

Load one word of flash using byte addressing. IAR has __flash pointers and GCC have pgm_read_byte_xx functions which load data from flash memory. This function used for compatibility between the compilers.

Parameters
addrByte address to load (last bit is ignored)
Returns
Word from program memory

References IAR_FLASH_PTR.

Referenced by is_flash_page_equal_to_buffer(), nvm_flash_erase_and_write_buffer(), sound_get_16_bit_sample(), and sound_get_next_sinewave_sample().

static void nvm_flash_split_write_app_page ( flash_addr_t  page_addr)
inlinestatic

Write a page within the application section.

Write a page within the application section with the data stored in the page buffer. The page needs to be erased before the write to avoid corruption of the data written.

Parameters
page_addrByte address to the page to delete

References nvm_common_spm(), and nvm_wait_until_ready().

Referenced by nvm_flash_erase_and_write_buffer(), and test_split_write_app_table().

static void nvm_flash_split_write_boot_page ( flash_addr_t  page_addr)
inlinestatic

Write a page within the boot section.

Write a page within the boot section with the data stored in the page buffer. The page needs to be erased before the write to avoid corruption of the data written.

Parameters
page_addrByte address to the page to delete

References nvm_common_spm(), and nvm_wait_until_ready().

Referenced by test_split_write_boot().

static void nvm_flash_write_user_page ( void  )
inlinestatic

Write the user calibration section page.

Write a the user calibration section page with the data stored in the page buffer. The page needs to be erased before the write to avoid corruption of the data written. There is only one page, so no parameters are needed.

References nvm_common_spm(), and nvm_wait_until_ready().

Referenced by nvm_user_sig_write_buffer().

void nvm_issue_flash_range_crc ( flash_addr_t  start_addr,
flash_addr_t  end_addr 
)

Issue flash range CRC command.

This function sets the FLASH range CRC command in the NVM.CMD register. It then loads the start and end byte address of the part of FLASH to generate a CRC-32 for into the ADDR and DATA registers and finally performs the execute command.

Note
Should only be called from the CRC module. The function saves and restores the NVM.CMD register, but if this function is called from an interrupt, interrupts must be disabled before this function is called.
Parameters
start_addrend byte address
end_addrstart byte address

References ccp_write_io().

void nvm_user_sig_read_buffer ( flash_addr_t  address,
void *  buf,
uint16_t  len 
)

Read buffer within the user section.

Parameters
addressthe address to where to read
bufpointer to the data
lenthe number of bytes to read

References FLASH_PAGE_SIZE, and nvm_read_user_signature_row().

Referenced by nvm_read(), nvm_read_char(), nvram_read(), and test_read().

void nvm_user_sig_write_buffer ( flash_addr_t  address,
const void *  buf,
uint16_t  len,
bool  b_blank_check 
)

Write specific parts of user flash section.

Parameters
addressthe address to where to write
bufpointer to the data
lenthe number of bytes to write
b_blank_checkif True then the page flash is checked before write to run or not the erase page command.

Set b_blank_check to false if all application flash is erased before.

References FLASH_PAGE_SIZE, LSB, MSB, nvm_flash_erase_user_section(), nvm_flash_load_word_to_buffer(), nvm_flash_write_user_page(), and nvm_read_user_signature_row().

Referenced by nvm_write(), nvm_write_char(), nvram_write(), test_read(), test_write_and_erase(), and test_write_no_erase().