Embedded Design Handbook

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

4.2.3.3.3. Flash Memory Devices

The HAL BSP library supports parallel common flash interface (CFI) memory devices and Intel erasable, programmable, configurable serial (EPCS) flash memory devices. A uniform API is available for both flash memory types, providing read, write, and erase capabilities.

Memory Initialization, Querying, and Device Support

Every flash memory device is queried by the HAL during system initialization to determine the kind of flash memory and the functions that should be used to manage it. This process is automatically performed by the alt_sys_init() function, if the device drivers are not explicitly omitted and the small driver configuration is not set.

After initialization, you can query the flash memory for status information with the alt_flash_get_flash_info() function. This function returns a pointer to an array of flash region structures—C structures of type struct flash_region—and the number of regions on the flash device.

For additional information about the struct flash_region structure, refer to the source file HAL/inc/sys/alt_flash_types.h in the BSP project directory.

Accessing the Flash Memory

The alt_flash_open() function opens a flash memory device and returns a descriptor for that flash memory device. After you complete reading and writing the flash memory, call the alt_flash_close() function to close it safely.

The HAL flash memory device model provides you with two flash access APIs, one simple and one fine-grained The simple API takes a buffer of data and writes it to the flash memory device, erasing the sectors if necessary. The fine-grained API enables you to manage your flash device on a block-by-block basis.

Both APIs can be used in the system. The type of data you store determines the most useful API for your application. The following general design guidelines help you determine which API to use for your data storage needs:

Simple API—This API is useful for storing arbitrary streams of bytes, if the exact flash sector location is not important. Examples of this type of data are log or data files generated by the system during run-time, which must be accessed later in a continuous stream somewhere in flash memory.

Fine-Grained API—This API is useful for storing units of data, or data sets, which must be aligned on absolute sector boundaries. Examples of this type of data include persistent user configuration values, FPGA hardware images, and application images, which must be stored and accessed in a given flash sector (or sectors).

For examples that demonstrate the use of APIs, refer to the “Using Flash Devices” section in the Developing Programs Using the Hardware Abstraction Layer chapter of the Nios® II Gen2 Software Developer's Handbook.

Configuration and Use Limitations

If you use flash memories in your system, be aware of the following properties of this memory:

  • Code Storage—If your application runs code directly from the flash memory, the flash manipulation functions are disabled. This setting prevents the processor from erasing the memory that holds the code it is running. In this case, the symbols ALT_TEXT_DEVICE, ALT_RODATA_DEVICE, and ALT_EXCEPTIONS_DEVICE must all have values different from the flash memory peripheral. (Note that each of these #define symbols names a memory device, not an address within a memory device).
  • Small Driver—If the small driver flag is set for the software—the hal.enable_reduced_device_drivers setting is enabled—then the flash memory peripherals are not automatically initialized. In this case, your application must call the initialization routines explicitly.
  • Thread safety—Most of the flash access routines are not thread-safe. If you use any of these routines, construct your application so that only one thread in the system accesses these function.
  • EPCS flash memory limitations—The Intel EPCS memory has a serial interface. Therefore, it cannot run Nios® II instructions and is not visible to the Nios® II processor as a standard random-access memory device. Use the Intel-supplied flash memory access routines to read data from this device.
  • File System—The HAL flash memory API does not support a flash file system in which data can be stored and retrieved using a conventional file handle. However, you can store your data in flash memory before you run your application, using the read-only zip file system and the Nios® II flash programmer utility. For information about the read-only zip file system, refer to “Read-Only Zip File System”.

For more information about the configuration and use limitations of flash memory, refer to the “Using Flash Devices” section in the Developing Programs Using the Hardware Abstraction Layer chapter of the Nios® II Gen2 Software Developer's Handbook. For more information about the API for the flash memory access routines, refer to the HAL API Reference chapter of the Nios® II Gen2 Software Developer’s Handbook.