Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Using alt_flash_open_dev

Altera_Forum
Honored Contributor II
2,678 Views

Hello, 

 

I have a Nios2 system that runs from flash (flash = program memory). 

My program needs to access flash at a certain moment, it needs to write/read some data to it. The alt_flash_open_dev function always returns 0, this means that my system can't access the flash. 

Did anybody have a similar problem and how did you solve it? Maybe I have to put the flash_access code in FPGA internal memory? 

 

thanks and best regards, 

Dolphin
0 Kudos
10 Replies
Altera_Forum
Honored Contributor II
1,131 Views

Hi DOLPHIN, 

 

> I have a Nios2 system that runs from flash (flash = program memory). 

> My program needs to access flash at a certain moment, it needs to write/read some data to it. 

 

I believe the hal prevents you from doing this -- when you try to write to the flash, your flash 

device will no longer be driving array data on the data bus ... and your CPU will be (trying) to 

execute status info from the device. 

 

> Did anybody have a similar problem and how did you solve it? 

 

Yes, this was discussed here: 

 

http://www.niosforum.com/forum/index.php?a...f=17&t=1900&hl= (http://www.niosforum.com/forum/index.php?act=st&f=17&t=1900&hl=

 

Regards, 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

Hi, 

 

I have been doing some trials with the flash access: 

- If my program runs from flash, I can't access the flash as a data memory (alt_flash_open_dev returns FAIL). 

- If my program runs from internal data memory, I can access the flash as a data memory (alt_flash_open_dev returns PASS). 

- if my program runs from flash but the flash access is placed in a seperate section that is located in FPGA internal RAM, I can't access the flash (alt_flash_open_dev returns FAIL). 

 

I think that the latter case should work. 

What do you think about this? 

 

thanks, 

Dolphin
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

Hello, 

 

I just found out what the problem is. 

I use the following function to access flash: 

 

extern int ReadFlash (FILE* fp) __attribute__ ((section (".FPGARAM2"))); 

 

int ReadFlash (FILE* fp) 

alt_flash_fd* fd=0; 

while (!fd) {fd = alt_flash_open_dev("/dev/Flash");} 

alt_flash_close_dev("/dev/Flash"); 

return 1;} 

 

This function is extern and located in RAM, but my function calls alt_flash_open_dev AND this function is still in FLASH. 

How can I solve this? 

 

best regards, 

Dolphin
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

Hello, 

 

I just found out what the problem is. 

I use the following function to access flash: 

 

extern int ReadFlash (FILE* fp) __attribute__ ((section (".FPGARAM2"))); 

 

int ReadFlash (FILE* fp) 

alt_flash_fd* fd=0; 

while (!fd) {fd = alt_flash_open_dev("/dev/Flash");} 

alt_flash_close_dev("/dev/Flash"); 

return 1;} 

 

This function is extern and located in RAM, but my function calls alt_flash_open_dev AND this function is still in FLASH. 

How can I solve this? 

 

best regards, 

Dolphin
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

Dolphin, 

 

You should read the post to your question by Scott. 

 

The HAL prevents you from using the drivers to access a flash you're executing from, with good reason. If you try to write to the flash the data values read from a flash become status bits, so you're next instruction fetch could fetch status bits, which is unlikely to be what you want to do. 

 

If you look in altera_avalon_cfi_flash.h you'll see how it's done. I would caution against changing this unless you understand the issue I raised above and in Scott's post. 

 

It's also curious that you're writing a ReadFlash function, why not just read from the address?
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

Hello, 

 

I understand the problem with the status bits (I have read the post from Scott). 

That's why I have placed my FlashAccess function in FPGA internal memory (by using a section attribute). 

My FlashAccess function uses the alt_flash_open_dev function and the problem is that this function is still located in the flash. 

 

Do you know how to place the alt_flash_open_dev function in a section in FPGA internal memory? 

You suggest to read from the address, can you explain me how to do that? (or where I can find an example?). 

 

