Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers

Flash Memory

Altera_Forum
Honored Contributor II
1,995 Views

Hello. I am using Altera DE2 Board with Flash (1Mb). I want to know how to store data into and read data from flash memory. For example, I want to store 1-10 into the flash. Then, I will read the number 1-10 from the flash memory for addition. Thanks!

0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
858 Views

Add these header files: 

# include "sys/alt_flash.h" 

# include "sys/alt_flash_dev.h" 

and take a look to the functions available in them.
0 Kudos
Altera_Forum
Honored Contributor II
858 Views

thanks. but i dont really understand. can i have some sample code? just a simple writing and reading to/from flash.

0 Kudos
Altera_Forum
Honored Contributor II
858 Views

First of all init the flash device and possibly get extra info about the flash device, like block size: 

flash_region *regions; int numRegions; int error = 0; pFlashDevice = alt_flash_open_dev(FLASH_CONTROLLER_NAME); if (pFlashDevice <= 0) error = -1; if (!error) error = alt_epcs_flash_get_info(pFlashDevice, &regions, &numRegions); if (!error) { flash_block_size = regions->block_size; flash_max_addr = regions->region_size; This is for erasing a single block: 

alt_epcs_flash_erase_block(pFlashDevice, block_address); This is for writing any data to a previously erased area: 

int buf = { 1, 2,3,4,5,6,7,8,9,10 }; rv = alt_epcs_flash_write_block(pFlashDevice, block_address, data_offset_inside_block , buf, 10); Then, read back data with: 

alt_epcs_flash_read(pFlashDevice, addr, buf, len);
0 Kudos
Altera_Forum
Honored Contributor II
858 Views

 

--- Quote Start ---  

First of all init the flash device and possibly get extra info about the flash device, like block size: 

flash_region *regions; int numRegions; int error = 0; pFlashDevice = alt_flash_open_dev(FLASH_CONTROLLER_NAME); if (pFlashDevice <= 0) error = -1; if (!error) error = alt_epcs_flash_get_info(pFlashDevice, &regions, &numRegions); if (!error) { flash_block_size = regions->block_size; flash_max_addr = regions->region_size;  

i am using cfi, is it the same? besides, my pflashdevice is <= 0. i dunno why. is it sth to do with reset vector as my reset vector is sdram. 

 

This is for writing any data to a previously erased area: 

int buf = { 1, 2,3,4,5,6,7,8,9,10 }; rv = alt_epcs_flash_write_block(pFlashDevice, block_address, data_offset_inside_block , buf, 10);  

why need rv when write data? 

 

Then, read back data with: 

alt_epcs_flash_read(pFlashDevice, addr, buf, len);  

where is the data when we read it from the flash? like i want to print out the data. 

 

--- Quote End ---  

 

 

please help me with the ques in bold. thanks!
0 Kudos
Altera_Forum
Honored Contributor II
858 Views

why need rv when write data? 

This is not mandatory, but recommended to check if data has been written correctlywhere is the data when we read it from the flash? like i want to print out the data. 

???  

What's the problem? The buf array contains your data. 

Maybe this way is clearer: 

int buf[10]; 

alt_flash_read(pFlashDevice, addr, buf, 10);Remark: 

remove epcs_ from the previous post. I copied and pasted from a project using epcs instead of parallel flash and forgot to change function names.
0 Kudos
Altera_Forum
Honored Contributor II
858 Views

thank you very much

0 Kudos
Reply