Xmega Application Note


NVM driver flash handling
[NVM driver]

Functions for handling internal flash memory. More...

Collaboration diagram for NVM driver flash handling:

Defines

#define IAR_FLASH_PTR   __farflash

Typedefs

typedef uint32_t flash_addr_t
 Size of a flash page in bytes.

Functions

static void nvm_flash_atomic_write_app_page (flash_addr_t page_addr)
 Erase and write a page within the application section.
static void nvm_flash_atomic_write_boot_page (flash_addr_t page_addr)
 Erase and write a page within the boot section.
static void nvm_flash_erase_app (flash_addr_t page_addr)
 Erase entire application section.
static void nvm_flash_erase_app_page (flash_addr_t page_addr)
 Erase a page within the application section.
static void nvm_flash_erase_boot_page (flash_addr_t page_addr)
 Erase a page within the boot section.
static void nvm_flash_erase_user_section (void)
 Erase the user calibration section page.
void nvm_flash_flush_buffer (void)
 Flush flash page buffer.
void nvm_flash_load_word_to_buffer (uint32_t word_addr, uint16_t data)
 Load word into flash page buffer.
static uint8_t nvm_flash_read_byte (flash_addr_t addr)
 Load byte from flash memory.
static uint16_t nvm_flash_read_word (flash_addr_t addr)
 Load word from flash memory.
static void nvm_flash_split_write_app_page (flash_addr_t page_addr)
 Write a page within the application section.
static void nvm_flash_split_write_boot_page (flash_addr_t page_addr)
 Write a page within the boot section.
static void nvm_flash_write_user_page (void)
 Write the user calibration section page.

Detailed Description

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.

Define Documentation

#define IAR_FLASH_PTR   __farflash

Flash pointer type to use for accessing flash memory with IAR

Definition at line 438 of file nvm.h.

Referenced by nvm_flash_read_byte(), and nvm_flash_read_word().


Typedef Documentation

typedef uint32_t flash_addr_t

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. Data type for holding flash memory addresses.

Definition at line 433 of file nvm.h.


Function Documentation

static void nvm_flash_atomic_write_app_page ( flash_addr_t  page_addr  )  [inline, static]

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_addr Byte address to the page to delete

Definition at line 556 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00557 {
00558         nvm_wait_until_ready();
00559         nvm_common_spm(page_addr, NVM_CMD_ERASE_WRITE_APP_PAGE_gc);
00560 }

Here is the call graph for this function:

static void nvm_flash_atomic_write_boot_page ( flash_addr_t  page_addr  )  [inline, static]

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_addr Byte address to the page to delete

Definition at line 598 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00599 {
00600         nvm_wait_until_ready();
00601         nvm_common_spm(page_addr, NVM_CMD_ERASE_WRITE_BOOT_PAGE_gc);
00602 }

Here is the call graph for this function:

static void nvm_flash_erase_app ( flash_addr_t  page_addr  )  [inline, static]

Erase entire application section.

Erase all of the application section.

Parameters:
page_addr Address to anywhere within the application section

Definition at line 514 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00515 {
00516         nvm_wait_until_ready();
00517         nvm_common_spm(page_addr, NVM_CMD_ERASE_APP_gc);
00518 }

Here is the call graph for this function:

static void nvm_flash_erase_app_page ( flash_addr_t  page_addr  )  [inline, static]

Erase a page within the application section.

Erase one page within the application section

Parameters:
page_addr Byte address to the page to delete

Definition at line 527 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00528 {
00529         nvm_wait_until_ready();
00530         nvm_common_spm(page_addr, NVM_CMD_ERASE_APP_PAGE_gc);
00531 }

Here is the call graph for this function:

static void nvm_flash_erase_boot_page ( flash_addr_t  page_addr  )  [inline, static]

Erase a page within the boot section.

Erase one page within the boot section

Parameters:
page_addr Byte address to the page to delete

Definition at line 570 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00571 {
00572         nvm_wait_until_ready();
00573         nvm_common_spm(page_addr, NVM_CMD_ERASE_BOOT_PAGE_gc);
00574 }

Here is the call graph for this function:

static void nvm_flash_erase_user_section ( void   )  [inline, static]

Erase the user calibration section page.

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

Definition at line 611 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00612 {
00613         nvm_wait_until_ready();
00614         nvm_common_spm(0, NVM_CMD_ERASE_USER_SIG_ROW_gc);
00615 }

Here is the call graph for this function:

void nvm_flash_flush_buffer ( void   ) 

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.

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_addr Address to store data. The upper bits beyond the page size is ignored. FLASH_PAGE_SIZE
data Data word to load into the page buffer
static uint8_t nvm_flash_read_byte ( flash_addr_t  addr  )  [inline, static]

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:
addr Byte address to load
Returns:
Byte from program memory

Definition at line 450 of file nvm.h.

References IAR_FLASH_PTR.

00451 {
00452 #if defined(__GNUC__)
00453         return pgm_read_byte_far(addr);
00454 #elif defined(__ICCAVR__)
00455         uint8_t IAR_FLASH_PTR *flashptr = (uint8_t IAR_FLASH_PTR *)addr;
00456         return *flashptr;
00457 #else
00458 #  error Unknown compiler
00459 #endif
00460 }

static uint16_t nvm_flash_read_word ( flash_addr_t  addr  )  [inline, static]

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:
addr Byte address to load (last bit is ignored)
Returns:
Word from program memory

Definition at line 472 of file nvm.h.

References IAR_FLASH_PTR.

00473 {
00474 #if defined(__GNUC__)
00475         return pgm_read_word_far(addr);
00476 #elif defined(__ICCAVR__)
00477         uint16_t IAR_FLASH_PTR *flashptr = (uint16_t IAR_FLASH_PTR *)addr;
00478         return *flashptr;
00479 #endif
00480 }

static void nvm_flash_split_write_app_page ( flash_addr_t  page_addr  )  [inline, static]

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_addr Byte address to the page to delete

Definition at line 542 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00543 {
00544         nvm_wait_until_ready();
00545         nvm_common_spm(page_addr, NVM_CMD_WRITE_APP_PAGE_gc);
00546 }

Here is the call graph for this function:

static void nvm_flash_split_write_boot_page ( flash_addr_t  page_addr  )  [inline, static]

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_addr Byte address to the page to delete

Definition at line 584 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00585 {
00586         nvm_wait_until_ready();
00587         nvm_common_spm(page_addr, NVM_CMD_WRITE_BOOT_PAGE_gc);
00588 }

Here is the call graph for this function:

static void nvm_flash_write_user_page ( void   )  [inline, static]

Write the user calibration section page.

Write a the user calibratino 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 paramaters are needed.

Definition at line 625 of file nvm.h.

References nvm_common_spm(), and nvm_wait_until_ready().

00626 {
00627         nvm_wait_until_ready();
00628         nvm_common_spm(0, NVM_CMD_WRITE_USER_SIG_ROW_gc);
00629 }

Here is the call graph for this function:

@DOC_TITLE@
Generated on Fri Oct 22 12:15:26 2010 for AVR1300 Using the Xmega ADC by doxygen 1.6.3