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

alt_read_flash()

Altera_Forum
Honored Contributor II
3,343 Views

Hello, 

 

What is the meaning of 5000 in alt_read_flash(fd, 0, dest, 5000)? I cannot understand the arguments inside alt_read_flash. I read a few articles and notes but still cannot get its meaning. pls help. thanks!
0 Kudos
14 Replies
Altera_Forum
Honored Contributor II
1,188 Views

int alt_read_flash( alt_flash_fd* fd, 

int offset, 

void* dest_addr, 

int length ); 

 

In the "Using Flash Devices" section of the Nios II SW Developers Handbook.  

 

"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()."
0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

hi... thanks for the reply. i am quite confused with "length". what does it mean by "amount of data read" which is "length"? is it in bytes or the number of data to be read? example, i am writing 1Mb of data to flash and want to read it out. so what should be the length for alt_write_flash and alt_read_flash? 

 

thanks!
0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

It is in bytes.

0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

So, it means, if my data is 1Mb, so the length is 1Mb?

0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

Correct. You write however many bytes you would like to read out in that field. You can look at some of the memtest examples for their usage of it as well.  

 

You do have to play nicely with the region size though if you are managing the file locations yourself. So, don't expect to write a partial block and not have the other contents of that block erased. But that is a whole other topic kinda.
0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

 

--- Quote Start ---  

So, it means, if my data is 1Mb, so the length is 1Mb? 

--- Quote End ---  

 

 

Be careful about  

1 Mb = 1 Mega bits  

1 Mb = 1 mega Bytes (1 mega octet) = 8 megabits
0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

Hi, mmTsuchi. I am transferring 1MB (1 Mega Byte) of data into flash. I am using alt_write_flash. So, what should be the length in alt_write_flash. pls help. I have been debugging the whole day but still cannot understand. Thanks!

0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

1048576, or 0x100000

0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

Yes, i put that before but the return value for alt_write_flash is an error. cannot write.

0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views
0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

What is the error code? Have you tried writing a smaller value? is there a boundry that it appears to not write on? In all my use of it i write only block_size chunks at a time. Don't know if you have to do that or not, but all my code does. So, you would have to read the regions data structure, which  

 

 

alt_flash_fd* fd; int number_of_regions; flash_region *regions; fd = alt_flash_open_dev(flashName); if (!fd) return -EACCES; retval = alt_get_flash_info(fd, &regions, &number_of_regions); if (retval) { alt_flash_close_dev(fd); return errno; }  

 

Then regions->block_size contains the size of the write blocks on the flash device. 

 

::edited to include code tags.
0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

yes, i tried for smaller value n it works fine. i dont quite understand regarding writing in block. please help. sorry, i am new. thanks!

0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

Flash is structured not as one large memory but as 1-N regions and each region has 1-N blocks of a paticular size. You can not usually erase just a single byte of flash, so you have to erase the whole block, and then make any changes you need. I know, poor explanation, but google may have a better one, or altera. Either way, you have to take that into account when making a change as you may inadvertently corrupt part of a block if you try and do a partial write. But, per your other thread, if you are just doing a single write then reading back and it is failing that should not be a corruption problem. But, try only writing a block at a time, and looping until you have written all of your data. The code to get the block size is above.

0 Kudos
Altera_Forum
Honored Contributor II
1,188 Views

Thanks! No, it is a good explanation! i will give it a try and if cant, i will leave a msg here again.

0 Kudos
Reply