thanks and best regards, 

Dolphin
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

Dolphin, 

 

If you look at the header file I mentioned in my previous post you will see that all the flash functions will not work if you have any of the sections text, rw_data or ro_data in flash. You could remove these macros, but you will have to ensure that any flash accesses are not to the sector you're running code from 

 

As for your question <div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

You suggest to read from the address, can you explain me how to do that? (or where I can find an example?).[/b] 

--- Quote End ---  

 

 

It&#39;s a simple case of using pointers 

 

char* test; 

char result; 

 

test = (char*)SOME_FLASH_ADDRESS; 

 

result = *test; 

 

Will do the job.
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

 

--- Quote Start ---  

 

Hi DOLPHIN, 

 

> I have a Nios2 system that runs from flash (flash = program memory). 

> My program needs to access flash at a certain moment, it needs to write/read some data to it. 

 

I believe the hal prevents you from doing this -- when you try to write to the flash, your flash 

device will no longer be driving array data on the data bus ... and your CPU will be (trying) to 

execute status info from the device. 

 

> Did anybody have a similar problem and how did you solve it? 

 

Yes, this was discussed here: 

 

http://www.niosforum.com/forum/index.php?a...f=17&t=1900&hl= (http://www.niosforum.com/forum/index.php?act=st&f=17&t=1900&hl=

 

Dear Scott, 

I tried to look at this post but it didn&#39;t work.  

 

I have another question on this topic. We are having a similar problem - we are not able to get access to our EPCS Flash device using the function alt_flash_open_dev. We keep getting return value of "Fail" 

 

As far as I understand from this post - you can&#39;t access (read or write) to the EPCS if you are running code from it ? Thats fine - in our IDE environment we have set the linker script parameters to an External ASRAM and On Chip Memory. Is there any where else we have to look - e.g. the boot loader location (to tell you the truth - I don&#39;t know where this is set and stored ) ? We have set the reset address within the SOPC environment to the EPCS device and the exception address to our ASRAM. We have no problems with doing flash programming from the IDE of both the SOF and the firmware. Also on reset to our device the firmware is copied correctly from our EPCS to our ASRAM and runs from there with no bugs. 

 

We are trying to write addional firmware and an application which will enable us to read and write to our EPCS so that we can make updates without using the download cable. Can you give us any hints ? 

 

Thanks 

 

Shmuel
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

Shmuel, 

What settings are you using in the IDE syslib? As told by Altera support: 

 

"when you check on the “small C library” option and the “reduced device drivers” settings, the flash driver will not be initialized. You can refer to the file altera_avalon_cfi_flash.h line 143." 

 

As a result, I was never able to succesfully open the flash device. I was trying to do something very similar as you are on a custom board. My system is based around a Cyclone part which configures and boots from a flash memory, Am29LV065, using the same config controller CPLD as provided with the development kits. I am running software strictly from the on-chip RAM.  

 

Apparently, I am memory limited in my design. I reached an impasse with my problem. I can only get the software design to fit in on-chip memory if I use "small C library" and "reduced device drivers" which won&#39;t allow the use of the HAL flash routines. I have not found a way to increase availability of on-chip data ram on my Cyclone chip which is at 75% memory utilization and 55% logic utilization. I briefly tried running the software from the flash memory but was not able to get the software to run. 

 

My workaround has been to write my own flash routines and writing only the functions I need. 

 

Good luck and please post if you find a solution. 

 

Thanks. 

Steve
0 Kudos
Altera_Forum
Honored Contributor II
1,131 Views

Hi, 

 

I could not access to the link posted by Scott.  

 

May I know what's the reason whereby if i set my .text to flash, alt_flash_open_dev is failed? 

 

Could anyone guide me on how to do if i really need to set flash device in my program memory at the same time writing and reading flash? 

 

Thanks for the help.  

 

Regards, 

caridee
0 Kudos
Reply