- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
--- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.offsetflash->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 }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Mmm... very strange.
Try to write into next blocks, offset 0x20000, 0x30000, ... Same result?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page