Nios® II Software Developer Handbook

ID 683525
Date 8/28/2023
Public
Document Table of Contents

8.7.4.1. Create a Flash Driver

Flash device drivers must provide an instance of the alt_flash_dev structure, defined in sys/alt_flash_dev.h. The following code shows the structure:
struct alt_flash_dev
{
 alt_llist llist; // internal use only
 const char* name;
 alt_flash_open open;
 alt_flash_close close;
 alt_flash_write write;
 alt_flash_read read;
 alt_flash_get_flash_info get_info;
 alt_flash_erase_block erase_block;
 alt_flash_write_block write_block;
 void* base_addr;
 int length;
 int number_of_regions;
 flash_region region_info[ALT_MAX_NUMBER_OF_FLASH_REGIONS]; 
alt_flash_lock lock};

The first parameter llist is for internal use, and must always be set to the value ALT_LLIST_ENTRY. name is the location of the device in the HAL file system and is the name of the device as defined in system.h.

The seven fields open to write_block are function pointers that implement the functionality behind the application API calls to the following functions:

  • alt_flash_open_dev()
  • alt_flash_close_dev()
  • alt_write_flash()
  • alt_read_flash()
  • alt_get_flash_info()
  • alt_erase_flash_block()
  • alt_write_flash_block()
  • alt_flash_lock()

where:

  • the base_addr parameter is the base address of the flash memory
  • length is the size of the flash in bytes
  • number_of_regions is the number of erase regions in the flash
  • region_info contains information about the location and size of the blocks in the flash device

For more information about the format of the flash_region structure, refer to “Using Flash Devices” in "Developing Programs Using the Hardware Abstraction Layer".

Some flash devices, such as common flash interface (CFI)-compliant devices, allow you to read out the number of regions and their configuration at run time. For all other flash devices, these two fields must be defined at compile time.