Flash page driver

Low-level flash page interface.

This interface provides a very simple and straight forward way for writing a MCU’s internal flash. This interface is only capable of reading, verifying, and writing complete flash pages, it has no support for partial flash access. This enables for very slim and efficient implementations.

A module for more fine-grained access of memory locations can easily be programmed on top of this interface.

Note

Flash memory has only a limited amount of erase cycles (mostly around 10K times), so using this interface in some kind of loops can damage you MCU!

enum @148
FLASHPAGE_OK =  0
everything succeeded
FLASHPAGE_NOMATCH = -1
page differs from target data
void * flashpage_addr(int page)

Translate the given page number into the page’s starting address.

Note

The given page MUST be valid, otherwise the returned address points to an undefined memory location!

Parameters

page:page number to get the address of

Return values

  • starting memory address of the given page
int flashpage_page(void * addr)

Translate the given address into the corresponding page number.

The given address can be any address inside a page.

Note

The given address MUST be a valid flash address!

Parameters

addr:address inside the targeted page

Return values

  • page containing the given address
void flashpage_write(int page, const void * data)

Write the given page with the given data.

Parameters

page:page to write
data:data to write to the page, MUST be FLASHPAGE_SIZE byte. Set to NULL for page erase only.

void flashpage_write_raw(void * target_addr, const void * data, msp430_types.h::size_t len)

Write any number of data bytes to a given location in the flash memory.

Both target address and data address must be aligned to FLASHPAGE_RAW_ALIGN. len must be a multiple of FLASHPAGE_RAW_BLOCKSIZE. This function doesn’t erase any area in flash, thus be sure the targeted memory area is erased before writing on it (using the flashpage_write function).

Parameters

target_addr:address in flash to write to. MUST be aligned to FLASHPAGE_RAW_ALIGNMENT.
data:data to write to the address. MUST be aligned to FLASHPAGE_RAW_ALIGNMENT.
len:length of the data to be written. It MUST be multiple of FLASHPAGE_RAW_BLOCKSIZE. Also, ensure it doesn’t exceed the actual flash memory size.

void flashpage_read(int page, void * data)

Read the given page into the given memory location.

Parameters

page:page to read
data:memory to write the page to, MUST be FLASHPAGE_SIZE byte

int flashpage_verify(int page, const void * data)

Verify the given page against the given data.

Parameters

page:page to verify
data:data to compare page against, MUST be FLASHPAGE_SIZE byte of data

Return values

  • FLASHPAGE_OK if data in the page is identical to data
  • FLASHPAGE_NOMATCH if data and page content diverge
int flashpage_write_and_verify(int page, const void * data)

Write the given page and verify the results.

This is a convenience function wrapping flashpage_write and flashpage_verify.

Parameters

page:page to write
data:data to write to the page, MUST be FLASHPAGE_SIZE byte.

Return values

  • FLASHPAGE_OK on success
  • FLASHPAGE_NOMATCH if data and page content diverge
CPU_FLASH_BASE

Per default, we expect the internal flash to start at address 0.

1
(0)
FLASHPAGE_RAW_BLOCKSIZE

For raw writings to the flash, this constant must define the minimum write length allowed by the MCU.

FLASHPAGE_RAW_ALIGNMENT

The buffers to be written to the flash MUST be aligned, as well as the address on which the buffer is written to the flash.

This variable must be defined for that purpose, according to the MCU align requirements.