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

Flash memory problem

Altera_Forum
Honored Contributor II
2,171 Views

Hello, is it true that I cannot write 1MB of data into flash memory (4M)?  

I am using Altera DE2 Board. My code is as follow: 

 

 

--- Quote Start ---  

# include <stdio.h># include <stdlib.h># include <unistd.h># include "altera_avalon_pio_regs.h"# include "system.h"# include "alt_types.h"# include "sys/alt_dma.h"# include "sys/alt_flash.h" # include "sys/alt_flash_dev.h" 

# define loop 262144# define size 1048576 

 

int main () 

alt_flash_fd* fd; 

int ret_code, i; 

int *src_ptr; 

int *dest_ptr; 

 

src_ptr = SDRAM_BASE + 0x200000; //Location of data written in SDRAM 

dest_ptr = EXT_FLASH_BASE; //Location of data written in flash 

 

//Open flash 

fd = alt_flash_open_dev(EXT_FLASH_NAME); 

if(fd!=NULL) 

//Generate data into SDRAM 

for(i=0;i<loop;i++) 

IOWR(src_ptr, i, i); 

 

//Write data into flash 

ret_code = alt_write_flash(fd, 0, src_ptr, size); 

if(ret_code != 0) 

printf("Error writing into flash: %d\n", ret_code); 

exit(1); 

 

//Read from flash 

for(i=0;i<loop;i++) 

printf("Value at %X is %d, written at is %d\n", dest_ptr, *dest_ptr, IORD(src_ptr, i)); 

dest_ptr++; 

}else 

printf("Unable to open flash\n"); 

exit(1); 

alt_flash_close_dev(fd); 

return 0; 

 

--- Quote End ---  

I keep on getting error msg: Cannot write into flash and the error code is -5. pls help me. i am not sure whether is my coding problem or writing 1MB into flash is a special case. pls help! thanks!
0 Kudos
11 Replies
Altera_Forum
Honored Contributor II
705 Views

The -5 means EIO code (see errno.h file) 

This can be generated if HAL driver can't erase the flash block. 

Are you sure the flash is not write protected?  

Then, try initially to write a small amount of data, not the complete 1M buffer.
0 Kudos
Altera_Forum
Honored Contributor II
705 Views

How do I check if the flash is write-protected? 

Yes, I did tried to write 50KB of data and it works fine. 

I am using DE2 Board
0 Kudos
Altera_Forum
Honored Contributor II
705 Views

 

--- Quote Start ---  

How do I check if the flash is write-protected? 

 

--- Quote End ---  

 

Flash memories can have two data protection methods: 

- a Write Protect (/WP) pin which usually must be left floating or pulled up to allow write. If you tie it to ground you can't write data.  

- a 'soft' protection you can enable/disable through appropriate commands on the flash interface. Depending on the device, you can restrict protection to single sectors or apply it to the whole device. 

Refer to the component datasheet for both. 

 

 

--- Quote Start ---  

 

Yes, I did tried to write 50KB of data and it works fine. 

I am using DE2 Board 

--- Quote End ---  

 

I think the alt_flash_write function provided by HAL driver can manage any data size. It should take care of splitting large data in actual flash sectors. 

However, I believe that the function operates on a sector basis, so the entire sector is erased when you call alt_flash_write, even if you write only a few bytes. Same if your offset is not at the beginning of a sector: all data at lower addresses in that sector will be lost. 

Usually sector size is 64kB, so I'd suggest you try to write up to this size; if the function fails with bigger size, then the problem is almost certainly due to some problem in spreading data writes across different sectors.
0 Kudos
Altera_Forum
Honored Contributor II
705 Views

Hi, so, it means now i try to set my data to 64KB but not 1MB to test first. but, what should i do to write in 1MB of data? split the data in 64KB? sorry, i am new in this. pls help. thanks! 

 

latest: i tried with 64KB and it works fine. but i change it to 128KB of data, it got error, -5 at the writing part.
0 Kudos
Altera_Forum
Honored Contributor II
705 Views

First of all get information about your flash device. 

After alt_flash_open_dev call, define 

alt_flash_cfi_dev* flash = (alt_flash_cfi_dev*)fd; 

Then you can read: 

flash->dev.number_of_regions 

For every flash region: 

flash->dev.region_info.offset 

flash->dev.region_info.region_size 

flash->dev.region_info.number_of_blocks 

flash->dev.region_info.block_size 

Probably you have a single flash region, so you need read these only for i=0 

If block_size is indeed 64kB then my supposition is probably correct and you must limit write size to 64kB (and align writes to block boundaries) 

You can simply do something like this: 

int addr = 0; 

while (addr < size) { 

//Write data into flash 

ret_code = alt_write_flash(fd, addr, src_ptr+addr, 0x10000); 

if(ret_code != 0) 

printf("Error writing into flash: %d\n", ret_code); 

exit(1); 

addr += 0x10000; // next 64kb block 

}
0 Kudos
Altera_Forum
Honored Contributor II
705 Views

I checked the datasheet and yes, 1 sector is 64KB. I tried your way but the output is Error writing into flash: -5. error still exists.

0 Kudos
Altera_Forum
Honored Contributor II
705 Views

You previously said that a single 64kB works fine. 

So, I understand that now the first 64kb write at offset=0 is ok while the second write at offset=0x10000 fails. Is this right?
0 Kudos
Altera_Forum
Honored Contributor II
705 Views

Yes, when I transfer 65536 (64K) of data, it works fine. the result is as followed for 64KB: (the last 3 results) 

 

 

--- Quote Start ---  

 

Value at 140FFF4 is 16381, written is 16381 

Value at 140FFF8 is 16382, written is 16382 

Value at 140FFFC is 16383, written is 16383 

 

--- Quote End ---  

my flash base is 0x1400000. the last data was written into 0x140FFFC as shown above. 

 

now i set the size to be 131072 (128K). the output is: 

 

 

--- Quote Start ---  

 

Error writing into flash: -5 

 

--- Quote End ---  

 

--- Quote Start ---  

So, I understand that now the first 64kb write at offset=0 is ok while the second write at offset=0x10000 fails. Is this right?  

--- Quote End ---  

Yes, at second write at offset 0x10000 fails.
0 Kudos
Altera_Forum
Honored Contributor II
705 Views

Mmm... very strange. 

Try to write into next blocks, offset 0x20000, 0x30000, ... 

Same result?
0 Kudos
Altera_Forum
Honored Contributor II
705 Views

yeah it is strange. yes i tried to write into next block at offset 0x20000 and it is fine. i mean i just directly jump to second sector without writing into first sector and it is ok. if i change to 128KB, meaning if success, it will occupy 2 sectors. but that case, it returns code -5, as mentioned in my previous post.

0 Kudos
Altera_Forum
Honored Contributor II
705 Views

The only explanation I can give you is that the second flash block is locked. 

You can read the lock status and possibly unlock it in order to allow erase and write operations. I don't know if the HAl driver supports these operations; if not, you must make it manually with direct IOWR/IORD; it's not difficult, but you must search the required command sequence in the flash device datasheet.
0 Kudos
Reply