Nios® V Processor Software Developer Handbook

ID 743810
Date 7/08/2024
Public
Document Table of Contents

7.8.3.3. alt_write_flash_block()

alt_write_flash_block() writes to a single block in the flash memory. The prototype is:
int alt_write_flash_block( 
   alt_flash_fd* fd,
   int block_offset,
   int data_offset,
   const void *data,
   int length
)

This function writes to the flash memory identified by the handle fd. It writes to the block located block_offset bytes from the start of the flash device. The function writes length bytes of data from the location pointed to by data to the location data_offset bytes from the start of the flash device.

Note: These program and erase functions do not perform address checking, and do not verify whether a write operation spans into the next block. You must pass in valid information about the blocks to program or erase.

Using the Fine-Grained Flash Access API Functions

#include <string.h>
#include "sys/alt_flash.h"
#include "stdtypes.h"
#include "system.h"
#define BUF_SIZE 100
int main (void)
{
   flash_region* regions;
   alt_flash_fd* fd;
   int number_of_regions;
   int ret_code;
   char write_data[BUF_SIZE];
/* Set write_data to all 0xa */
   memset(write_data, 0xA, BUF_SIZE);
   fd = alt_flash_open_dev(EXT_FLASH_NAME);
   if (fd)
   {
      ret_code = alt_get_flash_info(fd, &regions, &number_of_regions);
      if (number_of_regions && (regions->offset == 0))
      {
/* Erase the first block */
   ret_code = alt_erase_flash_block(fd,
   regions->offset,
   regions->block_size);
   if (ret_code == 0) {
   /*
   * Write BUF_SIZE bytes from write_data 100 bytes to
   * the first block of the flash
   */
   ret_code = alt_write_flash_block (
   fd,
   regions->offset,
   regions->offset+0x100,
   write_data,
   BUF_SIZE );
      }
   }
  }
return 0;
}