Specifications

Simple Flash Access
This interface consists of the functions alt_flash_open_dev(), alt_write_flash(),
alt_read_flash(), and alt_flash_close_dev().
For more information about the use of all of these functions in one code example, refer to the code in the
“Using the Simple Flash API Functions to Access a Flash Device Named /dev/ext/flash” example in the
"Fine-Grained Flash Access" section.
You open a flash device by calling alt_flash_open_dev(), which returns a file handle to a flash device.
This function takes a single argument that is the name of the flash device, as defined in system.h.
After you obtain a handle, you can use the alt_write_flash() function to write data to the flash device.
The prototype is:
int alt_write_flash( alt_flash_fd* fd,
int offset,
const void* src_addr,
int length )
A call to this function writes to the flash device identified by the handle fd. The driver writes the data
starting at offset bytes from the base of the flash device. The data written comes from the address
pointed to by src_addr, and the amount of data written is length.
There is also an alt_read_flash() function to read data from the flash device. The prototype is:
int alt_read_flash( alt_flash_fd* fd,
int offset,
void* dest_addr,
int length )
A call to alt_read_flash() reads from the flash device with the handle fd, offset bytes from the
beginning of the flash device. The function writes the data to location pointed to by dest_addr, and the
amount of data read is length. For most flash devices, you can access the contents as standard memory,
making it unnecessary to use alt_read_flash().
The function alt_flash_close_dev() takes a file handle and closes the device. The prototype for this
function is:
void alt_flash_close_dev(alt_flash_fd* fd )
Related Information
Fine-Grained Flash Access on page 6-19
Block Erasure or Corruption
Generally, flash memory is divided into blocks. alt_write_flash() might need to erase the contents of a
block before it can write data to it. In this case, it makes no attempt to preserve the existing contents of the
block. This action can lead to unexpected data corruption (erasure), if you are performing writes that do
not fall on block boundaries. If you wish to preserve existing flash memory contents, use the fine-grained
flash functions. These are discussed in the following section.
The "Example of Writing Flash and Causing Unexpected Data Corruption" table (Table 6-7) shows the
example of an 8-kilobyte (KB) flash memory comprising two 4-KB blocks. First write 5 KB of all 0xAA to
flash memory at address 0x0000, and then write 2 KB of all 0xBB to address 0x1400. After the first write
succeeds (at time t(2)), the flash memory contains 5 KB of 0xAA, and the rest is empty (that is, 0xFF).
6-18
Simple Flash Access
NII5V2
2015.05.14
Altera Corporation
Developing Programs Using the Hardware Abstraction Layer
Send Feedback